Mon Nov 17 23:42:03 1997 Bruno Haible <haible@ilog.fr>
[official-gcc.git] / libio / indstream.cc
blob15a1bb2778d34c32a5af65958aee88e812f73d7f
1 /* This is part of libio/iostream, providing -*- C++ -*- input/output.
2 Copyright (C) 1993 Free Software Foundation
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.
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.
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.
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.
25 Written by Per Bothner (bothner@cygnus.com). */
27 #ifdef __GNUG__
28 #pragma implementation
29 #endif
31 #include <indstream.h>
33 indirectbuf::indirectbuf(streambuf *get, streambuf *put, int delete_mode)
34 : streambuf()
36 _get_stream = get;
37 _put_stream = put == NULL ? get : put;
38 _delete_flags = delete_mode;
41 indirectbuf::~indirectbuf()
43 if (_delete_flags & ios::in) delete get_stream();
44 if (_delete_flags & ios::out) delete put_stream();
47 streamsize indirectbuf::xsputn(const char* s, streamsize n)
49 return put_stream()->sputn(s, n);
52 streamsize indirectbuf::xsgetn(char* s, streamsize n)
54 return get_stream()->sgetn(s, n);
57 int indirectbuf::overflow(int c /* = EOF */)
59 if (c == EOF)
60 return put_stream()->overflow(c);
61 else
62 return put_stream()->sputc(c);
65 int indirectbuf::underflow()
67 return get_stream()->sgetc();
70 int indirectbuf::uflow()
72 return get_stream()->sbumpc();
75 streampos indirectbuf::seekoff(streamoff off, _seek_dir dir, int mode)
77 int ret_val = 0;
78 int select = mode == 0 ? (ios::in|ios::out) : mode;
79 streambuf *gbuf = (select & ios::in) ? get_stream() : (streambuf*)NULL;
80 streambuf *pbuf = (select & ios::out) ? put_stream() : (streambuf*)NULL;
81 if (gbuf == pbuf)
82 ret_val = gbuf->seekoff(off, dir, mode);
83 else {
84 if (gbuf)
85 ret_val = gbuf->seekoff(off, dir, ios::in);
86 if (pbuf && ret_val != EOF)
87 ret_val = pbuf->seekoff(off, dir, ios::out);
89 return ret_val;
92 streampos indirectbuf::seekpos(streampos pos, int mode)
94 int ret_val = EOF;
95 int select = mode == 0 ? (ios::in|ios::out) : mode;
96 streambuf *gbuf = (select & ios::in) ? get_stream() : (streambuf*)NULL;
97 streambuf *pbuf = (select & ios::out) ? put_stream() : (streambuf*)NULL;
98 if (gbuf == pbuf && gbuf != NULL)
99 ret_val = gbuf->seekpos(pos, mode);
100 else {
101 if (gbuf)
102 ret_val = gbuf->seekpos(pos, ios::in);
103 if (pbuf && ret_val != EOF)
104 ret_val = pbuf->seekpos(pos, ios::out);
106 return ret_val;
109 int indirectbuf::sync()
111 streambuf *gbuf = get_stream();
112 int get_ret_val = gbuf ? gbuf->sync() : 0;
113 streambuf *pbuf = put_stream();
114 int put_ret_val = (pbuf && pbuf != gbuf) ? pbuf->sync() : 0;
115 return get_ret_val || put_ret_val;
118 int indirectbuf::pbackfail(int c)
120 return get_stream()->sputbackc(c);