00001 #ifndef __CFileStream__ 00002 #define __CFileStream__ 00003 00004 // =========================================================================== 00005 00006 #include "../Basics/CSystemString.hpp" 00007 using Exponent::Basics::CSystemString; 00008 00009 // =========================================================================== 00010 00011 namespace Exponent 00012 { 00013 namespace IO 00014 { 00035 class CFileStream : public CCountedObject 00036 { 00038 EXPONENT_CLASS_DECLARATION; 00041 // =========================================================================== 00042 00043 public: 00044 00045 // =========================================================================== 00046 00051 enum EStreamMode 00052 { 00053 e_input = 0, 00054 e_output, 00055 }; 00056 00057 // =========================================================================== 00058 00062 CFileStream(); 00063 00070 CFileStream(const CSystemString &filename, const EStreamMode status, const bool fileIsLittleEndian = true); 00071 00075 virtual ~CFileStream(); 00076 00077 // =========================================================================== 00078 00086 bool openStream(const CSystemString &path, const EStreamMode mode, const bool fileIsLittleEndian = true); 00087 00091 void closeStream(); 00092 00093 // =========================================================================== 00094 00099 bool isStreamOpen() const { return m_streamIsOpen; } 00100 00105 bool validForOutput(); 00106 00111 bool validForInput(); 00112 00117 void setStreamEndianess(const bool fileIsLittleEndian = true); 00118 00119 // =========================================================================== 00120 00126 bool moveToStreamPosition(const long position); 00127 00133 long getStreamPosition(); 00134 00140 bool advanceStream(const long amount); 00141 00145 bool moveToStreamStart(); 00146 00151 long getStreamSize(); 00152 00153 // =========================================================================== 00154 00161 bool writeDataToStream(const void *data, const size_t numberOfBytes); 00162 00169 bool readDataFromStream(void *data, const size_t numberOfBytes); 00170 00177 bool readShortsFromStream(short *data, const unsigned long numberOfElements); 00178 00179 // =========================================================================== 00180 00186 CFileStream &operator << (const char &byte); 00187 00193 CFileStream &operator << (const unsigned char &byte); 00194 00200 CFileStream &operator << (const bool &byte); 00201 00207 CFileStream &operator << (const short &byte); 00208 00214 CFileStream &operator << (const unsigned short &byte); 00215 00221 CFileStream &operator << (const long &value); 00222 00228 CFileStream &operator << (const unsigned long &value); 00229 00235 CFileStream &operator << (const float &value); 00236 00242 CFileStream &operator << (const double &value); 00243 00244 // =========================================================================== 00245 00251 CFileStream &operator >> (char &byte); 00252 00258 CFileStream &operator >> (unsigned char &byte); 00259 00265 CFileStream &operator >> (bool &byte); 00266 00272 CFileStream &operator >> (short &byte); 00273 00279 CFileStream &operator >> (unsigned short &byte); 00280 00286 CFileStream &operator >> (long &value); 00287 00293 CFileStream &operator >> (unsigned long &value); 00294 00300 CFileStream &operator >> (float &value); 00301 00307 CFileStream &operator >> (double &value); 00308 00309 // =========================================================================== 00310 00315 bool hasErrorOccurred() const { return m_errorState; } 00316 00321 bool hasReachEndOfFile() const; 00322 00323 // =========================================================================== 00324 00325 protected: 00326 00327 // =========================================================================== 00328 00333 void write16Bits(const void *value); 00334 00339 void write32Bits(const void *value); 00340 00345 void write64Bits(const void *value); 00346 00347 // =========================================================================== 00348 00353 void read16Bits(void *value); 00354 00359 void read32Bits(void *value); 00360 00365 void read64Bits(void *value); 00366 00367 // =========================================================================== 00368 00373 bool swapBits() const; 00374 00375 // =========================================================================== 00376 00377 const static unsigned int CFILESTREAM_8BIT_SIZE = 1; 00378 const static unsigned int CFILESTREAM_16BIT_SIZE = 2; 00379 const static unsigned int CFILESTREAM_32BIT_SIZE = 4; 00380 const static unsigned int CFILESTREAM_64BIT_SIZE = 8; 00382 // =========================================================================== 00383 00384 FILE *m_file; 00385 EStreamMode m_streamStatus; 00386 bool m_streamIsOpen; 00387 bool m_errorState; 00388 bool m_fileIsLittleEndian; 00389 }; 00390 } 00391 } 00392 #endif // End of CFileStream.hpp