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