libUTL++
Stream.h
1 #pragma once
2 
4 
5 #include <libutl/Exception.h>
6 
8 
9 UTL_NS_BEGIN;
10 
12 
13 class String;
14 
16 
22 {
23  io_eof = 3,
24  io_error = 4,
25  io_bol = 5,
27 };
28 
30 
31 typedef Stream& (*smanip0)(Stream& stream);
32 
34 
66 
68 class Stream : public Object, protected FlagsMI
69 {
71 
72 public:
73  virtual void copy(const Object& rhs);
74 
76  virtual void close() = 0;
77 
79 
80 
81  bool
82  isOwner() const
83  {
84  return getFlag(flg_owner);
85  }
86 
88  void
89  setOwner(bool owner)
90  {
91  setFlag(flg_owner, owner);
92  }
93 
95  size_t
96  getInputCount() const
97  {
98  return _inCount;
99  }
100 
102  size_t
104  {
105  return _outCount;
106  }
107 
109  uint_t
110  getMode() const
111  {
112  uint_t res = 0;
113  if (isInput()) res |= io_rd;
114  if (isOutput()) res |= io_wr;
115  return res;
116  }
117 
119  const String& getName() const;
120 
122  const String* getNamePtr() const;
123 
125  void setName(const String& name);
126 
128  void setName(String* name);
129 
131  virtual bool isBOL() const;
132 
134  virtual void setBOL(bool p_bol);
135 
137  virtual bool isInput() const;
138 
140  virtual void setInput(bool input);
141 
143  virtual bool isOutput() const;
144 
146  virtual void setOutput(bool output);
147 
149  bool
150  isRDWR() const
151  {
152  return (isInput() && isOutput());
153  }
154 
156  void
158  {
159  _base = base;
160  }
161 
166  void
168  {
169  setInput((mode & io_rd) != 0);
170  setOutput((mode & io_wr) != 0);
171  }
173 
175 
176 
177  uint_t
178  getIndent() const
179  {
180  return _indent;
181  }
182 
184  void
186  {
187  _indent = indent;
188  }
189 
191  void
192  indent(uint_t num = 4)
193  {
194  _indent += num;
195  }
196 
198  void
199  unindent(uint_t num = 4)
200  {
201  if (num > _indent)
202  _indent = 0;
203  else
204  _indent -= num;
205  }
207 
209 
210 
214  void checkOK();
215 
217  void clearEOF();
218 
220  void clearError();
221 
223  virtual bool eof() const;
224 
226  virtual void setEOF(bool p_eof);
227 
229  virtual bool error() const;
230 
232  virtual void setError(bool p_error);
233 
235  bool ok() const;
237 
239 
240 
247  size_t copyData(Stream& in, size_t numBytes = size_t_max, size_t bufSize = KB(4));
248 
253  void
254  get(byte_t& b)
255  {
256  read(&b, 1);
257  }
258 
263  void
264  get(char& c)
265  {
266  get((byte_t&)c);
267  }
268 
273  byte_t
274  get()
275  {
276  byte_t b;
277  get(b);
278  return b;
279  }
280 
285  bool
287  {
288  if (_bitMask == 0x80) get(_bitByte);
289  bool res = ((_bitByte & _bitMask) != 0);
290  _bitMask >>= 1;
291  if (_bitMask == 0) _bitMask = 0x80;
292  return res;
293  }
294 
300  uint32_t
301  getBits(uint_t numBits)
302  {
303  ASSERTD(numBits <= 32);
304  uint32_t res = 0, mask = 1 << (numBits - 1);
305  while (mask != 0)
306  {
307  if (getBit()) res |= mask;
308  mask >>= 1;
309  }
310  return res;
311  }
312 
314  virtual Stream& readLine(String& str);
315 
323  virtual size_t read(byte_t* array, size_t maxBytes, size_t minBytes = size_t_max) = 0;
325 
327 
328 
329  Stream&
331  {
332  ASSERTD(isOutput());
333  write(&b, 1);
334  return self;
335  }
336 
338  Stream&
339  put(char c)
340  {
341  if (c == '\n')
342  return newline();
343  else
344  {
345  indentIfBOL();
346  return put((byte_t)c);
347  }
348  }
349 
351  Stream&
352  put(int c)
353  {
354  return put((byte_t)c);
355  }
356 
358  Stream& put(const char* str);
359 
361  Stream& put(const char* str, size_t len);
362 
364  Stream&
365  putBit(bool b)
366  {
367  if (b) _bitByte |= _bitMask;
368  _bitMask >>= 1;
369  if (!_bitMask)
370  {
371  put(_bitByte);
372  _bitMask = 0x80;
373  _bitByte = 0;
374  }
375  return self;
376  }
377 
379  Stream&
381  {
382  while (_bitMask != 0x80) putBit(false);
383  return self;
384  }
385 
391  Stream&
392  putBits(uint32_t n, uint_t numBits)
393  {
394  uint32_t mask = 1 << (numBits - 1);
395  while (mask != 0)
396  {
397  putBit((n & mask) != 0);
398  mask >>= 1;
399  }
400  return self;
401  }
402 
404  virtual Stream&
406  {
407  put((byte_t)'\n');
408  setBOL(true);
409  return self;
410  }
411 
413  Stream&
414  putLine(const char* str)
415  {
416  put(str);
417  return newline();
418  }
419 
421  void writeSpaces(size_t num);
422 
428  virtual void write(const byte_t* array, size_t num) = 0;
430 
435  virtual Stream&
437  {
438  return self;
439  }
440 
442 
443 
444  Stream& operator<<(smanip0 manip)
445  {
446  return manip(self);
447  }
448 
450  Stream& operator<<(void* ptr);
451 
453  Stream& operator<<(const char* str);
454 
455  // char
456  Stream& operator<<(char c)
457  {
458  put(c);
459  return self;
460  }
461  Stream& operator>>(char& c)
462  {
463  get(c);
464  return self;
465  }
466 
467  // byte
469  {
470  put(b);
471  return self;
472  }
474  {
475  get(b);
476  return self;
477  }
478 
479  // int16_t
482 
483  // uint16_t
486 
487  // int32_t
490 
491  // uint32_t
494 
495  // long
496  Stream& operator<<(long n);
497  Stream& operator>>(long& n);
498 
499  // ulong_t
502 
503 #if UTL_SIZEOF_LONG == 4
504  // int64_t
507 
508  // uint64_t
511 #endif
512 
513  // double
514  Stream& operator<<(double n);
515  Stream& operator>>(double& n);
517 
518  static void utl_init();
519 
520 protected:
521  virtual void clear();
522 
524 
525 
526  inline void
528  {
529  if (!isBOL()) return;
530  setBOL(false);
531  _indentIfBOL();
532  }
533 
535  void
537  {
538  uint_t indent = _indent;
539  while (indent > 256)
540  {
541  write(spaces, 256);
542  indent -= 256;
543  }
544  write(spaces, indent);
545  }
547 
549 
550 
551  void throwStreamEOFex();
552 
554  void throwStreamErrorEx();
556 
558 
559 
564  void readToken(char* buf, size_t size);
565 
572  void readUntilWS(char* buf, size_t size);
573 
575  byte_t skipWS();
577 
578  // track # input/output bytes
579  size_t _inCount;
580  size_t _outCount;
581  // bit i/o
582  byte_t _bitMask;
583  byte_t _bitByte;
584 
585 private:
586  static byte_t spaces[256];
587  enum flg_t
588  {
589  flg_owner
590  };
591 
592 private:
593  void init();
594  void deInit();
595 
596 private:
597  String* _name;
598  uint_t _base;
599  uint_t _indent;
600 };
601 
603 
608 inline utl::Stream&
610 {
611  return stream.put('\n');
612 }
613 
615 
620 inline utl::Stream&
622 {
623  stream.newline();
624  return stream.flush();
625 }
626 
628 
633 inline utl::Stream&
635 {
636  return stream.flush();
637 }
638 
640 
641 UTL_NS_END;
642 
644 
652 utl::Stream& operator<<(utl::Stream& lhs, const utl::Object& rhs);
653 
655 
664 
666 
674 utl::Stream& operator<<(utl::Stream& lhs, const utl::String& rhs);
virtual Stream & flush(uint_t mode=io_wr)
Flush the stream (if it is buffered).
Definition: Stream.h:436
void setIndent(uint_t indent)
Set the indentation level.
Definition: Stream.h:185
uint_t getMode() const
Get the current mode.
Definition: Stream.h:110
end-of-file
Definition: Stream.h:23
void deInit()
De-initialize UTL++.
void indentIfBOL()
If begin-of-line flag is true, clear the flag and indent.
Definition: Stream.h:527
Stream & put(byte_t b)
Write the given character.
Definition: Stream.h:330
line-buffered
Definition: Stream.h:26
bool isOwner() const
Get the owner flag.
Definition: Stream.h:82
utl::Stream & operator<<(utl::Stream &lhs, const utl::Object &rhs)
Output an object&#39;s string representation to a stream (with Object::toString()).
void setBase(uint_t base)
Set the base, for writing integer values.
Definition: Stream.h:157
utl::Stream & operator>>(utl::Stream &lhs, utl::String &rhs)
Read a string from the given input stream.
size_t getInputCount() const
Get the count of input bytes.
Definition: Stream.h:96
size_t getOutputCount() const
Get the count of output bytes.
Definition: Stream.h:103
unsigned char byte_t
Unsigned character.
Definition: types.h:31
Character string.
Definition: String.h:31
Mix-in to provide 64-bits for space-efficient storage of up to 64 boolean flags.
Definition: FlagsMI.h:25
const size_t size_t_max
Maximum size_t value.
long int64_t
Signed 64-bit integer.
Definition: types.h:164
bool isRDWR() const
Determine whether the stream is an input/output stream.
Definition: Stream.h:150
unsigned int uint32_t
Unsigned 32-bit integer.
Definition: types.h:115
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
utl::Stream & flush(utl::Stream &stream)
Flush the stream&#39;s output.
Definition: Stream.h:634
void indent(uint_t num=4)
Increase indentation by the given number of spaces.
Definition: Stream.h:192
utl::Stream & endl(utl::Stream &stream)
Output a newline to the stream.
Definition: Stream.h:609
unsigned long uint64_t
Unsigned 64-bit integer.
Definition: types.h:154
Stream & operator<<(smanip0 manip)
Execute a manipulator (e.g.
Definition: Stream.h:444
I/O error.
Definition: Stream.h:24
void _indentIfBOL()
Write _indent spaces at beginning of line.
Definition: Stream.h:536
bool getBit()
Get a single bit.
Definition: Stream.h:286
virtual Stream & newline()
Write a newline.
Definition: Stream.h:405
uint32_t getBits(uint_t numBits)
Get multiple bits.
Definition: Stream.h:301
ios_flg_t
Stream flags.
Definition: Stream.h:21
#define UTL_CLASS_DECL_ABC(DC, BC)
Declaration of standard UTL++ functionality for an abstract base class (ABC).
Definition: macros.h:650
unsigned int uint_t
Unsigned integer.
Definition: types.h:59
utl::Stream & endlf(utl::Stream &stream)
Output a newline to the stream and flush its output.
Definition: Stream.h:621
Stream & put(char c)
Write the given character.
Definition: Stream.h:339
Stream I/O abstraction.
Definition: Stream.h:68
void unindent(uint_t num=4)
Decrease indentation by the given number of spaces.
Definition: Stream.h:199
unsigned short uint16_t
Unsigned 16-bit integer.
Definition: types.h:101
unsigned long ulong_t
Unsigned long integer.
Definition: types.h:73
signed int int32_t
Signed 32-bit integer.
Definition: types.h:121
begin-of-line
Definition: Stream.h:25
constexpr size_t KB(size_t n)
Convert size in kilobytes to size in bytes.
Definition: util_inl.h:1008
Stream & put(int c)
Write the given character.
Definition: Stream.h:352
signed short int16_t
Signed 16-bit integer.
Definition: types.h:107
Stream & putBits()
Write all outstanding bits.
Definition: Stream.h:380
void setMode(uint_t mode)
Set the mode.
Definition: Stream.h:167
Root of UTL++ class hierarchy.
Definition: Object.h:52
Stream & putLine(const char *str)
Write the given string, followed by a newline.
Definition: Stream.h:414
Stream & putBits(uint32_t n, uint_t numBits)
Write multiple bits.
Definition: Stream.h:392
void setOwner(bool owner)
Set the owner flag.
Definition: Stream.h:89
uint_t getIndent() const
Get the indentation level.
Definition: Stream.h:178
void init()
Initialize UTL++.
#define ASSERTD
Do an assertion in DEBUG mode only.
Stream & putBit(bool b)
Write the given bit.
Definition: Stream.h:365