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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
|
// license:GPL-2.0+
// copyright-holders:Couriersud
/*
* pstream.h
*/
#ifndef PSTREAM_H_
#define PSTREAM_H_
#include <cstdarg>
#include <cstddef>
#include <stdexcept>
#include "pconfig.h"
#include "pstring.h"
#include "palloc.h"
#include "pfmtlog.h"
namespace plib {
// -----------------------------------------------------------------------------
// pstream: things common to all streams
// -----------------------------------------------------------------------------
class pstream
{
P_PREVENT_COPYING(pstream)
public:
using pos_type = std::size_t;
static const pos_type SEEK_EOF = (pos_type) -1;
explicit pstream(const unsigned flags) : m_flags(flags)
{
}
virtual ~pstream()
{
}
bool bad() const { return ((m_flags & FLAG_ERROR) != 0); }
bool seekable() const { return ((m_flags & FLAG_SEEKABLE) != 0); }
void seek(const pos_type n)
{
check_seekable();
return vseek(n);
}
pos_type tell()
{
return vtell();
}
protected:
virtual void vseek(const pos_type n) = 0;
virtual pos_type vtell() = 0;
static const unsigned FLAG_EOF = 0x01;
static const unsigned FLAG_ERROR = 0x02;
static const unsigned FLAG_SEEKABLE = 0x04;
static const unsigned FLAG_CLOSED = 0x08; /* convenience flag */
bool closed() { return ((m_flags & FLAG_CLOSED) != 0); }
void set_flag(const unsigned flag)
{
m_flags |= flag;
}
void clear_flag(const unsigned flag)
{
m_flags &= ~flag;
}
void check_not_eof() const
{
if (m_flags & FLAG_EOF)
throw pexception("unexpected eof");
}
void check_seekable() const
{
if (!(m_flags & FLAG_SEEKABLE))
throw pexception("stream is not seekable");
}
unsigned flags() const { return m_flags; }
private:
unsigned m_flags;
};
// -----------------------------------------------------------------------------
// pistream: input stream
// -----------------------------------------------------------------------------
class pistream : public pstream
{
P_PREVENT_COPYING(pistream)
public:
explicit pistream(const unsigned flags) : pstream(flags) {}
virtual ~pistream() {}
bool eof() const { return ((flags() & FLAG_EOF) != 0) || bad(); }
/* this digests linux & dos/windows text files */
bool readline(pstring &line);
bool read(char &c)
{
return (read(&c, 1) == 1);
}
unsigned read(void *buf, const unsigned n)
{
return vread(buf, n);
}
protected:
/* read up to n bytes from stream */
virtual unsigned vread(void *buf, const unsigned n) = 0;
private:
};
// -----------------------------------------------------------------------------
// postream: output stream
// -----------------------------------------------------------------------------
class postream : public pstream
{
P_PREVENT_COPYING(postream)
public:
explicit postream(unsigned flags) : pstream(flags) {}
virtual ~postream() {}
/* this digests linux & dos/windows text files */
void writeline(const pstring &line)
{
write(line);
write(10);
}
void write(const pstring &text)
{
write(text.cstr(), text.blen());
}
void write(const char c)
{
write(&c, 1);
}
void write(const void *buf, const unsigned n)
{
vwrite(buf, n);
}
void write(pistream &strm);
protected:
/* write n bytes to stream */
virtual void vwrite(const void *buf, const unsigned n) = 0;
private:
};
// -----------------------------------------------------------------------------
// pomemstream: output string stream
// -----------------------------------------------------------------------------
class pomemstream : public postream
{
P_PREVENT_COPYING(pomemstream)
public:
pomemstream();
virtual ~pomemstream();
char *memory() const { return m_mem; }
unsigned size() const { return m_size; }
protected:
/* write n bytes to stream */
virtual void vwrite(const void *buf, const unsigned n) override;
virtual void vseek(const pos_type n) override;
virtual pos_type vtell() override;
private:
pos_type m_pos;
pos_type m_capacity;
pos_type m_size;
char *m_mem;
};
class postringstream : public postream
{
P_PREVENT_COPYING(postringstream )
public:
postringstream() : postream(0) { }
virtual ~postringstream() { }
const pstringbuffer &str() { return m_buf; }
protected:
/* write n bytes to stream */
virtual void vwrite(const void *buf, const unsigned n) override
{
m_buf.cat(buf, n);
}
virtual void vseek(const pos_type n) override { }
virtual pos_type vtell() override { return m_buf.len(); }
private:
pstringbuffer m_buf;
};
// -----------------------------------------------------------------------------
// pofilestream: file output stream
// -----------------------------------------------------------------------------
class pofilestream : public postream
{
P_PREVENT_COPYING(pofilestream)
public:
explicit pofilestream(const pstring &fname);
virtual ~pofilestream();
void close();
protected:
pofilestream(void *file, const bool do_close);
/* write n bytes to stream */
virtual void vwrite(const void *buf, const unsigned n) override;
virtual void vseek(const pos_type n) override;
virtual pos_type vtell() override;
private:
void *m_file;
pos_type m_pos;
bool m_actually_close;
void init(void *file);
};
// -----------------------------------------------------------------------------
// pstderr: write to stderr
// -----------------------------------------------------------------------------
class pstderr : public pofilestream
{
P_PREVENT_COPYING(pstderr)
public:
pstderr();
};
// -----------------------------------------------------------------------------
// pstdout: write to stdout
// -----------------------------------------------------------------------------
class pstdout : public pofilestream
{
P_PREVENT_COPYING(pstdout)
public:
pstdout();
};
// -----------------------------------------------------------------------------
// pifilestream: file input stream
// -----------------------------------------------------------------------------
class pifilestream : public pistream
{
P_PREVENT_COPYING(pifilestream)
public:
explicit pifilestream(const pstring &fname);
virtual ~pifilestream();
void close();
protected:
pifilestream(void *file, const bool do_close);
/* read up to n bytes from stream */
virtual unsigned vread(void *buf, const unsigned n) override;
virtual void vseek(const pos_type n) override;
virtual pos_type vtell() override;
private:
void *m_file;
pos_type m_pos;
bool m_actually_close;
void init(void *file);
};
// -----------------------------------------------------------------------------
// pstdin: reads from stdin
// -----------------------------------------------------------------------------
class pstdin : public pifilestream
{
P_PREVENT_COPYING(pstdin)
public:
pstdin();
};
// -----------------------------------------------------------------------------
// pimemstream: input memory stream
// -----------------------------------------------------------------------------
class pimemstream : public pistream
{
P_PREVENT_COPYING(pimemstream)
public:
pimemstream(const void *mem, const pos_type len);
explicit pimemstream(const pomemstream &ostrm);
virtual ~pimemstream();
protected:
/* read up to n bytes from stream */
virtual unsigned vread(void *buf, const unsigned n) override;
virtual void vseek(const pos_type n) override;
virtual pos_type vtell() override;
private:
pos_type m_pos;
pos_type m_len;
char *m_mem;
};
// -----------------------------------------------------------------------------
// pistringstream: input string stream
// -----------------------------------------------------------------------------
class pistringstream : public pimemstream
{
P_PREVENT_COPYING(pistringstream)
public:
pistringstream(const pstring &str) : pimemstream(str.cstr(), str.len()), m_str(str) { }
private:
/* only needed for a reference till destruction */
pstring m_str;
};
// -----------------------------------------------------------------------------
// pstream_fmt_writer_t: writer on top of ostream
// -----------------------------------------------------------------------------
class pstream_fmt_writer_t : public plib::pfmt_writer_t<>
{
P_PREVENT_COPYING(pstream_fmt_writer_t)
public:
explicit pstream_fmt_writer_t(postream &strm) : m_strm(strm) {}
virtual ~pstream_fmt_writer_t() { }
protected:
virtual void vdowrite(const pstring &ls) const override
{
m_strm.write(ls);
}
private:
postream &m_strm;
};
}
#endif /* PSTREAM_H_ */
|