00001 // -*- C++ -*- 00002 00003 // PLearn (A C++ Machine Learning Library) 00004 // Copyright (C) 2002 Xavier Saint-Mleux <saintmlx@iro.umontreal.ca> 00005 // 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 00043 #ifndef pl_streambuf_INC 00044 #define pl_streambuf_INC 00045 00046 //#include <iosfwd> 00047 #include <iostream> 00048 //#include "plerror.h" 00049 #include <plearn/base/PP.h> 00050 00051 namespace PLearn { 00052 00053 using namespace std; 00054 00055 class ioassignstream; //fwd decl. 00056 class pl_streambuf; // fwd decl. 00057 class pl_streammarker; // fwd decl. 00058 00063 class pl_streambuf : public streambuf, public PPointable 00064 { 00065 #if __GNUC__ < 3 && !defined(WIN32) 00066 typedef int int_type; 00067 typedef char char_type; 00068 #endif 00069 00070 static const int_type eof = EOF; 00071 00072 friend class pl_streammarker; 00073 friend class ioassignstream; 00074 00075 private: 00076 typedef streambuf inherited; 00077 00078 protected: 00079 00080 streambuf& original_buf; //<! original buffer that needs marking 00081 char* inbuf; //<! buffer for marking: keeps all chars since first active mark 00082 int inbuflen; //<! current length of inbuf 00083 static const int pback_size= 4; //<! length of default put back area 00084 static const int min_buf_size= 16; //<! minimum buffer size 00085 00086 pl_streammarker* first_marker; //<! ptr. to the head of a linked list of active markers 00087 00088 virtual int_type underflow(); //<! underflow redefined 00089 virtual int_type uflow(); //<! uflow redefined 00090 00091 00092 virtual streamsize xsgetn(char* s, streamsize n); 00093 virtual streamsize xsputn(const char* s, streamsize n); 00094 00095 00096 virtual int_type overflow(int_type meta = pl_streambuf::eof); //<! overflow redefined 00097 virtual int_type sync(); //<! sync redefined 00098 virtual int_type pbackfail(int_type c= eof); //<! pbackfail redefined 00099 00100 //inline int curpos() const { return gptr() - inbuf; } //<! return current position within the input buffer 00101 // norman: explicit cast: 00102 inline int curpos() const { return (int)(gptr() - inbuf); } //<! return current position within the input buffer 00103 00104 public: 00105 00107 pl_streambuf(streambuf& _original_buf, int_type _inbuflen= 0); 00108 virtual ~pl_streambuf(); 00109 00110 void seekmark(const pl_streammarker& mark); //<! reposition input to the marked position 00111 00112 }; 00113 00117 class pl_streammarker 00118 { 00119 friend class pl_streambuf; 00120 00121 protected: 00122 00123 pl_streambuf* buf; //<! marked buffer 00124 pl_streammarker* next_marker; //<! next marker for the marked buffer (linked list) 00125 int pos; //<! marked position 00126 00127 public: 00128 00129 pl_streammarker(pl_streambuf* _buf); //<! ctor. to mark a pl_streambuf 00130 pl_streammarker(streambuf* _buf); //<! ctor. to mark an STL streambuf (...only if it's really a pl_streambuf) 00131 00132 virtual ~pl_streammarker(); 00133 00134 }; 00135 00136 00137 } // namespace PLearn 00138 00139 #endif //ndef pl_streambuf_INC