home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / gnu / lib / g++-include / streambuf.h < prev    next >
C/C++ Source or Header  |  1994-12-22  |  16KB  |  469 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. // #define _IO_NEW_STREAMS // Future optimization, turned off for now.
  34.  
  35. extern "C" {
  36. #include <libio.h>
  37. }
  38. //#include <_G_config.h>
  39. #ifdef _IO_NEED_STDARG_H
  40. #include <stdarg.h>
  41. #endif
  42. #ifndef _IO_va_list
  43. #define _IO_va_list char *
  44. #endif
  45.  
  46. #ifndef EOF
  47. #define EOF (-1)
  48. #endif
  49. #ifndef NULL
  50. #ifdef __GNUC__
  51. #define NULL ((void*)0)
  52. #else
  53. #define NULL (0)
  54. #endif
  55. #endif
  56.  
  57. #ifndef _IO_wchar_t
  58. #define _IO_wchar_t short
  59. #endif
  60.  
  61. class istream; /* Work-around for a g++ name mangling bug. Fixed in 2.6. */
  62. class ostream; class streambuf;
  63.  
  64. // In case some header files defines these as macros.
  65. #undef open
  66. #undef close
  67.  
  68. extern "C" int __underflow(struct _IO_FILE*);
  69. extern "C" int __overflow(struct _IO_FILE*, int);
  70.  
  71. typedef _IO_off_t streamoff;
  72. typedef _IO_fpos_t streampos;
  73. typedef _IO_ssize_t streamsize;
  74.  
  75. typedef unsigned long __fmtflags;
  76. typedef unsigned char __iostate;
  77.  
  78. struct _ios_fields
  79. { // The data members of an ios.
  80.   // Directly using _strbuf is dangerous, because the vtable
  81.   // pointer can be NULL.  Use rdbuf() when in doubt.
  82.     streambuf *_strbuf;
  83.     ostream* _tie;
  84.     int _width;
  85.     __fmtflags _flags;
  86.     _IO_wchar_t _fill;
  87.     __iostate _state;
  88.     __iostate _exceptions;
  89.     int _precision;
  90.  
  91.     void *_arrays; /* Support for ios::iword and ios::pword. */
  92. };
  93.  
  94. #define _IOS_GOOD    0
  95. #define _IOS_EOF    1
  96. #define _IOS_FAIL    2
  97. #define _IOS_BAD    4
  98.  
  99. #define _IO_INPUT    1
  100. #define _IO_OUTPUT    2
  101. #define _IO_ATEND    4
  102. #define _IO_APPEND    8
  103. #define _IO_TRUNC    16
  104. #define _IO_NOCREATE    32
  105. #define _IO_NOREPLACE    64
  106. #define _IO_BIN        128
  107.  
  108. #ifdef _STREAM_COMPAT
  109. enum state_value {
  110.     _good = _IOS_GOOD,
  111.     _eof = _IOS_EOF,
  112.     _fail = _IOS_FAIL,
  113.     _bad = _IOS_BAD };
  114. enum open_mode {
  115.     input = _IO_INPUT,
  116.     output = _IO_OUTPUT,
  117.     atend = _IO_ATEND,
  118.     append = _IO_APPEND };
  119. #endif
  120.  
  121. class ios : public _ios_fields {
  122.   ios& operator=(ios&);  /* Not allowed! */
  123.   public:
  124.     typedef __fmtflags fmtflags;
  125.     typedef int iostate;
  126.     typedef int openmode;
  127.     typedef int streamsize;
  128.     enum io_state {
  129.     goodbit = _IOS_GOOD,
  130.     eofbit = _IOS_EOF,
  131.     failbit = _IOS_FAIL,
  132.     badbit = _IOS_BAD };
  133.     enum open_mode {
  134.     in = _IO_INPUT,
  135.     out = _IO_OUTPUT,
  136.     ate = _IO_ATEND,
  137.     app = _IO_APPEND,
  138.     trunc = _IO_TRUNC,
  139.     nocreate = _IO_NOCREATE,
  140.     noreplace = _IO_NOREPLACE,
  141.     bin = _IOS_BIN };
  142.     enum seek_dir { beg, cur, end};
  143.     // ANSI: typedef enum seek_dir seekdir; etc
  144.     // NOTE: If adding flags here, before to update ios::bitalloc().
  145.     enum { skipws=_IO_SKIPWS,
  146.        left=_IO_LEFT, right=_IO_RIGHT, internal=_IO_INTERNAL,
  147.        dec=_IO_DEC, oct=_IO_OCT, hex=_IO_HEX,
  148.        showbase=_IO_SHOWBASE, showpoint=_IO_SHOWPOINT,
  149.        uppercase=_IO_UPPERCASE, showpos=_IO_SHOWPOS,
  150.        scientific=_IO_SCIENTIFIC, fixed=_IO_FIXED,
  151.        unitbuf=_IO_UNITBUF, stdio=_IO_STDIO
  152. #ifndef _IO_NEW_STREAMS
  153.        , dont_close=_IO_DONT_CLOSE // Don't delete streambuf on stream destruction
  154. #endif
  155.        };
  156.     enum { // Masks.
  157.     basefield=dec+oct+hex,
  158.     floatfield = scientific+fixed,
  159.     adjustfield = left+right+internal
  160.     };
  161.  
  162. #ifdef _IO_THROW
  163.     class failure : public xmsg {
  164.     ios* _stream;
  165.       public:
  166.     failure(ios* stream) { _stream = stream; }
  167.     failure(string cause, ios* stream) { _stream = stream; }
  168.     ios* rdios() const { return _stream; }
  169.     };
  170. #endif
  171.  
  172.     ostream* tie() const { return _tie; }
  173.     ostream* tie(ostream* val) { ostream* save=_tie; _tie=val; return save; }
  174.  
  175.     // Methods to change the format state.
  176.     _IO_wchar_t fill() const { return (_IO_wchar_t)_fill; }
  177.     _IO_wchar_t fill(_IO_wchar_t newf)
  178.     {_IO_wchar_t oldf = (_IO_wchar_t)_fill; _fill = (char)newf; return oldf;}
  179.     fmtflags flags() const { return _flags; }
  180.     fmtflags flags(fmtflags new_val) {
  181.     fmtflags old_val = _flags; _flags = new_val; return old_val; }
  182.     int precision() const { return _precision; }
  183.     int precision(int newp) {
  184.     unsigned short oldp = _precision; _precision = (unsigned short)newp;
  185.     return oldp; }
  186.     fmtflags setf(fmtflags val) {
  187.     fmtflags oldbits = _flags;
  188.     _flags |= val; return oldbits; }
  189.     fmtflags setf(fmtflags val, fmtflags mask) {
  190.     fmtflags oldbits = _flags;
  191.     _flags = (_flags & ~mask) | (val & mask); return oldbits; }
  192.     fmtflags unsetf(fmtflags mask) {
  193.     fmtflags oldbits = _flags;
  194.     _flags &= ~mask; return oldbits; }
  195.     int width() const { return _width; }
  196.     int width(int val) { int save = _width; _width = val; return save; }
  197.  
  198. #ifdef _IO_THROW
  199.     void _throw_failure() const { throw new ios::failure(this); }
  200. #else
  201.     void _throw_failure() const { }
  202. #endif
  203. #ifdef _STREAM_COMPAT
  204.     void _IO_fix_vtable();  /* TEMPORARY - for binary compatibility */
  205.     void _IO_fix_vtable() const; /* TEMPORARY - for binary compatibility */
  206. #endif
  207.     void clear(iostate state = 0) {
  208.     _state = _strbuf ? state : state|badbit;
  209.     if (_state & _exceptions) _throw_failure(); }
  210.     void set(iostate flag) { _state |= flag;
  211.     if (_state & _exceptions) _throw_failure(); }
  212.     void setstate(iostate flag) { _state |= flag; // ANSI
  213.     if (_state & _exceptions) _throw_failure(); }
  214.     int good() const { return _state == 0; }
  215.     int eof() const { return _state & ios::eofbit; }
  216.     int fail() const { return _state & (ios::badbit|ios::failbit); }
  217.     int bad() const { return _state & ios::badbit; }
  218.     iostate rdstate() const { return _state; }
  219.     operator void*() const { return fail() ? (void*)0 : (void*)(-1); }
  220.     int operator!() const { return fail(); }
  221.     iostate exceptions() const { return _exceptions; }
  222.     void exceptions(iostate enable) {
  223.     _exceptions = enable;
  224.     if (_state & _exceptions) _throw_failure(); }
  225.  
  226.     streambuf* rdbuf() const { return _strbuf; }
  227.     streambuf* rdbuf(streambuf *_s) {
  228.       streambuf *_old = _strbuf; _strbuf = _s; clear (); return _old; }
  229.  
  230.     static int sync_with_stdio(int on);
  231.     static void sync_with_stdio() { sync_with_stdio(1); }
  232.     static fmtflags bitalloc();
  233.     static int xalloc();
  234.     void*& pword(int);
  235.     void* pword(int) const;
  236.     long& iword(int);
  237.     long iword(int) const;
  238.  
  239. #ifdef _STREAM_COMPAT
  240.     void unset(state_value flag) { _state &= ~flag; }
  241.     void close();
  242.     int is_open();
  243.     int readable();
  244.     int writable();
  245. #endif
  246.  
  247.     // Used to initialize standard streams. Not needed in this implementation.
  248.     class Init {
  249.     public:
  250.       Init () { }
  251.     };
  252.  
  253.   protected:
  254.     ios(streambuf* sb = 0, ostream* tie_to = 0);
  255.     virtual ~ios();
  256.     void init(streambuf* sb, ostream* tie = 0);
  257. };
  258.  
  259. #if __GNUG__==1
  260. typedef int _seek_dir;
  261. #else
  262. typedef ios::seek_dir _seek_dir;
  263. #endif
  264.  
  265. // Magic numbers and bits for the _flags field.
  266. // The magic numbers use the high-order bits of _flags;
  267. // the remaining bits are abailable for variable flags.
  268. // Note: The magic numbers must all be negative if stdio
  269. // emulation is desired.
  270.  
  271. // A streammarker remembers a position in a buffer.
  272. // You are guaranteed to be able to seek back to it if it is saving().
  273. class streammarker : private _IO_marker {
  274.     friend class streambuf;
  275.     void