libUTL++
BufferedStream.h
1 #pragma once
2 
4 
5 #include <libutl/Stream.h>
6 #include <libutl/Vector.h>
7 
9 
10 UTL_NS_BEGIN;
11 
13 
14 class BufferedStream;
15 
17 
18 typedef BufferedStream& (*bsmanip0)(BufferedStream& stream);
19 
21 
36 
38 class BufferedStream : public Stream
39 {
41 
42 public:
51  bool streamOwner = true,
52  size_t iBufSize = KB(16),
53  size_t oBufSize = KB(16))
54  {
55  init();
56  setStream(stream, streamOwner, iBufSize, oBufSize);
57  }
58 
59  virtual void copy(const Object& rhs);
60 
61  virtual void close();
62 
64 
65  virtual bool isInput() const;
66  virtual void setInput(bool input);
67  virtual bool isOutput() const;
68  virtual void setOutput(bool output);
69  virtual bool eof() const;
70  virtual void setEOF(bool eof);
71  virtual bool error() const;
72  virtual void setError(bool error);
74 
76  const Stream*
77  getStream() const
78  {
79  return _stream;
80  }
81 
83  Stream*
85  {
86  return _stream;
87  }
88 
96  void setStream(Stream* stream,
97  bool streamOwner = true,
98  size_t iBufSize = KB(16),
99  size_t oBufSize = KB(16));
100 
102  Stream* takeStream();
103 
109  bool eofBlocking();
110 
116  void
117  setMode(uint_t mode, bool p_setBufs = true)
118  {
119  setInput((mode & io_rd) != 0);
120  setOutput((mode & io_wr) != 0);
121  if (p_setBufs) setBufs();
122  }
123 
125 
126  void
127  get(byte_t& b)
128  {
129  ASSERTD(isInput());
130  _inCount++;
131  if (_iBufPos == _iBufLim) underflow();
132  b = _iBuf[_iBufPos++];
133  }
134 
135  void
136  get(char& c)
137  {
138  get((byte_t&)c);
139  }
140 
141  byte_t
142  get()
143  {
144  byte_t b;
145  get(b);
146  return b;
147  }
148 
150  byte_t peek();
151 
152  virtual Stream& readLine(String& str);
153 
154  virtual size_t read(byte_t* array, size_t maxBytes, size_t minBytes = size_t_max);
155 
157  void
159  {
160  ASSERTD(_iBufPos > 0);
161  _inCount--;
162  _iBuf[--_iBufPos] = b;
163  }
164 
166  void
167  unget(char c)
168  {
169  unget((byte_t)c);
170  }
172 
174 
175 
176  virtual BufferedStream&
178  {
179  return newline(false);
180  }
181 
184  newline(bool forceFlush)
185  {
186  put((byte_t)'\n');
187  setBOL(true);
188  if (forceFlush || isLineBuffered()) flush();
189  return self;
190  }
191 
195  {
196  ASSERTD(isOutput());
197  _outCount++;
198  _oBuf[_oBufPos++] = b;
199  if (_oBufPos == _oBuf.size()) overflow();
200  return self;
201  }
202 
205  put(char c)
206  {
207  if (c == '\n')
208  return newline();
209  else
210  {
211  indentIfBOL();
212  return put((byte_t)c);
213  }
214  }
215 
218  put(int c)
219  {
220  return put((byte_t)c);
221  }
222 
223  virtual void write(const byte_t* array, size_t num);
225 
227 
228 
229  bool
230  hasInput() const
231  {
232  return (_iBufPos < _iBufLim);
233  }
234 
235  virtual BufferedStream& flush(uint_t mode = io_wr);
236 
238  bool
240  {
241  return getFlag(io_lineBuffered);
242  }
243 
245  void
246  setLineBuffered(bool lineBuffered)
247  {
248  setFlag(io_lineBuffered, lineBuffered);
249  }
250 
256  void setBufs(size_t iBufSize = KB(16), size_t oBufSize = KB(16));
257 
262  void setInputBuf(size_t size = KB(16));
263 
268  void setOutputBuf(size_t size = KB(16));
270 
272 
273  BufferedStream& operator<<(bsmanip0 manip)
274  {
275  return manip(self);
276  }
277  BufferedStream& operator<<(void* ptr)
278  {
279  static_cast<Stream&>(self) << ptr;
280  return self;
281  }
282  BufferedStream& operator<<(const char* str)
283  {
284  static_cast<Stream&>(self) << str;
285  return self;
286  }
287  BufferedStream& operator<<(char c)
288  {
289  static_cast<Stream&>(self) << c;
290  return self;
291  }
292  BufferedStream& operator>>(char& c)
293  {
294  static_cast<Stream&>(self) >> c;
295  return self;
296  }
298  {
299  static_cast<Stream&>(self) << b;
300  return self;
301  }
303  {
304  static_cast<Stream&>(self) >> b;
305  return self;
306  }
307  BufferedStream& operator<<(short n)
308  {
309  static_cast<Stream&>(self) << n;
310  return self;
311  }
312  BufferedStream& operator>>(short& n)
313  {
314  static_cast<Stream&>(self) >> n;
315  return self;
316  }
318  {
319  static_cast<Stream&>(self) << n;
320  return self;
321  }
323  {
324  static_cast<Stream&>(self) >> n;
325  return self;
326  }
327  BufferedStream& operator<<(int n)
328  {
329  static_cast<Stream&>(self) << n;
330  return self;
331  }
332  BufferedStream& operator>>(int& n)
333  {
334  static_cast<Stream&>(self) >> n;
335  return self;
336  }
338  {
339  static_cast<Stream&>(self) << n;
340  return self;
341  }
343  {
344  static_cast<Stream&>(self) >> n;
345  return self;
346  }
347  BufferedStream& operator<<(long n)
348  {
349  static_cast<Stream&>(self) << n;
350  return self;
351  }
352  BufferedStream& operator>>(long& n)
353  {
354  static_cast<Stream&>(self) >> n;
355  return self;
356  }
358  {
359  static_cast<Stream&>(self) << n;
360  return self;
361  }
363  {
364  static_cast<Stream&>(self) >> n;
365  return self;
366  }
367 #if UTL_SIZEOF_LONG == 4
369  {
370  static_cast<Stream&>(self) << n;
371  return self;
372  }
374  {
375  static_cast<Stream&>(self) >> n;
376  return self;
377  }
379  {
380  static_cast<Stream&>(self) << n;
381  return self;
382  }
384  {
385  static_cast<Stream&>(self) >> n;
386  return self;
387  }
388 #endif
389  BufferedStream& operator<<(double n)
390  {
391  static_cast<Stream&>(self) << n;
392  return self;
393  }
394  BufferedStream& operator>>(double& n)
395  {
396  static_cast<Stream&>(self) >> n;
397  return self;
398  }
400 protected:
401  virtual void clear();
402 
404 
405 
409  virtual void underflow();
411 
413 
414 
418  virtual void overflow();
420 protected:
421  // input buffer
422  size_t _iBufPos, _iBufLim;
423  Vector<byte_t> _iBuf;
424 
425  // output buffer
426  size_t _oBufPos;
427  Vector<byte_t> _oBuf;
428 
429  // buffered stream
430  Stream* _stream;
431 
432 private:
433  void init();
434  void deInit();
435 };
436 
438 
443 inline utl::BufferedStream&
445 {
446  return stream.newline(false);
447 }
448 
450 
455 inline utl::BufferedStream&
457 {
458  return stream.newline(true);
459 }
460 
462 
467 inline utl::BufferedStream&
469 {
470  return stream.flush();
471 }
472 
474 
475 UTL_NS_END;
476 
478 
488 
490 
499 
501 
void unget(char c)
Unget a char.
virtual BufferedStream & flush(uint_t mode=io_wr)
Flush the stream (if it is buffered).
utl::BufferedStream & flush(utl::BufferedStream &stream)
Flush the stream&#39;s output.
void deInit()
De-initialize UTL++.
utl::BufferedStream & endlf(utl::BufferedStream &stream)
Output a newline to the stream and flush its output.
BufferedStream & put(byte_t b)
Write the given byte.
line-buffered
Definition: Stream.h:26
#define UTL_CLASS_DECL(DC, BC)
Declaration of standard UTL++ functionality for a non-template class.
Definition: macros.h:688
Stream * getStream()
Get the stream.
unsigned char byte_t
Unsigned character.
Definition: types.h:31
Character string.
Definition: String.h:31
BufferedStream & newline(bool forceFlush)
Write a newline.
BufferedStream(Stream *stream, bool streamOwner=true, size_t iBufSize=KB(16), size_t oBufSize=KB(16))
Constructor.
const Stream * getStream() const
Get the stream.
bool hasInput() const
Has unread input?
const size_t size_t_max
Maximum size_t value.
long int64_t
Signed 64-bit integer.
Definition: types.h:164
utl::BufferedStream & operator<<(utl::BufferedStream &lhs, const utl::Object &rhs)
Output an object&#39;s string representation to a stream (with Object::toString()).
write
Definition: util.h:59
void copy(T *dest, const T *src, size_t len)
Copy one array of objects to another.
Definition: util_inl.h:690
read
Definition: util.h:58
BufferedStream & put(int c)
Write the given character.
unsigned long uint64_t
Unsigned 64-bit integer.
Definition: types.h:154
bool isLineBuffered() const
Determine whether the stream is line-buffered.
unsigned int uint_t
Unsigned integer.
Definition: types.h:59
void setMode(uint_t mode, bool p_setBufs=true)
Set the mode.
Stream I/O abstraction.
Definition: Stream.h:68
unsigned short ushort_t
Unsigned short integer.
Definition: types.h:45
BufferedStream & put(char c)
Write the given character.
utl::BufferedStream & endl(utl::BufferedStream &stream)
Output a newline to the stream.
unsigned long ulong_t
Unsigned long integer.
Definition: types.h:73
constexpr size_t KB(size_t n)
Convert size in kilobytes to size in bytes.
Definition: util_inl.h:1008
void unget(byte_t b)
Un-get a byte.
Buffered stream.
Root of UTL++ class hierarchy.
Definition: Object.h:52
virtual BufferedStream & newline()
Write a newline.
void setLineBuffered(bool lineBuffered)
Set the line-buffered flag.
utl::BufferedStream & operator>>(utl::BufferedStream &lhs, utl::String &rhs)
Read a string from the given input stream.
void init()
Initialize UTL++.
#define ASSERTD
Do an assertion in DEBUG mode only.