You need to sign in or sign up before continuing.
Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#ifndef UTIL_PIPESTREAM_H
#define UTIL_PIPESTREAM_H
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include "util.h"
namespace util
{
namespace pipestream_manip {
struct setmax_t {
size_t max;
};
struct enable_multiline_strings_t {
bool value;
};
}
inline pipestream_manip::setmax_t setmax(size_t max)
{
pipestream_manip::setmax_t retval = { max };
return retval;
}
const pipestream_manip::enable_multiline_strings_t enable_multiline_strings = { true };
const pipestream_manip::enable_multiline_strings_t disable_multiline_strings = { false };
template <size_t buffersize>
class sizedpipestream
{
protected:
uint8_t wbuffer[buffersize+1]; // +1 to avoid 0 sized arrays
uint8_t rbuffer[buffersize+1];
size_t wbufferpos;
size_t rbufferpos;
size_t rbuffersize;
size_t maxopsize;
int fds[2];
bool is_eof;
bool enable_multiline_strings;
static const int READ_END = 0;
static const int WRITE_END = 1;
public:
sizedpipestream();
~sizedpipestream();
void close();
void close_read();
void close_write();
bool eof();
void flush();
int fill(size_t byte_count);
int steal_output(int fd);
int steal_input(int fd);
int get_read_fd();
int get_write_fd();
sizedpipestream<buffersize> & operator<<(const string & str);
sizedpipestream<buffersize> & operator<<(const char * str);
sizedpipestream<buffersize> & operator<<(const signed char & value);
sizedpipestream<buffersize> & operator<<(const signed short & value);
sizedpipestream<buffersize> & operator<<(const signed int & value);
sizedpipestream<buffersize> & operator<<(const signed long & value);
sizedpipestream<buffersize> & operator<<(const unsigned char & value);
sizedpipestream<buffersize> & operator<<(const unsigned short & value);
sizedpipestream<buffersize> & operator<<(const unsigned int & value);
sizedpipestream<buffersize> & operator<<(const unsigned long & value);
sizedpipestream<buffersize> & operator>>(string & str);
sizedpipestream<buffersize> & operator>>(char * & str);
sizedpipestream<buffersize> & operator>>( signed char & value);
sizedpipestream<buffersize> & operator>>( signed short & value);
sizedpipestream<buffersize> & operator>>( signed int & value);
sizedpipestream<buffersize> & operator>>( signed long & value);
sizedpipestream<buffersize> & operator>>(unsigned char & value);
sizedpipestream<buffersize> & operator>>(unsigned short & value);
sizedpipestream<buffersize> & operator>>(unsigned int & value);
sizedpipestream<buffersize> & operator>>(unsigned long & value);
sizedpipestream<buffersize> & operator<<(pipestream_manip::enable_multiline_strings_t value);
sizedpipestream<buffersize> & operator>>(pipestream_manip::setmax_t max);
protected:
template <typename T>
sizedpipestream<buffersize> & write_primitive(T value);
template <typename T>
int read_primitive(T & value);
private:
sizedpipestream(sizedpipestream<buffersize> & other);
sizedpipestream<buffersize> & operator=(sizedpipestream<buffersize> & other);
};
typedef sizedpipestream<512> pipestream;
#include "pipestream.cpp"
} // namespace util
#endif