1/* This is part of libio/iostream, providing -*- C++ -*- input/output. 2Copyright (C) 1993 Free Software Foundation 3 4This file is part of the GNU IO Library. This library is free 5software; you can redistribute it and/or modify it under the 6terms of the GNU General Public License as published by the 7Free Software Foundation; either version 2, or (at your option) 8any later version. 9 10This library is distributed in the hope that it will be useful, 11but WITHOUT ANY WARRANTY; without even the implied warranty of 12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13GNU General Public License for more details. 14 15You should have received a copy of the GNU General Public License 16along with this library; see the file COPYING. If not, write to the Free 17Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 18 19As a special exception, if you link this library with files 20compiled with a GNU compiler to produce an executable, this does not cause 21the resulting executable to be covered by the GNU General Public License. 22This exception does not however invalidate any other reasons why 23the executable file might be covered by the GNU General Public License. */ 24 25/* Written by Per Bothner (bothner@cygnus.com). */ 26 27#ifdef __GNUG__ 28#pragma implementation 29#endif 30 31#include "libioP.h" 32#include <stdiostream.h> 33 34// A stdiobuf is "tied" to a FILE object (as used by the stdio package). 35// Thus a stdiobuf is always synchronized with the corresponding FILE, 36// though at the cost of some overhead. (If you use the implementation 37// of stdio supplied with this library, you don't need stdiobufs.) 38// This implementation inherits from filebuf, but implement the virtual 39// functions sys_read/..., using the stdio functions fread/... instead 40// of the low-level read/... system calls. This has the advantage that 41// we get all of the nice filebuf semantics automatically, though 42// with some overhead. 43 44 45#ifndef SEEK_SET 46#define SEEK_SET 0 47#endif 48#ifndef SEEK_CUR 49#define SEEK_CUR 1 50#endif 51#ifndef SEEK_END 52#define SEEK_END 2 53#endif 54 55stdiobuf::stdiobuf(FILE *f) : filebuf(fileno(f)) 56{ 57 _file = f; 58 // Turn off buffer in stdiobuf. Instead, rely on buffering in (FILE). 59 // Thus the stdiobuf will be synchronized with the FILE. 60 setbuf(NULL, 0); 61} 62 63stdiobuf::~stdiobuf() 64{ 65 /* Only needed if we're buffered. Not buffered is the default. */ 66 _IO_do_flush((_IO_FILE*)this); 67} 68 69streamsize stdiobuf::sys_read(char* buf, streamsize size) 70{ 71 // A minor optimization, but it makes a noticable difference. 72 // A bigger optimization would be to write stdiobuf::underflow, 73 // but that has some modularity disadvantages. Re-evaluate that 74 // after we have gotten rid of the double indirection. FIXME 75 if (size == 1) 76 { 77 register int ch = getc(_file); 78 if (ch == EOF) 79 return 0; 80 *buf = (char)ch; 81 return 1; 82 } 83 else 84 return fread(buf, 1, size, _file); 85} 86 87streamsize stdiobuf::sys_write(const char *buf, streamsize n) 88{ 89 _IO_ssize_t count = fwrite(buf, 1, n, _file); 90 if (_offset >= 0) 91 _offset += n; 92 return count; 93} 94 95streampos stdiobuf::sys_seek(streamoff offset, _seek_dir dir) 96{ 97 // Normally, equivalent to: fdir=dir 98 int fdir = 99 (dir == ios::beg) ? SEEK_SET : 100 (dir == ios::cur) ? SEEK_CUR : 101 (dir == ios::end) ? SEEK_END : 102 dir; 103 return fseek(_file, offset, fdir); 104} 105 106int stdiobuf::sys_close() 107{ 108 int status = fclose(_file); 109 _file = NULL; 110 return status; 111} 112 113int stdiobuf::sync() 114{ 115 if (_IO_do_flush((_IO_FILE*)this)) 116 return EOF; 117 if (!(xflags() & _IO_NO_WRITES)) 118 if (fflush(_file)) 119 return EOF; 120 return 0; 121} 122 123int stdiobuf::overflow(int c /* = EOF*/) 124{ 125 if (filebuf::overflow(c) == EOF) 126 return EOF; 127 if (c != EOF) 128 return c; 129 return fflush(_file); 130} 131 132streamsize stdiobuf::xsputn(const char* s, streamsize n) 133{ 134 if (buffered ()) 135 { 136 // The filebuf implementation of sputn loses. 137 return streambuf::xsputn(s, n); 138 } 139 else 140 return fwrite (s, 1, n, _file); 141} 142 143void stdiobuf::buffered (int b) 144{ 145 if (b) 146 { 147 if (_flags & _IO_UNBUFFERED) 148 { /* Was unbuffered, make it buffered. */ 149 _flags &= ~_IO_UNBUFFERED; 150 } 151 } 152 else 153 { 154 if (!(_flags & _IO_UNBUFFERED)) 155 { /* Was buffered, make it unbuffered. */ 156 setbuf(NULL, 0); 157 } 158 } 159} 160