RavEngine
Loading...
Searching...
No Matches
PxFileBuf.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2008-2022 NVIDIA Corporation. All rights reserved.
26// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
27// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
28
29
30#ifndef PSFILEBUFFER_PXFILEBUF_H
31#define PSFILEBUFFER_PXFILEBUF_H
32
33#include "foundation/PxSimpleTypes.h"
34
39#if !PX_DOXYGEN
40namespace physx
41{
42
43namespace general_PxIOStream2
44{
45#endif
46
48
60{
61public:
62
63 enum EndianMode
64 {
65 ENDIAN_NONE = 0, // do no conversion for endian mode
66 ENDIAN_BIG = 1, // always read/write data as natively big endian (Power PC, etc.)
67 ENDIAN_LITTLE = 2 // always read/write data as natively little endian (Intel, etc.) Default Behavior!
68 };
69
70 PxFileBuf(EndianMode mode=ENDIAN_LITTLE)
71 {
72 setEndianMode(mode);
73 }
74
75 virtual ~PxFileBuf(void)
76 {
77
78 }
79
85 static const uint32_t STREAM_SEEK_END=0xFFFFFFFF;
86
87 enum OpenMode
88 {
89 OPEN_FILE_NOT_FOUND,
90 OPEN_READ_ONLY, // open file buffer stream for read only access
91 OPEN_WRITE_ONLY, // open file buffer stream for write only access
92 OPEN_READ_WRITE_NEW, // open a new file for both read/write access
93 OPEN_READ_WRITE_EXISTING // open an existing file for both read/write access
94 };
95
96 virtual OpenMode getOpenMode(void) const = 0;
97
98 bool isOpen(void) const
99 {
100 return getOpenMode()!=OPEN_FILE_NOT_FOUND;
101 }
102
103 enum SeekType
104 {
105 SEEKABLE_NO = 0,
106 SEEKABLE_READ = 0x1,
107 SEEKABLE_WRITE = 0x2,
108 SEEKABLE_READWRITE = 0x3
109 };
110
111 virtual SeekType isSeekable(void) const = 0;
112
113 void setEndianMode(EndianMode e)
114 {
115 mEndianMode = e;
116 if ( (e==ENDIAN_BIG && !isBigEndian() ) ||
117 (e==ENDIAN_LITTLE && isBigEndian() ) )
118 {
119 mEndianSwap = true;
120 }
121 else
122 {
123 mEndianSwap = false;
124 }
125 }
126
127 EndianMode getEndianMode(void) const
128 {
129 return mEndianMode;
130 }
131
132 virtual uint32_t getFileLength(void) const = 0;
133
140 virtual uint32_t seekRead(uint32_t loc) = 0;
141
148 virtual uint32_t seekWrite(uint32_t loc) = 0;
149
158 virtual uint32_t read(void *mem,uint32_t len) = 0;
159
160
169 virtual uint32_t peek(void *mem,uint32_t len) = 0;
170
179 virtual uint32_t write(const void *mem,uint32_t len) = 0;
180
186 virtual uint32_t tellRead(void) const = 0;
187
193 virtual uint32_t tellWrite(void) const = 0;
194
198 virtual void flush(void) = 0;
199
203 virtual void close(void) {}
204
205 void release(void)
206 {
207 PX_DELETE_THIS;
208 }
209
210 static PX_INLINE bool isBigEndian()
211 {
212 int32_t i = 1;
213 return *(reinterpret_cast<char*>(&i))==0;
214 }
215
216 PX_INLINE void swap2Bytes(void* _data) const
217 {
218 char *data = static_cast<char *>(_data);
219 char one_byte;
220 one_byte = data[0]; data[0] = data[1]; data[1] = one_byte;
221 }
222
223 PX_INLINE void swap4Bytes(void* _data) const
224 {
225 char *data = static_cast<char *>(_data);
226 char one_byte;
227 one_byte = data[0]; data[0] = data[3]; data[3] = one_byte;
228 one_byte = data[1]; data[1] = data[2]; data[2] = one_byte;
229 }
230
231 PX_INLINE void swap8Bytes(void *_data) const
232 {
233 char *data = static_cast<char *>(_data);
234 char one_byte;
235 one_byte = data[0]; data[0] = data[7]; data[7] = one_byte;
236 one_byte = data[1]; data[1] = data[6]; data[6] = one_byte;
237 one_byte = data[2]; data[2] = data[5]; data[5] = one_byte;
238 one_byte = data[3]; data[3] = data[4]; data[4] = one_byte;
239 }
240
241
242 PX_INLINE void storeDword(uint32_t v)
243 {
244 if ( mEndianSwap )
245 swap4Bytes(&v);
246
247 write(&v,sizeof(v));
248 }
249
250 PX_INLINE void storeFloat(float v)
251 {
252 if ( mEndianSwap )
253 swap4Bytes(&v);
254 write(&v,sizeof(v));
255 }
256
257 PX_INLINE void storeDouble(double v)
258 {
259 if ( mEndianSwap )
260 swap8Bytes(&v);
261 write(&v,sizeof(v));
262 }
263
264 PX_INLINE void storeByte(uint8_t b)
265 {
266 write(&b,sizeof(b));
267 }
268
269 PX_INLINE void storeWord(uint16_t w)
270 {
271 if ( mEndianSwap )
272 swap2Bytes(&w);
273 write(&w,sizeof(w));
274 }
275
276 uint8_t readByte(void)
277 {
278 uint8_t v=0;
279 read(&v,sizeof(v));
280 return v;
281 }
282
283 uint16_t readWord(void)
284 {
285 uint16_t v=0;
286 read(&v,sizeof(v));
287 if ( mEndianSwap )
288 swap2Bytes(&v);
289 return v;
290 }
291
292 uint32_t readDword(void)
293 {
294 uint32_t v=0;
295 read(&v,sizeof(v));
296 if ( mEndianSwap )
297 swap4Bytes(&v);
298 return v;
299 }
300
301 float readFloat(void)
302 {
303 float v=0;
304 read(&v,sizeof(v));
305 if ( mEndianSwap )
306 swap4Bytes(&v);
307 return v;
308 }
309
310 double readDouble(void)
311 {
312 double v=0;
313 read(&v,sizeof(v));
314 if ( mEndianSwap )
315 swap8Bytes(&v);
316 return v;
317 }
318
319private:
320 bool mEndianSwap; // whether or not the endian should be swapped on the current platform
321 EndianMode mEndianMode; // the current endian mode behavior for the stream
322};
323
324PX_POP_PACK
325
326#if !PX_DOXYGEN
327} // end of namespace
328
329using namespace general_PxIOStream2;
330
331namespace general_PxIOStream = general_PxIOStream2;
332
333} // end of namespace
334#endif
335
338#endif // PSFILEBUFFER_PXFILEBUF_H
Callback class for data serialization.
Definition PxFileBuf.h:60
virtual uint32_t peek(void *mem, uint32_t len)=0
Reads from the stream into a buffer but does not advance the read location.
virtual uint32_t seekWrite(uint32_t loc)=0
Seeks the stream to a particular location for writing.
virtual void flush(void)=0
Causes any temporarily cached data to be flushed to the stream.
virtual uint32_t tellRead(void) const =0
Reports the current stream location read aqccess.
static const uint32_t STREAM_SEEK_END
Declares a constant to seek to the end of the stream.
Definition PxFileBuf.h:85
virtual uint32_t write(const void *mem, uint32_t len)=0
Writes a buffer of memory to the stream.
virtual uint32_t read(void *mem, uint32_t len)=0
Reads from the stream into a buffer.
virtual uint32_t tellWrite(void) const =0
Reports the current stream location for write access.
virtual void close(void)
Close the stream.
Definition PxFileBuf.h:203
virtual uint32_t seekRead(uint32_t loc)=0
Seeks the stream to a particular location for reading.
#define PX_PUSH_PACK_DEFAULT
Definition PxPreprocessor.h:313
#define PX_INLINE
Definition PxPreprocessor.h:320
uint32 uint32_t
Definition fwd.hpp:131
uint8 uint8_t
Definition fwd.hpp:103
uint16 uint16_t
Definition fwd.hpp:117
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39