Merge from vendor branch BIND:
[dragonfly.git] / contrib / libio / indstream.cc
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 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.
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 (bothner@cygnus.com). */
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 streamsize indirectbuf::xsputn(const char* s, streamsize n)
48 {
49     return put_stream()->sputn(s, n);
50 }
51
52 streamsize indirectbuf::xsgetn(char* s, streamsize 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()->sgetc();
68 }
69
70 int indirectbuf::uflow()
71 {
72     return get_stream()->sbumpc();
73 }
74
75 streampos indirectbuf::seekoff(streamoff off, _seek_dir dir, int mode)
76 {
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);
88     }
89     return ret_val;
90 }
91
92 streampos indirectbuf::seekpos(streampos pos, int mode)
93 {
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);
105     }
106     return ret_val;
107 }
108
109 int indirectbuf::sync()
110 {
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;
116 }
117
118 int indirectbuf::pbackfail(int c)
119 {
120     return get_stream()->sputbackc(c);
121 }