Stream.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2007 Werner Mayer <wmayer[at]users.sourceforge.net>     *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #ifndef BASE_STREAM_H
00025 #define BASE_STREAM_H
00026 
00027 
00028 #include <fstream>
00029 #include <ios>
00030 #include <iostream>
00031 #include <sstream>
00032 #include <string>
00033 #include <vector>
00034 
00035 class QByteArray;
00036 class QIODevice;
00037 class QBuffer;
00038 
00039 namespace Base {
00040 
00041 class BaseExport Stream
00042 {
00043 public:
00044     enum ByteOrder { BigEndian, LittleEndian };
00045     
00046     ByteOrder byteOrder() const;
00047     void setByteOrder(ByteOrder);
00048 
00049 protected:
00050     Stream();
00051     virtual ~Stream(); 
00052 
00053     bool _swap;
00054 };
00055 
00060 class BaseExport OutputStream : public Stream
00061 {
00062 public:
00063     OutputStream(std::ostream &rout);
00064     ~OutputStream();
00065 
00066     OutputStream& operator << (bool b);
00067     OutputStream& operator << (int8_t ch);
00068     OutputStream& operator << (uint8_t uch);
00069     OutputStream& operator << (int16_t s);
00070     OutputStream& operator << (uint16_t us);
00071     OutputStream& operator << (int32_t i);
00072     OutputStream& operator << (uint32_t ui);
00073     OutputStream& operator << (int64_t l);
00074     OutputStream& operator << (uint64_t ul);
00075     OutputStream& operator << (float f);
00076     OutputStream& operator << (double d);
00077 
00078 private:
00079     OutputStream (const OutputStream&);
00080     void operator = (const OutputStream&);
00081 
00082 private:
00083     std::ostream& _out;
00084 };
00085 
00090 class BaseExport InputStream : public Stream
00091 {
00092 public:
00093     InputStream(std::istream &rin);
00094     ~InputStream();
00095 
00096     InputStream& operator >> (bool& b);
00097     InputStream& operator >> (int8_t& ch);
00098     InputStream& operator >> (uint8_t& uch);
00099     InputStream& operator >> (int16_t& s);
00100     InputStream& operator >> (uint16_t& us);
00101     InputStream& operator >> (int32_t& i);
00102     InputStream& operator >> (uint32_t& ui);
00103     InputStream& operator >> (int64_t& l);
00104     InputStream& operator >> (uint64_t& ul);
00105     InputStream& operator >> (float& f);
00106     InputStream& operator >> (double& d);
00107 
00108     operator bool() const
00109     {
00110         // test if _Ipfx succeeded
00111         return !_in.eof();
00112     }
00113 
00114 private:
00115     InputStream (const InputStream&);
00116     void operator = (const InputStream&);
00117 
00118 private:
00119     std::istream& _in;
00120 };
00121 
00122 // ----------------------------------------------------------------------------
00123 
00129 class BaseExport ByteArrayOStreambuf : public std::streambuf
00130 {
00131 public:
00132     explicit ByteArrayOStreambuf(QByteArray& ba);
00133     ~ByteArrayOStreambuf();
00134 
00135 protected:
00136     virtual int_type overflow(std::streambuf::int_type v);
00137     virtual std::streamsize xsputn (const char* s, std::streamsize num);
00138     virtual pos_type seekoff(std::streambuf::off_type off,
00139         std::ios_base::seekdir way,
00140         std::ios_base::openmode which =
00141             std::ios::in | std::ios::out);
00142     virtual pos_type seekpos(std::streambuf::pos_type sp,
00143         std::ios_base::openmode which =
00144             std::ios::in | std::ios::out);
00145 
00146 private:
00147     QBuffer* _buffer;
00148 };
00149 
00155 class BaseExport ByteArrayIStreambuf : public std::streambuf
00156 {
00157 public:
00158     explicit ByteArrayIStreambuf(const QByteArray& buf);
00159     ~ByteArrayIStreambuf();
00160 
00161 protected:
00162     virtual int_type uflow();
00163     virtual int_type underflow();
00164     virtual int_type pbackfail(int_type ch);
00165     virtual std::streamsize showmanyc();
00166     virtual pos_type seekoff(std::streambuf::off_type off,
00167         std::ios_base::seekdir way,
00168         std::ios_base::openmode which = 
00169             std::ios::in | std::ios::out);
00170     virtual pos_type seekpos(std::streambuf::pos_type pos, 
00171         std::ios_base::openmode which =
00172             std::ios::in | std::ios::out);
00173 
00174 private:
00175     const QByteArray& _buffer;
00176     int _beg, _end, _cur;
00177 };
00178 
00184 class BaseExport IODeviceOStreambuf : public std::streambuf
00185 {
00186 public:
00187     IODeviceOStreambuf(QIODevice* dev);
00188     ~IODeviceOStreambuf();
00189 
00190 protected:
00191     virtual int_type overflow(std::streambuf::int_type v);
00192     virtual std::streamsize xsputn (const char* s, std::streamsize num);
00193     virtual pos_type seekoff(std::streambuf::off_type off,
00194         std::ios_base::seekdir way,
00195         std::ios_base::openmode which =
00196             std::ios::in | std::ios::out);
00197     virtual pos_type seekpos(std::streambuf::pos_type sp,
00198         std::ios_base::openmode which =
00199             std::ios::in | std::ios::out);
00200 protected:
00201     QIODevice* device;
00202 };
00203 
00209 class BaseExport IODeviceIStreambuf : public std::streambuf
00210 {
00211 public:
00212     IODeviceIStreambuf(QIODevice* dev);
00213     ~IODeviceIStreambuf();
00214 
00215 protected:
00216     virtual int_type underflow();
00217     virtual pos_type seekoff(std::streambuf::off_type off,
00218         std::ios_base::seekdir way,
00219         std::ios_base::openmode which =
00220             std::ios::in | std::ios::out);
00221     virtual pos_type seekpos(std::streambuf::pos_type sp,
00222         std::ios_base::openmode which =
00223             std::ios::in | std::ios::out);
00224 
00225 protected:
00226     QIODevice* device;
00227     /* data buffer:
00228      * - at most, pbSize characters in putback area plus
00229      * - at most, bufSize characters in ordinary read buffer
00230      */
00231     static const int pbSize = 4;        // size of putback area
00232     static const int bufSize = 1024;    // size of the data buffer
00233     char buffer[bufSize+pbSize];        // data buffer
00234 };
00235 
00236 class BaseExport Streambuf : public std::streambuf
00237 {
00238 public:
00239     explicit Streambuf(const std::string& data);
00240     ~Streambuf();
00241 
00242 protected:
00243     virtual int_type uflow();
00244     virtual int_type underflow();
00245     virtual int_type pbackfail(int_type ch);
00246     virtual std::streamsize showmanyc();
00247     virtual pos_type seekoff(std::streambuf::off_type off,
00248         std::ios_base::seekdir way,
00249         std::ios_base::openmode which = 
00250             std::ios::in | std::ios::out);
00251     virtual pos_type seekpos(std::streambuf::pos_type pos, 
00252         std::ios_base::openmode which =
00253             std::ios::in | std::ios::out);
00254 
00255 private:
00256     std::string::const_iterator _beg;
00257     std::string::const_iterator _end;
00258     std::string::const_iterator _cur;
00259 };
00260 
00261 // ----------------------------------------------------------------------------
00262 
00263 class FileInfo;
00264 
00271 class BaseExport ofstream : public std::ofstream
00272 {
00273 public:
00274     ofstream(const FileInfo& fi, ios_base::openmode mode =
00275                                  std::ios::out | std::ios::trunc);
00276     virtual ~ofstream();
00277 };
00278 
00285 class BaseExport ifstream : public std::ifstream
00286 {
00287 public:
00288     ifstream(const FileInfo& fi, ios_base::openmode mode = 
00289                                  std::ios::in);
00290     virtual ~ifstream();
00291 };
00292 
00293 } // namespace Base
00294 
00295 #endif // BASE_STREAM_H

Generated on Wed Nov 23 19:00:41 2011 for FreeCAD by  doxygen 1.6.1