Annotation of GNUtools/libg++/libio/streambuf.cc, revision 1.1.1.1

1.1       root        1: /* This is part of libio/iostream, providing -*- C++ -*- input/output.
                      2: Copyright (C) 1991, 1992, 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: /* Written by Per Bothner ([email protected]). */
                     26: 
                     27: #define _STREAM_COMPAT
                     28: #ifdef __GNUG__
                     29: #pragma implementation
                     30: #endif
                     31: #include "iostreamP.h"
                     32: #include <string.h>
                     33: #include <stdarg.h>
                     34: #include <errno.h>
                     35: #ifndef errno
                     36: extern int errno;
                     37: #endif
                     38: 
                     39: void streambuf::_un_link() { _IO_un_link(this); }
                     40: 
                     41: void streambuf::_link_in() { _IO_link_in(this); }
                     42: 
                     43: int streambuf::switch_to_get_mode()
                     44: { return _IO_switch_to_get_mode(this); }
                     45: 
                     46: void streambuf::free_backup_area()
                     47: { _IO_free_backup_area(this); }
                     48: 
                     49: #if 0
                     50: int streambuf::switch_to_put_mode()
                     51: { return _IO_:switch_to_put_mode(this); }
                     52: #endif
                     53: 
                     54: int __overflow(streambuf* sb, int c)
                     55: {
                     56:     return sb->overflow(c);
                     57: }
                     58: 
                     59: int streambuf::underflow()
                     60: { return EOF; }
                     61: 
                     62: int streambuf::overflow(int c /* = EOF */)
                     63: { return EOF; }
                     64: 
                     65: streamsize streambuf::xsputn(register const char* s, streamsize n)
                     66: { return _IO_default_xsputn(this, s, n); }
                     67: 
                     68: streamsize streambuf::xsgetn(char* s, streamsize n)
                     69: { return _IO_default_xsgetn(this, s, n); }
                     70: 
                     71: int streambuf::ignore(int n)
                     72: {
                     73:     register int more = n;
                     74:     for (;;) {
                     75:        int count = _IO_read_end - _IO_read_ptr; // Data available.
                     76:        if (count > 0) {
                     77:            if (count > more)
                     78:                count = more;
                     79:            _IO_read_ptr += count;
                     80:            more -= count;
                     81:        }
                     82:        if (more == 0 || __underflow(this) == EOF)
                     83:            break;
                     84:     }
                     85:     return n - more;
                     86: }
                     87: 
                     88: int streambuf::sync()
                     89: {
                     90:     if (pptr() == pbase())
                     91:        return 0;
                     92:     return EOF;
                     93: }
                     94: 
                     95: int streambuf::pbackfail(int c)
                     96: {
                     97:   return _IO_default_pbackfail(this, c);
                     98: }
                     99: 
                    100: streambuf* streambuf::setbuf(char* p, int len)
                    101: {
                    102:     if (sync() == EOF)
                    103:        return NULL;
                    104:     if (p == NULL || len == 0) {
                    105:        unbuffered(1);
                    106:        setb(_shortbuf, _shortbuf+1, 0);
                    107:     }
                    108:     else {
                    109:        unbuffered(0);
                    110:        setb(p, p+len, 0);
                    111:     }
                    112:     setp(0, 0);
                    113:     setg(0, 0, 0);
                    114:     return this;
                    115: }
                    116: 
                    117: streampos streambuf::seekpos(streampos pos, int mode)
                    118: {
                    119:     return seekoff(pos, ios::beg, mode);
                    120: }
                    121: 
                    122: streampos streambuf::sseekpos(streampos pos, int mode)
                    123: {
                    124:   return _IO_seekpos (this, pos, convert_to_seekflags (0, mode));
                    125: }
                    126: 
                    127: void streambuf::setb(char* b, char* eb, int a)
                    128: { _IO_setb(this, b, eb, a); }
                    129: 
                    130: int streambuf::doallocate() { return _IO_default_doallocate(this); }
                    131: 
                    132: void streambuf::doallocbuf() { _IO_doallocbuf(this); }
                    133: 
                    134: /* The following are jump table entries that just call the virtual method */
                    135: 
                    136: static int _IO_sb_overflow(_IO_FILE *fp, int c)
                    137: { return ((streambuf*)fp)->overflow(c); }
                    138: static int _IO_sb_underflow(_IO_FILE *fp)
                    139: { return ((streambuf*)fp)->underflow(); }
                    140: static _IO_size_t _IO_sb_xsputn(_IO_FILE *fp, const void *s, _IO_size_t n)
                    141: { return ((streambuf*)fp)->xsputn((const char*)s, n); }
                    142: static _IO_size_t _IO_sb_xsgetn(_IO_FILE *fp, void *s, _IO_size_t n)
                    143: { return ((streambuf*)fp)->xsgetn((char*)s, n); }
                    144: static int _IO_sb_close(_IO_FILE *fp)
                    145: { return ((streambuf*)fp)->sys_close(); }
                    146: static int _IO_sb_stat(_IO_FILE *fp, void *b)
                    147: { return ((streambuf*)fp)->sys_stat(b); }
                    148: static int _IO_sb_doallocate(_IO_FILE *fp)
                    149: { return ((streambuf*)fp)->doallocate(); }
                    150: 
                    151: static _IO_pos_t _IO_sb_seekoff(_IO_FILE *fp, _IO_off_t pos, _IO_seekflags m)
                    152: {
                    153:   int mode = ((m & _IO_seek_not_in) ? 0 : ios::in)
                    154:     + ((m & _IO_seek_not_out) ? 0 : ios::out);
                    155:   return ((streambuf*)fp)->seekoff(pos, (_seek_dir)((int)m & 3), mode);
                    156: }
                    157: 
                    158: static _IO_pos_t _IO_sb_seekpos(_IO_FILE *fp, _IO_pos_t pos, _IO_seekflags m)
                    159: {
                    160:   int mode = ((m & _IO_seek_not_in) ? 0 : ios::in)
                    161:     + ((m & _IO_seek_not_out) ? 0 : ios::out);
                    162:   return ((streambuf*)fp)->seekpos(pos, mode);
                    163: }
                    164: 
                    165: static int _IO_sb_pbackfail(_IO_FILE *fp, int ch)
                    166: { return ((streambuf*)fp)->pbackfail(ch); }
                    167: static void _IO_sb_finish(_IO_FILE *fp)
                    168: { ((streambuf*)fp)->~streambuf(); }
                    169: static _IO_ssize_t _IO_sb_read(_IO_FILE *fp, void *buf, _IO_ssize_t n)
                    170: { return ((streambuf*)fp)->sys_read((char*)buf, n); }
                    171: static _IO_ssize_t _IO_sb_write(_IO_FILE *fp, const void *buf, _IO_ssize_t n)
                    172: { return ((streambuf*)fp)->sys_write((const char*)buf, n); }
                    173: static int _IO_sb_sync(_IO_FILE *fp)
                    174: { return ((streambuf*)fp)->sync(); }
                    175: static _IO_pos_t _IO_sb_seek(_IO_FILE *fp, _IO_off_t off, int dir)
                    176: { return ((streambuf*)fp)->sys_seek(off, (_seek_dir)dir); }
                    177: static int _IO_sb_setbuf(_IO_FILE *fp, char *buf, _IO_ssize_t n)
                    178: { return ((streambuf*)fp)->setbuf(buf, n) == NULL ? EOF : 0; }
                    179: 
                    180: /* This callbacks in this jumptable just call the corresponding
                    181:    virtual function, so that C functions can access (potentially user-defined)
                    182:    streambuf-derived objects.
                    183:    Contrast the builtinbuf class, which does the converse:  Allow
                    184:    C++ virtual calls to to be used on _IO_FILE objects that are builtin
                    185:    (or defined by C code). */
                    186: 
                    187: 
                    188: struct _IO_jump_t _IO_streambuf_jumps = {
                    189:   _IO_sb_overflow,
                    190:   _IO_sb_underflow,
                    191:   _IO_sb_xsputn,
                    192:   _IO_sb_xsgetn,
                    193:   _IO_sb_read,
                    194:   _IO_sb_write,
                    195:   _IO_sb_doallocate,
                    196:   _IO_sb_pbackfail,
                    197:   _IO_sb_setbuf,
                    198:   _IO_sb_sync,
                    199:   _IO_sb_finish,
                    200:   _IO_sb_close,
                    201:   _IO_sb_stat,
                    202:   _IO_sb_seek,
                    203:   _IO_sb_seekoff,
                    204:   _IO_sb_seekpos,
                    205: };
                    206: 
                    207: streambuf::streambuf(int flags)
                    208: {
                    209:   _IO_init(this, flags);
                    210:   _jumps = &_IO_streambuf_jumps;
                    211: }
                    212: 
                    213: streambuf::~streambuf() { _IO_default_finish(this); }
                    214: 
                    215: streampos
                    216: streambuf::seekoff(streamoff, _seek_dir, int mode /*=ios::in|ios::out*/)
                    217: {
                    218:     return EOF;
                    219: }
                    220: 
                    221: streampos
                    222: streambuf::sseekoff(streamoff o , _seek_dir d, int m /*=ios::in|ios::out*/)
                    223: {
                    224:   return _IO_seekoff (this, o, convert_to_seekflags (d, m));
                    225: }
                    226: 
                    227: int streambuf::sputbackc(char c)
                    228: {
                    229:   return _IO_sputbackc(this, c);
                    230: }
                    231: 
                    232: int streambuf::sungetc()
                    233: {
                    234:   return _IO_sungetc(this);
                    235: }
                    236: 
                    237: #if 0 /* Work in progress */
                    238: void streambuf::collumn(int c)
                    239: {
                    240:     if (c == -1)
                    241:        _collumn = -1;
                    242:     else
                    243:        _collumn = c - (_IO_write_ptr - _IO_write_base);
                    244: }
                    245: #endif
                    246: 
                    247: 
                    248: int streambuf::get_column()
                    249: {
                    250:     if (_cur_column) 
                    251:        return _IO_adjust_column(_cur_column - 1, pbase(), pptr() - pbase());
                    252:     return -1;
                    253: }
                    254: 
                    255: int streambuf::set_column(int i)
                    256: {
                    257:     _cur_column = i+1;
                    258:     return 0;
                    259: }
                    260: 
                    261: int streambuf::flush_all() { return _IO_flush_all (); }
                    262: 
                    263: void streambuf::flush_all_linebuffered()
                    264: { _IO_flush_all_linebuffered(); }
                    265: 
                    266: int streambuf::sys_stat(void *)
                    267: {
                    268: #ifdef EIO
                    269:   errno = EIO;
                    270: #endif
                    271:   return -1;
                    272: }
                    273: 
                    274: streamsize streambuf::sys_read(char* buf, streamsize size)
                    275: {
                    276:   return 0;
                    277: }
                    278: 
                    279: streamsize streambuf::sys_write(const char* buf, streamsize size)
                    280: {
                    281:   return 0;
                    282: }
                    283: 
                    284: streampos streambuf::sys_seek(streamoff, _seek_dir)
                    285: {
                    286:   return EOF;
                    287: }
                    288: 
                    289: int streambuf::sys_close() { return 0; /* Suceess; do nothing */ }
                    290: 
                    291: streammarker::streammarker(streambuf *sb)
                    292: {
                    293:   _IO_init_marker(this, sb);
                    294: }
                    295: 
                    296: streammarker::~streammarker()
                    297: {
                    298:   _IO_remove_marker(this);
                    299: }
                    300: 
                    301: #define BAD_DELTA EOF
                    302: 
                    303: int streammarker::delta(streammarker& other_mark)
                    304: {
                    305:   return _IO_marker_difference(this, &other_mark);
                    306: }
                    307: 
                    308: int streammarker::delta()
                    309: {
                    310:   return _IO_marker_delta(this);
                    311: }
                    312: 
                    313: int streambuf::seekmark(streammarker& mark, int delta /* = 0 */)
                    314: {
                    315:   return _IO_seekmark(this, &mark, delta);
                    316: }
                    317: 
                    318: void streambuf::unsave_markers()
                    319: {
                    320:   _IO_unsave_markers(this);
                    321: }
                    322: 
                    323: int ios::readable() { return !(rdbuf()->_flags & _IO_NO_READS); }
                    324: int ios::writable() { return !(rdbuf()->_flags & _IO_NO_WRITES); }
                    325: int ios::is_open() { return rdbuf()
                    326:                         && (rdbuf()->_flags & _IO_NO_READS+_IO_NO_WRITES)
                    327:                             != _IO_NO_READS+_IO_NO_WRITES; }
                    328: 
                    329: #if defined(linux)
                    330: #define IO_CLEANUP
                    331: #endif
                    332: 
                    333: #ifdef IO_CLEANUP
                    334:   IO_CLEANUP
                    335: #else
                    336: struct __io_defs {
                    337:     __io_defs() { }
                    338:     ~__io_defs() { _IO_flush_all (); }
                    339: };   
                    340: __io_defs io_defs__;
                    341: #endif

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.