Annotation of GNUtools/libg++/libio/stdiostream.cc, revision 1.1

1.1     ! root        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: /* Written by Per Bothner ([email protected]). */
        !            26: 
        !            27: #ifdef __GNUG__
        !            28: #pragma implementation
        !            29: #endif
        !            30: 
        !            31: #include <stdiostream.h>
        !            32: 
        !            33: // A stdiobuf is "tied" to a FILE object (as used by the stdio package).
        !            34: // Thus a stdiobuf is always synchronized with the corresponding FILE,
        !            35: // though at the cost of some overhead.  (If you use the implementation
        !            36: // of stdio supplied with this library, you don't need stdiobufs.)
        !            37: // This implementation inherits from filebuf, but implement the virtual
        !            38: // functions sys_read/..., using the stdio functions fread/... instead
        !            39: // of the low-level read/... system calls.  This has the advantage that
        !            40: // we get all of the nice filebuf semantics automatically, though
        !            41: // with some overhead.
        !            42: 
        !            43: 
        !            44: #ifndef SEEK_SET
        !            45: #define SEEK_SET 0
        !            46: #endif
        !            47: #ifndef SEEK_CUR
        !            48: #define SEEK_CUR 1
        !            49: #endif
        !            50: #ifndef SEEK_END
        !            51: #define SEEK_END 2
        !            52: #endif
        !            53: 
        !            54: stdiobuf::stdiobuf(FILE *f) : filebuf(fileno(f))
        !            55: {
        !            56:     _file = f;
        !            57:     // Turn off buffer in stdiobuf.  Instead, rely on buffering in (FILE).
        !            58:     // Thus the stdiobuf will be synchronized with the FILE.
        !            59:     setbuf(NULL, 0);
        !            60: }
        !            61: 
        !            62: _IO_ssize_t stdiobuf::sys_read(char* buf, _IO_size_t size)
        !            63: {
        !            64:     return fread(buf, 1, size, _file);
        !            65: }
        !            66: 
        !            67: _IO_ssize_t stdiobuf::sys_write(const void *buf, _IO_size_t n)
        !            68: {
        !            69:     _IO_ssize_t count = fwrite(buf, 1, n, _file);
        !            70:     if (_offset >= 0)
        !            71:        _offset += n;
        !            72:     return count;
        !            73: }
        !            74: 
        !            75: _IO_pos_t stdiobuf::sys_seek(_IO_pos_t offset, _seek_dir dir)
        !            76: {
        !            77:     // Normally, equivalent to: fdir=dir
        !            78:     int fdir =
        !            79:        (dir == ios::beg) ? SEEK_SET :
        !            80:         (dir == ios::cur) ? SEEK_CUR :
        !            81:         (dir == ios::end) ? SEEK_END :
        !            82:         dir;
        !            83:     return fseek(_file, offset, fdir);
        !            84: }
        !            85: 
        !            86: int stdiobuf::sys_close()
        !            87: {
        !            88:     int status = fclose(_file);
        !            89:     _file = NULL;
        !            90:     return status;
        !            91: }
        !            92: 
        !            93: int stdiobuf::sync()
        !            94: {
        !            95:     if (filebuf::sync() == EOF)
        !            96:        return EOF;
        !            97:     if (!(xflags() & _IO_NO_WRITES))
        !            98:        if (fflush(_file))
        !            99:            return EOF;
        !           100: #if 0
        !           101:        // This loses when writing to a pipe.
        !           102:     if (fseek(_file, 0, SEEK_CUR) == EOF)
        !           103:        return EOF;
        !           104: #endif
        !           105:     return 0;
        !           106: }
        !           107: 
        !           108: int stdiobuf::overflow(int c /* = EOF*/)
        !           109: {
        !           110:     if (filebuf::overflow(c) == EOF)
        !           111:        return EOF;
        !           112:     if (c != EOF)
        !           113:        return c;
        !           114:     return fflush(_file);
        !           115: }
        !           116: 
        !           117: int stdiobuf::xsputn(const char* s, int n)
        !           118: {
        !           119:     // The filebuf implementation of sputn loses.
        !           120:     return streambuf::xsputn(s, n);
        !           121: }

unix.superglobalmegacorp.com

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