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-2018 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 
00034 #include "foundation/PxSimpleTypes.h"
00035 
00040 #if !PX_DOXYGEN
00041 namespace physx
00042 {
00043 
00044 namespace general_PxIOStream2
00045 {
00046 #endif
00047 
00048 PX_PUSH_PACK_DEFAULT
00049 
00060 class PxFileBuf
00061 {
00062 public:
00063 
00064     enum EndianMode
00065     {
00066         ENDIAN_NONE     = 0, // do no conversion for endian mode
00067         ENDIAN_BIG      = 1, // always read/write data as natively big endian (Power PC, etc.)
00068         ENDIAN_LITTLE   = 2 // always read/write data as natively little endian (Intel, etc.) Default Behavior!
00069     };
00070 
00071     PxFileBuf(EndianMode mode=ENDIAN_LITTLE)
00072     {
00073         setEndianMode(mode);
00074     }
00075 
00076     virtual ~PxFileBuf(void)
00077     {
00078 
00079     }
00080 
00086     static const uint32_t STREAM_SEEK_END=0xFFFFFFFF;
00087 
00088     enum OpenMode
00089     {
00090         OPEN_FILE_NOT_FOUND,
00091         OPEN_READ_ONLY,                 // open file buffer stream for read only access
00092         OPEN_WRITE_ONLY,                // open file buffer stream for write only access
00093         OPEN_READ_WRITE_NEW,            // open a new file for both read/write access
00094         OPEN_READ_WRITE_EXISTING        // open an existing file for both read/write access
00095     };
00096 
00097     virtual OpenMode    getOpenMode(void) const  = 0;
00098 
00099     bool isOpen(void) const
00100     {
00101         return getOpenMode()!=OPEN_FILE_NOT_FOUND;
00102     }
00103 
00104     enum SeekType
00105     {
00106         SEEKABLE_NO         = 0,
00107         SEEKABLE_READ       = 0x1,
00108         SEEKABLE_WRITE      = 0x2,
00109         SEEKABLE_READWRITE  = 0x3
00110     };
00111 
00112     virtual SeekType isSeekable(void) const = 0;
00113 
00114     void    setEndianMode(EndianMode e)
00115     {
00116         mEndianMode = e;
00117         if ( (e==ENDIAN_BIG && !isBigEndian() ) ||
00118              (e==ENDIAN_LITTLE && isBigEndian() ) )
00119         {
00120             mEndianSwap = true;
00121         }
00122         else
00123         {
00124             mEndianSwap = false;
00125         }
00126     }
00127 
00128     EndianMode  getEndianMode(void) const
00129     {
00130         return mEndianMode;
00131     }
00132 
00133     virtual uint32_t getFileLength(void) const = 0;
00134 
00141     virtual uint32_t    seekRead(uint32_t loc) = 0;
00142 
00149     virtual uint32_t    seekWrite(uint32_t loc) = 0;
00150 
00159     virtual uint32_t    read(void *mem,uint32_t len) = 0;
00160 
00161 
00170     virtual uint32_t    peek(void *mem,uint32_t len) = 0;
00171 
00180     virtual uint32_t    write(const void *mem,uint32_t len) = 0;
00181 
00187     virtual uint32_t    tellRead(void) const = 0;
00188 
00194     virtual uint32_t    tellWrite(void) const = 0;
00195 
00199     virtual void    flush(void) = 0;
00200 
00204     virtual void close(void) {}
00205 
00206     void release(void)
00207     {
00208         delete this;
00209     }
00210 
00211     static PX_INLINE bool isBigEndian()
00212      {
00213        int32_t i = 1;
00214         return *(reinterpret_cast<char*>(&i))==0;
00215     }
00216 
00217     PX_INLINE void swap2Bytes(void* _data) const
00218     {
00219         char *data = static_cast<char *>(_data);
00220         char one_byte;
00221         one_byte = data[0]; data[0] = data[1]; data[1] = one_byte;
00222     }
00223 
00224     PX_INLINE void swap4Bytes(void* _data) const
00225     {
00226         char *data = static_cast<char *>(_data);
00227         char one_byte;
00228         one_byte = data[0]; data[0] = data[3]; data[3] = one_byte;
00229         one_byte = data[1]; data[1] = data[2]; data[2] = one_byte;
00230     }
00231 
00232     PX_INLINE void swap8Bytes(void *_data) const
00233     {
00234         char *data = static_cast<char *>(_data);
00235         char one_byte;
00236         one_byte = data[0]; data[0] = data[7]; data[7] = one_byte;
00237         one_byte = data[1]; data[1] = data[6]; data[6] = one_byte;
00238         one_byte = data[2]; data[2] = data[5]; data[5] = one_byte;
00239         one_byte = data[3]; data[3] = data[4]; data[4] = one_byte;
00240     }
00241 
00242 
00243     PX_INLINE void storeDword(uint32_t v)
00244     {
00245         if ( mEndianSwap )
00246             swap4Bytes(&v);
00247 
00248         write(&v,sizeof(v));
00249     }
00250 
00251     PX_INLINE void storeFloat(float v)
00252     {
00253         if ( mEndianSwap )
00254             swap4Bytes(&v);
00255         write(&v,sizeof(v));
00256     }
00257 
00258     PX_INLINE void storeDouble(double v)
00259     {
00260         if ( mEndianSwap )
00261             swap8Bytes(&v);
00262         write(&v,sizeof(v));
00263     }
00264 
00265     PX_INLINE  void storeByte(uint8_t b)
00266     {
00267         write(&b,sizeof(b));
00268     }
00269 
00270     PX_INLINE void storeWord(uint16_t w)
00271     {
00272         if ( mEndianSwap )
00273             swap2Bytes(&w);
00274         write(&w,sizeof(w));
00275     }
00276 
00277     uint8_t readByte(void) 
00278     {
00279         uint8_t v=0;
00280         read(&v,sizeof(v));
00281         return v;
00282     }
00283 
00284     uint16_t readWord(void) 
00285     {
00286         uint16_t v=0;
00287         read(&v,sizeof(v));
00288         if ( mEndianSwap )
00289             swap2Bytes(&v);
00290         return v;
00291     }
00292 
00293     uint32_t readDword(void) 
00294     {
00295         uint32_t v=0;
00296         read(&v,sizeof(v));
00297         if ( mEndianSwap )
00298             swap4Bytes(&v);
00299         return v;
00300     }
00301 
00302     float readFloat(void) 
00303     {
00304         float v=0;
00305         read(&v,sizeof(v));
00306         if ( mEndianSwap )
00307             swap4Bytes(&v);
00308         return v;
00309     }
00310 
00311     double readDouble(void) 
00312     {
00313         double v=0;
00314         read(&v,sizeof(v));
00315         if ( mEndianSwap )
00316             swap8Bytes(&v);
00317         return v;
00318     }
00319 
00320 private:
00321     bool        mEndianSwap;    // whether or not the endian should be swapped on the current platform
00322     EndianMode  mEndianMode;    // the current endian mode behavior for the stream
00323 };
00324 
00325 PX_POP_PACK
00326 
00327 #if !PX_DOXYGEN
00328 } // end of namespace
00329 
00330 using namespace general_PxIOStream2;
00331 
00332 namespace general_PxIOStream = general_PxIOStream2;
00333 
00334 } // end of namespace
00335 #endif
00336 
00339 #endif // PSFILEBUFFER_PXFILEBUF_H


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