summaryrefslogtreecommitdiff
path: root/gnu/egcs/libio/streambuf.cc
blob: 057d1e6e4ce9bca11feeff1dbd69bb33f8b06e6e (plain)
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/* This is part of libio/iostream, providing -*- C++ -*- input/output.
Copyright (C) 1991, 1992, 1993, 1995 Free Software Foundation

This file is part of the GNU IO Library.  This library is free
software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option)
any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this library; see the file COPYING.  If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

As a special exception, if you link this library with files
compiled with a GNU compiler to produce an executable, this does not cause
the resulting executable to be covered by the GNU General Public License.
This exception does not however invalidate any other reasons why
the executable file might be covered by the GNU General Public License. */

/* Written by Per Bothner (bothner@cygnus.com). */

#define _STREAM_COMPAT
#ifdef __GNUG__
#pragma implementation
#endif
#include "iostreamP.h"
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#ifndef errno
extern int errno;
#endif

void streambuf::_un_link() { _IO_un_link(this); }

void streambuf::_link_in() { _IO_link_in(this); }

int streambuf::switch_to_get_mode()
{ return _IO_switch_to_get_mode(this); }

void streambuf::free_backup_area()
{ _IO_free_backup_area(this); }

#if 0
int streambuf::switch_to_put_mode()
{ return _IO_:switch_to_put_mode(this); }
#endif

int __overflow(streambuf* sb, int c)
{
    return sb->overflow(c);
}

int streambuf::underflow()
{ return EOF; }

int streambuf::uflow()
{ return _IO_default_uflow (this); }

int streambuf::overflow(int /* = EOF */)
{ return EOF; }

streamsize streambuf::xsputn(register const char* s, streamsize n)
{ return _IO_default_xsputn(this, s, n); }

streamsize streambuf::xsgetn(char* s, streamsize n)
{ return _IO_default_xsgetn(this, s, n); }

int streambuf::ignore(int n)
{
    register int more = n;
    for (;;) {
	int count = _IO_read_end - _IO_read_ptr; // Data available.
	if (count > 0) {
	    if (count > more)
		count = more;
	    _IO_read_ptr += count;
	    more -= count;
	}
	if (more == 0 || __underflow(this) == EOF)
	    break;
    }
    return n - more;
}

int streambuf::sync()
{
  return 0;
}

int streambuf::pbackfail(int c)
{
  return _IO_default_pbackfail(this, c);
}

streambuf* streambuf::setbuf(char* p, int len)
{
    if (sync() == EOF)
	return NULL;
    if (p == NULL || len == 0) {
	unbuffered(1);
	setb(_shortbuf, _shortbuf+1, 0);
    }
    else {
	unbuffered(0);
	setb(p, p+len, 0);
    }
    setp(0, 0);
    setg(0, 0, 0);
    return this;
}

streampos streambuf::seekpos(streampos pos, int mode)
{
    return seekoff(pos, ios::beg, mode);
}

streampos streambuf::sseekpos(streampos pos, int mode)
{
  return _IO_seekpos (this, pos, mode);
}

void streambuf::setb(char* b, char* eb, int a)
{ _IO_setb(this, b, eb, a); }

int streambuf::doallocate() { return _IO_default_doallocate(this); }

void streambuf::doallocbuf() { _IO_doallocbuf(this); }

#if !_IO_UNIFIED_JUMPTABLES
/* The following are jump table entries that just call the virtual method */

static int _IO_sb_overflow(_IO_FILE *fp, int c)
{ return ((streambuf*)fp)->overflow(c); }
static int _IO_sb_underflow(_IO_FILE *fp)
{ return ((streambuf*)fp)->underflow(); }
static _IO_size_t _IO_sb_xsputn(_IO_FILE *fp, const void *s, _IO_size_t n)
{ return ((streambuf*)fp)->xsputn((const char*)s, n); }
static _IO_size_t _IO_sb_xsgetn(_IO_FILE *fp, void *s, _IO_size_t n)
{ return ((streambuf*)fp)->xsgetn((char*)s, n); }
static int _IO_sb_close(_IO_FILE *fp)
{ return ((streambuf*)fp)->sys_close(); }
static int _IO_sb_stat(_IO_FILE *fp, void *b)
{ return ((streambuf*)fp)->sys_stat(b); }
static int _IO_sb_doallocate(_IO_FILE *fp)
{ return ((streambuf*)fp)->doallocate(); }

static _IO_pos_t _IO_sb_seekoff(_IO_FILE *fp, _IO_off_t pos, int dir, int mode)
{
  return ((streambuf*)fp)->seekoff(pos, (ios::seek_dir)dir, mode);
}

static _IO_pos_t _IO_sb_seekpos(_IO_FILE *fp, _IO_pos_t pos, int mode)
{
  return ((streambuf*)fp)->seekpos(pos, mode);
}

static int _IO_sb_pbackfail(_IO_FILE *fp, int ch)
{ return ((streambuf*)fp)->pbackfail(ch); }
static void _IO_sb_finish(_IO_FILE *fp, int)
{ ((streambuf*)fp)->~streambuf(); }
static _IO_ssize_t _IO_sb_read(_IO_FILE *fp, void *buf, _IO_ssize_t n)
{ return ((streambuf*)fp)->sys_read((char*)buf, n); }
static _IO_ssize_t _IO_sb_write(_IO_FILE *fp, const void *buf, _IO_ssize_t n)
{ return ((streambuf*)fp)->sys_write((const char*)buf, n); }
static int _IO_sb_sync(_IO_FILE *fp)
{ return ((streambuf*)fp)->sync(); }
static _IO_pos_t _IO_sb_seek(_IO_FILE *fp, _IO_off_t off, int dir)
{ return ((streambuf*)fp)->sys_seek(off, (_seek_dir)dir); }
static _IO_FILE* _IO_sb_setbuf(_IO_FILE *fp, char *buf, _IO_ssize_t n)
{ return ((streambuf*)fp)->setbuf(buf, n); }

