RavEngine
Loading...
Searching...
No Matches
PsFileBuffer.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
27#ifndef PSFILEBUFFER_PSFILEBUFFER_H
28#define PSFILEBUFFER_PSFILEBUFFER_H
29
30#include "filebuf/PxFileBuf.h"
31
32#include "foundation/PxUserAllocated.h"
33#include <stdio.h>
34
35namespace physx
36{
37namespace general_PxIOStream2
38{
39
40//Use this class if you want to use your own allocator
42{
43public:
44 PxFileBufferBase(const char *fileName,OpenMode mode)
45 {
46 mOpenMode = mode;
47 mFph = NULL;
48 mFileLength = 0;
49 mSeekRead = 0;
50 mSeekWrite = 0;
51 mSeekCurrent = 0;
52 switch ( mode )
53 {
54 case OPEN_READ_ONLY:
55 mFph = fopen(fileName,"rb");
56 break;
57 case OPEN_WRITE_ONLY:
58 mFph = fopen(fileName,"wb");
59 break;
60 case OPEN_READ_WRITE_NEW:
61 mFph = fopen(fileName,"wb+");
62 break;
63 case OPEN_READ_WRITE_EXISTING:
64 mFph = fopen(fileName,"rb+");
65 break;
66 case OPEN_FILE_NOT_FOUND:
67 break;
68 }
69 if ( mFph )
70 {
71 fseek(mFph,0L,SEEK_END);
72 mFileLength = static_cast<uint32_t>(ftell(mFph));
73 fseek(mFph,0L,SEEK_SET);
74 }
75 else
76 {
77 mOpenMode = OPEN_FILE_NOT_FOUND;
78 }
79 }
80
81 virtual ~PxFileBufferBase()
82 {
83 close();
84 }
85
86 virtual void close()
87 {
88 if( mFph )
89 {
90 fclose(mFph);
91 mFph = 0;
92 }
93 }
94
95 virtual SeekType isSeekable(void) const
96 {
97 return mSeekType;
98 }
99
100 virtual uint32_t read(void* buffer, uint32_t size)
101 {
102 uint32_t ret = 0;
103 if ( mFph )
104 {
105 setSeekRead();
106 ret = static_cast<uint32_t>(::fread(buffer,1,size,mFph));
107 mSeekRead+=ret;
108 mSeekCurrent+=ret;
109 }
110 return ret;
111 }
112
113 virtual uint32_t peek(void* buffer, uint32_t size)
114 {
115 uint32_t ret = 0;
116 if ( mFph )
117 {
118 uint32_t loc = tellRead();
119 setSeekRead();
120 ret = static_cast<uint32_t>(::fread(buffer,1,size,mFph));
121 mSeekCurrent+=ret;
122 seekRead(loc);
123 }
124 return ret;
125 }
126
127 virtual uint32_t write(const void* buffer, uint32_t size)
128 {
129 uint32_t ret = 0;
130 if ( mFph )
131 {
132 setSeekWrite();
133 ret = static_cast<uint32_t>(::fwrite(buffer,1,size,mFph));
134 mSeekWrite+=ret;
135 mSeekCurrent+=ret;
136 if ( mSeekWrite > mFileLength )
137 {
138 mFileLength = mSeekWrite;
139 }
140 }
141 return ret;
142 }
143
144 virtual uint32_t tellRead(void) const
145 {
146 return mSeekRead;
147 }
148
149 virtual uint32_t tellWrite(void) const
150 {
151 return mSeekWrite;
152 }
153
154 virtual uint32_t seekRead(uint32_t loc)
155 {
156 mSeekRead = loc;
157 if ( mSeekRead > mFileLength )
158 {
159 mSeekRead = mFileLength;
160 }
161 return mSeekRead;
162 }
163
164 virtual uint32_t seekWrite(uint32_t loc)
165 {
166 mSeekWrite = loc;
167 if ( mSeekWrite > mFileLength )
168 {
169 mSeekWrite = mFileLength;
170 }
171 return mSeekWrite;
172 }
173
174 virtual void flush(void)
175 {
176 if ( mFph )
177 {
178 ::fflush(mFph);
179 }
180 }
181
182 virtual OpenMode getOpenMode(void) const
183 {
184 return mOpenMode;
185 }
186
187 virtual uint32_t getFileLength(void) const
188 {
189 return mFileLength;
190 }
191
192private:
193 // Moves the actual file pointer to the current read location
194 void setSeekRead(void)
195 {
196 if ( mSeekRead != mSeekCurrent && mFph )
197 {
198 if ( mSeekRead >= mFileLength )
199 {
200 fseek(mFph,0L,SEEK_END);
201 }
202 else
203 {
204 fseek(mFph,static_cast<long>(mSeekRead),SEEK_SET);
205 }
206 mSeekCurrent = mSeekRead = static_cast<uint32_t>(ftell(mFph));
207 }
208 }
209 // Moves the actual file pointer to the current write location
210 void setSeekWrite(void)
211 {
212 if ( mSeekWrite != mSeekCurrent && mFph )
213 {
214 if ( mSeekWrite >= mFileLength )
215 {
216 fseek(mFph,0L,SEEK_END);
217 }
218 else
219 {
220 fseek(mFph,static_cast<long>(mSeekWrite),SEEK_SET);
221 }
222 mSeekCurrent = mSeekWrite = static_cast<uint32_t>(ftell(mFph));
223 }
224 }
225
226
227 FILE *mFph;
228 uint32_t mSeekRead;
229 uint32_t mSeekWrite;
230 uint32_t mSeekCurrent;
231 uint32_t mFileLength;
232 SeekType mSeekType;
233 OpenMode mOpenMode;
234};
235
236//Use this class if you want to use PhysX memory allocator
238{
239public:
240 PsFileBuffer(const char *fileName,OpenMode mode): PxFileBufferBase(fileName, mode) {}
241};
242
243}
244using namespace general_PxIOStream2;
245}
246
247#endif // PSFILEBUFFER_PSFILEBUFFER_H
Definition PxUserAllocated.h:43
Definition PsFileBuffer.h:238
Callback class for data serialization.
Definition PxFileBuf.h:60
virtual uint32_t read(void *buffer, uint32_t size)
Reads from the stream into a buffer.
Definition PsFileBuffer.h:100
virtual uint32_t seekWrite(uint32_t loc)
Seeks the stream to a particular location for writing.
Definition PsFileBuffer.h:164
virtual void flush(void)
Causes any temporarily cached data to be flushed to the stream.
Definition PsFileBuffer.h:174
virtual uint32_t write(const void *buffer, uint32_t size)
Writes a buffer of memory to the stream.
Definition PsFileBuffer.h:127
virtual uint32_t tellWrite(void) const
Reports the current stream location for write access.
Definition PsFileBuffer.h:149
virtual uint32_t seekRead(uint32_t loc)
Seeks the stream to a particular location for reading.
Definition PsFileBuffer.h:154
virtual uint32_t peek(void *buffer, uint32_t size)
Reads from the stream into a buffer but does not advance the read location.
Definition PsFileBuffer.h:113
virtual uint32_t tellRead(void) const
Reports the current stream location read aqccess.
Definition PsFileBuffer.h:144
virtual void close()
Close the stream.
Definition PsFileBuffer.h:86
uint32 uint32_t
Definition fwd.hpp:131
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39