00001 // -*- C++ -*- 00002 00003 // PLearn (A C++ Machine Learning Library) 00004 // Copyright (C) 1998 Pascal Vincent 00005 // Copyright (C) 1999-2002 Pascal Vincent, Yoshua Bengio and University of Montreal 00006 // Copyright (C) 2002 Xavier Saint-Mleux <saintmlx@iro.umontreal.ca> 00007 // 00008 00009 // Redistribution and use in source and binary forms, with or without 00010 // modification, are permitted provided that the following conditions are met: 00011 // 00012 // 1. Redistributions of source code must retain the above copyright 00013 // notice, this list of conditions and the following disclaimer. 00014 // 00015 // 2. Redistributions in binary form must reproduce the above copyright 00016 // notice, this list of conditions and the following disclaimer in the 00017 // documentation and/or other materials provided with the distribution. 00018 // 00019 // 3. The name of the authors may not be used to endorse or promote 00020 // products derived from this software without specific prior written 00021 // permission. 00022 // 00023 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00024 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00025 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00026 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00027 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00028 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00029 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00030 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00031 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00032 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00033 // 00034 // This file is part of the PLearn library. For more information on the PLearn 00035 // library, go to the PLearn Web site at www.plearn.org 00036 00044 #ifndef pl_fdstream_INC 00045 #define pl_fdstream_INC 00046 00047 #include <iostream> 00048 #include <plearn/base/PP.h> 00049 00050 namespace PLearn { 00051 using namespace std; 00052 00053 const int pl_dftbuflen= 4096; 00054 00056 class pl_fdstreambuf : public streambuf, public PPointable 00057 { 00058 protected: 00059 int fd; //<! the file descriptor 00060 char* inbuf; //<! input buffer 00061 int inbuf_capacity; //<! length of inbuf 00062 00063 protected: 00064 00066 inline void reserveInputBuffer(int buflen) 00067 { 00068 if(buflen>inbuf_capacity) 00069 { 00070 if(inbuf) 00071 delete[] inbuf; 00072 inbuf = new char[buflen]; 00073 inbuf_capacity = buflen; 00074 } 00075 } 00076 00077 virtual int overflow(int c=EOF); 00078 virtual int underflow(); 00079 virtual streamsize showmanyc(); 00080 virtual streamsize xsputn(const char* s, streamsize n); 00081 virtual streamsize xsgetn(char* s, streamsize n); 00082 virtual streambuf* setbuf(char* p, int len); 00083 virtual int sync(); 00084 00085 public: 00086 pl_fdstreambuf(int fd_, int inbufsize); 00087 virtual ~pl_fdstreambuf(); 00088 00089 }; 00090 00091 class pl_fdstream: public iostream 00092 { 00093 protected: 00094 char* outbuffer; 00095 00096 public: 00097 00098 pl_fdstream() 00099 :iostream(0), outbuffer(0) 00100 {} 00101 00102 pl_fdstream(int fd, int inbufsize= pl_dftbuflen, int outbufsize= pl_dftbuflen) 00103 :iostream(0), outbuffer(0) 00104 { init(fd, inbufsize, outbufsize); } 00105 00106 void init(int fd, int inbufsize= pl_dftbuflen, int outbufsize= pl_dftbuflen); 00107 00109 void attach(int fd); 00110 00111 ~pl_fdstream(); 00112 }; 00113 00114 } // end of namespace PLearn 00115 00116 #endif //ndef pl_fdstream_INC 00117