PxFileBuf.h

Go to the documentation of this file.
00001 // This code contains NVIDIA Confidential Information and is disclosed to you
00002 // under a form of NVIDIA software license agreement provided separately to you.
00003 //
00004 // Notice
00005 // NVIDIA Corporation and its licensors retain all intellectual property and
00006 // proprietary rights in and to this software and related documentation and
00007 // any modifications thereto. Any use, reproduction, disclosure, or
00008 // distribution of this software and related documentation without an express
00009 // license agreement from NVIDIA Corporation is strictly prohibited.
00010 //
00011 // ALL NVIDIA DESIGN SPECIFICATIONS, CODE ARE PROVIDED "AS IS.". NVIDIA MAKES
00012 // NO WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO
00013 // THE MATERIALS, AND EXPRESSLY DISCLAIMS ALL IMPLIED WARRANTIES OF NONINFRINGEMENT,
00014 // MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE.
00015 //
00016 // Information and code furnished is believed to be accurate and reliable.
00017 // However, NVIDIA Corporation assumes no responsibility for the consequences of use of such
00018 // information or for any infringement of patents or other rights of third parties that may
00019 // result from its use. No license is granted by implication or otherwise under any patent
00020 // or patent rights of NVIDIA Corporation. Details are subject to change without notice.
00021 // This code supersedes and replaces all information previously supplied.
00022 // NVIDIA Corporation products are not authorized for use as critical
00023 // components in life support devices or systems without express written approval of
00024 // NVIDIA Corporation.
00025 //
00026 // Copyright (c) 2008-2017 NVIDIA Corporation. All rights reserved.
00027 // Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
00028 // Copyright (c) 2001-2004 NovodeX AG. All rights reserved.  
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, // do no conversion for endian mode
00065         ENDIAN_BIG      = 1, // always read/write data as natively big endian (Power PC, etc.)
00066         ENDIAN_LITTLE   = 2 // always read/write data as natively little endian (Intel, etc.) Default Behavior!
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,                 // open file buffer stream for read only access
00090         OPEN_WRITE_ONLY,                // open file buffer stream for write only access
00091         OPEN_READ_WRITE_NEW,            // open a new file for both read/write access
00092         OPEN_READ_WRITE_EXISTING        // open an existing file for both read/write access
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;    // whether or not the endian should be swapped on the current platform
00320     EndianMode  mEndianMode;    // the current endian mode behavior for the stream
00321 };
00322 
00323 PX_POP_PACK
00324 
00325 #if !PX_DOXYGEN
00326 } // end of namespace
00327 
00328 using namespace general_PxIOStream2;
00329 
00330 namespace general_PxIOStream = general_PxIOStream2;
00331 
00332 } // end of namespace
00333 #endif
00334 
00337 #endif // PSFILEBUFFER_PXFILEBUF_H


Copyright © 2008-2017 NVIDIA Corporation, 2701 San Tomas Expressway, Santa Clara, CA 95050 U.S.A. All rights reserved. www.nvidia.com