Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
bfiofile.cpp
Go to the documentation of this file.
1/*
2** Command & Conquer Generals Zero Hour(tm)
3** Copyright 2025 Electronic Arts Inc.
4**
5** This program is free software: you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation, either version 3 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19/***********************************************************************************************
20 *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
21 ***********************************************************************************************
22 * *
23 * Project Name : Command & Conquer *
24 * *
25 * $Archive:: /Commando/Library/BFIOFILE.CPP $*
26 * *
27 * $Author:: Greg_h $*
28 * *
29 * $Modtime:: 7/22/97 11:37a $*
30 * *
31 * $Revision:: 1 $*
32 * *
33 *---------------------------------------------------------------------------------------------*
34 * Functions: *
35 * BufferIOFileClass::BufferIOFileClass -- Filename based constructor for a file object. *
36 * BufferIOFileClass::BufferIOFileClass -- default constructor for a file object. *
37 * BufferIOFileClass::Cache -- Load part or all of a file data into RAM. *
38 * BufferIOFileClass::Close -- Perform a closure of the file. *
39 * BufferIOFileClass::Commit -- Writes the cache to the file if it has changed. *
40 * BufferIOFileClass::Free -- Frees the allocated buffer. *
41 * BufferIOFileClass::Is_Available -- Checks for existence of file cached or on disk. *
42 * BufferIOFileClass::Is_Open -- Determines if the file is open. *
43 * BufferIOFileClass::Open -- Assigns name and opens file in one operation. *
44 * BufferIOFileClass::Open -- Opens the file object with the rights specified. *
45 * BufferIOFileClass::Read -- Reads data from the file cache. *
46 * BufferIOFileClass::Seek -- Moves the current file pointer in the file. *
47 * BufferIOFileClass::Set_Name -- Checks for name changed for a cached file. *
48 * BufferIOFileClass::Size -- Determines size of file (in bytes). *
49 * BufferIOFileClass::Write -- Writes data to the file cache. *
50 * BufferIOFileClass::~BufferIOFileClass -- Destructor for the file object. *
51 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
52
53#include "always.h"
54#include "bfiofile.h"
55#include <string.h>
56
57
58/***********************************************************************************************
59 * BufferIOFileClass::BufferIOFileClass -- Filename based constructor for a file object. *
60 * *
61 * This constructor is called when a file object is created with a supplied filename, but *
62 * not opened at the same time. In this case, an assumption is made that the supplied *
63 * filename is a constant string. A duplicate of the filename string is not created since *
64 * it would be wasteful in that case. *
65 * *
66 * INPUT: filename -- The filename to assign to this file object. *
67 * *
68 * OUTPUT: none *
69 * *
70 * WARNINGS: none *
71 * *
72 * HISTORY: *
73 * 11/10/1995 DRD : Created. *
74 *=============================================================================================*/
76 IsAllocated(false),
77 IsOpen(false),
78 IsDiskOpen(false),
79 IsCached(false),
80 IsChanged(false),
81 UseBuffer(false),
82 BufferRights(0),
83 Buffer(0),
84 BufferSize(0),
85 BufferPos(0),
86 BufferFilePos(0),
87 BufferChangeBeg(-1),
88 BufferChangeEnd(-1),
89 FileSize(0),
90 FilePos(0),
91 TrueFileStart(0)
92{
94}
95
96
97/***********************************************************************************************
98 * BufferIOFileClass::BufferIOFileClass -- default constructor for a file object. *
99 * *
100 * This is the default constructor for a file object. *
101 * *
102 * INPUT: none *
103 * *
104 * OUTPUT: none *
105 * *
106 * WARNINGS: none *
107 * *
108 * HISTORY: *
109 * 11/10/1995 DRD : Created. *
110 *=============================================================================================*/
112 IsAllocated(false),
113 IsOpen(false),
114 IsDiskOpen(false),
115 IsCached(false),
116 IsChanged(false),
117 UseBuffer(false),
118 BufferRights(0),
119 Buffer(0),
120 BufferSize(0),
121 BufferPos(0),
122 BufferFilePos(0),
123 BufferChangeBeg(-1),
124 BufferChangeEnd(-1),
125 FileSize(0),
126 FilePos(0),
127 TrueFileStart(0)
128{
129}
130
131
132/***********************************************************************************************
133 * BufferIOFileClass::~BufferIOFileClass -- Destructor for the file object. *
134 * *
135 * This destructor will free all memory allocated thru using Cache routines. *
136 * *
137 * INPUT: none *
138 * *
139 * OUTPUT: none *
140 * *
141 * WARNINGS: none *
142 * *
143 * HISTORY: *
144 * 11/10/1995 DRD : Created. *
145 *=============================================================================================*/
150
151
152/***********************************************************************************************
153 * BufferIOFileClass::Cache -- Load part or all of a file data into RAM. *
154 * *
155 * INPUT: none *
156 * *
157 * OUTPUT: bool; Was the file load successful? It could fail if there wasn't enough room *
158 * to allocate the raw data block. *
159 * *
160 * WARNINGS: This routine goes to disk for a potentially very long time. *
161 * *
162 * HISTORY: *
163 * 11/10/1995 DRD : Created. *
164 *=============================================================================================*/
165bool BufferIOFileClass::Cache( long size, void * ptr )
166{
167 if (Buffer) {
168 //
169 // if trying to cache again with size or ptr fail
170 //
171 if (size || ptr) {
172 return( false);
173 } else {
174 return( true);
175 }
176 }
177
178 if (Is_Available()) {
179 FileSize = Size();
180 } else {
181 FileSize = 0;
182 }
183
184 if (size) {
185 //
186 // minimum buffer size for performance
187 //
188 if (size < MINIMUM_BUFFER_SIZE) {
189 size = MINIMUM_BUFFER_SIZE;
190
191 /*
192 ** Specifying a size smaller than the minimum is an error
193 ** IF a buffer pointer was also specified. In such a case the
194 ** system cannot use the buffer.
195 */
196 if (ptr) {
197 Error(EINVAL);
198 }
199 }
200
201 BufferSize = size;
202 } else {
203 BufferSize = FileSize;
204 }
205
206 //
207 // if size == 0 and a ptr to a buffer is specified then that is invalid.
208 // if the BufferSize is 0 then this must be a new file and no size was
209 // specified so exit.
210 //
211 if ((size == 0 && ptr) || !BufferSize) {
212 return( false);
213 }
214
215 if (ptr) {
216 Buffer = ptr;
217 } else {
218 Buffer = new char [BufferSize];
219 }
220
221 if (Buffer) {
222 IsAllocated = true;
223 IsDiskOpen = false;
224 BufferPos = 0;
225 BufferFilePos = 0;
226 BufferChangeBeg = -1;
227 BufferChangeEnd = -1;
228 FilePos = 0;
229 TrueFileStart = 0;
230
231 //
232 // the file was checked for availability then set the FileSize
233 //
234 if (FileSize) {
235 long readsize;
236 int opened = false;
237 long prevpos = 0;
238
239
240 if (FileSize <= BufferSize) {
241 readsize = FileSize;
242 } else {
243 readsize = BufferSize;
244 }
245
246 if (Is_Open()) {
247 //
248 // get previous file position
249 //
250 prevpos = Seek(0);
251
252 //
253 // get true file position
254 //
255 if (BASECLASS::Is_Open()) {
256 TrueFileStart = BASECLASS::Seek(0);
257 } else {
258 TrueFileStart = prevpos;
259 }
260
261 if (FileSize <= BufferSize) {
262 //
263 // if previous position is non-zero seek to the beginning
264 //
265 if (prevpos) {
266 Seek(0, SEEK_SET);
267 }
268
269 //
270 // set the buffer position for future reads/writes
271 //
272 BufferPos = prevpos;
273 } else {
274 BufferFilePos = prevpos;
275 }
276
277 FilePos = prevpos;
278 } else {
279 if (Open()) {
280 TrueFileStart = BASECLASS::Seek(0);
281 opened = true;
282 }
283 }
284
285 long actual = Read(Buffer, readsize);
286
287 if (actual != readsize) {
288 Error(EIO);
289 }
290
291 if (opened) {
292 Close();
293 } else {
294 //
295 // seek to the previous position in the file
296 //
297 Seek(prevpos, SEEK_SET);
298 }
299
300 IsCached = true;
301 }
302
303 UseBuffer = true;
304 return(true);
305 }
306
307 Error(ENOMEM);
308
309 return(false);
310}
311
312
313/***********************************************************************************************
314 * BufferIOFileClass::Free -- Frees the allocated buffer. *
315 * *
316 * This routine will free the buffer. By using this in conjunction with the *
317 * Cache() function, one can maintain tight control of memory usage. *
318 * *
319 * INPUT: none *
320 * *
321 * OUTPUT: none *
322 * *
323 * WARNINGS: none *
324 * *
325 * HISTORY: *
326 * 11/10/1995 DRD : Created. *
327 *=============================================================================================*/
329{
330 if (Buffer) {
331 if (IsAllocated) {
332 delete [] Buffer;
333 IsAllocated = false;
334 }
335
336 Buffer = 0;
337 }
338
339 BufferSize = 0;
340 IsOpen = false;
341 IsCached = false;
342 IsChanged = false;
343 UseBuffer = false;
344}
345
346
347/***********************************************************************************************
348 * BufferIOFileClass::Commit -- Writes the cache to the file if it has changed. *
349 * *
350 * *
351 * INPUT: none *
352 * *
353 * OUTPUT: false, did not need to write the buffer. *
354 * true, wrote the buffer. *
355 * *
356 * WARNINGS: none *
357 * *
358 * HISTORY: *
359 * 11/15/1995 DRD : Created. *
360 *=============================================================================================*/
362{
363 long size;
364
365
366 if (UseBuffer) {
367 if (IsChanged) {
368 size = BufferChangeEnd - BufferChangeBeg;
369
370 if (IsDiskOpen) {
371 BASECLASS::Seek( TrueFileStart + BufferFilePos + BufferChangeBeg, SEEK_SET);
372 BASECLASS::Write( Buffer, size);
373 BASECLASS::Seek( TrueFileStart + FilePos, SEEK_SET);
374 } else {
376 BASECLASS::Seek( TrueFileStart + BufferFilePos + BufferChangeBeg, SEEK_SET);
377 BASECLASS::Write( Buffer, size);
379 }
380
381 IsChanged = false;
382 return( true);
383 } else {
384 return( false);
385 }
386 } else {
387 return( false);
388 }
389}
390
391
392/***********************************************************************************************
393 * BufferIOFileClass::Set_Name -- Checks for name changed for a cached file. *
394 * *
395 * Checks for a previous filename and that it is cached. If so, then check the *
396 * new filename against the old. If they are the same then return that filename. *
397 * Otherwise, the file object's name is set with just the raw filename as passed *
398 * to this routine. *
399 * *
400 * INPUT: filename -- Pointer to the filename to set as the name of this file object. *
401 * *
402 * OUTPUT: Returns a pointer to the final and complete filename of this file object. This *
403 * may have a path attached to the file. *
404 * *
405 * WARNINGS: none *
406 * *
407 * HISTORY: *
408 * 11/15/1995 DRD : Created. *
409 *=============================================================================================*/
410char const * BufferIOFileClass::Set_Name(char const * filename)
411{
412 if (File_Name() && UseBuffer) {
413 if (strcmp(filename, File_Name() ) == 0) {
414 return( File_Name());
415 } else {
416 Commit();
417 IsCached = false;
418 }
419 }
420
421 BASECLASS::Set_Name(filename);
422 return( File_Name());
423}
424
425
426/***********************************************************************************************
427 * BufferIOFileClass::Is_Available -- Checks for existence of file cached or on disk. *
428 * *
429 * *
430 * INPUT: none *
431 * *
432 * OUTPUT: bool; Is the file available for opening? *
433 * *
434 * WARNINGS: none *
435 * *
436 * HISTORY: *
437 * 11/16/1995 DRD : Created. *
438 *=============================================================================================*/
440{
441 if (UseBuffer) {
442 return(true);
443 }
444
445 return(BASECLASS::Is_Available());
446}
447
448
449/***********************************************************************************************
450 * BufferIOFileClass::Is_Open -- Determines if the file is open. *
451 * *
452 * If part or all of the file is cached, then return that it is opened. A closed file *
453 * doesn't have a valid pointer. *
454 * *
455 * INPUT: none *
456 * *
457 * OUTPUT: bool; Is the file open? *
458 * *
459 * WARNINGS: none *
460 * *
461 * HISTORY: *
462 * 11/14/1995 DRD : Created. *
463 *=============================================================================================*/
465{
466 if (IsOpen && UseBuffer) {
467 return( true);
468 }
469
470 return(BASECLASS::Is_Open());
471}
472
473
474/***********************************************************************************************
475 * BufferIOFileClass::Open -- Assigns name and opens file in one operation. *
476 * *
477 * This routine will assign the specified filename to the file object and open it at the *
478 * same time. If the file object was already open, then it will be closed first. If the *
479 * file object was previously assigned a filename, then it will be replaced with the new *
480 * name. Typically, this routine is used when an anonymous file object has been crated and *
481 * now it needs to be assigned a name and opened. *
482 * *
483 * INPUT: filename -- The filename to assign to this file object. *
484 * *
485 * rights -- The open file access rights to use. *
486 * *
487 * OUTPUT: bool; Was the file opened? The return value of this is moot, since the open file *
488 * is designed to never return unless it succeeded. *
489 * *
490 * WARNINGS: none *
491 * *
492 * HISTORY: *
493 * 11/14/1995 DRD : Created. *
494 *=============================================================================================*/
495int BufferIOFileClass::Open(char const * filename, int rights)
496{
497 Set_Name(filename);
498 return( BufferIOFileClass::Open( rights ));
499}
500
501
502/***********************************************************************************************
503 * BufferIOFileClass::Open -- Opens the file object with the rights specified. *
504 * *
505 * This routine is used to open the specified file object with the access rights indicated. *
506 * This only works if the file has already been assigned a filename. It is guaranteed, by *
507 * the error handler, that this routine will always return with success. *
508 * *
509 * INPUT: rights -- The file access rights to use when opening this file. This is a *
510 * combination of READ and/or WRITE bit flags. *
511 * *
512 * OUTPUT: bool; Was the file opened successfully? This will always return true by reason of *
513 * the error handler. *
514 * *
515 * WARNINGS: none *
516 * *
517 * HISTORY: *
518 * 11/14/1995 DRD : Created. *
519 *=============================================================================================*/
521{
523
524 if (UseBuffer) {
525
526 BufferRights = rights; // save rights requested for checks later
527
528 if (rights != READ ||
529 (rights == READ && FileSize > BufferSize)) {
530
531 if (rights == WRITE) {
532 BASECLASS::Open(rights);
534 rights = READ | WRITE;
535 TrueFileStart = 0; // now writing to single file
536 }
537
538 if (TrueFileStart) {
539 UseBuffer = false;
540 Open( rights);
541 UseBuffer = true;
542 } else {
543 BASECLASS::Open( rights);
544 }
545
546 IsDiskOpen = true;
547
548 if (BufferRights == WRITE) {
549 FileSize = 0;
550 }
551
552 } else {
553 IsDiskOpen = false;
554 }
555
556 BufferPos = 0;
557 BufferFilePos = 0;
558 BufferChangeBeg = -1;
559 BufferChangeEnd = -1;
560 FilePos = 0;
561 IsOpen = true;
562 } else {
563 BASECLASS::Open( rights);
564 }
565
566 return( true);
567}
568
569
570/***********************************************************************************************
571 * BufferIOFileClass::Write -- Writes data to the file cache. *
572 * *
573 * *
574 * INPUT: buffer -- Pointer to the buffer that holds the data to be written. *
575 * *
576 * size -- The number of bytes to write. *
577 * *
578 * OUTPUT: Returns the number of bytes actually written. *
579 * *
580 * WARNINGS: none *
581 * *
582 * HISTORY: *
583 * 11/15/1995 DRD : Created. *
584 *=============================================================================================*/
585int BufferIOFileClass::Write(void const * buffer, int size)
586{
587 int opened = false;
588
589 if (!Is_Open()) {
590 if (!Open(WRITE)) {
591 return(0);
592 }
593 TrueFileStart = BASECLASS::Seek(0);
594 opened = true;
595 }
596
597 if (UseBuffer) {
598 int sizewritten = 0;
599
600 if (BufferRights != READ) {
601 while (size) {
602 int sizetowrite;
603
604 if (size >= (BufferSize - BufferPos)) {
605 sizetowrite = (BufferSize - BufferPos);
606 } else {
607 sizetowrite = size;
608 }
609
610 if (sizetowrite != BufferSize) {
611
612 if (!IsCached) {
613 int readsize;
614
615 if (FileSize < BufferSize) {
616 readsize = FileSize;
617 BufferFilePos = 0;
618 } else {
619 readsize = BufferSize;
620 BufferFilePos = FilePos;
621 }
622
623 if (TrueFileStart) {
624 UseBuffer = false;
625 Seek( FilePos, SEEK_SET);
626 Read( Buffer, BufferSize);
627 Seek( FilePos, SEEK_SET);
628 UseBuffer = true;
629 } else {
630 BASECLASS::Seek( BufferFilePos, SEEK_SET);
631 BASECLASS::Read( Buffer, readsize);
632 }
633
634 BufferPos = 0;
635 BufferChangeBeg = -1;
636 BufferChangeEnd = -1;
637
638 IsCached = true;
639 }
640 }
641
642 memmove((char *)Buffer + BufferPos, (char *)buffer + sizewritten, sizetowrite);
643
644 IsChanged = true;
645 sizewritten += sizetowrite;
646 size -= sizetowrite;
647
648 if (BufferChangeBeg == -1) {
649 BufferChangeBeg = BufferPos;
650 BufferChangeEnd = BufferPos;
651 } else {
652 if (BufferChangeBeg > BufferPos) {
653 BufferChangeBeg = BufferPos;
654 }
655 }
656
657 BufferPos += sizetowrite;
658
659 if (BufferChangeEnd < BufferPos) {
660 BufferChangeEnd = BufferPos;
661 }
662
663 FilePos = BufferFilePos + BufferPos;
664
665 if (FileSize < FilePos) {
666 FileSize = FilePos;
667 }
668
669 //
670 // end of buffer reached?
671 //
672 if (BufferPos == BufferSize) {
673 Commit();
674
675 BufferPos = 0;
676 BufferFilePos = FilePos;
677 BufferChangeBeg = -1;
678 BufferChangeEnd = -1;
679
680 if (size && FileSize > FilePos) {
681 if (TrueFileStart) {
682 UseBuffer = false;
683 Seek( FilePos, SEEK_SET);
684 Read( Buffer, BufferSize);
685 Seek( FilePos, SEEK_SET);
686 UseBuffer = true;
687 } else {
688 BASECLASS::Seek( FilePos, SEEK_SET);
689 BASECLASS::Read( Buffer, BufferSize);
690 }
691 } else {
692 IsCached = false;
693 }
694 }
695 }
696 } else {
697 Error(EACCES);
698 }
699
700 size = sizewritten;
701 } else {
702 size = BASECLASS::Write(buffer, size);
703 }
704
705 if (opened) {
706 Close();
707 }
708
709 return( size);
710}
711
712
713/***********************************************************************************************
714 * BufferIOFileClass::Read -- Reads data from the file cache. *
715 * *
716 * *
717 * INPUT: buffer -- Pointer to the buffer to place the read data. *
718 * *
719 * size -- The number of bytes to read. *
720 * *
721 * OUTPUT: Returns the actual number of bytes read (this could be less than requested). *
722 * *
723 * WARNINGS: none *
724 * *
725 * HISTORY: *
726 * 11/15/1995 DRD : Created. *
727 *=============================================================================================*/
728int BufferIOFileClass::Read(void * buffer, int size)
729{
730 int opened = false;
731
732 if (!Is_Open()) {
733 if (Open()) {
734 TrueFileStart = BASECLASS::Seek(0);
735 opened = true;
736 }
737 }
738
739 if (UseBuffer) {
740 long sizeread = 0;
741
742 if (BufferRights != WRITE) {
743 while (size) {
744 long sizetoread;
745
746 if (size >= (BufferSize - BufferPos)) {
747 sizetoread = (BufferSize - BufferPos);
748 } else {
749 sizetoread = size;
750 }
751
752 if (!IsCached) {
753 long readsize;
754
755 if (FileSize < BufferSize) {
756 readsize = FileSize;
757 BufferFilePos = 0;
758 } else {
759 readsize = BufferSize;
760 BufferFilePos = FilePos;
761 }
762
763 if (TrueFileStart) {
764 UseBuffer = false;
765 Seek( FilePos, SEEK_SET);
766 Read( Buffer, BufferSize);
767 Seek( FilePos, SEEK_SET);
768 UseBuffer = true;
769 } else {
770 BASECLASS::Seek( BufferFilePos, SEEK_SET);
771 BASECLASS::Read( Buffer, readsize);
772 }
773
774 BufferPos = 0;
775 BufferChangeBeg = -1;
776 BufferChangeEnd = -1;
777
778 IsCached = true;
779 }
780
781 memmove((char *)buffer + sizeread, (char *)Buffer + BufferPos, sizetoread);
782
783 sizeread += sizetoread;
784 size -= sizetoread;
785 BufferPos += sizetoread;
786 FilePos = BufferFilePos + BufferPos;
787
788 //
789 // end of buffer reached?
790 //
791 if (BufferPos == BufferSize) {
792 Commit();
793
794 BufferPos = 0;
795 BufferFilePos = FilePos;
796 BufferChangeBeg = -1;
797 BufferChangeEnd = -1;
798
799 if (size && FileSize > FilePos) {
800 if (TrueFileStart) {
801 UseBuffer = false;
802 Seek( FilePos, SEEK_SET);
803 Read( Buffer, BufferSize);
804 Seek( FilePos, SEEK_SET);
805 UseBuffer = true;
806 } else {
807 BASECLASS::Seek( FilePos, SEEK_SET);
808 BASECLASS::Read( Buffer, BufferSize);
809 }
810 } else {
811 IsCached = false;
812 }
813 }
814 }
815 } else {
816 Error(EACCES);
817 }
818
819 size = sizeread;
820 } else {
821 size = BASECLASS::Read(buffer, size);
822 }
823
824 if (opened) {
825 Close();
826 }
827
828 return( size);
829}
830
831
832/***********************************************************************************************
833 * BufferIOFileClass::Seek -- Moves the current file pointer in the file. *
834 * *
835 * This routine will change the current file pointer to the position specified. It follows *
836 * the same rules the a normal Seek() does, but if the file is part of the mixfile system, *
837 * then only the position value needs to be updated. *
838 * *
839 * INPUT: pos -- The position to move the file to relative to the position indicated *
840 * by the "dir" parameter. *
841 * *
842 * dir -- The direction to affect the position change against. This can be *
843 * either SEEK_CUR, SEEK_END, or SEEK_SET. *
844 * *
845 * OUTPUT: Returns with the position of the new location. *
846 * *
847 * WARNINGS: none *
848 * *
849 * HISTORY: *
850 * 11/15/1995 DRD : Created. *
851 *=============================================================================================*/
853{
854 if (UseBuffer) {
855 bool adjusted = false;
856
857 switch (dir) {
858 case SEEK_END:
859 FilePos = FileSize;
860 break;
861
862 case SEEK_SET:
863 FilePos = 0;
864 break;
865
866 case SEEK_CUR:
867 default:
868 break;
869 }
870
871 if (TrueFileStart) {
872 if (pos >= TrueFileStart) {
873 pos -= TrueFileStart;
874 adjusted = true;
875 }
876 }
877
878 FilePos += pos;
879
880 if (FilePos < 0) {
881 FilePos = 0;
882 }
883
884 if (FilePos > FileSize) {
885 FilePos = FileSize;
886 }
887
888 if (FileSize <= BufferSize) {
889 BufferPos = FilePos;
890 } else {
891 if (FilePos >= BufferFilePos &&
892 FilePos < (BufferFilePos + BufferSize)) {
893 BufferPos = FilePos - BufferFilePos;
894 } else {
895 Commit();
896// check!!
897 if (TrueFileStart) {
898 UseBuffer = false;
899 Seek(FilePos, SEEK_SET);
900 UseBuffer = true;
901 } else {
902 BASECLASS::Seek(FilePos, SEEK_SET);
903 }
904
905 IsCached = false;
906 }
907 }
908
909 if (TrueFileStart && adjusted) {
910 return( FilePos + TrueFileStart);
911 }
912
913 return( FilePos);
914 }
915
916 return( BASECLASS::Seek(pos, dir));
917}
918
919
920/***********************************************************************************************
921 * BufferIOFileClass::Size -- Determines size of file (in bytes). *
922 * *
923 * If part or all of the file is cached, then the size of the file is already *
924 * determined and available. Otherwise, go to the low level system to find the file *
925 * size. *
926 * *
927 * INPUT: none *
928 * *
929 * OUTPUT: Returns with the number of bytes in the file. *
930 * *
931 * WARNINGS: none *
932 * *
933 * HISTORY: *
934 * 11/14/1995 DRD : Created. *
935 *=============================================================================================*/
937{
938 if (IsOpen && UseBuffer) {
939 return( FileSize);
940 }
941
942 return( BASECLASS::Size());
943}
944
945
946/***********************************************************************************************
947 * BufferIOFileClass::Close -- Perform a closure of the file. *
948 * *
949 * Call Commit() to write the buffer if the file is cached and the buffer has changed, *
950 * then call lower level Close(). *
951 * *
952 * INPUT: none *
953 * *
954 * OUTPUT: none *
955 * *
956 * WARNINGS: none *
957 * *
958 * HISTORY: *
959 * 11/14/1995 DRD : Created. *
960 *=============================================================================================*/
962{
963 if (UseBuffer) {
964 Commit();
965
966 if (IsDiskOpen) {
967
968 if (TrueFileStart) {
969 UseBuffer = false;
970 Close();
971 UseBuffer = true;
972 } else {
974 }
975
976 IsDiskOpen = false;
977 }
978
979 IsOpen = false;
980 } else {
982 }
983}
984
#define SEEK_SET
Definition WWFILE.H:55
#define SEEK_CUR
Definition WWFILE.H:56
#define SEEK_END
Definition WWFILE.H:57
@ false
Definition bool.h:59
char dir[_MAX_DIR]
Definition autorun.cpp:215
virtual int Write(void const *buffer, int size)
Definition bfiofile.cpp:585
virtual bool Is_Available(int forced=false)
Definition bfiofile.cpp:439
virtual int Read(void *buffer, int size)
Definition bfiofile.cpp:728
virtual int Seek(int pos, int dir=SEEK_CUR)
Definition bfiofile.cpp:852
void Free(void)
Definition bfiofile.cpp:328
virtual int Open(char const *filename, int rights=READ)
Definition bfiofile.cpp:495
virtual ~BufferIOFileClass(void)
Definition bfiofile.cpp:146
bool Commit(void)
Definition bfiofile.cpp:361
virtual void Close(void)
Definition bfiofile.cpp:961
bool Cache(long size=0, void *ptr=NULL)
Definition bfiofile.cpp:165
virtual int Size(void)
Definition bfiofile.cpp:936
virtual bool Is_Open(void) const
Definition bfiofile.cpp:464
virtual char const * Set_Name(char const *filename)
Definition bfiofile.cpp:410
@ WRITE
Definition WWFILE.H:72
virtual char const * Set_Name(char const *filename)
Definition rawfile.cpp:313
virtual bool Is_Available(int forced=false)
Definition rawfile.cpp:489
virtual int Open(char const *filename, int rights=READ)
Definition rawfile.cpp:356
virtual int Seek(int pos, int dir=SEEK_CUR)
Definition rawfile.cpp:773
virtual char const * File_Name(void) const
Definition RAWFILE.H:186
virtual int Size(void)
Definition rawfile.cpp:842
virtual bool Is_Open(void) const
Definition rawfile.cpp:176
virtual int Write(void const *buffer, int size)
Definition rawfile.cpp:698
virtual void Close(void)
Definition rawfile.cpp:561
virtual void Error(int error, int canretry=false, char const *filename=NULL)
Definition rawfile.cpp:204
virtual int Read(void *buffer, int size)
Definition rawfile.cpp:614