1/* This is part of libio/iostream, providing -*- C++ -*- input/output. 2Copyright (C) 1993 Free Software Foundation 3 4This file is part of the GNU IO Library. This library is free 5software; you can redistribute it and/or modify it under the 6terms of the GNU General Public License as published by the 7Free Software Foundation; either version 2, or (at your option) 8any later version. 9 10This library is distributed in the hope that it will be useful, 11but WITHOUT ANY WARRANTY; without even the implied warranty of 12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13GNU General Public License for more details. 14 15You should have received a copy of the GNU General Public License 16along with this library; see the file COPYING. If not, write to the Free 17Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 18 19As a special exception, if you link this library with files 20compiled with a GNU compiler to produce an executable, this does not cause 21the resulting executable to be covered by the GNU General Public License. 22This exception does not however invalidate any other reasons why 23the executable file might be covered by the GNU General Public License. 24 25Written by Per Bothner (bothner@cygnus.com). */ 26 27#ifdef __GNUG__ 28#pragma implementation 29#endif 30 31#include <indstream.h> 32 33indirectbuf::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 41indirectbuf::~indirectbuf() 42{ 43 if (_delete_flags & ios::in) delete get_stream(); 44 if (_delete_flags & ios::out) delete put_stream(); 45} 46 47streamsize indirectbuf::xsputn(const char* s, streamsize n) 48{ 49 return put_stream()->sputn(s, n); 50} 51 52streamsize indirectbuf::xsgetn(char* s, streamsize n) 53{ 54 return get_stream()->sgetn(s, n); 55} 56 57int 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 65int indirectbuf::underflow() 66{ 67 return get_stream()->sgetc(); 68} 69 70int indirectbuf::uflow() 71{ 72 return get_stream()->sbumpc(); 73} 74 75streampos 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 92streampos 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 109int 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 118int indirectbuf::pbackfail(int c) 119{ 120 return get_stream()->sputbackc(c); 121} 122