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