PLearn 0.1
pl_fdstream.cc
Go to the documentation of this file.
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 
00037 
00038 #include <iostream>
00039 
00040 // norman: set win32 functions
00041 #ifdef WIN32
00042 #include <io.h>
00043 #define read _read
00044 #define write _write
00045 #else
00046 #include <unistd.h>
00047 #endif
00048 #include "pl_fdstream.h"
00049 
00050 namespace PLearn {
00051 using namespace std;
00052 
00053 
00054 pl_fdstreambuf::pl_fdstreambuf(int fd_, int inbufsize) 
00055     :fd(fd_), inbuf(0), inbuf_capacity(0)
00056 {
00057     if(inbufsize > 0)
00058         reserveInputBuffer(inbufsize);
00059 }
00060 
00061 
00062 pl_fdstreambuf::~pl_fdstreambuf()
00063 {
00064     if(inbuf)
00065         delete[] inbuf;
00066 }
00067 
00068 streambuf* pl_fdstreambuf::setbuf(char* p, int len)
00069 {
00070     if(p && len>0)
00071         setp(p, p+len-1); // -1 because we want space to put the extra character passed to overflow()
00072     else
00073         setp(0,0);
00074     return this;
00075 }
00076 
00078 streamsize  pl_fdstreambuf::showmanyc()
00079 { return -1; }
00080 
00081 int pl_fdstreambuf::underflow()
00082 {
00083     int msglength= int(read(fd, inbuf, inbuf_capacity));
00084     if(msglength < 1)
00085         return EOF;
00086     setg(inbuf, inbuf, inbuf+msglength);
00087     return *inbuf;
00088 }
00089 
00090 int pl_fdstreambuf::overflow(int c)
00091 {
00092     if(c!=EOF)
00093     {
00094         if(pbase()) // buffered mode
00095         {
00096             streamsize n = streamsize(pptr() - pbase());
00097             *pptr()= char(c); // put it in extra space
00098             if(write(fd, pbase(), n) < 0)
00099                 return EOF;
00100             pbump(-n);
00101         }
00102         else // unbuffered mode
00103         {
00104             char tinybuf[1];
00105             tinybuf[0] = c;
00106             //cout << "writing " << (char)c << endl;
00107             if(write(fd, tinybuf, 1) < 0)
00108                 return EOF;
00109         }
00110     }
00111     else // extra char is EOF, we ignore it
00112     {
00113         if(pbase()) // buffered mode
00114         {
00115             streamsize n = streamsize(pptr() - pbase());
00116             if(write(fd, pbase(), n) < 0)
00117                 return EOF;
00118             pbump(-n);
00119         }
00120     }
00121     return c;
00122 }
00123 
00124 int pl_fdstreambuf::sync()
00125 {
00126     streamsize n = streamsize(pptr() - pbase());
00127     if(n>0)
00128     {
00129         if(write(fd, pbase(), n) < 0)
00130             return EOF;
00131         pbump(-n);
00132     }
00133     return 0;
00134 }
00135 
00136 // Smart implementation of xsputn:
00137 // If n is greater than the buffer size we send the message directly
00138 // Thus, we don't waste time copying stuff into the buffer unnecessarily
00139 // (ex: whhen sending a long vector for ex.).
00140 streamsize pl_fdstreambuf::xsputn(const char* s, streamsize n)
00141 {      
00142     if(n>epptr()-pptr())  // n greater than buffer size! 
00143     {
00144         // Let's not waste time copying stuff into the buffer, send it directly
00145         sync(); // first make sure we send what's left in the buffer
00146         if(write(fd, (char *)s, n) < 0)
00147             return 0;
00148     }
00149     else  // call the default method
00150         streambuf::xsputn(s,n);
00151 
00152     return n;
00153 }
00154 
00155 //use default implementation
00156 streamsize pl_fdstreambuf::xsgetn(char* s, streamsize n)
00157 { return streambuf::xsgetn(s, n); }
00158 
00159 void pl_fdstream::init(int fd, int inbufsize, int outbufsize)
00160 {
00161     rdbuf(new pl_fdstreambuf(fd, inbufsize));
00162     if(outbufsize<=1)
00163 #if __GNUC__ < 3 && !defined(WIN32)
00164         rdbuf()->setbuf(0,0);
00165 #else
00166     rdbuf()->pubsetbuf(0,0);
00167 #endif
00168     else
00169     {
00170         outbuffer = new char[outbufsize];
00171 #if __GNUC__ < 3 && !defined(WIN32)
00172         rdbuf()->setbuf(outbuffer,outbufsize);
00173 #else
00174         rdbuf()->pubsetbuf(outbuffer,outbufsize);
00175 #endif
00176     }
00177 }
00178 
00179 void pl_fdstream::attach(int fd)
00180 {
00181     rdbuf(new pl_fdstreambuf(fd, pl_dftbuflen));
00182     outbuffer= new char[pl_dftbuflen];
00183 #if __GNUC__ < 3 && !defined(WIN32)
00184     rdbuf()->setbuf(outbuffer, pl_dftbuflen);
00185 #else
00186     rdbuf()->pubsetbuf(outbuffer, pl_dftbuflen);
00187 #endif
00188 }
00189 
00190 
00191 pl_fdstream::~pl_fdstream()
00192 {
00193     flush();
00194     delete rdbuf(0); // delete pl_fdstreambuf
00195     if(outbuffer)
00196         delete[] outbuffer;
00197 }
00198 
00199 } // end of namespace PLearn
00200 
00201 
00202 /*
00203   Local Variables:
00204   mode:c++
00205   c-basic-offset:4
00206   c-file-style:"stroustrup"
00207   c-file-offsets:((innamespace . 0)(inline-open . 0))
00208   indent-tabs-mode:nil
00209   fill-column:79
00210   End:
00211 */
00212 // 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