summaryrefslogtreecommitdiff
path: root/libs
diff options
context:
space:
mode:
authorRobin Gareus <robin@gareus.org>2018-11-19 02:26:43 +0100
committerRobin Gareus <robin@gareus.org>2018-11-19 02:26:43 +0100
commit103ef2ba08e59c21f84899450f04a062ac60334e (patch)
tree4fa9cba3c2445fd9d1e873330d3c705f58937b69 /libs
parent3977fbae516abfcc5b1c34209e4f07a1c76698c0 (diff)
Add API to write raw data to child processes.
Diffstat (limited to 'libs')
-rw-r--r--libs/pbd/pbd/system_exec.h11
-rw-r--r--libs/pbd/system_exec.cc38
2 files changed, 28 insertions, 21 deletions
diff --git a/libs/pbd/pbd/system_exec.h b/libs/pbd/pbd/system_exec.h
index a8a30ba18d..c5fb583152 100644
--- a/libs/pbd/pbd/system_exec.h
+++ b/libs/pbd/pbd/system_exec.h
@@ -164,12 +164,19 @@ class LIBPBD_API SystemExec
*/
void close_stdin ();
/** write into child-program's STDIN
- * @param d data to write
+ * @param d text to write
* @param len length of data to write, if it is 0 (zero), d.length() is
* used to determine the number of bytes to transmit.
* @return number of bytes written.
*/
- int write_to_stdin (std::string d, size_t len=0);
+ size_t write_to_stdin (std::string const& d, size_t len=0);
+
+ /** write into child-program's STDIN
+ * @param data data to write
+ * @param bytes length of data to write
+ * @return number of bytes written.
+ */
+ size_t write_to_stdin (const void* d, size_t bytes=0);
/** The ReadStdout signal is emitted when the application writes to STDOUT.
* it passes the written data and its length in bytes as arguments to the bound
diff --git a/libs/pbd/system_exec.cc b/libs/pbd/system_exec.cc
index 351e4ee066..ed36c507ff 100644
--- a/libs/pbd/system_exec.cc
+++ b/libs/pbd/system_exec.cc
@@ -351,6 +351,16 @@ SystemExec::to_s () const
#endif
}
+size_t
+SystemExec::write_to_stdin (std::string const& d, size_t len)
+{
+ const char *data = d.c_str();
+ if (len == 0) {
+ len = d.length();
+ }
+ return write_to_stdin ((const void*)data, len);
+}
+
#ifdef PLATFORM_WINDOWS /* Windows Process */
/* HELPER FUNCTIONS */
@@ -582,21 +592,16 @@ SystemExec::close_stdin()
destroy_pipe(stdinP);
}
-int
-SystemExec::write_to_stdin(std::string d, size_t len)
+size_t
+SystemExec::write_to_stdin(const void* data, size_t bytes)
{
- const char *data;
DWORD r,c;
::pthread_mutex_lock(&write_lock);
- data=d.c_str();
- if (len == 0) {
- len=(d.length());
- }
c=0;
- while (c < len) {
- if (!WriteFile(stdinP[1], data+c, len-c, &r, NULL)) {
+ while (c < bytes) {
+ if (!WriteFile(stdinP[1], data+c, bytes-c, &r, NULL)) {
if (GetLastError() == 0xE8 /*NT_STATUS_INVALID_USER_BUFFER*/) {
Sleep(100);
continue;
@@ -950,27 +955,22 @@ SystemExec::close_stdin()
close_fd(pout[1]);
}
-int
-SystemExec::write_to_stdin(std::string d, size_t len)
+size_t
+SystemExec::write_to_stdin(const void* data, size_t bytes)
{
- const char *data;
ssize_t r;
size_t c;
::pthread_mutex_lock(&write_lock);
- data=d.c_str();
- if (len == 0) {
- len=(d.length());
- }
c=0;
- while (c < len) {
+ while (c < bytes) {
for (;;) {
- r=::write(pin[1], data+c, len-c);
+ r=::write(pin[1], data+c, bytes-c);
if (r < 0 && (errno == EINTR || errno == EAGAIN)) {
sleep(1);
continue;
}
- if ((size_t) r != (len-c)) {
+ if ((size_t) r != (bytes-c)) {
::pthread_mutex_unlock(&write_lock);
return c;
}