/* This callbacks in this jumptable just call the corresponding
   virtual function, so that C functions can access (potentially user-defined)
   streambuf-derived objects.
   Contrast the builtinbuf class, which does the converse:  Allow
   C++ virtual calls to be used on _IO_FILE objects that are builtin
   (or defined by C code). */


struct _IO_jump_t _IO_streambuf_jumps = {
  JUMP_INIT_DUMMY,
  JUMP_INIT(finish, _IO_sb_finish),
  JUMP_INIT(overflow, _IO_sb_overflow),
  JUMP_INIT(underflow, _IO_sb_underflow),
  JUMP_INIT(uflow, _IO_default_uflow),
  JUMP_INIT(pbackfail, _IO_sb_pbackfail),
  JUMP_INIT(xsputn, _IO_sb_xsputn),
  JUMP_INIT(xsgetn, _IO_sb_xsgetn),
  JUMP_INIT(seekoff, _IO_sb_seekoff),
  JUMP_INIT(seekpos, _IO_sb_seekpos),
  JUMP_INIT(setbuf, _IO_sb_setbuf),
  JUMP_INIT(sync, _IO_sb_sync),
  JUMP_INIT(doallocate, _IO_sb_doallocate),
  JUMP_INIT(read, _IO_sb_read),
  JUMP_INIT(write, _IO_sb_write),
  JUMP_INIT(seek, _IO_sb_seek),
  JUMP_INIT(close, _IO_sb_close),
  JUMP_INIT(stat, _IO_sb_stat)
};
#endif

streambuf::streambuf(int flags)
{
#ifdef _IO_MTSAFE_IO
  _lock = new _IO_lock_t;
#endif
  _IO_init(this, flags);
#if !_IO_UNIFIED_JUMPTABLES
  _jumps = &_IO_streambuf_jumps;
#endif
}

streambuf::~streambuf()
{
  _IO_default_finish(this,0);
#ifdef _IO_MTSAFE_IO
  if (this != _IO_stdin && this != _IO_stdout && this != _IO_stderr)
    delete _lock;
#endif
}

streampos
streambuf::seekoff(streamoff, _seek_dir, int /*=ios::in|ios::out*/)
{
    return EOF;
}

streampos
streambuf::sseekoff(streamoff o , _seek_dir d, int m /*=ios::in|ios::out*/)
{
  return _IO_seekoff (this, o, d, m);
}

int streambuf::sputbackc(char c)
{
  return _IO_sputbackc(this, c);
}

int streambuf::sungetc()
{
  return _IO_sungetc(this);
}

#if 0 /* Work in progress */
void streambuf::collumn(int c)
{
    if (c == -1)
	_collumn = -1;
    else
	_collumn = c - (_IO_write_ptr - _IO_write_base);
}
#endif


int streambuf::get_column()
{
    if (_cur_column) 
	return _IO_adjust_column(_cur_column - 1, pbase(), pptr() - pbase());
    return -1;
}

int streambuf::set_column(int i)
{
    _cur_column = i+1;
    return 0;
}

int streambuf::flush_all() { return _IO_flush_all (); }

void streambuf::flush_all_linebuffered()
{ _IO_flush_all_linebuffered(); }

int streambuf::sys_stat(void *)
{
#ifdef EIO
  errno = EIO;
#endif
  return -1;
}

streamsize streambuf::sys_read(char* /*buf*/, streamsize /*size*/)
{
  return 0;
}

streamsize streambuf::sys_write(const char* /*buf*/, streamsize /*size*/)
{
  return 0;
}

streampos streambuf::sys_seek(streamoff, _seek_dir)
{
  return EOF;
}

int streambuf::sys_close() { return 0; /* Suceess; do nothing */ }

#if _G_IO_IO_FILE_VERSION == 0x20001
int streambuf::showmanyc()
{
  return -1;
}

void streambuf::imbue(void *)
{
}
#endif

streammarker::streammarker(streambuf *sb)
{
  _IO_init_marker(this, sb);
}

streammarker::~streammarker()
{
  _IO_remove_marker(this);
}

#define BAD_DELTA EOF

int streammarker::delta(streammarker& other_mark)
{
  return _IO_marker_difference(this, &other_mark);
}

int streammarker::delta()
{
  return _IO_marker_delta(this);
}

int streambuf::seekmark(streammarker& mark, int delta /* = 0 */)
{
  return _IO_seekmark(this, &mark, delta);
}

void streambuf::unsave_markers()
{
  _IO_unsave_markers(this);
}

int ios::readable() { return !(rdbuf()->_flags & _IO_NO_READS); }
int ios::writable() { return !(rdbuf()->_flags & _IO_NO_WRITES); }
int ios::is_open() { return rdbuf()
			 && (rdbuf()->_flags & _IO_NO_READS+_IO_NO_WRITES)
			     != _IO_NO_READS+_IO_NO_WRITES; }

#if defined(linux)
#define IO_CLEANUP
#endif

#ifdef IO_CLEANUP
  IO_CLEANUP
#else
struct __io_defs {
    ~__io_defs() { _IO_cleanup (); }
};   
__io_defs io_defs__;
#endif