/home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/latest/src/external/stlplus/include/stlplus/textio.hpp Source File

Ratpac-two: /home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/latest/src/external/stlplus/include/stlplus/textio.hpp Source File
Ratpac-two
textio.hpp
1 #ifndef TEXTIO_HPP
2 #define TEXTIO_HPP
3 /*------------------------------------------------------------------------------
4 
5  Author: Andy Rushton
6  Copyright: (c) Andy Rushton, 2004
7  License: BSD License, see ../docs/license.html
8 
9  ------------------------------------------------------------------------------*/
10 #include <string>
11 #include <vector>
12 
13 #include "format_types.hpp"
14 #include "os_fixes.hpp"
15 
17 // Builtin error codes - you can add your own when you create a new derivative
18 // buffer in which case you may wish to overload the error_string function of
19 // the buffer to give a textual form to your errors.
20 // All internal error codes are negative to differentiate them from system error codes.
21 
22 extern const int textio_uninitialised;
23 extern const int textio_put_failed;
24 extern const int textio_format_error;
25 extern const int textio_get_failed;
26 extern const int textio_open_failed;
27 
29 // Text Output Device
31 
32 // declaring class as friend is no longer sufficient to inform compiler of
33 // type's existence as of gcc 4.1
34 class obuff;
35 class itext;
36 
37 class otext {
38  protected:
39  friend class obuff;
40  friend class itext;
41  obuff* m_buffer;
42 
43  public:
45  // Local Types
46  // These local enumerations are in the otext namespace
47 
48  // Newline conversion
49  // The user of TextIO should always use '\n' for newlines, then TextIO will do the conversions
50  // default: native
51  // I appended the _mode suffix to avoid conflicts with macros
52  enum newline_t {
53  binary_mode, // no end of line conversion
54  unix_mode, // Unix conversions (LF)
55  msdos_mode, // MS-DOS conversion (CR-LF)
56  macos_mode, // MacOS conversion (CR)
57  // the mode of the platform you compiled this on
58 #if defined(_WIN32)
59  native_mode = msdos_mode
60 #else
61  native_mode = unix_mode
62 #endif
63  };
64 #ifndef __CINT__
65  friend std::string to_string(newline_t);
66 #else
67  friend std::string to_string(int);
68 #endif
69  // Open Mode
70  // only used for otext devices where the two modes make sense, e.g. files but not pipes
71  enum open_t {
72  overwrite, // destroy previous contents (default)
73  append // append to previous contents
74  };
75 
76 #ifndef __CINT__
77  friend std::string to_string(open_t);
78 #else
79  friend std::string to_string(int);
80 #endif
81 
82  // profile for function manipulators
83  typedef void (*manipulator_function)(otext&);
84 
86  // Member functions
87 
88  // create an uninitialised otext
89  otext(void);
90 
91  // create an initialised otext by attaching a buffer (any derivative of obuff)
92  otext(obuff*);
93 
94  // close the otext if its open by deleting the buffer (thus calling its destructor)
95  virtual ~otext(void);
96 
97  // test whether this otext has a buffer attached
98  bool initialised(void) const;
99 
100  // initialise otext by attaching a buffer (any derivative of obuff)
101  // if a buffer is already attached, delete it first
102  void open(obuff*);
103 
104  // close the otext if its open by deleting the buffer (thus calling its destructor)
105  void close(void);
106 
107  // copy and assign create aliases - no deep copy is available - no deep copy makes sense!
108  otext(const otext&);
109  otext& operator=(const otext&);
110 
111  // test the buffer's error function
112  bool error(void) const;
113  int error_number(void) const;
114  std::string error_string(void) const;
115  void set_error(int error);
116  void clear_error(void);
117 
118  // newline control
119  void set_newline_mode(newline_t newline = native_mode);
120  void set_unix_mode(void);
121  void set_msdos_mode(void);
122  void set_macos_mode(void);
123  void set_native_mode(void);
124  void set_binary_mode(void);
125  newline_t newline_mode(void) const;
126  bool is_unix_mode(void) const;
127  bool is_msdos_mode(void) const;
128  bool is_macos_mode(void) const;
129  bool is_native_mode(void) const;
130  bool is_binary_mode(void) const;
131 
132  // integer formatting control
133  void set_integer_width(unsigned width = 0);
134  unsigned integer_width(void) const;
135  void set_integer_radix(unsigned radix = 10);
136  unsigned integer_radix(void) const;
137  void set_integer_display(radix_display_t display = radix_c_style_or_hash);
138  radix_display_t integer_display(void) const;
139 
140  // floating-point formatting control
141  void set_real_width(unsigned width = 0);
142  unsigned real_width(void) const;
143  void set_real_precision(unsigned width = 6);
144  unsigned real_precision(void) const;
145  void set_real_display(real_display_t display = display_mixed);
146  real_display_t real_display(void) const;
147 
148  // low-level character write
149  // put writes a single character
150  // it is an error to put negative values, specifically EOF (-1)
151  // the character must be unsigned char, since otherwise there are two definitions of -1
152  bool put(int ch);
153 
154  // composite operations on strings (C and STL) which repeatly call the above
155  bool put(const char*);
156  bool put(const std::string&);
157 
158  // number of characters written through the otext member functions
159  unsigned long bytes(void) const;
160  // line and column for last character written
161  // this only really has meaning if you are using text conversion mode (i.e. not binary)
162  unsigned line(void) const;
163  unsigned column(void) const;
164 
165  // flush the buffer explicitly (for example to synchronise standard output with standard input)
166  void flush(void);
167 
168  // test whether the device is capable of accepting/not accepting output
169  operator bool(void) const;
170  bool operator!(void) const;
171 
172  // the pipe operators << are the main functions used with otext and its derivates
173  // they are used in the form:
174  // device << object1 << object2 << object3;
175 
176  // single character
177  // TODO - wide char
178  otext& operator<<(char);
179  otext& operator<<(signed char);
180  otext& operator<<(unsigned char);
181 
182  // string output
183  otext& operator<<(const char*);
184  otext& operator<<(const std::string&);
185 
186  // string vector - writes whole array as a series of newline separated strings
187  otext& operator<<(const std::vector<std::string>&);
188 
189  // integer output
190  otext& operator<<(bool);
191  otext& operator<<(short);
192  otext& operator<<(unsigned short);
193  otext& operator<<(int);
194  otext& operator<<(unsigned int);
195  otext& operator<<(long);
196  otext& operator<<(unsigned long);
197 
198  // floating point output
199  otext& operator<<(float);
200  otext& operator<<(double);
201 
202  // pointer output, compatible with >> operator for void*;
203  otext& operator<<(const void*);
204 
205  // manipulator - applies passed function to stream;
206  otext& operator<<(manipulator_function);
207 
208  // pipe operator - pours one stream into the other until eof();
209  // this is an easy way to copy one device into another
210  otext& operator<<(itext&);
211 };
212 
214 // output manipulators, used in the form: fout << flush << endl;
215 
216 // simply flush the buffer (if any)
217 void flush(otext&);
218 
219 // write a newline, iostream style
220 void endl(otext&);
221 
222 // close the device
223 void close(otext&);
224 
225 // set radix to hex/octal/decimal
226 void hex(otext&);
227 void oct(otext&);
228 void dec(otext&);
229 
230 // the following used to be manipulators, but for efficiency they are now defined as consts;
231 const char newline = '\n';
232 const char space = ' ';
233 const char tab = '\t';
234 const char null = '\0';
235 
237 // Text Input Device
239 
240 class ibuff;
241 
242 class itext {
243  protected:
244  friend class otext;
245  friend class ibuff;
246  ibuff* m_buffer;
247 
248  public:
250  // Newline conversion
251  // When conversion is on, all end-of-line conventions (LF, CR-LF, CR) will be converted into a '\n'
252  // When conversion is off, data comes in unmodified - note that this can make the eoln() test act strangely
253  enum newline_t {
254  binary_mode, // no end of line conversion
255  convert_mode // recognise and convert all end-of-line conventions
256  };
257 #ifndef __CINT__
258  friend std::string to_string(newline_t);
259 #else
260  friend std::string to_string(int);
261 #endif
262 
263  typedef void (*manipulator_function)(itext&);
264 
265  public:
266  // create an uninitialised itext
267  itext(void);
268 
269  // create an itext and initialise it with a buffer (any derivative of ibuff)
270  itext(ibuff*);
271 
272  // closes the itext if it is open and destroys any structures - including the buffer
273  virtual ~itext(void);
274 
275  // test whether the itext has a buffer attached
276  bool initialised(void) const;
277 
278  // attach a buffer (any derivative of ibuff)
279  void open(ibuff*);
280 
281  // detach the buffer and delete it (causing the destructor and therefore any closedown actions to be called)
282  void close(void);
283 
284  // copy and assignment create aliases - it is not sensible to allow a deep copy (think about it)
285  itext(const itext&);
286  itext& operator=(const itext&);
287 
288  // test the buffer's error flag or retrieve its value
289  bool error(void) const;
290  int error_number(void) const;
291  std::string error_string(void) const;
292  void set_error(int error);
293  void clear_error(void);
294 
295  // formatting control
296  void set_newline_mode(newline_t newline = convert_mode);
297  void set_convert_mode(void);
298  void set_binary_mode(void);
299  newline_t newline_mode(void) const;
300  bool is_convert_mode(void);
301  bool is_binary_mode(void);
302 
303  // test for the two special 'characters' end-of-file (-1) and end-of-line (\n) using peek()
304  bool eof(void);
305  bool eoln(void);
306 
307  // low-level character access - peek allows one character lookahead, get allows one character to be read
308  // Both return -1 to indicate EOF
309  int peek(void);
310  int get(void);
311 
312  // number of characters read through the itext member functions
313  unsigned long bytes(void) const;
314  // line and column for last character read
315  // this only really has meaning if you are using text conversion mode (i.e. not binary)
316  unsigned line(void) const;
317  unsigned column(void) const;
318 
319  // tests for whether an itext has/hasn't got text to be read
320  bool good(void);
321  operator bool(void);
322  bool operator!(void);
323 
325  // input-pipe operators are the main usage of itext
326  // used in the form:
327  // device >> object1 >> object2 >> object3;
328  // device is any derivative of itext, object is any type with a >> operator defined (you can overload)
329 
330  // just get next character, including any whitespace or end of line character;
331  itext& operator>>(char&);
332  itext& operator>>(signed char&);
333  itext& operator>>(unsigned char&);
334 
335  // skipwhite before reading then read until a whitespace is found
336  // this is a kind of tokenising operator
337  itext& operator>>(std::string&);
338  // gets the whole line
339  bool getline(std::string& line);
340 
341  // get the whole file as a vector of strings, using newlines to split the input
342  itext& operator>>(std::vector<std::string>&);
343 
344  // integer operations: skipwhite then read an integer in any of the recognised formats:
345  // decimal: 12345
346  // octal: 012345
347  // hex: 0x12345
348  // hash: 13#12345
349  itext& operator>>(bool&);
350  itext& operator>>(short&);
351  itext& operator>>(unsigned short&);
352  itext& operator>>(int&);
353  itext& operator>>(unsigned int&);
354  itext& operator>>(long&);
355  itext& operator>>(unsigned long&);
356 
357  // real operations, skipwhite then read floating-point number - fraction and exponent are optional
358  itext& operator>>(float&);
359  itext& operator>>(double&);
360 
361  // Hide this function to make some GCC versions happy
362  // pointer operator, skipwhite, then reads a pointer written by << operator for void*
363  // itext& operator >> (void*&);
364 
365  // manipulator - applies passed manipulator function to stream;
366  itext& operator>>(manipulator_function);
367 
368  // pipe operator - pours one stream into the other until eof();
369  // this is the easiest way to make a copy
370  itext& operator>>(otext&);
371 };
372 
373 // manipulators, used in the form: fin >> skipwhite >> ch
374 
375 // skip all whitespace as defined by isspace() in <cctype>
376 void skipwhite(itext&);
377 
378 // skip up to one whitespace as defined by isspace() in <cctype>
379 void skiponewhite(itext&);
380 
381 // skip whitespace excluding end of line - i.e. only spacing characters
382 void skipspaces(itext&);
383 
384 // skip all text until and including the next end-of-line character
385 void skipline(itext&);
386 
387 // skip all whitespace, but stop after an end-of-line character
388 void skipendl(itext&);
389 
390 // close the device
391 void close(itext&);
392 
394 // Internals
396 
397 // Output Buffer
398 
399 class obuff {
400  public:
401  // constructor initialises output buffer with the mode fields - line buffering mode and newline conversion mode
402  obuff(bool line_buffer = false, otext::newline_t newline = otext::native_mode);
403 
404  // Total number of characters written through put()
405  // also number of lines and column number of last byte written
406  // Note this may not correspond to the number of characters sent to otext due to newline conversion
407  void increment(bool newline = false);
408  unsigned long bytes(void) const;
409  unsigned line(void) const;
410  unsigned column(void) const;
411 
412  // get line buffering mode - the TextIO device calls flush on every newline when in line beffering mode
413  void set_line_buffer(bool mode);
414  bool line_buffer(void) const;
415 
416  // get newline handling mode - used by TextIO device to process CR/LF characters
417  void set_newline_mode(otext::newline_t newline);
418  otext::newline_t newline_mode(void) const;
419 
420  // Integer formatting fields
421  // field width for next formattable integer type, default: 0
422  unsigned integer_width(void) const;
423  void set_integer_width(unsigned);
424  // base for integer display - from 2-36, default: 10
425  unsigned integer_radix(void) const;
426  void set_integer_radix(unsigned);
427  // how to display an integer - see string_utilities, default: c_style_or_hash
428  radix_display_t integer_display(void) const;
429  void set_integer_display(radix_display_t);
430 
431  // Real formatting fields
432  // field width for next formattable real type, default: 0
433  unsigned real_width(void) const;
434  void set_real_width(unsigned);
435  // number of significant digits
436  unsigned real_precision(void) const;
437  void set_real_precision(unsigned);
438  // how to display a real - see string_utilities, default: display_mixed
439  real_display_t real_display(void) const;
440  void set_real_display(real_display_t);
441 
442  // Error number
443  // zero if there is no error, any integer for an error
444  // this may be any of the definitions defined in <errno.h> or any derivative specific number
445  // this is set by otext itself but may also be set in any of your own derivatives
446  void set_error(int error);
447  void clear_error(void);
448  int error_number(void) const;
449  virtual std::string error_string(void) const;
450 
451  // Customisation
452  // To create a new output buffer, derive from this and then customise the
453  // virtual functions. Also provide sensible constructors - for example FileIO
454  // would have a constructor taking a filename and open mode. You only HAVE to
455  // provide the abstract function put(char), all others are optional. These
456  // functions are implicitly called by the otext class which contains your
457  // buffer.
458 
459  // you should provide a destructor (also virtual) if your buffer needs closedown actions such as closing a file/pipe
460 
461  // the default flush does nothing
462  // use this to flush any internal buffer - not relevant if your device isn't buffered
463  // note that if you are buffering, you should not rely on flush being called explicitly when the buffer is full
464  // - you need to manage the buffer yourself
465  // flush is called from TextIO to synchronise devices e.g. stdin and stdout
466  // It will be called on every newline if the buffer is in line buffer mode
467  virtual void flush(void);
468 
469  // the only operation that you must provide
470  // sends a single character to the target device
471  // returns the number of characters written (0 or 1)
472  virtual unsigned put(unsigned char) = 0;
473 
474  // use this to do any mopping up required on closing a buffer
475  // the default destructor does nothing - overload this only if you need to
476  virtual ~obuff(void);
477 
478  private:
479  // disallow copying
480  obuff& operator=(const obuff&);
481  obuff(const obuff&);
482 
483  protected:
484  friend class otext;
485  bool m_line_buffer;
486  otext::newline_t m_newline;
487  unsigned long m_bytes;
488  unsigned m_line;
489  unsigned m_column;
490  unsigned m_integer_width;
491  unsigned m_integer_radix;
492  radix_display_t m_integer_display;
493  unsigned m_real_width;
494  unsigned m_real_precision;
495  real_display_t m_real_display;
496  int m_error_number;
497  int m_aliases;
498 };
499 
500 // Input Buffer
501 
502 class ibuff {
503  public:
504  ibuff(itext::newline_t newline = itext::convert_mode);
505 
506  // Total number of characters read through get()
507  // also number of lines and column number of last byte read
508  // Note this may not correspond to the number of characters read from the device due to newline conversion
509  void increment(bool newline = false);
510  unsigned long bytes(void) const;
511  unsigned line(void) const;
512  unsigned column(void) const;
513 
514  // get newline handling mode - used by TextIO device to process CR/LF characters
515  void set_newline_mode(itext::newline_t newline);
516  itext::newline_t newline_mode(void) const;
517 
518  // Error number
519  // zero if there is no error, any integer for an error
520  // this may be any of the errno definitions defined in <errno.h> or any derivative specific number
521  // this is set by itext itself but may also be set in any of your own derivatives
522  void set_error(int error);
523  void clear_error(void);
524  int error_number(void) const;
525  virtual std::string error_string(void) const;
526 
527  // Customisation
528  // create a new buffer by customising the following functions
529  // you should also provide a destructor (also virtual) if your buffer needs closedown actions
530  // it REALLY IS as simple as that - see the existing derivatives for examples
531 
532  // peek and get are abstract functions, so you must provide them
533  // peek gets the next character without consuming it, get gets the same character but also consumes it
534  // this means that text input requires one-character lookahead
535  virtual int peek(void) = 0;
536  virtual int get(void) = 0;
537 
538  // do any mopping up and close the input
539  // default destructor does nothing
540  virtual ~ibuff(void);
541 
542  private:
543  // disallow copying
544  ibuff& operator=(const ibuff&);
545  ibuff(const ibuff&);
546 
547  protected:
548  friend class itext;
549  itext::newline_t m_newline_mode;
550  unsigned long m_bytes;
551  unsigned m_line;
552  unsigned m_column;
553  int m_error_number;
554  int m_aliases;
555 };
556 
558 #endif
Definition: textio.hpp:502
Definition: textio.hpp:242
Definition: textio.hpp:399
Definition: textio.hpp:37