Stream.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
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
00228
00229
00230
00231 static const int pbSize = 4;
00232 static const int bufSize = 1024;
00233 char buffer[bufSize+pbSize];
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 }
00294
00295 #endif // BASE_STREAM_H