bfiofile.cpp 39 KB

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