00001 // -*- C++ -*- 00002 00003 // StdPStreamBuf.h 00004 // 00005 // Copyright (C) 2004 Pascal Vincent 00006 // 00007 // Redistribution and use in source and binary forms, with or without 00008 // modification, are permitted provided that the following conditions are met: 00009 // 00010 // 1. Redistributions of source code must retain the above copyright 00011 // notice, this list of conditions and the following disclaimer. 00012 // 00013 // 2. Redistributions in binary form must reproduce the above copyright 00014 // notice, this list of conditions and the following disclaimer in the 00015 // documentation and/or other materials provided with the distribution. 00016 // 00017 // 3. The name of the authors may not be used to endorse or promote 00018 // products derived from this software without specific prior written 00019 // permission. 00020 // 00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 // 00032 // This file is part of the PLearn library. For more information on the PLearn 00033 // library, go to the PLearn Web site at www.plearn.org 00034 00035 /* ******************************************************* 00036 * $Id: StdPStreamBuf.h,v 1.1 2004/06/26 00:24:14 plearner Exp $ 00037 ******************************************************* */ 00038 00039 // Authors: Pascal Vincent 00040 00043 00044 00045 #ifndef StdPStreamBuf_INC 00046 #define StdPStreamBuf_INC 00047 00048 #include "PStreamBuf.h" 00049 #include <iostream> 00050 00051 #if STREAMBUFVER == 0 00052 #include "pl_streambuf.h" 00053 #include "pl_fdstream.h" 00054 #endif 00055 00056 namespace PLearn { 00057 using namespace std; 00058 00059 class StdPStreamBuf: public PStreamBuf 00060 { 00061 00062 private: 00063 00064 typedef PStreamBuf inherited; 00065 00066 protected: 00067 // ********************* 00068 // * protected options * 00069 // ********************* 00070 istream* pin; //<! underlying input stream 00071 ostream* pout; //<! underlying output stream 00072 bool own_pin, own_pout; //<! true if {pin|pout} was created internally 00073 00074 #if STREAMBUFVER == 0 00075 PP<pl_streambuf> the_inbuf; //<! markable input buffer 00076 PP<pl_fdstreambuf> the_fdbuf; //<! buffer on a POSIX file descriptor 00077 00080 streambuf* original_bufin; 00081 streambuf* original_bufout; 00082 00084 void initInBuf(); 00085 #endif 00086 00087 00088 public: 00089 00090 // Default constructor, make sure the implementation in the .cc 00091 // initializes all fields to reasonable default values. 00092 StdPStreamBuf(); 00093 00095 StdPStreamBuf(istream* pin_, bool own_pin_=false); 00096 00098 StdPStreamBuf(ostream* pout_, bool own_pout_=false); 00099 00101 StdPStreamBuf(iostream* pios_, bool own_pios_=false); 00102 00104 StdPStreamBuf(istream* pin_, ostream* pout_, bool own_pin_=false, bool own_pout_=false); 00105 00106 virtual ~StdPStreamBuf(); 00107 00108 00109 void setIn(istream* pin_, bool own_pin_=false); 00110 00111 void setOut(ostream* pout_, bool own_pout_=false); 00112 00114 void attach(int fd); 00115 00116 // ****************** 00117 // * Object methods * 00118 // ****************** 00119 00120 protected: 00121 00122 virtual streamsize read_(char* p, streamsize n); 00123 00125 virtual void write_(const char* p, streamsize n); 00126 00127 public: 00128 inline istream* rawin() { return pin; } //<! access to underlying istream 00129 inline ostream* rawout() { return pout; } 00130 00131 #if STREAMBUFVER == 0 00134 inline pl_streambuf* pl_rdbuf() { return the_inbuf; } 00135 #endif 00136 00137 }; 00138 00139 } // end of namespace PLearn 00140 00141 #endif