1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
/***
*stdiostr.cxx -
*
* Copyright (c) 1991-1992, Microsoft Corporation. All rights reserved.
*
*Purpose:
*
*Revision History:
*
* 07-10-91 KRS Created.
* 08-26-91 KRS Switch out cout/cerr. etc. for Windows non-QuickWin.
* 09-09-91 KRS Modify sync_with_stdio() for filebuf defaults.
* 09-12-91 KRS Add stdiostream class.
* 09-19-91 KRS Use delbuf(1) in stdiostream constructor.
* 09-20-91 KRS C700 #4453: Improve efficiency in overflow().
* 10-21-91 KRS Eliminate last use of default iostream constructor.
* 10-24-91 KRS Avoid virtual calls from virtual functions.
* 11-13-91 KRS Split out streambuf::dbp() into separate file.
* Improve default buffer handling in underflow/overflow.
* Fix bug in sync().
* 01-20-92 KRS C700 #5803: account for CR/LF pairs in ssync().
*
*******************************************************************************/
#include <cruntime.h>
#include <internal.h>
#include <string.h>
#include <stdiostr.h>
#pragma hdrstop
extern "C" {
#include <file2.h>
#include <msdos.h>
}
#include <dos.h>
stdiobuf::stdiobuf(FILE * f)
: streambuf()
{
unbuffered(1); // initially unbuffered
_str = f;
}
stdiobuf::~stdiobuf()
// : ~streambuf()
{
stdiobuf::sync(); // make sure buffer flushed
// _str = 0;
}
int stdiobuf::setrwbuf(int readsize, int writesize)
{
char * tbuf;
unbuffered(!(readsize+writesize));
if (unbuffered())
return(0);
tbuf = new char[(readsize+writesize)];
if (!tbuf)
return(EOF);
setb( tbuf, tbuf + (readsize+writesize), 1);
if (readsize)
{
setg(base(),base()+readsize,base()+readsize);
}
else
{
setg(0,0,0);
}
if (writesize)
{
setp(base()+readsize,ebuf());
}
else
{
setp(0,0);
}
return(1);
}
int stdiobuf::overflow(int c) {
long count, nout;
if (allocate()==EOF) // make sure there is a reserve area
return EOF;
if (!unbuffered() && epptr())
{
if ((count = pptr() - pbase()) > 0)
{
nout=fwrite((void *) pbase(), 1, (int)count, _str);
pbump(-(int)nout);
if (nout != count)
{
memmove(pbase(),pbase()+nout,(int)(count-nout));
return(EOF);
}
}
}
if ((!unbuffered()) && (!epptr()))
setp(base()+(blen()>>1),ebuf()); // hack: default to 2nd half
if (c!=EOF)
{
if ((!unbuffered()) && (pptr() < epptr())) // guard against recursion
sputc(c);
else
return fputc(c, _str);
}
return(1); // return something other than EOF if successful
}
int stdiobuf::underflow()
{
int count;
if (allocate()==EOF) // make sure there is a reserve area
return EOF;
if ((!unbuffered()) && (!egptr()))
setg(base(),(base()+(blen()>>1)),(base()+(blen()>>1))); // hack: default
if (unbuffered() || (!egptr()))
return fgetc(_str);
if (gptr() >= egptr())
// buffer empty, try for more
{
if (!(count = fread((void *)eback(), 1, (size_t)(egptr()-eback()), _str)))
return(EOF); // reach EOF, nothing read
setg(eback(),(egptr()-count),egptr()); // _gptr = _egptr - count
if (gptr()!=eback())
{
// CONSIDER: expensive, is there a better sol'n?
memmove(gptr(), eback(), count); // overlapping memory!
}
}
return sbumpc(); // CONSIDER: possible recursion
}
streampos stdiobuf::seekoff(streamoff off, ios::seek_dir dir, int)
{
int fdir;
long retpos;
switch (dir) {
case ios::beg :
fdir = SEEK_SET;
break;
case ios::cur :
fdir = SEEK_CUR;
break;
case ios::end :
fdir = SEEK_END;
break;
default:
// error
return(EOF);
}
// CONSIDER: is this needed?
stdiobuf::overflow(EOF);
if (fseek(_str, off, fdir))
return (EOF);
if ((retpos=ftell(_str))==-1L)
return(EOF);
return((streampos)retpos);
}
int stdiobuf::pbackfail(int c)
{
if (eback()<gptr()) return sputbackc((char)c); // CONSIDER: should never happen
if (stdiobuf::seekoff( -1, ios::cur, ios::in)==EOF)
return EOF;
if (!unbuffered() && egptr())
{
memmove((gptr()+1),gptr(),(egptr()-(gptr()+1)));
*gptr()=(char)c;
}
return(c);
}
int stdiobuf::sync()
{
long count;
char * p;
char flags;
if (!unbuffered())
{
if (stdiobuf::overflow(EOF)==EOF)
return(EOF);
if ((count=in_avail())>0)
{
flags = _osfile[_fileno(_str)];
if (flags & FTEXT)
{
// If text mode, need to account for CR/LF etc.
for (p = gptr(); p < egptr(); p++)
if (*p == '\n')
count++;
// account for EOF if read, not counted by _read
// UNDONE: is this necessary?
if (_str->_flag & _IOCTRLZ)
count++;
#if 0
// UNDONE: is this correct?
if ((gptr()==eback()) && (flags & FCRLF))
count--;
#endif
}
if (stdiobuf::seekoff( -count, ios::cur, ios::in)==EOF)
return(EOF);
setg(eback(),egptr(),egptr()); // empty get area (_gptr = _egptr;)
}
}
return(0);
}
stdiostream::stdiostream(FILE * file)
: iostream(new stdiobuf(file))
{
istream::delbuf(1);
ostream::delbuf(1);
// CONSIDER: do anything else?
}
stdiostream::~stdiostream()
{
// CONSIDER: do anything else?
}
// include here for better granularity
int ios::sunk_with_stdio = 0;
void ios::sync_with_stdio()
{
#if ((!defined(_WINDOWS)) || defined(_QWIN))
if (!sunk_with_stdio) // first time only
{
// printf("In sync_with_stdio\n");
cin = new stdiobuf(stdin);
cin.delbuf(1);
cin.setf(ios::stdio);
cout = new stdiobuf(stdout);
cout.delbuf(1);
cout.setf(ios::stdio|ios::unitbuf);
((stdiobuf*)(cout.rdbuf()))->setrwbuf(0,80); // UNDONE: size??
cerr = new stdiobuf(stderr);
cerr.delbuf(1);
cerr.setf(ios::stdio|ios::unitbuf);
((stdiobuf*)(cerr.rdbuf()))->setrwbuf(0,80); // UNDONE: size??
clog = new stdiobuf(stderr);
clog.delbuf(1);
clog.setf(ios::stdio);
((stdiobuf*)(clog.rdbuf()))->setrwbuf(0,BUFSIZ);
sunk_with_stdio++;
}
#endif
}
|