1//
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions
4// are met:
5// * Redistributions of source code must retain the above copyright
6// notice, this list of conditions and the following disclaimer.
7// * Redistributions in binary form must reproduce the above copyright
8// notice, this list of conditions and the following disclaimer in the
9// documentation and/or other materials provided with the distribution.
10// * Neither the name of NVIDIA CORPORATION nor the names of its
11// contributors may be used to endorse or promote products derived
12// from this software without specific prior written permission.
13//
14// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
15// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25//
26// Copyright (c) 2008-2021 NVIDIA Corporation. All rights reserved.
27
28#ifndef PSFILEBUFFER_PSFILEBUFFER_H
29#define PSFILEBUFFER_PSFILEBUFFER_H
30
31#include "filebuf/PxFileBuf.h"
32
33#include "Ps.h"
34#include "PsUserAllocated.h"
35#include <stdio.h>
36
37namespace physx
38{
39namespace general_PxIOStream2
40{
41 using namespace shdfnd;
42
43//Use this class if you want to use your own allocator
44class PxFileBufferBase : public PxFileBuf
45{
46public:
47 PxFileBufferBase(const char *fileName,OpenMode mode)
48 {
49 mOpenMode = mode;
50 mFph = NULL;
51 mFileLength = 0;
52 mSeekRead = 0;
53 mSeekWrite = 0;
54 mSeekCurrent = 0;
55 switch ( mode )
56 {
57 case OPEN_READ_ONLY:
58 mFph = fopen(filename: fileName,modes: "rb");
59 break;
60 case OPEN_WRITE_ONLY:
61 mFph = fopen(filename: fileName,modes: "wb");
62 break;
63 case OPEN_READ_WRITE_NEW:
64 mFph = fopen(filename: fileName,modes: "wb+");
65 break;
66 case OPEN_READ_WRITE_EXISTING:
67 mFph = fopen(filename: fileName,modes: "rb+");
68 break;
69 case OPEN_FILE_NOT_FOUND:
70 break;
71 }
72 if ( mFph )
73 {
74 fseek(stream: mFph,off: 0L,SEEK_END);
75 mFileLength = static_cast<uint32_t>(ftell(stream: mFph));
76 fseek(stream: mFph,off: 0L,SEEK_SET);
77 }
78 else
79 {
80 mOpenMode = OPEN_FILE_NOT_FOUND;
81 }
82 }
83
84 virtual ~PxFileBufferBase()
85 {
86 close();
87 }
88
89 virtual void close()
90 {
91 if( mFph )
92 {
93 fclose(stream: mFph);
94 mFph = 0;
95 }
96 }
97
98 virtual SeekType isSeekable(void) const
99 {
100 return mSeekType;
101 }
102
103 virtual uint32_t read(void* buffer, uint32_t size)
104 {
105 uint32_t ret = 0;
106 if ( mFph )
107 {
108 setSeekRead();
109 ret = static_cast<uint32_t>(::fread(ptr: buffer,size: 1,n: size,stream: mFph));
110 mSeekRead+=ret;
111 mSeekCurrent+=ret;
112 }
113 return ret;
114 }
115
116 virtual uint32_t peek(void* buffer, uint32_t size)
117 {
118 uint32_t ret = 0;
119 if ( mFph )
120 {
121 uint32_t loc = tellRead();
122 setSeekRead();
123 ret = static_cast<uint32_t>(::fread(ptr: buffer,size: 1,n: size,stream: mFph));
124 mSeekCurrent+=ret;
125 seekRead(loc);
126 }
127 return ret;
128 }
129
130 virtual uint32_t write(const void* buffer, uint32_t size)
131 {
132 uint32_t ret = 0;
133 if ( mFph )
134 {
135 setSeekWrite();
136 ret = static_cast<uint32_t>(::fwrite(ptr: buffer,size: 1,n: size,s: mFph));
137 mSeekWrite+=ret;
138 mSeekCurrent+=ret;
139 if ( mSeekWrite > mFileLength )
140 {
141 mFileLength = mSeekWrite;
142 }
143 }
144 return ret;
145 }
146
147 virtual uint32_t tellRead(void) const
148 {
149 return mSeekRead;
150 }
151
152 virtual uint32_t tellWrite(void) const
153 {
154 return mSeekWrite;
155 }
156
157 virtual uint32_t seekRead(uint32_t loc)
158 {
159 mSeekRead = loc;
160 if ( mSeekRead > mFileLength )
161 {
162 mSeekRead = mFileLength;
163 }
164 return mSeekRead;
165 }
166
167 virtual uint32_t seekWrite(uint32_t loc)
168 {
169 mSeekWrite = loc;
170 if ( mSeekWrite > mFileLength )
171 {
172 mSeekWrite = mFileLength;
173 }
174 return mSeekWrite;
175 }
176
177 virtual void flush(void)
178 {
179 if ( mFph )
180 {
181 ::fflush(stream: mFph);
182 }
183 }
184
185 virtual OpenMode getOpenMode(void) const
186 {
187 return mOpenMode;
188 }
189
190 virtual uint32_t getFileLength(void) const
191 {
192 return mFileLength;
193 }
194
195private:
196 // Moves the actual file pointer to the current read location
197 void setSeekRead(void)
198 {
199 if ( mSeekRead != mSeekCurrent && mFph )
200 {
201 if ( mSeekRead >= mFileLength )
202 {
203 fseek(stream: mFph,off: 0L,SEEK_END);
204 }
205 else
206 {
207 fseek(stream: mFph,off: static_cast<long>(mSeekRead),SEEK_SET);
208 }
209 mSeekCurrent = mSeekRead = static_cast<uint32_t>(ftell(stream: mFph));
210 }
211 }
212 // Moves the actual file pointer to the current write location
213 void setSeekWrite(void)
214 {
215 if ( mSeekWrite != mSeekCurrent && mFph )
216 {
217 if ( mSeekWrite >= mFileLength )
218 {
219 fseek(stream: mFph,off: 0L,SEEK_END);
220 }
221 else
222 {
223 fseek(stream: mFph,off: static_cast<long>(mSeekWrite),SEEK_SET);
224 }
225 mSeekCurrent = mSeekWrite = static_cast<uint32_t>(ftell(stream: mFph));
226 }
227 }
228
229
230 FILE *mFph;
231 uint32_t mSeekRead;
232 uint32_t mSeekWrite;
233 uint32_t mSeekCurrent;
234 uint32_t mFileLength;
235 SeekType mSeekType;
236 OpenMode mOpenMode;
237};
238
239//Use this class if you want to use PhysX memory allocator
240class PsFileBuffer: public PxFileBufferBase, public UserAllocated
241{
242public:
243 PsFileBuffer(const char *fileName,OpenMode mode): PxFileBufferBase(fileName, mode) {}
244};
245
246}
247using namespace general_PxIOStream2;
248}
249
250#endif // PSFILEBUFFER_PSFILEBUFFER_H
251

source code of qtquick3dphysics/src/3rdparty/PhysX/source/filebuf/include/PsFileBuffer.h