PLearn 0.1
|
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 9199 2008-07-03 15:00:12Z nouiz $ 00037 ******************************************************* */ 00038 00040 #include "PStreamBuf.h" 00041 #include <plearn/io/pl_log.h> 00042 00043 #define PSTREAMBUF_NO_GET (-1000) 00044 00045 namespace PLearn { 00046 using namespace std; 00047 00048 PStreamBuf::PStreamBuf(bool is_readable_, bool is_writable_, 00049 streamsize inbuf_capacity, streamsize outbuf_capacity, 00050 streamsize unget_capacity) 00051 :is_readable(is_readable_), 00052 is_writable(is_writable_), 00053 last_get(PSTREAMBUF_NO_GET), 00054 ungetsize(0), 00055 inbuf_chunksize(0), 00056 inbuf(0), inbuf_p(0), inbuf_end(0), 00057 outbuf_chunksize(0), 00058 outbuf(0), outbuf_p(0), outbuf_end(0) 00059 { 00060 setBufferCapacities(inbuf_capacity, outbuf_capacity, unget_capacity); 00061 } 00062 00063 PStreamBuf::~PStreamBuf() 00064 { 00065 if (inbuf) 00066 delete[] inbuf; 00067 if (outbuf) 00068 delete[] outbuf; 00069 } 00070 00071 void PStreamBuf::setBufferCapacities(streamsize inbuf_capacity, 00072 streamsize outbuf_capacity, streamsize unget_capacity) 00073 { 00074 if (inbuf_capacity < 1) 00075 inbuf_capacity = 1; 00076 if (unget_capacity < 1) 00077 unget_capacity = 1; 00078 ungetsize = unget_capacity; 00079 inbuf_chunksize = inbuf_capacity; 00080 outbuf_chunksize = outbuf_capacity; 00081 00082 if (inbuf) 00083 delete[] inbuf; 00084 if (ungetsize + inbuf_chunksize <= 0) 00085 inbuf = inbuf_p = inbuf_end = 0; 00086 else 00087 { 00088 inbuf = new char[ungetsize+inbuf_chunksize]; 00089 inbuf_p = inbuf+ungetsize; 00090 inbuf_end = inbuf_p; 00091 } 00092 00093 if (outbuf) 00094 { 00095 flush(); 00096 delete[] outbuf; 00097 } 00098 if (outbuf_chunksize <= 0) 00099 outbuf = outbuf_p = outbuf_end = 0; 00100 else 00101 { 00102 outbuf = new char[outbuf_chunksize]; 00103 outbuf_p = outbuf; 00104 outbuf_end = outbuf+outbuf_chunksize; 00105 } 00106 } 00107 00108 PStreamBuf::streamsize PStreamBuf::read_(char* p, streamsize n) 00109 { 00110 PLERROR("read_ not implemented for this PStreamBuf"); 00111 return 0; 00112 } 00113 00116 void PStreamBuf::write_(const char* p, streamsize n) 00117 { 00118 PLERROR("write_ not implemented for this PStreamBuf"); 00119 } 00120 00121 PStreamBuf::streamsize PStreamBuf::refill_in_buf() 00122 { 00123 #ifdef BOUNDCHECK 00124 if(!isReadable()) 00125 PLERROR("Called PStreamBuf::refill_in_buf on a buffer not marked as readable"); 00126 #endif 00127 00128 inbuf_p = inbuf + ungetsize; 00129 inbuf_end= inbuf_p; //buf empty until read_ finished 00130 streamsize n = read_(inbuf_p, inbuf_chunksize); 00131 inbuf_end = inbuf_p + n; 00132 return n; 00133 } 00134 00135 PStreamBuf::streamsize PStreamBuf::read(char* p, streamsize n) 00136 { 00137 #ifdef BOUNDCHECK 00138 if(!isReadable()) 00139 PLERROR("Called PStreamBuf::read on a buffer not marked as readable"); 00140 #endif 00141 00142 streamsize nleft = n; 00143 00144 streamsize inbuf_n = (streamsize)(inbuf_end-inbuf_p); 00145 if(inbuf_n) // First copy what's left in the buffer 00146 { 00147 streamsize k = nleft<inbuf_n ?nleft :inbuf_n; 00148 memcpy(p,inbuf_p,k); 00149 inbuf_p += k; 00150 p += k; 00151 nleft -= k; 00152 } 00153 00154 if(nleft) // need some more ? 00155 { 00156 if(nleft>=inbuf_chunksize) // large block: read it directly 00157 { 00158 streamsize nr= read_(p,nleft); 00159 nleft-= nr; 00160 p+= nr; 00161 while(nleft > 0 && nr > 0) // need some more and not eof? 00162 { 00163 nr= read_(p,nleft); 00164 nleft-= nr; 00165 p+= nr; 00166 } 00167 } 00168 else // small block: read it in the buffer first 00169 { 00170 inbuf_n = refill_in_buf(); 00171 while(nleft > 0 && inbuf_n > 0) // need some more and not eof? 00172 { 00173 streamsize k = nleft<inbuf_n ?nleft :inbuf_n; 00174 memcpy(p,inbuf_p,k); 00175 inbuf_p += k; 00176 nleft -= k; 00177 p+= k; 00178 if(nleft > 0) 00179 inbuf_n = refill_in_buf(); 00180 } 00181 } 00182 } 00183 00184 streamsize nread = n-nleft; 00185 00186 if (nread > 0) 00187 last_get = (unsigned char) p[-1];//p has advanced and now points one after the end 00188 else 00189 last_get = EOF; 00190 00191 return nread; 00192 } 00193 00194 void PStreamBuf::unread(const char* p, streamsize n) 00195 { 00196 if(streamsize(inbuf_p-inbuf)<n) 00197 PLERROR("Cannot unread that many characters: %ld, input buffer bound reached (you may want to increase the unget_capacity)", long(n)); 00198 00199 inbuf_p -= n; 00200 memcpy(inbuf_p,p,n); 00201 } 00202 00203 void PStreamBuf::putback(char c) 00204 { 00205 if(inbuf_p<=inbuf) 00206 PLERROR("Cannot putback('%c') Input buffer bound reached (you may want to increase the unget_capacity)",c); 00207 00208 inbuf_p--; 00209 *inbuf_p = c; 00210 } 00211 00212 void PStreamBuf::unget() 00213 { 00214 if (last_get == PSTREAMBUF_NO_GET) 00215 PLERROR("In PStreamBuf::unget - You called unget() more than once or you did not call get() first"); 00216 if (last_get != EOF) 00217 putback((char) last_get); 00218 last_get = PSTREAMBUF_NO_GET; 00219 } 00220 00221 00222 void PStreamBuf::flush() 00223 { 00224 streamsize n = (streamsize)(outbuf_p-outbuf); 00225 if(n) 00226 { 00227 write_(outbuf, n); 00228 outbuf_p = outbuf; 00229 } 00230 } 00231 00232 00233 void PStreamBuf::write(const char* p, streamsize n) 00234 { 00235 #ifdef BOUNDCHECK 00236 if(!isWritable()) 00237 PLERROR("Called PStreamBuf::write on a buffer not marked as writable"); 00238 #endif 00239 if(outbuf_chunksize>0) // buffered 00240 { 00241 streamsize bufrem = (streamsize)(outbuf_end-outbuf_p); 00242 if(n<=bufrem) 00243 { 00244 memcpy(outbuf_p, p, n); 00245 outbuf_p += n; 00246 } 00247 else // n>bufrem 00248 { 00249 memcpy(outbuf_p, p, bufrem); 00250 outbuf_p += bufrem; 00251 flush(); 00252 p += bufrem; 00253 n -= bufrem; 00254 if(n>outbuf_chunksize) 00255 write_(p, n); 00256 else 00257 { 00258 memcpy(outbuf_p, p, n); 00259 outbuf_p += n; 00260 } 00261 } 00262 } 00263 else // unbuffered 00264 write_(p,n); 00265 } 00266 00267 bool PStreamBuf::good() const 00268 { 00269 if (is_readable) 00270 return !eof(); 00271 else 00272 return is_writable; 00273 } 00274 00275 00276 } // end of namespace PLearn 00277 00278 00279 /* 00280 Local Variables: 00281 mode:c++ 00282 c-basic-offset:4 00283 c-file-style:"stroustrup" 00284 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00285 indent-tabs-mode:nil 00286 fill-column:79 00287 End: 00288 */ 00289 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :