00001 // -*- C++ -*- 00002 00003 // PStreamBuf.cc 00004 // 00005 // Copyright (C) 2003 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: PStreamBuf.cc,v 1.4 2004/06/26 00:24:14 plearner Exp $ 00037 ******************************************************* */ 00038 00040 #include "PStreamBuf.h" 00041 00042 namespace PLearn { 00043 using namespace std; 00044 00045 PStreamBuf::PStreamBuf(streamsize inbuf_capacity, streamsize outbuf_capacity, 00046 streamsize unget_capacity) 00047 :ungetsize(unget_capacity), 00048 inbuf_chunksize(inbuf_capacity), 00049 inbuf(0), inbuf_p(0), inbuf_end(0), 00050 outbuf_chunksize(outbuf_capacity), 00051 outbuf(0), outbuf_p(0), outbuf_end(0), 00052 is_readable(false), 00053 is_writable(false), 00054 is_random_accessible(false) 00055 { 00056 build_(); 00057 } 00058 00059 PStreamBuf::~PStreamBuf() 00060 { 00061 if(inbuf) 00062 delete[] inbuf; 00063 if(outbuf) 00064 { 00065 flush(); 00066 delete[] outbuf; 00067 } 00068 } 00069 00070 void PStreamBuf::build_() 00071 { 00072 if(inbuf) 00073 delete[] inbuf; 00074 if(ungetsize+inbuf_chunksize>0) 00075 { 00076 inbuf = new char[ungetsize+inbuf_chunksize]; 00077 inbuf_p = inbuf+ungetsize; 00078 inbuf_end = inbuf_p; 00079 } 00080 00081 if(outbuf) 00082 { 00083 flush(); 00084 delete[] outbuf; 00085 } 00086 if(outbuf_chunksize<0) 00087 { 00088 outbuf = new char[outbuf_chunksize]; 00089 outbuf_p = outbuf; 00090 outbuf_end = outbuf+outbuf_chunksize; 00091 } 00092 } 00093 00094 PStreamBuf::streamsize PStreamBuf::read_(char* p, streamsize n) 00095 { 00096 PLERROR("read_ not implemented for this PStremBuf"); 00097 return 0; 00098 } 00099 00102 void PStreamBuf::write_(const char* p, streamsize n) 00103 { 00104 PLERROR("write_ not implemented for this PStremBuf"); 00105 } 00106 00107 00108 00109 /* 00110 00111 // Initially I planned to derive this class from Object. 00112 // But when attempting compilation I found out back in include hell!!! 00113 // (Object needs PStream which needs PStremBuf which needs Object) 00114 // Getting out of this mess would not be easy: we need efficiency, so many of 00115 // the concerned calls really should stay inline (hence in the .h). 00116 // Finally I opted to have it derive from PPoitable rather than Object. 00117 // That's why all typical Object stuff are commented out. 00118 // (Pascal) 00119 00120 PLEARN_IMPLEMENT_ABSTRACT_OBJECT(PStreamBuf, "Base class for PLearn strem-buffers", ""); 00121 00122 void PStreamBuf::declareOptions(OptionList& ol) 00123 { 00124 declareOption(ol, "inbuf_capacity", &PStreamBuf::inbuf_chunksize, OptionBase::buildoption, 00125 "The capacity of the input buffer"); 00126 declareOption(ol, "outbuf_capacity", &PStreamBuf::outbuf_chunksize, OptionBase::buildoption, 00127 "The capacity of the output buffer"); 00128 declareOption(ol, "unget_capacity", &PStreamBuf::ungetsize, OptionBase::buildoption, 00129 "Maximum number of characters that are guaranteed you can unget"); 00130 00131 declareOption(ol, "is_readable", &PStreamBuf::is_readable, OptionBase::buildoption, 00132 "Is this an input stream?"); 00133 declareOption(ol, "is_writable", &PStreamBuf::is_writable, OptionBase::buildoption, 00134 "Is this an output stream?"); 00135 declareOption(ol, "is_random_accessible", &PStreamBuf::is_random_accessible, OptionBase::buildoption, 00136 "Can we use getpos and setpos on this stream?"); 00137 00138 // Now call the parent class' declareOptions 00139 inherited::declareOptions(ol); 00140 } 00141 00142 // ### Nothing to add here, simply calls build_ 00143 void PStreamBuf::build() 00144 { 00145 inherited::build(); 00146 build_(); 00147 } 00148 00149 void PStreamBuf::makeDeepCopyFromShallowCopy(map<const void*, void*>& copies) 00150 { 00151 inherited::makeDeepCopyFromShallowCopy(copies); 00152 } 00153 00154 */ 00155 00156 00157 00158 00159 } // end of namespace PLearn