PxFileBuf.h

Go to the documentation of this file.
00001 //
00002 // Redistribution and use in source and binary forms, with or without
00003 // modification, are permitted provided that the following conditions
00004 // are met:
00005 //  * Redistributions of source code must retain the above copyright
00006 //    notice, this list of conditions and the following disclaimer.
00007 //  * Redistributions in binary form must reproduce the above copyright
00008 //    notice, this list of conditions and the following disclaimer in the
00009 //    documentation and/or other materials provided with the distribution.
00010 //  * Neither the name of NVIDIA CORPORATION nor the names of its
00011 //    contributors may be used to endorse or promote products derived
00012 //    from this software without specific prior written permission.
00013 //
00014 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
00015 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00016 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00017 // PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
00018 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00019 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00020 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00021 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00022 // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00023 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00024 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 
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-2018 NVIDIA Corporation, 2701 San Tomas Expressway, Santa Clara, CA 95050 U.S.A. All rights reserved. www.nvidia.com