mirror of
https://github.com/arun11299/cpp-subprocess.git
synced 2025-08-07 05:46:11 -04:00
fixed a bug in util::read_all() function and added a test for it (#23)
This commit is contained in:
parent
7ebcd80c05
commit
d74481c564
@ -306,9 +306,8 @@ namespace util
|
|||||||
*
|
*
|
||||||
* NOTE: `class Buffer` is a exposed public class. See below.
|
* NOTE: `class Buffer` is a exposed public class. See below.
|
||||||
*/
|
*/
|
||||||
template <typename Buffer>
|
|
||||||
// Requires Buffer to be of type class Buffer
|
static inline int read_all(int fd, std::vector<char>& buf)
|
||||||
static inline int read_all(int fd, Buffer& buf)
|
|
||||||
{
|
{
|
||||||
auto buffer = buf.data();
|
auto buffer = buf.data();
|
||||||
int total_bytes_read = 0;
|
int total_bytes_read = 0;
|
||||||
@ -329,8 +328,8 @@ namespace util
|
|||||||
|
|
||||||
//update the buffer pointer
|
//update the buffer pointer
|
||||||
buffer = buf.data();
|
buffer = buf.data();
|
||||||
buffer += rd_bytes;
|
|
||||||
total_bytes_read += rd_bytes;
|
total_bytes_read += rd_bytes;
|
||||||
|
buffer += total_bytes_read;
|
||||||
|
|
||||||
} else { // Partial data ? Continue reading
|
} else { // Partial data ? Continue reading
|
||||||
total_bytes_read += rd_bytes;
|
total_bytes_read += rd_bytes;
|
||||||
@ -338,6 +337,7 @@ namespace util
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
buf.erase(buf.begin()+total_bytes_read, buf.end()); // remove extra nulls
|
||||||
return total_bytes_read;
|
return total_bytes_read;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,6 +51,19 @@ void test_sleep()
|
|||||||
std::cout << "Sleep ended: ret code = " << p.retcode() << std::endl;
|
std::cout << "Sleep ended: ret code = " << p.retcode() << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void test_read_all()
|
||||||
|
{
|
||||||
|
Popen p = Popen({"echo","12345678"}, output{PIPE});
|
||||||
|
|
||||||
|
std::vector<char> 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"<<std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
test_exename();
|
test_exename();
|
||||||
test_input();
|
test_input();
|
||||||
@ -58,5 +71,6 @@ int main() {
|
|||||||
test_easy_piping();
|
test_easy_piping();
|
||||||
test_shell();
|
test_shell();
|
||||||
test_sleep();
|
test_sleep();
|
||||||
|
test_read_all();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user