00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef PSFILEBUFFER_PXFILEBUF_H
00032 #define PSFILEBUFFER_PXFILEBUF_H
00033
00038 #if !PX_DOXYGEN
00039 namespace physx
00040 {
00041
00042 namespace general_PxIOStream2
00043 {
00044 #endif
00045
00046 PX_PUSH_PACK_DEFAULT
00047
00058 class PxFileBuf
00059 {
00060 public:
00061
00062 enum EndianMode
00063 {
00064 ENDIAN_NONE = 0,
00065 ENDIAN_BIG = 1,
00066 ENDIAN_LITTLE = 2
00067 };
00068
00069 PxFileBuf(EndianMode mode=ENDIAN_LITTLE)
00070 {
00071 setEndianMode(mode);
00072 }
00073
00074 virtual ~PxFileBuf(void)
00075 {
00076
00077 }
00078
00084 static const uint32_t STREAM_SEEK_END=0xFFFFFFFF;
00085
00086 enum OpenMode
00087 {
00088 OPEN_FILE_NOT_FOUND,
00089 OPEN_READ_ONLY,
00090 OPEN_WRITE_ONLY,
00091 OPEN_READ_WRITE_NEW,
00092 OPEN_READ_WRITE_EXISTING
00093 };
00094
00095 virtual OpenMode getOpenMode(void) const = 0;
00096
00097 bool isOpen(void) const
00098 {
00099 return getOpenMode()!=OPEN_FILE_NOT_FOUND;
00100 }
00101
00102 enum SeekType
00103 {
00104 SEEKABLE_NO = 0,
00105 SEEKABLE_READ = 0x1,
00106 SEEKABLE_WRITE = 0x2,
00107 SEEKABLE_READWRITE = 0x3
00108 };
00109
00110 virtual SeekType isSeekable(void) const = 0;
00111
00112 void setEndianMode(EndianMode e)
00113 {
00114 mEndianMode = e;
00115 if ( (e==ENDIAN_BIG && !isBigEndian() ) ||
00116 (e==ENDIAN_LITTLE && isBigEndian() ) )
00117 {
00118 mEndianSwap = true;
00119 }
00120 else
00121 {
00122 mEndianSwap = false;
00123 }
00124 }
00125
00126 EndianMode getEndianMode(void) const
00127 {
00128 return mEndianMode;
00129 }
00130
00131 virtual uint32_t getFileLength(void) const = 0;
00132
00139 virtual uint32_t seekRead(uint32_t loc) = 0;
00140
00147 virtual uint32_t seekWrite(uint32_t loc) = 0;
00148
00157 virtual uint32_t read(void *mem,uint32_t len) = 0;
00158
00159
00168 virtual uint32_t peek(void *mem,uint32_t len) = 0;
00169
00178 virtual uint32_t write(const void *mem,uint32_t len) = 0;
00179
00185 virtual uint32_t tellRead(void) const = 0;
00186
00192 virtual uint32_t tellWrite(void) const = 0;
00193
00197 virtual void flush(void) = 0;
00198
00202 virtual void close(void) {}
00203
00204 void release(void)
00205 {
00206 delete this;
00207 }
00208
00209 static PX_INLINE bool isBigEndian()
00210 {
00211 int32_t i = 1;
00212 return *(reinterpret_cast<char*>(&i))==0;
00213 }
00214
00215 PX_INLINE void swap2Bytes(void* _data) const
00216 {
00217 char *data = static_cast<char *>(_data);
00218 char one_byte;
00219 one_byte = data[0]; data[0] = data[1]; data[1] = one_byte;
00220 }
00221
00222 PX_INLINE void swap4Bytes(void* _data) const
00223 {
00224 char *data = static_cast<char *>(_data);
00225 char one_byte;
00226 one_byte = data[0]; data[0] = data[3]; data[3] = one_byte;
00227 one_byte = data[1]; data[1] = data[2]; data[2] = one_byte;
00228 }
00229
00230 PX_INLINE void swap8Bytes(void *_data) const
00231 {
00232 char *data = static_cast<char *>(_data);
00233 char one_byte;
00234 one_byte = data[0]; data[0] = data[7]; data[7] = one_byte;
00235 one_byte = data[1]; data[1] = data[6]; data[6] = one_byte;
00236 one_byte = data[2]; data[2] = data[5]; data[5] = one_byte;
00237 one_byte = data[3]; data[3] = data[4]; data[4] = one_byte;
00238 }
00239
00240
00241 PX_INLINE void storeDword(uint32_t v)
00242 {
00243 if ( mEndianSwap )
00244 swap4Bytes(&v);
00245
00246 write(&v,sizeof(v));
00247 }
00248
00249 PX_INLINE void storeFloat(float v)
00250 {
00251 if ( mEndianSwap )
00252 swap4Bytes(&v);
00253 write(&v,sizeof(v));
00254 }
00255
00256 PX_INLINE void storeDouble(double v)
00257 {
00258 if ( mEndianSwap )
00259 swap8Bytes(&v);
00260 write(&v,sizeof(v));
00261 }
00262
00263 PX_INLINE void storeByte(uint8_t b)
00264 {
00265 write(&b,sizeof(b));
00266 }
00267
00268 PX_INLINE void storeWord(uint16_t w)
00269 {
00270 if ( mEndianSwap )
00271 swap2Bytes(&w);
00272 write(&w,sizeof(w));
00273 }
00274
00275 uint8_t readByte(void)
00276 {
00277 uint8_t v=0;
00278 read(&v,sizeof(v));
00279 return v;
00280 }
00281
00282 uint16_t readWord(void)
00283 {
00284 uint16_t v=0;
00285 read(&v,sizeof(v));
00286 if ( mEndianSwap )
00287 swap2Bytes(&v);
00288 return v;
00289 }
00290
00291 uint32_t readDword(void)
00292 {
00293 uint32_t v=0;
00294 read(&v,sizeof(v));
00295 if ( mEndianSwap )
00296 swap4Bytes(&v);
00297 return v;
00298 }
00299
00300 float readFloat(void)
00301 {
00302 float v=0;
00303 read(&v,sizeof(v));
00304 if ( mEndianSwap )
00305 swap4Bytes(&v);
00306 return v;
00307 }
00308
00309 double readDouble(void)
00310 {
00311 double v=0;
00312 read(&v,sizeof(v));
00313 if ( mEndianSwap )
00314 swap8Bytes(&v);
00315 return v;
00316 }
00317
00318 private:
00319 bool mEndianSwap;
00320 EndianMode mEndianMode;
00321 };
00322
00323 PX_POP_PACK
00324
00325 #if !PX_DOXYGEN
00326 }
00327
00328 using namespace general_PxIOStream2;
00329
00330 namespace general_PxIOStream = general_PxIOStream2;
00331
00332 }
00333 #endif
00334
00337 #endif // PSFILEBUFFER_PXFILEBUF_H