|
|
1.1 root 1: /* This is part of libio/iostream, providing -*- C++ -*- input/output.
2: Copyright (C) 1993 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 GNU CC; see the file COPYING. If not, write to
17: the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, 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 ([email protected]). */
26:
27: #ifdef __GNUG__
28: #pragma implementation
29: #endif
30:
31: #include <indstream.h>
32:
33: indirectbuf::indirectbuf(streambuf *get, streambuf *put, int delete_mode)
34: : streambuf()
35: {
36: _get_stream = get;
37: _put_stream = put == NULL ? get : put;
38: _delete_flags = delete_mode;
39: }
40:
41: indirectbuf::~indirectbuf()
42: {
43: if (_delete_flags & ios::in) delete get_stream();
44: if (_delete_flags & ios::out) delete put_stream();
45: }
46:
47: int indirectbuf::xsputn(const char* s, int n)
48: {
49: return put_stream()->sputn(s, n);
50: }
51:
52: int indirectbuf::xsgetn(char* s, int n)
53: {
54: return get_stream()->sgetn(s, n);
55: }
56:
57: int indirectbuf::overflow(int c /* = EOF */)
58: {
59: if (c == EOF)
60: return put_stream()->overflow(c);
61: else
62: return put_stream()->sputc(c);
63: }
64:
65: int indirectbuf::underflow()
66: {
67: return get_stream()->sbumpc();
68: }
69:
70: streampos indirectbuf::seekoff(streamoff off, _seek_dir dir, int mode)
71: {
72: int ret_val = 0;
73: int select = mode == 0 ? (ios::in|ios::out) : mode;
74: streambuf *gbuf = (select & ios::in) ? get_stream() : NULL;
75: streambuf *pbuf = (select & ios::out) ? put_stream() : NULL;
76: if (gbuf == pbuf)
77: ret_val = gbuf->seekoff(off, dir, mode);
78: else {
79: if (gbuf)
80: ret_val = gbuf->seekoff(off, dir, ios::in);
81: if (pbuf && ret_val != EOF)
82: ret_val = pbuf->seekoff(off, dir, ios::out);
83: }
84: return ret_val;
85: }
86:
87: streampos indirectbuf::seekpos(streampos pos, int mode)
88: {
89: int ret_val = EOF;
90: int select = mode == 0 ? (ios::in|ios::out) : mode;
91: streambuf *gbuf = (select & ios::in) ? get_stream() : NULL;
92: streambuf *pbuf = (select & ios::out) ? put_stream() : NULL;
93: if (gbuf == pbuf)
94: ret_val = gbuf->seekpos(pos, mode);
95: else {
96: if (gbuf)
97: ret_val = gbuf->seekpos(pos, ios::in);
98: if (pbuf && ret_val != EOF)
99: ret_val = pbuf->seekpos(pos, ios::out);
100: }
101: return ret_val;
102: }
103:
104: int indirectbuf::sync()
105: {
106: streambuf *gbuf = get_stream();
107: int get_ret_val = gbuf ? gbuf->sync() : 0;
108: streambuf *pbuf = put_stream();
109: int put_ret_val = (pbuf && pbuf != gbuf) ? pbuf->sync() : 0;
110: return get_ret_val || put_ret_val;
111: }
112:
113: int indirectbuf::pbackfail(int c)
114: {
115: return get_stream()->sputbackc(c);
116: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.