Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

StdPStreamBuf.cc

Go to the documentation of this file.
00001 // -*- C++ -*- 00002 00003 // StdPStreamBuf.cc 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.cc,v 1.1 2004/06/26 00:24:14 plearner Exp $ 00037 ******************************************************* */ 00038 00039 // Authors: Pascal Vincent 00040 00044 #include "StdPStreamBuf.h" 00045 00046 namespace PLearn { 00047 using namespace std; 00048 00049 00050 StdPStreamBuf::StdPStreamBuf() 00051 :pin(0), pout(0), own_pin(false), own_pout(false) 00052 #if STREAMBUFVER == 0 00053 , original_bufin(0), original_bufout(0) 00054 #endif 00055 {} 00056 00058 StdPStreamBuf::StdPStreamBuf(istream* pin_, bool own_pin_) 00059 :pin(pin_), pout(0), own_pin(own_pin_), own_pout(false) 00060 #if STREAMBUFVER == 0 00061 , original_bufin(pin_->rdbuf()), original_bufout(0) 00062 #endif 00063 { 00064 #if STREAMBUFVER == 0 00065 initInBuf(); 00066 #endif 00067 } 00068 00070 StdPStreamBuf::StdPStreamBuf(ostream* pout_, bool own_pout_) 00071 :pin(0), pout(pout_), own_pin(false), own_pout(own_pout_) 00072 #if STREAMBUFVER == 0 00073 , original_bufin(0), original_bufout(pout_->rdbuf()) 00074 #endif 00075 {} 00076 00078 StdPStreamBuf::StdPStreamBuf(iostream* pios_, bool own_pios_) 00079 :pin(pios_), pout(pios_), own_pin(own_pios_), own_pout(own_pios_) 00080 #if STREAMBUFVER == 0 00081 , original_bufin(pios_->rdbuf()), original_bufout(pios_->rdbuf()) 00082 #endif 00083 { 00084 #if STREAMBUFVER == 0 00085 initInBuf(); 00086 #endif 00087 } 00088 00090 StdPStreamBuf::StdPStreamBuf(istream* pin_, ostream* pout_, bool own_pin_, bool own_pout_) 00091 :pin(pin_), pout(pout_), own_pin(own_pin_), own_pout(own_pout_) 00092 #if STREAMBUFVER == 0 00093 , original_bufin(pin_->rdbuf()), original_bufout(pout_->rdbuf()) 00094 #endif 00095 { 00096 #if STREAMBUFVER == 0 00097 initInBuf(); 00098 #endif 00099 } 00100 00101 StdPStreamBuf::~StdPStreamBuf() 00102 { 00103 #if STREAMBUFVER == 0 00104 // am I the only PStream using this buffer? 00105 if(the_inbuf && 1 == the_inbuf->usage()) 00106 {// reset underlying streams's buffers before destroying the_inbuf 00107 if(pin) pin->rdbuf(original_bufin); 00108 if(pout) pout->rdbuf(original_bufout); 00109 } 00110 #endif 00111 if(own_pin && pin) delete pin; // delete pin if we created it 00112 if(own_pout && pout) delete pout; // delete pout if we created it 00113 } 00114 00115 #if STREAMBUFVER == 0 00116 void StdPStreamBuf::initInBuf() 00117 { 00118 if(pin) 00119 { 00120 the_inbuf= dynamic_cast<pl_streambuf*>(pin->rdbuf()); 00121 if(the_inbuf.isNull()) 00122 the_inbuf= new pl_streambuf(*pin->rdbuf()); 00123 pin->rdbuf(the_inbuf); 00124 } 00125 } 00126 #endif 00127 00128 00129 void StdPStreamBuf::setIn(istream* pin_, bool own_pin_) 00130 { 00131 if(pin && original_bufin) pin->rdbuf(original_bufin); 00132 if(own_pin) delete pin; 00133 pin = pin_; 00134 own_pin = own_pin_; 00135 the_fdbuf = 0; 00136 if(pin) 00137 { 00138 original_bufin = pin->rdbuf(); 00139 #if STREAMBUFVER == 0 00140 initInBuf(); 00141 #endif 00142 } 00143 else 00144 original_bufin = 0; 00145 } 00146 00147 void StdPStreamBuf::setOut(ostream* pout_, bool own_pout_) 00148 { 00149 if(pout && original_bufout) pout->rdbuf(original_bufout); 00150 if(own_pout) delete pout; 00151 pout= pout_; 00152 own_pout = own_pout_; 00153 if(pout) 00154 original_bufout= pout->rdbuf(); 00155 else 00156 original_bufout=0; 00157 } 00158 00160 void StdPStreamBuf::attach(int fd) 00161 { 00162 the_fdbuf= new pl_fdstreambuf(fd, pl_dftbuflen); 00163 the_inbuf= new pl_streambuf(*the_fdbuf); 00164 if(pin) 00165 pin->rdbuf(the_inbuf); 00166 else 00167 { 00168 own_pin= true; 00169 pin= new istream(the_inbuf); 00170 } 00171 if(pout) 00172 pout->rdbuf(the_fdbuf); 00173 else 00174 { 00175 own_pout= true; 00176 pout= new ostream(the_fdbuf); 00177 } 00178 } 00179 00180 StdPStreamBuf::streamsize StdPStreamBuf::read_(char* p, streamsize n) 00181 { 00182 if(pin==0) 00183 PLERROR("StdPStreamBuf::read_ with pin==0"); 00184 return pin->readsome(p,n); 00185 } 00186 00188 void StdPStreamBuf::write_(const char* p, streamsize n) 00189 { 00190 if(pout==0) 00191 PLERROR("StdPStreamBuf::write_ with pout==0"); 00192 pout->write(p,n); 00193 pout->flush(); 00194 } 00195 00196 } // end of namespace PLearn

Generated on Tue Aug 17 16:07:31 2004 for PLearn by doxygen 1.3.7