diff --git a/subprocess.hpp b/subprocess.hpp index 668e3ad..bdd3c3b 100755 --- a/subprocess.hpp +++ b/subprocess.hpp @@ -306,9 +306,8 @@ namespace util * * NOTE: `class Buffer` is a exposed public class. See below. */ - template - // Requires Buffer to be of type class Buffer - static inline int read_all(int fd, Buffer& buf) + + static inline int read_all(int fd, std::vector& buf) { auto buffer = buf.data(); int total_bytes_read = 0; @@ -329,8 +328,8 @@ namespace util //update the buffer pointer buffer = buf.data(); - buffer += rd_bytes; total_bytes_read += rd_bytes; + buffer += total_bytes_read; } else { // Partial data ? Continue reading total_bytes_read += rd_bytes; @@ -338,6 +337,7 @@ namespace util break; } } + buf.erase(buf.begin()+total_bytes_read, buf.end()); // remove extra nulls return total_bytes_read; } diff --git a/test/test_subprocess.cc b/test/test_subprocess.cc index d02aeb8..bf0a694 100755 --- a/test/test_subprocess.cc +++ b/test/test_subprocess.cc @@ -51,6 +51,19 @@ void test_sleep() std::cout << "Sleep ended: ret code = " << p.retcode() << std::endl; } +void test_read_all() +{ + Popen p = Popen({"echo","12345678"}, output{PIPE}); + + std::vector buf(6); + int rbytes = util::read_all(fileno(p.output()), buf); + + std::string out(buf.begin(), buf.end()); + + assert(out == "12345678\n" && rbytes == 9); // echo puts a new line at the end of output + std::cout<<"read_all() succeeded"<