OSDN Git Service

* stdiostream.cc (_POSIX_SOURCE): Define.
[pf3gnuchains/gcc-fork.git] / libio / stdiostream.cc
1 /* This is part of libio/iostream, providing -*- C++ -*- input/output.
2 Copyright (C) 1993, 1999 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 this library; see the file COPYING.  If not, write to the Free
17 Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 (bothner@cygnus.com). */
26
27 #ifdef __GNUG__
28 #pragma implementation
29 #endif
30
31 #ifndef _POSIX_SOURCE
32 # define _POSIX_SOURCE
33 #endif
34 #include <stdiostream.h>
35 #include "libioP.h"
36
37 // A stdiobuf is "tied" to a FILE object (as used by the stdio package).
38 // Thus a stdiobuf is always synchronized with the corresponding FILE,
39 // though at the cost of some overhead.  (If you use the implementation
40 // of stdio supplied with this library, you don't need stdiobufs.)
41 // This implementation inherits from filebuf, but implement the virtual
42 // functions sys_read/..., using the stdio functions fread/... instead
43 // of the low-level read/... system calls.  This has the advantage that
44 // we get all of the nice filebuf semantics automatically, though
45 // with some overhead.
46
47
48 #ifndef SEEK_SET
49 #define SEEK_SET 0
50 #endif
51 #ifndef SEEK_CUR
52 #define SEEK_CUR 1
53 #endif
54 #ifndef SEEK_END
55 #define SEEK_END 2
56 #endif
57
58 stdiobuf::stdiobuf(FILE *f) : filebuf(fileno(f))
59 {
60     _file = f;
61     // Turn off buffer in stdiobuf.  Instead, rely on buffering in (FILE).
62     // Thus the stdiobuf will be synchronized with the FILE.
63     setbuf(NULL, 0);
64 }
65
66 stdiobuf::~stdiobuf()
67 {
68   /* Only needed if we're buffered.  Not buffered is the default. */
69   _IO_do_flush((_IO_FILE*)this);
70 }
71
72 streamsize stdiobuf::sys_read(char* buf, streamsize size)
73 {
74   // A minor optimization, but it makes a noticable difference.
75   // A bigger optimization would be to write stdiobuf::underflow,
76   // but that has some modularity disadvantages.  Re-evaluate that
77   // after we have gotten rid of the double indirection.  FIXME
78   if (size == 1)
79     {
80       register int ch = getc(_file);
81       if (ch == EOF)
82         return 0;
83       *buf = (char)ch;
84       return 1;
85     }
86   else
87     return fread(buf, 1, size, _file);
88 }
89
90 streamsize stdiobuf::sys_write(const char *buf, streamsize n)
91 {
92     _IO_ssize_t count = fwrite(buf, 1, n, _file);
93     if (_offset >= 0)
94         _offset += n;
95     return count;
96 }
97
98 streampos stdiobuf::sys_seek(streamoff offset, _seek_dir dir)
99 {
100     // Normally, equivalent to: fdir=dir
101     int fdir =
102         (dir == ios::beg) ? SEEK_SET :
103         (dir == ios::cur) ? SEEK_CUR :
104         (dir == ios::end) ? SEEK_END :
105         dir;
106     return fseek(_file, offset, fdir);
107 }
108
109 int stdiobuf::sys_close()
110 {
111     int status = fclose(_file);
112     _file = NULL;
113     return status;
114 }
115
116 int stdiobuf::sync()
117 {
118   if (_IO_do_flush((_IO_FILE*)this))
119     return EOF;
120   if (!(xflags() & _IO_NO_WRITES))
121     if (fflush(_file))
122       return EOF;
123   return 0;
124 }
125
126 int stdiobuf::overflow(int c /* = EOF*/)
127 {
128     if (filebuf::overflow(c) == EOF)
129         return EOF;
130     if (c != EOF)
131         return c;
132     return fflush(_file);
133 }
134
135 streamsize stdiobuf::xsputn(const char* s, streamsize n)
136 {
137   if (buffered ())
138     {
139       // The filebuf implementation of sputn loses.
140       return streambuf::xsputn(s, n);
141     }
142   else
143     return fwrite (s, 1, n, _file);
144 }
145
146 void stdiobuf::buffered (int b)
147 {
148   if (b)
149     {
150       if (_flags & _IO_UNBUFFERED)
151         { /* Was unbuffered, make it buffered. */
152           _flags &= ~_IO_UNBUFFERED;
153         }
154     }
155   else
156     {
157       if (!(_flags & _IO_UNBUFFERED))
158         { /* Was buffered, make it unbuffered. */
159           setbuf(NULL, 0);
160         }
161     }
162 }