1 // This may look like C code, but it is really -*- C++ -*-
2 /*
3 Copyright (C) 1988 Free Software Foundation
4 written by Doug Lea (dl@rocky.oswego.edu)
5
6 This file is part of GNU CC.
7
8 GNU CC is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY. No author or distributor
10 accepts responsibility to anyone for the consequences of using it
11 or for whether it serves any particular purpose or works at all,
12 unless he says so in writing. Refer to the GNU CC General Public
13 License for full details.
14
15 Everyone is granted permission to copy, modify and redistribute
16 GNU CC, but only under the conditions described in the
17 GNU CC General Public License. A copy of this license is
18 supposed to have been given to you along with GNU CC so you
19 can know your rights and responsibilities. It should be in a
20 file named COPYING. Among other things, the copyright notice
21 and this notice must be preserved on all copies.
22 */
23
24 #ifndef _streambuf_h
25 #ifdef __GNUG__
26 #pragma once
27 #pragma interface
28 #endif
29 #define _streambuf_h 1
30
31 /* streambufs.
32 basic streambufs and filebufs are as in Stroustrup, ch 8,
33 but the base class contains virtual extensions that allow
34 most capabilities of libg++ Files to be used as streambufs
35 via `Filebufs'.
36 */
37
38 #include <stdio.h>
39 #include <builtin.h>
40 #include <Fmodes.h>
41
42 // see below for NO_LINE_BUFFER_STREAMBUFS
43
44 #ifndef BUFSIZE
45 #ifdef BUFSIZ
46 #define BUFSIZE BUFSIZ
47 #else
48 #define BUFSIZE 1024
49 #endif
50 #endif
51
52 enum open_mode // filebuf open modes
53 {
54 input=0,
55 output=1,
56 append=2
57 };
58
59 class streambuf
60 {
61 public:
62 char* base; // start of buffer
63 char* pptr; // put-pointer (and gptr fence)
64 char* gptr; // get-pointer
65 char* eptr; // last valid addr in buffer
66
67 char alloc; // true if we own freestore alloced buffer
68
69 streambuf();
70 streambuf(char* buf, int buflen);
71
72 virtual ~streambuf();
73
74 int doallocate();
75 int allocate();
76
77
78 int must_overflow(int ch); // true if should call overflow
79
80 virtual int overflow(int c = EOF); // flush -- return EOF if fail
81 virtual int underflow(); // fill -- return EOF if fail
82
83 int sgetc(); // get one char (as int) or EOF
84 int snextc(); // get and advance
85 void stossc(); // advance only
86
87 int sputbackc(char); // unget
88
89 int sputc(int c = EOF); // write one char
90
91 virtual streambuf* setbuf(char* buf, int buflen, int preloaded_count = 0);
92 // (not virtual in AT&T)
93
94 // the following aren't in AT&T version:
95
96 int sputs(const char* s); // write null-terminated str
97 int sputsn(const char* s, int len); // write len bytes
98
99 virtual const char* name();
100
101
102 virtual streambuf* open(const char* name, open_mode m);
103 virtual streambuf* open(const char* filename, io_mode m, access_mode a);
104 virtual streambuf* open(const char* filename, const char* m);
105 virtual streambuf* open(int filedesc, io_mode m);
106 virtual streambuf* open(FILE* fileptr);
107
108 virtual int is_open();
109 virtual int close();
110
111 virtual void error();
112 };
113
114
115 #if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
116
117
must_overflow(int ch)118 inline int streambuf::must_overflow(int ch)
119 {
120 #ifndef NO_LINE_BUFFER_STREAMBUF
121 return pptr >= eptr || ch == '\n';
122 #else
123 return pptr >= eptr;
124 #endif
125 }
126
127
allocate()128 inline int streambuf::allocate()
129 {
130 return (base == 0)? doallocate() : 0;
131 }
132
sgetc()133 inline int streambuf::sgetc()
134 {
135 return (gptr >= pptr)? underflow() : int((unsigned char)(*gptr));
136 }
137
138
snextc()139 inline int streambuf::snextc()
140 {
141 ++gptr;
142 return (gptr >= pptr)? underflow() : int((unsigned char)(*gptr));
143 }
144
145
stossc()146 inline void streambuf::stossc()
147 {
148 if (gptr >= pptr) underflow(); else gptr++;
149 }
150
151
sputbackc(char ch)152 inline int streambuf::sputbackc(char ch)
153 {
154 return (gptr > base)? int((unsigned char)(*--gptr = ch)) : EOF;
155 }
156
sputc(int ch)157 inline int streambuf::sputc(int ch)
158 {
159 return must_overflow(ch)? overflow(ch) :
160 int((unsigned char)(*pptr++ = char(ch)));
161 }
162
163 #endif
164
165 #endif
166