Webmaster  |  Imprint 
C++ Server Pages
Main  |  License  |  Documentation  |  Download 

/home/tommi/cxxtools/include/cxxtools/iostream.h

00001 /*
00002  * Copyright (C) 2005-2008 Marc Boris Duerner
00003  * 
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2.1 of the License, or (at your option) any later version.
00008  * 
00009  * As a special exception, you may use this file as part of a free
00010  * software library without restriction. Specifically, if other files
00011  * instantiate templates or use macros or inline functions from this
00012  * file, or you compile this file and link it with other files to
00013  * produce an executable, this file does not by itself cause the
00014  * resulting executable to be covered by the GNU General Public
00015  * License. This exception does not however invalidate any other
00016  * reasons why the executable file might be covered by the GNU Library
00017  * General Public License.
00018  * 
00019  * This library is distributed in the hope that it will be useful,
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00022  * Lesser General Public License for more details.
00023  * 
00024  * You should have received a copy of the GNU Lesser General Public
00025  * License along with this library; if not, write to the Free Software
00026  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00027  */
00028 #ifndef cxxtools_System_IOStream_h
00029 #define cxxtools_System_IOStream_h
00030 
00031 #include <cxxtools/api.h>
00032 #include <cxxtools/streambuffer.h>
00033 #include <iostream>
00034 #include <algorithm>
00035 
00036 namespace cxxtools {
00037 
00039 template <typename CharT>
00040 class BasicIStream : public std::basic_istream<CharT>
00041 {
00042     public:
00043         explicit BasicIStream(BasicStreamBuffer<CharT>* buffer = 0)
00044         : std::basic_istream<CharT>( buffer ),
00045           _buffer(buffer)
00046         { }
00047 
00048         ~BasicIStream()
00049         { }
00050 
00052         BasicStreamBuffer<CharT>* attachedBuffer()
00053         { return _buffer; }
00054 
00055         BasicStreamBuffer<CharT>* attachBuffer(BasicStreamBuffer<CharT>*  buffer)
00056         {
00057             BasicStreamBuffer<CharT>* tmp = _buffer;
00058             _buffer = buffer;
00059             rdbuf(buffer);
00060             return tmp;
00061         }
00062 
00064 
00069         std::streamsize peeksome(CharT* buffer, std::streamsize n)
00070         {
00071             if(this->rdbuf() == _buffer)
00072                 return _buffer->speekn(buffer, n);
00073 
00074             if(n > 0)
00075             {
00076                 buffer[0] = this->peek();
00077                 return 1;
00078             }
00079 
00080             return 0;
00081         }
00082 
00083     private:
00084         BasicStreamBuffer<CharT>* _buffer;
00085 };
00086 
00087 
00089 template <typename CharT>
00090 class BasicOStream : public std::basic_ostream<CharT>
00091 {
00092     public:
00093         explicit BasicOStream(BasicStreamBuffer<CharT>* buffer = 0)
00094         : std::basic_ostream<CharT>( buffer ),
00095           _buffer(buffer)
00096         { }
00097 
00098         ~BasicOStream()
00099         {}
00100 
00102         BasicStreamBuffer<CharT>* attachedBuffer()
00103         { return _buffer; }
00104 
00105         BasicStreamBuffer<CharT>* attachBuffer(BasicStreamBuffer<CharT>*  buffer)
00106         {
00107             BasicStreamBuffer<CharT>* tmp = _buffer;
00108             _buffer = buffer;
00109             rdbuf(buffer);
00110             return tmp;
00111         }
00112 
00113         std::streamsize writesome(CharT* buffer, std::streamsize n)
00114         {
00115             std::basic_streambuf<CharT>* current = std::basic_ios<CharT>::rdbuf();
00116             if(current != _buffer)
00117                 return 0;
00118 
00119             std::streamsize avail = _buffer->out_avail();
00120             if(avail == 0)
00121             {
00122                 return 0;
00123             }
00124 
00125             n = std::min(avail, n);
00126             return _buffer->sputn(buffer, n);
00127         }
00128 
00129     private:
00130         BasicStreamBuffer<CharT>* _buffer;
00131 };
00132 
00133 
00135 template <typename CharT>
00136 class BasicIOStream : public std::basic_iostream<CharT>
00137 {
00138     public:
00139         explicit BasicIOStream(BasicStreamBuffer<CharT>* buffer = 0)
00140         : std::basic_iostream<CharT>( buffer ),
00141           _buffer(buffer)
00142         { }
00143 
00144         ~BasicIOStream()
00145         { }
00146 
00148         BasicStreamBuffer<CharT>* attachedBuffer()
00149         { return _buffer; }
00150 
00151         BasicStreamBuffer<CharT>* attachBuffer(BasicStreamBuffer<CharT>*  buffer)
00152         {
00153             BasicStreamBuffer<CharT>* tmp = _buffer;
00154             _buffer = buffer;
00155             rdbuf(buffer);
00156             return tmp;
00157         }
00158 
00160 
00165         std::streamsize peeksome(CharT* buffer, std::streamsize n)
00166         {
00167             if(this->rdbuf() == _buffer)
00168                 return _buffer->speekn(buffer, n);
00169 
00170             if(n > 0)
00171             {
00172                 buffer[0] = this->peek();
00173                 return 1;
00174             }
00175 
00176             return 0;
00177         }
00178 
00179         std::streamsize writesome(CharT* buffer, std::streamsize n)
00180         {
00181             std::basic_streambuf<CharT>* current = std::basic_ios<CharT>::rdbuf();
00182             if(current != _buffer)
00183                 return 0;
00184 
00185             std::streamsize avail = _buffer->out_avail();
00186             if(avail == 0)
00187             {
00188                 return 0;
00189             }
00190 
00191             n = std::min(avail, n);
00192             return _buffer->sputn(buffer, n);
00193         }
00194 
00195     private:
00196         BasicStreamBuffer<CharT>* _buffer;
00197 };
00198 
00199 
00200 class CXXTOOLS_API IStream : public BasicIStream<char>
00201 {
00202     public:
00203         explicit IStream(size_t bufferSize = 8192, bool extend = false);
00204 
00205         ~IStream();
00206 
00207         explicit IStream(IODevice& device, size_t bufferSize = 8192, bool extend = false);
00208 
00209         StreamBuffer& buffer();
00210 
00211         IODevice* attachDevice(IODevice& device);
00212 
00213         IODevice* attachedDevice();
00214 
00215     private:
00216         StreamBuffer _buffer;
00217 };
00218 
00219 
00220 class CXXTOOLS_API OStream : public BasicOStream<char>
00221 {
00222     public:
00223         explicit OStream(size_t bufferSize = 8192, bool extend = false);
00224 
00225         explicit OStream(IODevice& device, size_t bufferSize = 8192, bool extend = false);
00226 
00227         ~OStream();
00228 
00229         StreamBuffer& buffer();
00230 
00231         IODevice* attachDevice(IODevice& device);
00232 
00233         IODevice* attachedDevice();
00234 
00235     private:
00236         StreamBuffer _buffer;
00237 };
00238 
00239 
00240 class CXXTOOLS_API IOStream : public BasicIOStream<char>
00241 {
00242     public:
00243         explicit IOStream(size_t bufferSize = 8192, bool extend = false);
00244 
00245         explicit IOStream(IODevice& device, size_t bufferSize = 8192, bool extend = false);
00246 
00247         ~IOStream();
00248 
00249         StreamBuffer& buffer();
00250 
00251         IODevice* attachDevice(IODevice& device);
00252 
00253         IODevice* attachedDevice();
00254 
00255     private:
00256         StreamBuffer _buffer;
00257 };
00258 
00259 } // !namespace cxxtools
00260 
00261 #endif
00262 
Copyright © 2008 The Tntnet Development Team
Tntnet 1.6