1 | #include "streamreader.h" |
---|---|
2 | #include <stdlib.h> |
3 | #include <string.h> |
4 | #include <unistd.h> |
5 | |
6 | using namespace jsonrpc; |
7 | using namespace std; |
8 | |
9 | StreamReader::StreamReader(size_t buffersize) : buffersize(buffersize), buffer(static_cast<char *>(malloc(buffersize))) {} |
10 | |
11 | StreamReader::~StreamReader() { free(buffer); } |
12 | |
13 | bool StreamReader::Read(std::string &target, int fd, char delimiter) { |
14 | ssize_t bytesRead; |
15 | do { |
16 | bytesRead = read(fd, this->buffer, buffersize); |
17 | if (bytesRead < 0) { |
18 | return false; |
19 | } else { |
20 | target.append(buffer, static_cast<size_t>(bytesRead)); |
21 | } |
22 | } while (memchr(buffer, delimiter, bytesRead) == NULL); //(target.find(delimiter) == string::npos && bytesRead > 0); |
23 | |
24 | target.pop_back(); |
25 | return true; |
26 | } |
27 |