home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / GCC / GERLIB_USR08B.LHA / gerlib / support / g++include / streambuf.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-12  |  16.9 KB  |  510 lines

  1. //    This is part of the iostream library, providing -*- C++ -*- input/output.
  2. //    Copyright (C) 1991 Per Bothner.
  3. //
  4. //    This library is free software; you can redistribute it and/or
  5. //    modify it under the terms of the GNU Library General Public
  6. //    License as published by the Free Software Foundation; either
  7. //    version 2 of the License, or (at your option) any later version.
  8. //
  9. //    This library is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. //    Library General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU Library General Public
  15.  
  16.  
  17. #ifndef _STREAMBUF_H
  18. #define _STREAMBUF_H
  19. #ifdef __GNUG__
  20. #pragma interface
  21. #endif
  22.  
  23. /* #define _G_IO_THROW */ /* Not implemented:  ios::failure */
  24.  
  25. #include <_G_config.h>
  26. #ifdef _G_NEED_STDARG_H
  27. #include <stdarg.h>
  28. #endif
  29.  
  30. #ifndef EOF
  31. #define EOF (-1)
  32. #endif
  33. #ifndef NULL
  34. #ifdef __GNUC__
  35. #define NULL ((void*)0)
  36. #else
  37. #define NULL (0)
  38. #endif
  39. #endif
  40.  
  41. class ostream; class streambuf; class backupbuf;
  42.  
  43. // In case some header files defines these as macros.
  44. #undef open
  45. #undef close
  46.  
  47. #ifdef _G_FRIEND_BUG
  48. extern int __UNDERFLOW(streambuf*);
  49. extern int __OVERFLOW(streambuf*, int);
  50. #endif
  51. extern "C" int __underflow(streambuf*);
  52. extern "C" int __overflow(streambuf*, int);
  53.  
  54. typedef _G_off_t streamoff;
  55. typedef _G_off_t streampos; // Should perhaps be _G_fpos_t ?
  56.  
  57. typedef unsigned long __fmtflags;
  58. typedef unsigned char __iostate;
  59.  
  60. struct _ios_fields { // The data members of an ios.
  61.     streambuf *_strbuf;
  62.     ostream* _tie;
  63.     int _width;
  64.     __fmtflags _flags;
  65.     _G_wchar_t _fill;
  66.     __iostate _state;
  67.     __iostate _exceptions;
  68.     int _precision;
  69. };
  70.  
  71. #define _IOS_GOOD    0
  72. #define _IOS_EOF    1
  73. #define _IOS_FAIL    2
  74. #define _IOS_BAD    4
  75.  
  76. #define _IOS_INPUT    1
  77. #define _IOS_OUTPUT    2
  78. #define _IOS_ATEND    4
  79. #define _IOS_APPEND    8
  80. #define _IOS_TRUNC    16
  81. #define _IOS_NOCREATE    32
  82. #define _IOS_NOREPLACE    64
  83. #define _IOS_BIN    128
  84.  
  85. #ifdef _STREAM_COMPAT
  86. enum state_value {
  87.     _good = _IOS_GOOD,
  88.     _eof = _IOS_EOF,
  89.     _fail = _IOS_FAIL,
  90.     _bad = _IOS_BAD };
  91. enum open_mode {
  92.     input = _IOS_INPUT,
  93.     output = _IOS_OUTPUT,
  94.     atend = _IOS_ATEND,
  95.     append = _IOS_APPEND };
  96. #endif
  97.  
  98. class ios : public _ios_fields {
  99.   public:
  100.     typedef __fmtflags fmtflags;
  101.     typedef int iostate;
  102.     typedef int openmode;
  103.     typedef int streamsize;
  104.     enum io_state {
  105.     goodbit = _IOS_GOOD,
  106.     eofbit = _IOS_EOF,
  107.     failbit = _IOS_FAIL,
  108.     badbit = _IOS_BAD };
  109.     enum open_mode {
  110.     in = _IOS_INPUT,
  111.     out = _IOS_OUTPUT,
  112.     ate = _IOS_ATEND,
  113.     app = _IOS_APPEND,
  114.     trunc = _IOS_TRUNC,
  115.     nocreate = _IOS_NOCREATE,
  116.     noreplace = _IOS_NOREPLACE,
  117.     bin = _IOS_BIN };
  118.     enum seek_dir { beg, cur, end};
  119.     // ANSI: typedef enum seek_dir seekdir; etc
  120.     enum { skipws=01, left=02, right=04, internal=010,
  121.        dec=020, oct=040, hex=0100,
  122.        showbase=0200, showpoint=0400, uppercase=01000, showpos=02000,
  123.        scientific=04000, fixed=010000, unitbuf=020000, stdio=040000,
  124.        dont_close=0100000 //Don't delete streambuf on stream destruction
  125.        };
  126.     enum { // Masks.
  127.     basefield=dec+oct+hex,
  128.     floatfield = scientific+fixed,
  129.     adjustfield = left+right+internal
  130.     };
  131.  
  132. #ifdef _G_IO_THROW
  133.     class failure : public xmsg {
  134.     ios* _stream;
  135.       public:
  136.     failure(ios* stream) { _stream = stream; }
  137.     failure(string cause, ios* stream) { _stream = stream; }
  138.     ios* rdios() const { return _stream; }
  139.     };
  140. #endif
  141.  
  142.     ostream* tie() const { return _tie; }
  143.     ostream* tie(ostream* val) { ostream* save=_tie; _tie=val; return save; }
  144.  
  145.     // Methods to change the format state.
  146.     _G_wchar_t fill() const { return (_G_wchar_t)_fill; }
  147.     _G_wchar_t fill(_G_wchar_t newf)
  148.     {_G_wchar_t oldf = (_G_wchar_t)_fill; _fill = (char)newf; return oldf;}
  149.     fmtflags flags() const { return _flags; }
  150.     fmtflags flags(fmtflags new_val) {
  151.     fmtflags old_val = _flags; _flags = new_val; return old_val; }
  152.     int precision() const { return _precision; }
  153.     int precision(int newp) {
  154.     unsigned short oldp = _precision; _precision = (unsigned short)newp;
  155.     return oldp; }
  156.     fmtflags setf(fmtflags val) {
  157.     fmtflags oldbits = _flags;
  158.     _flags |= val; return oldbits; }
  159.     fmtflags setf(fmtflags val, fmtflags mask) {
  160.     fmtflags oldbits = _flags;
  161.     _flags = (_flags & ~mask) | (val & mask); return oldbits; }
  162.     fmtflags unsetf(fmtflags mask) {
  163.     fmtflags oldbits = _flags & mask;
  164.     _flags &= ~mask; return oldbits; }
  165.     int width() const { return _width; }
  166.     int width(int val) { int save = _width; _width = val; return save; }
  167.  
  168. #ifdef _G_IO_THROW
  169.     void _throw_failure() { throw new ios::failure(this); }
  170. #else
  171.     void _throw_failure() { }
  172. #endif
  173.  
  174.     streambuf* rdbuf() const { return _strbuf; }
  175.     void clear(iostate state = 0) {
  176.     _state = _strbuf ? state : state|badbit;
  177.     if (_state & _exceptions) _throw_failure(); }
  178.     void set(iostate flag) { _state |= flag;
  179.     if (_state & _exceptions) _throw_failure(); }
  180.     void setstate(iostate flag) { _state |= flag; // ANSI
  181.     if (_state & _exceptions) _throw_failure(); }
  182.     int good() const { return _state == 0; }
  183.     int eof() const { return _state & ios::eofbit; }
  184.     int fail() const { return _state & (ios::badbit|ios::failbit); }
  185.     int bad() const { return _state & ios::badbit; }
  186.     iostate rdstate() const { return _state; }
  187.     operator void*() const { return fail() ? (void*)0 : (void*)(-1); }
  188.     int operator!() const { return fail(); }
  189.     iostate exceptions() const { return _exceptions; }
  190.     void exceptions(iostate enable) {
  191.     _exceptions = enable;
  192.     if (_state & _exceptions) _throw_failure(); }
  193.  
  194.     static int sync_with_stdio(int on);
  195.     static void sync_with_stdio() { sync_with_stdio(1); }
  196.  
  197. #ifdef _STREAM_COMPAT
  198.     void unset(state_value flag) { _state &= ~flag; }
  199.     void close();
  200.     int is_open();
  201.     int readable();
  202.     int writable();
  203. #endif
  204.  
  205.     // Used to initialize standard streams. Not needed in this implementation.
  206.     class Init {
  207.     public:
  208.       Init () { }
  209.     };
  210.  
  211.   protected:
  212.     ios(streambuf* sb = 0, ostream* tie = 0);
  213.     virtual ~ios();
  214.     void init(streambuf* sb) { _state=0; _strbuf=sb; }
  215. };
  216.  
  217. #if __GNUG__==1
  218. typedef int _seek_dir;
  219. #else
  220. typedef ios::seek_dir _seek_dir;
  221. #endif
  222.  
  223. // Magic numbers and bits for the _flags field.
  224. // The magic numbers use the high-order bits of _flags;
  225. // the remaining bits are abailable for variable flags.
  226. // Note: The magic numbers must all be negative if stdio
  227. // emulation is desired.
  228.  
  229. #define _IO_MAGIC 0xFBAD0000 /* Magic number */
  230. #define _OLD_STDIO_MAGIC 0xFABC0000 /* Emulate old stdio. */
  231. #define _IO_MAGIC_MASK 0xFFFF0000
  232. #define _S_USER_BUF 1 /* User owns buffer; don't delete it on close. */
  233. #define _S_UNBUFFERED 2
  234. #define _S_NO_READS 4 /* Reading not allowed */
  235. #define _S_NO_WRITES 8 /* Writing not allowd */
  236. #define _S_EOF_SEEN 0x10
  237. #define _S_ERR_SEEN 0x20
  238. #define _S_DELETE_DONT_CLOSE 0x40
  239. #define _S_LINKED 0x80 // Set if linked (using _chain) to streambuf::_list_all.
  240. #define _S_IN_BACKUP 0x100
  241. #define _S_LINE_BUF 0x200
  242. #define _S_TIED_PUT_GET 0x400 // Set if put and get pointer logicly tied.
  243. #define _S_CURRENTLY_PUTTING 0x800
  244. #define _S_IS_APPENDING 0x1000
  245. #define _S_IS_BACKUPBUF 0x4000
  246. #define _S_IS_FILEBUF 0x8000
  247.  
  248. // A streammarker remembers a position in a buffer.
  249. // You are guaranteed to be able to seek back to it if it is saving().
  250. class streammarker {
  251.     friend class streambuf;
  252. #ifdef _G_FRIEND_BUG
  253.     friend int __UNDERFLOW(streambuf*);
  254. #else
  255.     friend int __underflow(streambuf*);
  256. #endif
  257.     struct streammarker *_next;  // Only if saving()
  258.     streambuf *_sbuf; // Only valid if saving().
  259.     streampos _spos; // -2: means that _pos is valid.
  260.     void set_streampos(streampos sp) { _spos = sp; }
  261.     void set_offset(int offset) { _pos = offset; _spos = (streampos)(-2); }
  262.     // If _pos >= 0, it points to _buf->Gbase()+_pos.
  263.     // if _pos < 0, it points to _buf->eBptr()+_pos.
  264.     int _pos;
  265.   public:
  266.     streammarker(streambuf *sb);
  267.     ~streammarker();
  268.     int saving() { return  _spos == -2; }
  269.     int delta(streammarker&);
  270.     int delta();
  271. };
  272.  
  273. struct __streambuf {
  274.     // NOTE: If this is changed, also change __FILE in stdio/stdio.h!
  275.     int _flags;        /* High-order word is _IO_MAGIC; rest is flags. */
  276.     char* _gptr;    /* Current get pointer */
  277.     char* _egptr;    /* End of get area. */
  278.     char* _eback;    /* Start of putback+get area. */
  279.     char* _pbase;    /* Start of put area. */
  280.     char* _pptr;    /* Current put pointer. */
  281.     char* _epptr;    /* End of put area. */
  282.     char* _base;    /* Start of reserve area. */
  283.     char* _ebuf;    /* End of reserve area. */
  284.     struct streambuf *_chain;
  285.  
  286.     // The following fields are used to support backing up and undo.
  287.     friend class streammarker;
  288.     char *_other_gbase; // Pointer to start of non-current get area.
  289.     char *_aux_limit;  // Pointer to first valid character of backup area,
  290.     char *_other_egptr; // Pointer to end of non-current get area.
  291.     streammarker *_markers;
  292.  
  293. #define __HAVE_COLUMN /* temporary */
  294.     // 1+column number of pbase(); 0 is unknown.
  295.     unsigned short _cur_column;
  296.     char _unused;
  297.     char _shortbuf[1];
  298. };
  299.  
  300. extern unsigned __adjust_column(unsigned start, const char *line, int count);
  301.  
  302. struct streambuf : public __streambuf {
  303.     friend class ios;
  304.     friend class istream;
  305.     friend class ostream;
  306.     friend class streammarker;
  307. #ifdef _G_FRIEND_BUG
  308.     friend int __UNDERFLOW(streambuf*);
  309. #else
  310.     friend int __underflow(streambuf*);
  311. #endif
  312.   protected:
  313.     static streambuf* _list_all; /* List of open streambufs. */
  314.     streambuf*& xchain() { return _chain; }
  315.     void _un_link();
  316.     void _link_in();
  317.     char* gptr() const { return _gptr; }
  318.     char* pptr() const { return _pptr; }
  319.     char* egptr() const { return _egptr; }
  320.     char* epptr() const { return _epptr; }
  321.     char* pbase() const { return _pbase; }
  322.     char* eback() const { return _eback; }
  323.     char* base() const { return _base; }
  324.     char* ebuf() const { return _ebuf; }
  325.     int blen() const { return _ebuf - _base; }
  326.     void xput_char(char c) { *_pptr++ = c; }
  327.     int xflags() { return _flags; }
  328.     int xflags(int f) { int fl = _flags; _flags = f; return fl; }
  329.     void xsetflags(int f) { _flags |= f; }
  330.     void xsetflags(int f, int mask) { _flags = (_flags & ~mask) | (f & mask); }
  331.     void gbump(int n) { _gptr += n; }
  332.     void pbump(int n) { _pptr += n; }
  333.     void setb(char* b, char* eb, int a=0);
  334.     void setp(char* p, char* ep) { _pbase=_pptr=p; _epptr=ep; }
  335.     void setg(char* eb, char* g, char *eg) { _eback=eb; _gptr=g; _egptr=eg; }
  336.     char *shortbuf() { return _shortbuf; }
  337.  
  338.     int in_backup() { return _flags & _S_IN_BACKUP; }
  339.     // The start of the main get area:  FIXME:  wrong for write-mode filebuf?
  340.     char *Gbase() { return in_backup() ? _other_gbase : _eback; }
  341.     // The end of the main get area:
  342.     char *eGptr() { return in_backup() ? _other_egptr : _egptr; }
  343.     // The start of the backup area:
  344.     char *Bbase() { return in_backup() ? _eback : _other_gbase; }
  345.     char *Bptr() { return _aux_limit; }
  346.     // The end of the backup area:
  347.     char *eBptr() { return in_backup() ? _egptr : _other_egptr; }
  348.     char *Nbase() { return _other_gbase; }
  349.     char *eNptr() { return _other_egptr; }
  350.     int have_backup() { return _other_gbase != NULL; }
  351.     int have_markers() { return _markers != NULL; }
  352.     int _least_marker();
  353.     void switch_to_main_get_area();
  354.     void switch_to_backup_area();
  355.     void free_backup_area();
  356.     void unsave_markers(); // Make all streammarkers !saving().
  357.     int put_mode() { return _flags & _S_CURRENTLY_PUTTING; }
  358.     int switch_to_get_mode();
  359.     
  360.     streambuf(int flags=0);
  361.   public:
  362.     static int flush_all();
  363.     static void flush_all_linebuffered(); // Flush all line buffered files.
  364.     virtual int underflow() = 0; // Leave public for now
  365.     virtual int overflow(int c = EOF) = 0; // Leave public for now
  366.     virtual int doallocate();
  367.     virtual streampos seekoff(streamoff, _seek_dir, int mode=ios::in|ios::out);
  368.     virtual streampos seekpos(streampos pos, int mode = ios::in|ios::out);
  369.     int seekmark(streammarker& mark, int delta = 0);
  370.     int sputbackc(char c);
  371.     int sungetc();
  372.     virtual ~streambuf();
  373.     int unbuffered() { return _flags & _S_UNBUFFERED ? 1 : 0; }
  374.     int linebuffered() { return _flags & _S_LINE_BUF ? 1 : 0; }
  375.     void unbuffered(int i)
  376.     { if (i) _flags |= _S_UNBUFFERED; else _flags &= ~_S_UNBUFFERED; }
  377.     void linebuffered(int i)
  378.     { if (i) _flags |= _S_LINE_BUF; else _flags &= ~_S_LINE_BUF; }
  379.     int allocate() { // For AT&T compatibility
  380.     if (base() || unbuffered()) return 0;
  381.     else return doallocate(); }
  382.     // Allocate a buffer if needed; use _shortbuf if appropriate.
  383.     void allocbuf() { if (base() == NULL) doallocbuf(); }
  384.     void doallocbuf();
  385.     virtual int sync();
  386.     virtual int pbackfail(int c);
  387.     virtual streambuf* setbuf(char* p, int len);
  388.     int in_avail() { return _egptr - _gptr; }
  389.     int out_waiting() { return _pptr - _pbase; }
  390.     virtual int xsputn(const char* s, int n);
  391.     int sputn(const char* s, int n) { return xsputn(s, n); }
  392.     int padn(char pad, int n); // Emit 'n' copies of 'pad'.
  393.     virtual int xsgetn(char* s, int n);
  394.     int sgetn(char* s, int n) { return xsgetn(s, n); }
  395.     int ignore(int);
  396.     virtual int get_column();
  397.     virtual int set_column(int);
  398.     long sgetline(char* buf, _G_size_t n, char delim, int putback_delim);
  399.     int sbumpc() {
  400.     if (_gptr >= _egptr && __underflow(this) == EOF) return EOF;
  401.     else return *(unsigned char*)_gptr++; }
  402.     int sgetc() {
  403.     if (_gptr >= _egptr && __underflow(this) == EOF) return EOF;
  404.     else return *(unsigned char*)_gptr; }
  405.     int snextc() {
  406.     if (_gptr >= _egptr && __underflow(this) == EOF) return EOF;
  407.     else return _gptr++, sgetc(); }
  408.     int sputc(int c) {
  409.     if (_pptr >= _epptr) return __overflow(this, (unsigned char)c);
  410.     else return *_pptr++ = c, (unsigned char)c; }
  411.     void stossc() { if (_gptr < _egptr) _gptr++; }
  412.     int vscan(char const *fmt0, _G_va_list ap, ios* stream = NULL);
  413.     int scan(char const *fmt0 ...);
  414.     int vform(char const *fmt0, _G_va_list ap);
  415.     int form(char const *fmt0 ...);
  416. #if 0 /* Work in progress */
  417.     int collumn();  // Current collumn number (of put pointer). -1 is unknown.
  418.     void collumn(int c);  // Set collumn number of put pointer to c.
  419. #endif
  420. };
  421.  
  422. // A backupbuf is a streambuf with full backup and savepoints on reading.
  423. // All standard streambufs in the GNU iostream library are backupbufs.
  424.  
  425. // A backupbuf may have two get area:
  426. // - The main get area, and (sometimes) the putback area.
  427. // Whichever one of these contains the gptr is the current get area;
  428. // the other one is the non-current get area.
  429.  
  430. class backupbuf : public streambuf {
  431.     friend class streammarker;
  432.   protected:
  433.     backupbuf(int flags=0) : streambuf(flags|_S_IS_BACKUPBUF) { }
  434.   public:
  435.     virtual int pbackfail(int c);
  436.     virtual int underflow();
  437.     virtual int overflow(int c = EOF);
  438. };
  439.  
  440. #ifdef amiga
  441. #define _DO_NOT_CALL_SYSCLOSE 0x0001    // GM: don't call sys_close on ~filebuf
  442. #endif
  443.  
  444. struct __file_fields {
  445. #ifdef amiga
  446.     long _fileno;
  447. #else
  448.     short _fileno;
  449. #endif
  450.     int _blksize;
  451.     _G_off_t _offset;
  452. //    char* _save_gptr;  char* _save_egptr;
  453. };
  454.  
  455. class filebuf : public backupbuf {
  456.   protected:
  457.     struct __file_fields _fb;
  458.     void init();
  459.   public:
  460.     static const int openprot; // Non-ANSI AT&T-ism:  Default open protection.
  461. #ifdef amiga
  462.     int _flags2;    /* Sun Sep 26 21:47:37 1993 GM: place for special flags */
  463. #endif
  464.     filebuf();
  465.     filebuf(int fd);
  466.     filebuf(int fd, char* p, int len);
  467.     ~filebuf();
  468.     filebuf* attach(int fd);
  469.     filebuf* open(const char *filename, const char *mode);
  470.     filebuf* open(const char *filename, ios::openmode mode, int prot = 0664);
  471.     virtual int underflow();
  472.     virtual int overflow(int c = EOF);
  473. #ifdef amiga
  474.     int is_open() const { return _fb._fileno !=EOF; }
  475. #else
  476.     int is_open() const { return _fb._fileno >= 0; }
  477. #endif
  478.     int fd() const { return is_open() ? _fb._fileno : EOF; }
  479.     filebuf* close();
  480.     virtual int doallocate();
  481.     virtual streampos seekoff(streamoff, _seek_dir, int mode=ios::in|ios::out);
  482.     virtual streambuf* setbuf(char* p, int len);
  483.     int xsputn(const char* s, int n);
  484.     int xsgetn(char* s, int n);
  485.     virtual int sync();
  486.   protected: // See documentation in filebuf.C.
  487. //    virtual int pbackfail(int c);
  488.     int is_reading() { return eback() != egptr(); }
  489.     char* cur_ptr() { return is_reading() ?  gptr() : pptr(); }
  490.     /* System's idea of pointer */
  491.     char* file_ptr() { return eGptr(); }
  492.     int do_write(const char *data, int to_do);
  493.     int do_flush() { return do_write(_pbase, _pptr-_pbase); }
  494.     // Low-level operations (Usually invoke system calls.)
  495.     virtual _G_ssize_t sys_read(char* buf, _G_size_t size);
  496.     virtual _G_fpos_t sys_seek(_G_fpos_t, _seek_dir);
  497.     virtual _G_ssize_t sys_write(const void*, long);
  498.     virtual int sys_stat(void*); // Actually, a (struct stat*)
  499.     virtual int sys_close();
  500. };
  501.  
  502. inline ios::ios(streambuf* sb /* = 0 */, ostream* tie_to /* = 0 */) {
  503.         _state = sb ? ios::goodbit : ios::badbit; _exceptions=0;
  504.         _strbuf=sb; _tie = tie_to; _width=0; _fill=' ';
  505.         _flags=ios::skipws|ios::dec; _precision=6; }
  506. inline ios::~ios() {
  507.     if (!(_flags & (unsigned int)ios::dont_close)) delete _strbuf; }
  508.  
  509. #endif /* _STREAMBUF_H */
  510.