PLearn 0.1
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 4287 2005-10-20 16:29:47Z plearner $ 
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     :PStreamBuf(false,false),
00052      pin(0), pout(0), own_pin(false), own_pout(false)
00053 {}
00054 
00056 StdPStreamBuf::StdPStreamBuf(istream* pin_, bool own_pin_)
00057     :PStreamBuf(true,false),
00058      pin(pin_), pout(0), own_pin(own_pin_), own_pout(false)
00059 { 
00060 }
00061 
00063 StdPStreamBuf::StdPStreamBuf(ostream* pout_, bool own_pout_)
00064     :PStreamBuf(false,true),
00065      pin(0), pout(pout_), own_pin(false), own_pout(own_pout_)
00066 {}
00067 
00069 StdPStreamBuf::StdPStreamBuf(iostream* pios_, bool own_pios_)
00070     :PStreamBuf(true,true),
00071      pin(pios_), pout(pios_), own_pin(own_pios_), own_pout(own_pios_)
00072 { 
00073 }
00074 
00076 StdPStreamBuf::StdPStreamBuf(istream* pin_, ostream* pout_, bool own_pin_, bool own_pout_)
00077     :PStreamBuf(true,true),
00078      pin(pin_), pout(pout_), own_pin(own_pin_), own_pout(own_pout_)
00079 { 
00080 }
00081 
00082 StdPStreamBuf::~StdPStreamBuf()
00083 {
00084     flush();
00085     if (own_pin && pin)
00086         delete pin; // delete pin if we created it
00087     if (own_pout && pout)
00088         delete pout; // delete pout if we created it
00089 }
00090 
00091 
00092 
00093 void StdPStreamBuf::setIn(istream* pin_, bool own_pin_)
00094 {
00095     if (own_pin)
00096         delete pin;
00097     pin = pin_;
00098     own_pin = own_pin_;
00099     is_readable = (pin_!=0);
00100 }
00101 
00102 void StdPStreamBuf::setOut(ostream* pout_, bool own_pout_)
00103 {
00104     if (own_pout)
00105         delete pout;
00106     pout= pout_;
00107     own_pout = own_pout_;
00108     is_writable = (pout_!=0);
00109 }
00110   
00111 
00112 
00113 StdPStreamBuf::streamsize StdPStreamBuf::read_(char* p, streamsize n)
00114 {
00115     if (pin==0)
00116         PLERROR("StdPStreamBuf::read_ with pin==0");
00117     streamsize nread = pin->readsome(p, std::streamsize(n));
00118     if(nread>0)      
00119         return nread;
00120 
00121     // if nread==0 maybe it's because no chars were available now (non-blocking readsome)
00122     pin->read(p,1);
00123     return pin->gcount();
00124 }
00125 
00127 void StdPStreamBuf::write_(const char* p, streamsize n)
00128 {
00129     if (pout==0)      
00130         PLERROR("StdPStreamBuf::write_ with pout==0");
00131     pout->write(p, std::streamsize(n));
00132     pout->flush();
00133 }
00134 
00135 bool StdPStreamBuf::good() const
00136 {
00137     if (is_readable && is_writable)
00138         return !eof() && pout->good();
00139     else if (is_readable && !is_writable)
00140         return !eof();
00141     else if (!is_readable && is_writable)
00142         return pout->good();
00143     else
00144         return false;
00145 }
00146 
00147 } // end of namespace PLearn
00148 
00149 
00150 /*
00151   Local Variables:
00152   mode:c++
00153   c-basic-offset:4
00154   c-file-style:"stroustrup"
00155   c-file-offsets:((innamespace . 0)(inline-open . 0))
00156   indent-tabs-mode:nil
00157   fill-column:79
00158   End:
00159 */
00160 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines