rev |
line source |
nuclear@3
|
1 /**************************************************************************
|
nuclear@3
|
2
|
nuclear@3
|
3 Filename : OVR_File.cpp
|
nuclear@3
|
4 Content : File wrapper class implementation (Win32)
|
nuclear@3
|
5
|
nuclear@3
|
6 Created : April 5, 1999
|
nuclear@3
|
7 Authors : Michael Antonov
|
nuclear@3
|
8
|
nuclear@3
|
9 Copyright : Copyright 2012 Oculus VR, Inc. All Rights reserved.
|
nuclear@3
|
10
|
nuclear@3
|
11 Use of this software is subject to the terms of the Oculus license
|
nuclear@3
|
12 agreement provided at the time of installation or download, or which
|
nuclear@3
|
13 otherwise accompanies this software in either electronic or hard copy form.
|
nuclear@3
|
14
|
nuclear@3
|
15 **************************************************************************/
|
nuclear@3
|
16
|
nuclear@3
|
17 #define GFILE_CXX
|
nuclear@3
|
18
|
nuclear@3
|
19 // Standard C library (Captain Obvious guarantees!)
|
nuclear@3
|
20 #include <stdio.h>
|
nuclear@3
|
21
|
nuclear@3
|
22 #include "OVR_File.h"
|
nuclear@3
|
23
|
nuclear@3
|
24 namespace OVR {
|
nuclear@3
|
25
|
nuclear@3
|
26 // Buffered file adds buffering to an existing file
|
nuclear@3
|
27 // FILEBUFFER_SIZE defines the size of internal buffer, while
|
nuclear@3
|
28 // FILEBUFFER_TOLERANCE controls the amount of data we'll effectively try to buffer
|
nuclear@3
|
29 #define FILEBUFFER_SIZE (8192-8)
|
nuclear@3
|
30 #define FILEBUFFER_TOLERANCE 4096
|
nuclear@3
|
31
|
nuclear@3
|
32 // ** Constructor/Destructor
|
nuclear@3
|
33
|
nuclear@3
|
34 // Hidden constructor
|
nuclear@3
|
35 // Not supposed to be used
|
nuclear@3
|
36 BufferedFile::BufferedFile() : DelegatedFile(0)
|
nuclear@3
|
37 {
|
nuclear@3
|
38 pBuffer = (UByte*)OVR_ALLOC(FILEBUFFER_SIZE);
|
nuclear@3
|
39 BufferMode = NoBuffer;
|
nuclear@3
|
40 FilePos = 0;
|
nuclear@3
|
41 Pos = 0;
|
nuclear@3
|
42 DataSize = 0;
|
nuclear@3
|
43 }
|
nuclear@3
|
44
|
nuclear@3
|
45 // Takes another file as source
|
nuclear@3
|
46 BufferedFile::BufferedFile(File *pfile) : DelegatedFile(pfile)
|
nuclear@3
|
47 {
|
nuclear@3
|
48 pBuffer = (UByte*)OVR_ALLOC(FILEBUFFER_SIZE);
|
nuclear@3
|
49 BufferMode = NoBuffer;
|
nuclear@3
|
50 FilePos = pfile->LTell();
|
nuclear@3
|
51 Pos = 0;
|
nuclear@3
|
52 DataSize = 0;
|
nuclear@3
|
53 }
|
nuclear@3
|
54
|
nuclear@3
|
55
|
nuclear@3
|
56 // Destructor
|
nuclear@3
|
57 BufferedFile::~BufferedFile()
|
nuclear@3
|
58 {
|
nuclear@3
|
59 // Flush in case there's data
|
nuclear@3
|
60 if (pFile)
|
nuclear@3
|
61 FlushBuffer();
|
nuclear@3
|
62 // Get rid of buffer
|
nuclear@3
|
63 if (pBuffer)
|
nuclear@3
|
64 OVR_FREE(pBuffer);
|
nuclear@3
|
65 }
|
nuclear@3
|
66
|
nuclear@3
|
67 /*
|
nuclear@3
|
68 bool BufferedFile::VCopy(const Object &source)
|
nuclear@3
|
69 {
|
nuclear@3
|
70 if (!DelegatedFile::VCopy(source))
|
nuclear@3
|
71 return 0;
|
nuclear@3
|
72
|
nuclear@3
|
73 // Data members
|
nuclear@3
|
74 BufferedFile *psource = (BufferedFile*)&source;
|
nuclear@3
|
75
|
nuclear@3
|
76 // Buffer & the mode it's in
|
nuclear@3
|
77 pBuffer = psource->pBuffer;
|
nuclear@3
|
78 BufferMode = psource->BufferMode;
|
nuclear@3
|
79 Pos = psource->Pos;
|
nuclear@3
|
80 DataSize = psource->DataSize;
|
nuclear@3
|
81 return 1;
|
nuclear@3
|
82 }
|
nuclear@3
|
83 */
|
nuclear@3
|
84
|
nuclear@3
|
85 // Initializes buffering to a certain mode
|
nuclear@3
|
86 bool BufferedFile::SetBufferMode(BufferModeType mode)
|
nuclear@3
|
87 {
|
nuclear@3
|
88 if (!pBuffer)
|
nuclear@3
|
89 return false;
|
nuclear@3
|
90 if (mode == BufferMode)
|
nuclear@3
|
91 return true;
|
nuclear@3
|
92
|
nuclear@3
|
93 FlushBuffer();
|
nuclear@3
|
94
|
nuclear@3
|
95 // Can't set write mode if we can't write
|
nuclear@3
|
96 if ((mode==WriteBuffer) && (!pFile || !pFile->IsWritable()) )
|
nuclear@3
|
97 return 0;
|
nuclear@3
|
98
|
nuclear@3
|
99 // And SetMode
|
nuclear@3
|
100 BufferMode = mode;
|
nuclear@3
|
101 Pos = 0;
|
nuclear@3
|
102 DataSize = 0;
|
nuclear@3
|
103 return 1;
|
nuclear@3
|
104 }
|
nuclear@3
|
105
|
nuclear@3
|
106 // Flushes buffer
|
nuclear@3
|
107 void BufferedFile::FlushBuffer()
|
nuclear@3
|
108 {
|
nuclear@3
|
109 switch(BufferMode)
|
nuclear@3
|
110 {
|
nuclear@3
|
111 case WriteBuffer:
|
nuclear@3
|
112 // Write data in buffer
|
nuclear@3
|
113 FilePos += pFile->Write(pBuffer,Pos);
|
nuclear@3
|
114 Pos = 0;
|
nuclear@3
|
115 break;
|
nuclear@3
|
116
|
nuclear@3
|
117 case ReadBuffer:
|
nuclear@3
|
118 // Seek back & reset buffer data
|
nuclear@3
|
119 if ((DataSize-Pos)>0)
|
nuclear@3
|
120 FilePos = pFile->LSeek(-(int)(DataSize-Pos), Seek_Cur);
|
nuclear@3
|
121 DataSize = 0;
|
nuclear@3
|
122 Pos = 0;
|
nuclear@3
|
123 break;
|
nuclear@3
|
124 default:
|
nuclear@3
|
125 // not handled!
|
nuclear@3
|
126 break;
|
nuclear@3
|
127 }
|
nuclear@3
|
128 }
|
nuclear@3
|
129
|
nuclear@3
|
130 // Reloads data for ReadBuffer
|
nuclear@3
|
131 void BufferedFile::LoadBuffer()
|
nuclear@3
|
132 {
|
nuclear@3
|
133 if (BufferMode == ReadBuffer)
|
nuclear@3
|
134 {
|
nuclear@3
|
135 // We should only reload once all of pre-loaded buffer is consumed.
|
nuclear@3
|
136 OVR_ASSERT(Pos == DataSize);
|
nuclear@3
|
137
|
nuclear@3
|
138 // WARNING: Right now LoadBuffer() assumes the buffer's empty
|
nuclear@3
|
139 int sz = pFile->Read(pBuffer,FILEBUFFER_SIZE);
|
nuclear@3
|
140 DataSize = sz<0 ? 0 : (unsigned)sz;
|
nuclear@3
|
141 Pos = 0;
|
nuclear@3
|
142 FilePos += DataSize;
|
nuclear@3
|
143 }
|
nuclear@3
|
144 }
|
nuclear@3
|
145
|
nuclear@3
|
146
|
nuclear@3
|
147 // ** Overridden functions
|
nuclear@3
|
148
|
nuclear@3
|
149 // We override all the functions that can possibly
|
nuclear@3
|
150 // require buffer mode switch, flush, or extra calculations
|
nuclear@3
|
151
|
nuclear@3
|
152 // Tell() requires buffer adjustment
|
nuclear@3
|
153 int BufferedFile::Tell()
|
nuclear@3
|
154 {
|
nuclear@3
|
155 if (BufferMode == ReadBuffer)
|
nuclear@3
|
156 return int (FilePos - DataSize + Pos);
|
nuclear@3
|
157
|
nuclear@3
|
158 int pos = pFile->Tell();
|
nuclear@3
|
159 // Adjust position based on buffer mode & data
|
nuclear@3
|
160 if (pos!=-1)
|
nuclear@3
|
161 {
|
nuclear@3
|
162 OVR_ASSERT(BufferMode != ReadBuffer);
|
nuclear@3
|
163 if (BufferMode == WriteBuffer)
|
nuclear@3
|
164 pos += Pos;
|
nuclear@3
|
165 }
|
nuclear@3
|
166 return pos;
|
nuclear@3
|
167 }
|
nuclear@3
|
168
|
nuclear@3
|
169 SInt64 BufferedFile::LTell()
|
nuclear@3
|
170 {
|
nuclear@3
|
171 if (BufferMode == ReadBuffer)
|
nuclear@3
|
172 return FilePos - DataSize + Pos;
|
nuclear@3
|
173
|
nuclear@3
|
174 SInt64 pos = pFile->LTell();
|
nuclear@3
|
175 if (pos!=-1)
|
nuclear@3
|
176 {
|
nuclear@3
|
177 OVR_ASSERT(BufferMode != ReadBuffer);
|
nuclear@3
|
178 if (BufferMode == WriteBuffer)
|
nuclear@3
|
179 pos += Pos;
|
nuclear@3
|
180 }
|
nuclear@3
|
181 return pos;
|
nuclear@3
|
182 }
|
nuclear@3
|
183
|
nuclear@3
|
184 int BufferedFile::GetLength()
|
nuclear@3
|
185 {
|
nuclear@3
|
186 int len = pFile->GetLength();
|
nuclear@3
|
187 // If writing through buffer, file length may actually be bigger
|
nuclear@3
|
188 if ((len!=-1) && (BufferMode==WriteBuffer))
|
nuclear@3
|
189 {
|
nuclear@3
|
190 int currPos = pFile->Tell() + Pos;
|
nuclear@3
|
191 if (currPos>len)
|
nuclear@3
|
192 len = currPos;
|
nuclear@3
|
193 }
|
nuclear@3
|
194 return len;
|
nuclear@3
|
195 }
|
nuclear@3
|
196 SInt64 BufferedFile::LGetLength()
|
nuclear@3
|
197 {
|
nuclear@3
|
198 SInt64 len = pFile->LGetLength();
|
nuclear@3
|
199 // If writing through buffer, file length may actually be bigger
|
nuclear@3
|
200 if ((len!=-1) && (BufferMode==WriteBuffer))
|
nuclear@3
|
201 {
|
nuclear@3
|
202 SInt64 currPos = pFile->LTell() + Pos;
|
nuclear@3
|
203 if (currPos>len)
|
nuclear@3
|
204 len = currPos;
|
nuclear@3
|
205 }
|
nuclear@3
|
206 return len;
|
nuclear@3
|
207 }
|
nuclear@3
|
208
|
nuclear@3
|
209 /*
|
nuclear@3
|
210 bool BufferedFile::Stat(FileStats *pfs)
|
nuclear@3
|
211 {
|
nuclear@3
|
212 // Have to fix up length is stat
|
nuclear@3
|
213 if (pFile->Stat(pfs))
|
nuclear@3
|
214 {
|
nuclear@3
|
215 if (BufferMode==WriteBuffer)
|
nuclear@3
|
216 {
|
nuclear@3
|
217 SInt64 currPos = pFile->LTell() + Pos;
|
nuclear@3
|
218 if (currPos > pfs->Size)
|
nuclear@3
|
219 {
|
nuclear@3
|
220 pfs->Size = currPos;
|
nuclear@3
|
221 // ??
|
nuclear@3
|
222 pfs->Blocks = (pfs->Size+511) >> 9;
|
nuclear@3
|
223 }
|
nuclear@3
|
224 }
|
nuclear@3
|
225 return 1;
|
nuclear@3
|
226 }
|
nuclear@3
|
227 return 0;
|
nuclear@3
|
228 }
|
nuclear@3
|
229 */
|
nuclear@3
|
230
|
nuclear@3
|
231 int BufferedFile::Write(const UByte *psourceBuffer, int numBytes)
|
nuclear@3
|
232 {
|
nuclear@3
|
233 if ( (BufferMode==WriteBuffer) || SetBufferMode(WriteBuffer))
|
nuclear@3
|
234 {
|
nuclear@3
|
235 // If not data space in buffer, flush
|
nuclear@3
|
236 if ((FILEBUFFER_SIZE-(int)Pos)<numBytes)
|
nuclear@3
|
237 {
|
nuclear@3
|
238 FlushBuffer();
|
nuclear@3
|
239 // If bigger then tolerance, just write directly
|
nuclear@3
|
240 if (numBytes>FILEBUFFER_TOLERANCE)
|
nuclear@3
|
241 {
|
nuclear@3
|
242 int sz = pFile->Write(psourceBuffer,numBytes);
|
nuclear@3
|
243 if (sz > 0)
|
nuclear@3
|
244 FilePos += sz;
|
nuclear@3
|
245 return sz;
|
nuclear@3
|
246 }
|
nuclear@3
|
247 }
|
nuclear@3
|
248
|
nuclear@3
|
249 // Enough space in buffer.. so copy to it
|
nuclear@3
|
250 memcpy(pBuffer+Pos, psourceBuffer, numBytes);
|
nuclear@3
|
251 Pos += numBytes;
|
nuclear@3
|
252 return numBytes;
|
nuclear@3
|
253 }
|
nuclear@3
|
254 int sz = pFile->Write(psourceBuffer,numBytes);
|
nuclear@3
|
255 if (sz > 0)
|
nuclear@3
|
256 FilePos += sz;
|
nuclear@3
|
257 return sz;
|
nuclear@3
|
258 }
|
nuclear@3
|
259
|
nuclear@3
|
260 int BufferedFile::Read(UByte *pdestBuffer, int numBytes)
|
nuclear@3
|
261 {
|
nuclear@3
|
262 if ( (BufferMode==ReadBuffer) || SetBufferMode(ReadBuffer))
|
nuclear@3
|
263 {
|
nuclear@3
|
264 // Data in buffer... copy it
|
nuclear@3
|
265 if ((int)(DataSize-Pos) >= numBytes)
|
nuclear@3
|
266 {
|
nuclear@3
|
267 memcpy(pdestBuffer, pBuffer+Pos, numBytes);
|
nuclear@3
|
268 Pos += numBytes;
|
nuclear@3
|
269 return numBytes;
|
nuclear@3
|
270 }
|
nuclear@3
|
271
|
nuclear@3
|
272 // Not enough data in buffer, copy buffer
|
nuclear@3
|
273 int readBytes = DataSize-Pos;
|
nuclear@3
|
274 memcpy(pdestBuffer, pBuffer+Pos, readBytes);
|
nuclear@3
|
275 numBytes -= readBytes;
|
nuclear@3
|
276 pdestBuffer += readBytes;
|
nuclear@3
|
277 Pos = DataSize;
|
nuclear@3
|
278
|
nuclear@3
|
279 // Don't reload buffer if more then tolerance
|
nuclear@3
|
280 // (No major advantage, and we don't want to write a loop)
|
nuclear@3
|
281 if (numBytes>FILEBUFFER_TOLERANCE)
|
nuclear@3
|
282 {
|
nuclear@3
|
283 numBytes = pFile->Read(pdestBuffer,numBytes);
|
nuclear@3
|
284 if (numBytes > 0)
|
nuclear@3
|
285 {
|
nuclear@3
|
286 FilePos += numBytes;
|
nuclear@3
|
287 Pos = DataSize = 0;
|
nuclear@3
|
288 }
|
nuclear@3
|
289 return readBytes + ((numBytes==-1) ? 0 : numBytes);
|
nuclear@3
|
290 }
|
nuclear@3
|
291
|
nuclear@3
|
292 // Reload the buffer
|
nuclear@3
|
293 // WARNING: Right now LoadBuffer() assumes the buffer's empty
|
nuclear@3
|
294 LoadBuffer();
|
nuclear@3
|
295 if ((int)(DataSize-Pos) < numBytes)
|
nuclear@3
|
296 numBytes = (int)DataSize-Pos;
|
nuclear@3
|
297
|
nuclear@3
|
298 memcpy(pdestBuffer, pBuffer+Pos, numBytes);
|
nuclear@3
|
299 Pos += numBytes;
|
nuclear@3
|
300 return numBytes + readBytes;
|
nuclear@3
|
301
|
nuclear@3
|
302 /*
|
nuclear@3
|
303 // Alternative Read implementation. The one above is probably better
|
nuclear@3
|
304 // due to FILEBUFFER_TOLERANCE.
|
nuclear@3
|
305 int total = 0;
|
nuclear@3
|
306
|
nuclear@3
|
307 do {
|
nuclear@3
|
308 int bufferBytes = (int)(DataSize-Pos);
|
nuclear@3
|
309 int copyBytes = (bufferBytes > numBytes) ? numBytes : bufferBytes;
|
nuclear@3
|
310
|
nuclear@3
|
311 memcpy(pdestBuffer, pBuffer+Pos, copyBytes);
|
nuclear@3
|
312 numBytes -= copyBytes;
|
nuclear@3
|
313 pdestBuffer += copyBytes;
|
nuclear@3
|
314 Pos += copyBytes;
|
nuclear@3
|
315 total += copyBytes;
|
nuclear@3
|
316
|
nuclear@3
|
317 if (numBytes == 0)
|
nuclear@3
|
318 break;
|
nuclear@3
|
319 LoadBuffer();
|
nuclear@3
|
320
|
nuclear@3
|
321 } while (DataSize > 0);
|
nuclear@3
|
322
|
nuclear@3
|
323 return total;
|
nuclear@3
|
324 */
|
nuclear@3
|
325 }
|
nuclear@3
|
326 int sz = pFile->Read(pdestBuffer,numBytes);
|
nuclear@3
|
327 if (sz > 0)
|
nuclear@3
|
328 FilePos += sz;
|
nuclear@3
|
329 return sz;
|
nuclear@3
|
330 }
|
nuclear@3
|
331
|
nuclear@3
|
332
|
nuclear@3
|
333 int BufferedFile::SkipBytes(int numBytes)
|
nuclear@3
|
334 {
|
nuclear@3
|
335 int skippedBytes = 0;
|
nuclear@3
|
336
|
nuclear@3
|
337 // Special case for skipping a little data in read buffer
|
nuclear@3
|
338 if (BufferMode==ReadBuffer)
|
nuclear@3
|
339 {
|
nuclear@3
|
340 skippedBytes = (((int)DataSize-(int)Pos) >= numBytes) ? numBytes : (DataSize-Pos);
|
nuclear@3
|
341 Pos += skippedBytes;
|
nuclear@3
|
342 numBytes -= skippedBytes;
|
nuclear@3
|
343 }
|
nuclear@3
|
344
|
nuclear@3
|
345 if (numBytes)
|
nuclear@3
|
346 {
|
nuclear@3
|
347 numBytes = pFile->SkipBytes(numBytes);
|
nuclear@3
|
348 // Make sure we return the actual number skipped, or error
|
nuclear@3
|
349 if (numBytes!=-1)
|
nuclear@3
|
350 {
|
nuclear@3
|
351 skippedBytes += numBytes;
|
nuclear@3
|
352 FilePos += numBytes;
|
nuclear@3
|
353 Pos = DataSize = 0;
|
nuclear@3
|
354 }
|
nuclear@3
|
355 else if (skippedBytes <= 0)
|
nuclear@3
|
356 skippedBytes = -1;
|
nuclear@3
|
357 }
|
nuclear@3
|
358 return skippedBytes;
|
nuclear@3
|
359 }
|
nuclear@3
|
360
|
nuclear@3
|
361 int BufferedFile::BytesAvailable()
|
nuclear@3
|
362 {
|
nuclear@3
|
363 int available = pFile->BytesAvailable();
|
nuclear@3
|
364 // Adjust available size based on buffers
|
nuclear@3
|
365 switch(BufferMode)
|
nuclear@3
|
366 {
|
nuclear@3
|
367 case ReadBuffer:
|
nuclear@3
|
368 available += DataSize-Pos;
|
nuclear@3
|
369 break;
|
nuclear@3
|
370 case WriteBuffer:
|
nuclear@3
|
371 available -= Pos;
|
nuclear@3
|
372 if (available<0)
|
nuclear@3
|
373 available= 0;
|
nuclear@3
|
374 break;
|
nuclear@3
|
375 default:
|
nuclear@3
|
376 break;
|
nuclear@3
|
377 }
|
nuclear@3
|
378 return available;
|
nuclear@3
|
379 }
|
nuclear@3
|
380
|
nuclear@3
|
381 bool BufferedFile::Flush()
|
nuclear@3
|
382 {
|
nuclear@3
|
383 FlushBuffer();
|
nuclear@3
|
384 return pFile->Flush();
|
nuclear@3
|
385 }
|
nuclear@3
|
386
|
nuclear@3
|
387 // Seeking could be optimized better..
|
nuclear@3
|
388 int BufferedFile::Seek(int offset, int origin)
|
nuclear@3
|
389 {
|
nuclear@3
|
390 if (BufferMode == ReadBuffer)
|
nuclear@3
|
391 {
|
nuclear@3
|
392 if (origin == Seek_Cur)
|
nuclear@3
|
393 {
|
nuclear@3
|
394 // Seek can fall either before or after Pos in the buffer,
|
nuclear@3
|
395 // but it must be within bounds.
|
nuclear@3
|
396 if (((unsigned(offset) + Pos)) <= DataSize)
|
nuclear@3
|
397 {
|
nuclear@3
|
398 Pos += offset;
|
nuclear@3
|
399 return int (FilePos - DataSize + Pos);
|
nuclear@3
|
400 }
|
nuclear@3
|
401
|
nuclear@3
|
402 // Lightweight buffer "Flush". We do this to avoid an extra seek
|
nuclear@3
|
403 // back operation which would take place if we called FlushBuffer directly.
|
nuclear@3
|
404 origin = Seek_Set;
|
nuclear@3
|
405 OVR_ASSERT(((FilePos - DataSize + Pos) + (UInt64)offset) < ~(UInt64)0);
|
nuclear@3
|
406 offset = (int)(FilePos - DataSize + Pos) + offset;
|
nuclear@3
|
407 Pos = DataSize = 0;
|
nuclear@3
|
408 }
|
nuclear@3
|
409 else if (origin == Seek_Set)
|
nuclear@3
|
410 {
|
nuclear@3
|
411 if (((unsigned)offset - (FilePos-DataSize)) <= DataSize)
|
nuclear@3
|
412 {
|
nuclear@3
|
413 OVR_ASSERT((FilePos-DataSize) < ~(UInt64)0);
|
nuclear@3
|
414 Pos = (unsigned)offset - (unsigned)(FilePos-DataSize);
|
nuclear@3
|
415 return offset;
|
nuclear@3
|
416 }
|
nuclear@3
|
417 Pos = DataSize = 0;
|
nuclear@3
|
418 }
|
nuclear@3
|
419 else
|
nuclear@3
|
420 {
|
nuclear@3
|
421 FlushBuffer();
|
nuclear@3
|
422 }
|
nuclear@3
|
423 }
|
nuclear@3
|
424 else
|
nuclear@3
|
425 {
|
nuclear@3
|
426 FlushBuffer();
|
nuclear@3
|
427 }
|
nuclear@3
|
428
|
nuclear@3
|
429 /*
|
nuclear@3
|
430 // Old Seek Logic
|
nuclear@3
|
431 if (origin == Seek_Cur && offset + Pos < DataSize)
|
nuclear@3
|
432 {
|
nuclear@3
|
433 //OVR_ASSERT((FilePos - DataSize) >= (FilePos - DataSize + Pos + offset));
|
nuclear@3
|
434 Pos += offset;
|
nuclear@3
|
435 OVR_ASSERT(int (Pos) >= 0);
|
nuclear@3
|
436 return int (FilePos - DataSize + Pos);
|
nuclear@3
|
437 }
|
nuclear@3
|
438 else if (origin == Seek_Set && unsigned(offset) >= FilePos - DataSize && unsigned(offset) < FilePos)
|
nuclear@3
|
439 {
|
nuclear@3
|
440 Pos = unsigned(offset - FilePos + DataSize);
|
nuclear@3
|
441 OVR_ASSERT(int (Pos) >= 0);
|
nuclear@3
|
442 return int (FilePos - DataSize + Pos);
|
nuclear@3
|
443 }
|
nuclear@3
|
444
|
nuclear@3
|
445 FlushBuffer();
|
nuclear@3
|
446 */
|
nuclear@3
|
447
|
nuclear@3
|
448
|
nuclear@3
|
449 FilePos = pFile->Seek(offset,origin);
|
nuclear@3
|
450 return int (FilePos);
|
nuclear@3
|
451 }
|
nuclear@3
|
452
|
nuclear@3
|
453 SInt64 BufferedFile::LSeek(SInt64 offset, int origin)
|
nuclear@3
|
454 {
|
nuclear@3
|
455 if (BufferMode == ReadBuffer)
|
nuclear@3
|
456 {
|
nuclear@3
|
457 if (origin == Seek_Cur)
|
nuclear@3
|
458 {
|
nuclear@3
|
459 // Seek can fall either before or after Pos in the buffer,
|
nuclear@3
|
460 // but it must be within bounds.
|
nuclear@3
|
461 if (((unsigned(offset) + Pos)) <= DataSize)
|
nuclear@3
|
462 {
|
nuclear@3
|
463 Pos += (unsigned)offset;
|
nuclear@3
|
464 return SInt64(FilePos - DataSize + Pos);
|
nuclear@3
|
465 }
|
nuclear@3
|
466
|
nuclear@3
|
467 // Lightweight buffer "Flush". We do this to avoid an extra seek
|
nuclear@3
|
468 // back operation which would take place if we called FlushBuffer directly.
|
nuclear@3
|
469 origin = Seek_Set;
|
nuclear@3
|
470 offset = (SInt64)(FilePos - DataSize + Pos) + offset;
|
nuclear@3
|
471 Pos = DataSize = 0;
|
nuclear@3
|
472 }
|
nuclear@3
|
473 else if (origin == Seek_Set)
|
nuclear@3
|
474 {
|
nuclear@3
|
475 if (((UInt64)offset - (FilePos-DataSize)) <= DataSize)
|
nuclear@3
|
476 {
|
nuclear@3
|
477 Pos = (unsigned)((UInt64)offset - (FilePos-DataSize));
|
nuclear@3
|
478 return offset;
|
nuclear@3
|
479 }
|
nuclear@3
|
480 Pos = DataSize = 0;
|
nuclear@3
|
481 }
|
nuclear@3
|
482 else
|
nuclear@3
|
483 {
|
nuclear@3
|
484 FlushBuffer();
|
nuclear@3
|
485 }
|
nuclear@3
|
486 }
|
nuclear@3
|
487 else
|
nuclear@3
|
488 {
|
nuclear@3
|
489 FlushBuffer();
|
nuclear@3
|
490 }
|
nuclear@3
|
491
|
nuclear@3
|
492 /*
|
nuclear@3
|
493 OVR_ASSERT(BufferMode != NoBuffer);
|
nuclear@3
|
494
|
nuclear@3
|
495 if (origin == Seek_Cur && offset + Pos < DataSize)
|
nuclear@3
|
496 {
|
nuclear@3
|
497 Pos += int (offset);
|
nuclear@3
|
498 return FilePos - DataSize + Pos;
|
nuclear@3
|
499 }
|
nuclear@3
|
500 else if (origin == Seek_Set && offset >= SInt64(FilePos - DataSize) && offset < SInt64(FilePos))
|
nuclear@3
|
501 {
|
nuclear@3
|
502 Pos = unsigned(offset - FilePos + DataSize);
|
nuclear@3
|
503 return FilePos - DataSize + Pos;
|
nuclear@3
|
504 }
|
nuclear@3
|
505
|
nuclear@3
|
506 FlushBuffer();
|
nuclear@3
|
507 */
|
nuclear@3
|
508
|
nuclear@3
|
509 FilePos = pFile->LSeek(offset,origin);
|
nuclear@3
|
510 return FilePos;
|
nuclear@3
|
511 }
|
nuclear@3
|
512
|
nuclear@3
|
513 int BufferedFile::CopyFromStream(File *pstream, int byteSize)
|
nuclear@3
|
514 {
|
nuclear@3
|
515 // We can't rely on overridden Write()
|
nuclear@3
|
516 // because delegation doesn't override virtual pointers
|
nuclear@3
|
517 // So, just re-implement
|
nuclear@3
|
518 UByte buff[0x4000];
|
nuclear@3
|
519 int count = 0;
|
nuclear@3
|
520 int szRequest, szRead, szWritten;
|
nuclear@3
|
521
|
nuclear@3
|
522 while(byteSize)
|
nuclear@3
|
523 {
|
nuclear@3
|
524 szRequest = (byteSize > int(sizeof(buff))) ? int(sizeof(buff)) : byteSize;
|
nuclear@3
|
525
|
nuclear@3
|
526 szRead = pstream->Read(buff,szRequest);
|
nuclear@3
|
527 szWritten = 0;
|
nuclear@3
|
528 if (szRead > 0)
|
nuclear@3
|
529 szWritten = Write(buff,szRead);
|
nuclear@3
|
530
|
nuclear@3
|
531 count +=szWritten;
|
nuclear@3
|
532 byteSize-=szWritten;
|
nuclear@3
|
533 if (szWritten < szRequest)
|
nuclear@3
|
534 break;
|
nuclear@3
|
535 }
|
nuclear@3
|
536 return count;
|
nuclear@3
|
537 }
|
nuclear@3
|
538
|
nuclear@3
|
539 // Closing files
|
nuclear@3
|
540 bool BufferedFile::Close()
|
nuclear@3
|
541 {
|
nuclear@3
|
542 switch(BufferMode)
|
nuclear@3
|
543 {
|
nuclear@3
|
544 case WriteBuffer:
|
nuclear@3
|
545 FlushBuffer();
|
nuclear@3
|
546 break;
|
nuclear@3
|
547 case ReadBuffer:
|
nuclear@3
|
548 // No need to seek back on close
|
nuclear@3
|
549 BufferMode = NoBuffer;
|
nuclear@3
|
550 break;
|
nuclear@3
|
551 default:
|
nuclear@3
|
552 break;
|
nuclear@3
|
553 }
|
nuclear@3
|
554 return pFile->Close();
|
nuclear@3
|
555 }
|
nuclear@3
|
556
|
nuclear@3
|
557
|
nuclear@3
|
558 // ***** Global path helpers
|
nuclear@3
|
559
|
nuclear@3
|
560 // Find trailing short filename in a path.
|
nuclear@3
|
561 const char* OVR_CDECL GetShortFilename(const char* purl)
|
nuclear@3
|
562 {
|
nuclear@3
|
563 UPInt len = OVR_strlen(purl);
|
nuclear@3
|
564 for (UPInt i=len; i>0; i--)
|
nuclear@3
|
565 if (purl[i]=='\\' || purl[i]=='/')
|
nuclear@3
|
566 return purl+i+1;
|
nuclear@3
|
567 return purl;
|
nuclear@3
|
568 }
|
nuclear@3
|
569
|
nuclear@3
|
570 } // OVR
|
nuclear@3
|
571
|