home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 5 / FreshFish_July-August1994.bin / bbs / gnu / libg++-2.5.3-bin.lha / lib / g++-include / streambuf.h < prev    next >
C/C++ Source or Header  |  1994-07-11  |  16KB  |  461 lines

  1. /* This is part of libio/iostream, providing -*- C++ -*- input/output.
  2. Copyright (C) 1993 Free Software Foundation
  3.  
  4. This file is part of the GNU IO Library.  This library is free
  5. software; you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the
  7. Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU CC; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. As a special exception, if you link this library with files
  20. compiled with a GNU compiler to produce an executable, this does not cause
  21. the resulting executable to be covered by the GNU General Public License.
  22. This exception does not however invalidate any other reasons why
  23. the executable file might be covered by the GNU General Public License. */
  24.  
  25. #ifndef _STREAMBUF_H
  26. #define _STREAMBUF_H
  27. #ifdef __GNUG__
  28. #pragma interface
  29. #endif
  30.  
  31. /* #define _G_IO_THROW */ /* Not implemented:  ios::failure */
  32.  
  33. extern "C" {
  34. #include <libio.h>
  35. }
  36. //#include <_G_config.h>
  37. #ifdef _IO_NEED_STDARG_H
  38. #include <stdarg.h>
  39. #endif
  40. #ifndef _IO_va_list
  41. #define _IO_va_list char *
  42. #endif
  43.  
  44. #ifndef EOF
  45. #define EOF (-1)
  46. #endif
  47. #ifndef NULL
  48. #ifdef __GNUC__
  49. #define NULL ((void*)0)
  50. #else
  51. #define NULL (0)
  52. #endif
  53. #endif
  54.  
  55. #ifndef _IO_wchar_t
  56. #define _IO_wchar_t short
  57. #endif
  58.  
  59. class istream; /* Work-around for a g++ name mangling bug. */
  60. class ostream; class streambuf;
  61.  
  62. // In case some header files defines these as macros.
  63. #undef open
  64. #undef close
  65.  
  66. extern "C" int __underflow(struct _IO_FILE*);
  67. extern "C" int __overflow(struct _IO_FILE*, int);
  68.  
  69. typedef _IO_off_t streamoff;
  70. typedef _IO_fpos_t streampos;
  71. typedef _IO_ssize_t streamsize;
  72.  
  73. typedef unsigned long __fmtflags;
  74. typedef unsigned char __iostate;
  75.  
  76. struct _ios_fields
  77. { // The data members of an ios.
  78.   // Directly using _strbuf is dangerous, because the vtable
  79.   // pointer can be NULL.  Use rdbuf() when in doubt.
  80.     streambuf *_strbuf;
  81.     ostream* _tie;
  82.     int _width;
  83.     __fmtflags _flags;
  84.     _IO_wchar_t _fill;
  85.     __iostate _state;
  86.     __iostate _exceptions;
  87.     int _precision;
  88.  
  89.     void *_arrays; /* Support for ios::iword and ios::pword. */
  90. };
  91.  
  92. #define _IOS_GOOD    0
  93. #define _IOS_EOF    1
  94. #define _IOS_FAIL    2
  95. #define _IOS_BAD    4
  96.  
  97. #define _IO_INPUT    1
  98. #define _IO_OUTPUT    2
  99. #define _IO_ATEND    4
  100. #define _IO_APPEND    8
  101. #define _IO_TRUNC    16
  102. #define _IO_NOCREATE    32
  103. #define _IO_NOREPLACE    64
  104. #define _IO_BIN        128
  105.  
  106. #ifdef _STREAM_COMPAT
  107. enum state_value {
  108.     _good = _IOS_GOOD,
  109.     _eof = _IOS_EOF,
  110.     _fail = _IOS_FAIL,
  111.     _bad = _IOS_BAD };
  112. enum open_mode {
  113.     input = _IO_INPUT,
  114.     output = _IO_OUTPUT,
  115.     atend = _IO_ATEND,
  116.     append = _IO_APPEND };
  117. #endif
  118.  
  119. class ios : public _ios_fields {
  120.   public:
  121.     typedef __fmtflags fmtflags;
  122.     typedef int iostate;
  123.     typedef int openmode;
  124.     typedef int streamsize;
  125.     enum io_state {
  126.     goodbit = _IOS_GOOD,
  127.     eofbit = _IOS_EOF,
  128.     failbit = _IOS_FAIL,
  129.     badbit = _IOS_BAD };
  130.     enum open_mode {
  131.     in = _IO_INPUT,
  132.     out = _IO_OUTPUT,
  133.     ate = _IO_ATEND,
  134.     app = _IO_APPEND,
  135.     trunc = _IO_TRUNC,
  136.     nocreate = _IO_NOCREATE,
  137.     noreplace = _IO_NOREPLACE,
  138.     bin = _IOS_BIN };
  139.     enum seek_dir { beg, cur, end};
  140.     // ANSI: typedef enum seek_dir seekdir; etc
  141.     // NOTE: If adding flags here, before to update ios::bitalloc().
  142.     enum { skipws=_IO_SKIPWS,
  143.        left=_IO_LEFT, right=_IO_RIGHT, internal=_IO_INTERNAL,
  144.        dec=_IO_DEC, oct=_IO_OCT, hex=_IO_HEX,
  145.        showbase=_IO_SHOWBASE, showpoint=_IO_SHOWPOINT,
  146.        uppercase=_IO_UPPERCASE, showpos=_IO_SHOWPOS,
  147.        scientific=_IO_SCIENTIFIC, fixed=_IO_FIXED,
  148.        unitbuf=_IO_UNITBUF, stdio=_IO_STDIO,
  149.        dont_close=_IO_DONT_CLOSE // Don't delete streambuf on stream destruction
  150.        };
  151.     enum { // Masks.
  152.     basefield=dec+oct+hex,
  153.     floatfield = scientific+fixed,
  154.     adjustfield = left+right+internal
  155.     };
  156.  
  157. #ifdef _IO_THROW
  158.     class failure : public xmsg {
  159.     ios* _stream;
  160.       public:
  161.     failure(ios* stream) { _stream = stream; }
  162.     failure(string cause, ios* stream) { _stream = stream; }
  163.     ios* rdios() const { return _stream; }
  164.     };
  165. #endif
  166.  
  167.     ostream* tie() const { return _tie; }
  168.     ostream* tie(ostream* val) { ostream* save=_tie; _tie=val; return save; }
  169.  
  170.     // Methods to change the format state.
  171.     _IO_wchar_t fill() const { return (_IO_wchar_t)_fill; }
  172.     _IO_wchar_t fill(_IO_wchar_t newf)
  173.     {_IO_wchar_t oldf = (_IO_wchar_t)_fill; _fill = (char)newf; return oldf;}
  174.     fmtflags flags() const { return _flags; }
  175.     fmtflags flags(fmtflags new_val) {
  176.     fmtflags old_val = _flags; _flags = new_val; return old_val; }
  177.     int precision() const { return _precision; }
  178.     int precision(int newp) {
  179.     unsigned short oldp = _precision; _precision = (unsigned short)newp;
  180.     return oldp; }
  181.     fmtflags setf(fmtflags val) {
  182.     fmtflags oldbits = _flags;
  183.     _flags |= val; return oldbits; }
  184.     fmtflags setf(fmtflags val, fmtflags mask) {
  185.     fmtflags oldbits = _flags;
  186.     _flags = (_flags & ~mask) | (val & mask); return oldbits; }
  187.     fmtflags unsetf(fmtflags mask) {
  188.     fmtflags oldbits = _flags;
  189.     _flags &= ~mask; return oldbits; }
  190.     int width() const { return _width; }
  191.     int width(int val) { int save = _width; _width = val; return save; }
  192.  
  193. #ifdef _IO_THROW
  194.     void _throw_failure() const { throw new ios::failure(this); }
  195. #else
  196.     void _throw_failure() const { }
  197. #endif
  198.     void _IO_fix_vtable();  /* TEMPORARY - for binary compatibility */
  199.     void _IO_fix_vtable() const;
  200.     streambuf* rdbuf() const;
  201. #if 0
  202.     streambuf* rdbuf(streambuf *_s) {
  203.       streambuf *_old = _strbuf; _strbuf = _s; return _old; }
  204. #endif
  205.     void clear(iostate state = 0) {
  206.     _state = _strbuf ? state : state|badbit;
  207.     if (_state & _exceptions) _throw_failure(); }
  208.     void set(iostate flag) { _state |= flag;
  209.     if (_state & _exceptions) _throw_failure(); }
  210.     void setstate(iostate flag) { _state |= flag; // ANSI
  211.     if (_state & _exceptions) _throw_failure(); }
  212.     int good() const { return _state == 0; }
  213.     int eof() const { return _state & ios::eofbit; }
  214.     int fail() const { return _state & (ios::badbit|ios::failbit); }
  215.     int bad() const { return _state & ios::badbit; }
  216.     iostate rdstate() const { return _state; }
  217.     operator void*() const { return fail() ? (void*)0 : (void*)(-1); }
  218.     int operator!() const { return fail(); }
  219.     iostate exceptions() const { return _exceptions; }
  220.     void exceptions(iostate enable) {
  221.     _exceptions = enable;
  222.     if (_state & _exceptions) _throw_failure(); }
  223.  
  224.     static int sync_with_stdio(int on);
  225.     static void sync_with_stdio() { sync_with_stdio(1); }
  226.     static fmtflags bitalloc();
  227.     static int xalloc();
  228.     void*& pword(int);
  229.     void* pword(int) const;
  230.     long& iword(int);
  231.     long iword(int) const;
  232.  
  233. #ifdef _STREAM_COMPAT
  234.     void unset(state_value flag) { _state &= ~flag; }
  235.     void close();
  236.     int is_open();
  237.     int readable();
  238.     int writable();
  239. #endif
  240.  
  241.     // Used to initialize standard streams. Not needed in this implementation.
  242.     class Init {
  243.     public:
  244.       Init () { }
  245.     };
  246.  
  247.   protected:
  248.     ios(streambuf* sb = 0, ostream* tie = 0);
  249.     virtual ~ios();
  250.     void init(streambuf* sb) { _state=0; _strbuf=sb; }
  251. };
  252.  
  253. #if __GNUG__==1
  254. typedef int _seek_dir;
  255. #else
  256. typedef ios::seek_dir _seek_dir;
  257. #endif
  258.  
  259. // Magic numbers and bits for the _flags field.
  260. // The magic numbers use the high-order bits of _flags;
  261. // the remaining bits are abailable for variable flags.
  262. // Note: The magic numbers must all be negative if stdio
  263. // emulation is desired.
  264.  
  265. // A streammarker remembers a position in a buffer.
  266. // You are guaranteed to be able to seek back to it if it is saving().
  267. class streammarker : private _IO_marker {
  268.     friend class streambuf;
  269.     void set_offset(int offset) { _pos = offset; }
  270.   public:
  271.     streammarker(streambuf *sb);
  272.     ~streammarker();
  273.     int saving() { return  1; }
  274.     int delta(streammarker&);
  275.     int delta();
  276. };
  277.  
  278. extern unsigned __adjust_column(unsigned start, const cha