00001 // -*- c++ -*- 00002 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 00003 00004 // O S C L _ B I N _ S T R E A M 00005 00006 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 00007 00021 /* 00022 ** File: oscl_bin_stream.h 00023 ** 00024 ** Description: 00025 ** This module defines a set of binary stream classes to provide portability of code accross 00026 ** different platforms and compilers. Output and input stream classes are defined both for 00027 ** little endian and big endian byte orders. These classes should be used when sharing 00028 ** information across platforms (e.g. sending messages) instead of C structures. The classes 00029 ** will read/write in the correct byte order regardless of the platform you are running on. 00030 ** The classes are: 00031 ** OsclBinIStreamBigEndian - Input stream for big endian byte order. 00032 ** OsclBinIStreamLittleEndian - Input stream for little endian byte order. 00033 ** OsclBinOStreamBigEndian - Output stream for big endian byte order. 00034 ** OsclBinIStreamLittleEndian - Input stream for little endian byte order. 00035 ** 00036 ** The preprocessor defined constants BYTE_ORDER_BIG_ENDIAN, BYTE_ORDER_LITTLE_ENDIAN are defined 00037 ** in oscl_base.h and tell this module the byte order used by the native platform. This is used 00038 ** to decide if we can do a straight copy or we need to manipulate the field one byte at a time. 00039 ** The preprocessor defined constant INTEGERS_BYTE_ALIGNED tells us if the platform supports 00040 ** accessing integers at any address. 00041 */ 00042 #ifndef OSCL_BIN_STREAM_H_INCLUDED 00043 #define OSCL_BIN_STREAM_H_INCLUDED 00044 00045 /* 00046 ** Includes 00047 */ 00048 #ifndef OSCL_BASE_H_INCLUDED 00049 #include "oscl_base.h" 00050 #endif 00051 00052 00053 00054 /* 00055 ** Classes 00056 */ 00057 00058 class OsclBinStream 00059 { 00060 public: 00061 OsclBinStream() 00062 : state(GOOD_STATE), 00063 pBasePosition(0), 00064 pPosition(0) 00065 { 00066 } 00067 00069 00072 bool good(); 00073 00075 00078 bool eof(); 00079 00081 00084 bool fail(); 00085 00086 00088 00092 void Attach(void * buffer, uint32 l_length); 00093 00095 00100 void Attach(const uint32 numFragments, const OsclMemoryFragment * fragPtr); 00101 00102 00104 00108 uint32 tellg(); 00109 00111 00114 void Seek(uint32 absPosition); 00115 00117 00120 uint32 PositionInBlock(); 00121 00122 00124 00127 void seekFromCurrentPosition(int32 offset); 00128 00129 00130 00131 protected: 00132 00133 /* 00134 ** Description: 00135 ** Reserves space for storing/reading. If no space is available it sets the stream state 00136 ** to fail and returns false. If the end of the stream has been reached, it sets the state 00137 ** to end of stream. 00138 ** 00139 ** Returns: true if space was successfully reserved. 00140 ** Side effects: None. 00141 */ 00142 bool ReserveSpace(uint32 size); 00143 00144 bool HaveRoomInCurrentBlock(uint32 size); 00145 00146 typedef enum 00147 { 00148 GOOD_STATE, 00149 EOF_STATE, 00150 FAIL_STATE 00151 } state_t; 00152 00153 state_t state; 00154 00155 /* Position of the beginning of the data buffer */ 00156 uint8 * pBasePosition; 00157 00158 /* Current data buffer position */ 00159 uint8 * pPosition; 00160 00161 /* length of data buffer */ 00162 uint32 length; 00163 00164 const OsclMemoryFragment * nextFragPtr; 00165 int fragsLeft; 00166 00167 const OsclMemoryFragment * firstFragPtr; 00168 int numFrags; 00169 OsclMemoryFragment specialFragBuffer; 00170 00171 }; 00172 00173 /* 00174 ** Class OsclBinIStream 00175 ** This class implements the basic stream functions for an input stream. 00176 */ 00177 class OsclBinIStream : public OsclBinStream 00178 { 00179 private: 00180 public: 00181 /* 00182 ** Description: 00183 ** Constructor. 00184 ** 00185 ** Returns: None. 00186 ** Side effects: None. 00187 */ 00188 OsclBinIStream() 00189 { 00190 } 00191 00192 /* 00193 ** Description: 00194 ** Destructor. 00195 ** 00196 ** Returns: None. 00197 ** Side effects: None. 00198 */ 00199 ~OsclBinIStream() 00200 { 00201 } 00202 00204 00207 uint8 Read_uint8(); 00208 00210 00214 OsclBinIStream & get( 00215 int8 * data, 00216 int32 size 00217 ); 00218 00219 protected: 00220 00221 00222 }; 00223 00224 /* 00225 ** Class OsclBinIStreamLittleEndian 00226 ** This class implements a binary input stream using little endian byte ordering 00227 */ 00228 class OsclBinIStreamLittleEndian : public OsclBinIStream 00229 { 00230 public: 00231 OsclBinIStreamLittleEndian() 00232 { 00233 } 00234 00236 OsclBinIStreamLittleEndian & operator>>(int8 & data); 00237 00239 OsclBinIStreamLittleEndian & operator>>(uint8 & data); 00240 00242 OsclBinIStreamLittleEndian & operator>>(int16 & data); 00243 00245 OsclBinIStreamLittleEndian & operator>>(uint16 & data); 00246 00248 OsclBinIStreamLittleEndian & operator>>(int32 & data); 00249 00251 OsclBinIStreamLittleEndian & operator>>(uint32 & data); 00252 00253 protected: 00254 00255 /* 00256 ** Description: 00257 ** Reads an unsigned short from the stream. 00258 ** 00259 ** Returns: Unsigned short read from the stream. 00260 ** Side effects: None. 00261 */ 00262 uint16 Read_uint16(); 00263 /* 00264 ** Description: 00265 ** Reads an unsigned long from the stream. 00266 ** 00267 ** Returns: Unsigned long read from the stream. 00268 ** Side effects: None. 00269 */ 00270 uint32 Read_uint32(); 00271 }; 00272 00273 00274 /* 00275 ** Class OsclBinIStreamBigEndian 00276 ** This class implements a binary input stream using big endian byte ordering 00277 */ 00278 class OsclBinIStreamBigEndian : public OsclBinIStream 00279 { 00280 public: 00281 OsclBinIStreamBigEndian() 00282 { 00283 } 00284 00285 void Read(int8 & data); 00286 00287 void Read(uint8 & data); 00288 00289 void Read(int16 & data); 00290 00291 void Read(uint16 & data); 00292 00293 void Read(int32 & data); 00294 00295 void Read(uint32 & data); 00296 00298 OsclBinIStreamBigEndian & operator>>(int8 & data); 00299 00301 OsclBinIStream & operator>>(uint8 & data); 00302 00304 OsclBinIStreamBigEndian & operator>>(int16 & data); 00305 00307 OsclBinIStreamBigEndian & operator>>(uint16 & data); 00308 00310 OsclBinIStreamBigEndian & operator>>(int32 & data); 00311 00313 OsclBinIStreamBigEndian & operator>>(uint32 & data); 00314 00316 00319 uint16 Read_uint16(); 00320 00322 00325 uint32 Read_uint32(); 00326 protected: 00327 00328 }; 00329 00330 00332 class OsclBinOStream : public OsclBinStream 00333 { 00334 public: 00335 OsclBinOStream() 00336 { 00337 } 00338 00339 virtual ~OsclBinOStream() 00340 { 00341 } 00342 00344 OsclBinOStream & write( 00345 const int8 * data, /* data to store */ 00346 int32 size /* length of data to store */ 00347 ); 00348 00349 protected: 00350 }; 00351 00353 class OsclBinOStreamLittleEndian : public OsclBinOStream 00354 { 00355 public: 00356 OsclBinOStreamLittleEndian() 00357 { 00358 } 00359 00361 OsclBinOStreamLittleEndian & operator<<(const int8 & data); 00362 00364 OsclBinOStreamLittleEndian & operator<<(const uint8 & data); 00365 00367 OsclBinOStreamLittleEndian & operator<<(const int16 & data); 00368 00370 OsclBinOStreamLittleEndian & operator<<(const uint16 & data); 00371 00373 OsclBinOStreamLittleEndian & operator<<(const int32 & data); 00374 00376 OsclBinOStreamLittleEndian & operator<<(const uint32 & data); 00377 00378 protected: 00379 00381 void WriteUnsignedShort(const uint16 data); 00382 00384 void WriteUnsignedLong(const uint32 data); 00385 00386 }; 00387 00388 00390 class OsclBinOStreamBigEndian : public OsclBinOStream 00391 { 00392 public: 00393 OsclBinOStreamBigEndian() 00394 { 00395 } 00396 00398 OsclBinOStreamBigEndian & operator<<(const int8 & data); 00399 00401 OsclBinOStreamBigEndian & operator<<(const uint8 & data); 00402 00404 OsclBinOStreamBigEndian & operator<<(const int16 & data); 00405 00407 OsclBinOStreamBigEndian & operator<<(const uint16 & data); 00408 00410 OsclBinOStreamBigEndian & operator<<(const int32 & data); 00411 00413 OsclBinOStreamBigEndian & operator<<(const uint32 & data); 00414 00415 protected: 00416 00417 /* 00418 ** Description: 00419 ** Writes 'data' (unsigned short) to the stream. 00420 ** 00421 ** Returns: None. 00422 ** Side effects: None. 00423 */ 00424 void WriteUnsignedShort(const uint16 data); 00425 00426 /* 00427 ** Description: 00428 ** Writes 'data' (unsigned long) to the stream. 00429 ** 00430 ** Returns: None. 00431 ** Side effects: None. 00432 */ 00433 void WriteUnsignedLong(const uint32 data); 00434 }; 00435 00436 #if (!OSCL_DISABLE_INLINES) 00437 #include "oscl_bin_stream.inl" 00438 #endif 00439 00440 #endif 00441