FILE.CPP 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  1. /*
  2. ** Command & Conquer Red Alert(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 A S S O C I A T E S **
  20. ***************************************************************************
  21. * *
  22. * Project Name : Library *
  23. * *
  24. * File Name : FILE.CPP *
  25. * *
  26. * Programmer : Scott K. Bowen *
  27. * *
  28. * Start Date : September 13, 1993 *
  29. * *
  30. * Last Update : September 13, 1993 [SKB] *
  31. * *
  32. *-------------------------------------------------------------------------*
  33. * Functions: *
  34. * File_Exists -- Searches for and FINDS the specified file. *
  35. * File_Size -- Determine the size of the specified file. *
  36. * Open_File -- Opens a file for access. *
  37. * Close_File -- Closes a file previously opened with Open_File. *
  38. * Seek_File -- Adjusts the position of the file pointer. *
  39. * Read_File -- Reads a block of data from a file. *
  40. * Write_File -- Writes a block of data to a file. *
  41. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  42. #ifndef WWSTD_H
  43. #include <wwstd.h>
  44. #endif
  45. #ifndef FILE_H
  46. #include "_file.h"
  47. #endif
  48. #ifndef WWMEM_H
  49. #include <wwmem.h>
  50. #endif
  51. #include <fcntl.h>
  52. #include <io.h>
  53. #include <sys\stat.h>
  54. #define DEBUGPRINT FALSE
  55. /*=========================================================================*/
  56. /* The following PRIVATE functions are in this file: */
  57. /*=========================================================================*/
  58. /*= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =*/
  59. /***************************************************************************
  60. * OPEN_FILE -- Opens a file for access. *
  61. * *
  62. * This routine will open the specified file for access. It functions *
  63. * in a similar manner to the regular DOS open command. *
  64. * *
  65. * INPUT: filename -- Pointer to the filename to open. *
  66. * *
  67. * mode -- Access mode to use. *
  68. * READ: A file opened for READ access, MUST be available. *
  69. * This may cause a disk swap message to appear. *
  70. * *
  71. * WRITE:A file opened for WRITE access (only), need not be *
  72. * available. If it can't be found, then it will be *
  73. * created in the current directory. *
  74. * *
  75. * OUTPUT: Returns with the westwood file handle. If ERROR is returned *
  76. * it means that the programmer took over the file error handler *
  77. * and signaled that an open access failure should not keep trying*
  78. * for success. *
  79. * *
  80. * WARNINGS: If you take over the file error handling code, you must be *
  81. * sure to anticipate a possible ERROR value being returned. *
  82. * If you open a file for READ or READ|WRITE, then the file *
  83. * must previously exist. To create a file for read and write *
  84. * access, first open it for WRITE, then re-open it for *
  85. * read and write. *
  86. * *
  87. * HISTORY: *
  88. * 11/11/1991 JLB : Created. *
  89. *=========================================================================*/
  90. int cdecl Open_File(char const *file_name, int mode)
  91. {
  92. int file_handle; // Westwood file handle.
  93. int handle; // DOS file handle.
  94. unsigned int local_mode; // DOS access mode number.
  95. int index; // FileData index (if available).
  96. BOOL immediate; // Is the file immediately available?
  97. FileDataType *filedata; // Pointer to the current FileData.
  98. FileDataType *parent; // Pointer to the current FileData.
  99. FileHandleType *filehandletable; // Pointer to the current file handle.
  100. CallingDOSInt++;
  101. #if(DEBUGPRINT)
  102. Mono_Printf("Open_File('%s')\r", file_name);
  103. #endif
  104. /*
  105. ** Is there a slot in the FileHandleTable? If not, then exit with
  106. ** a file error.
  107. */
  108. filehandletable = FileHandleTable;
  109. for (file_handle = 0; file_handle < TABLE_MAX; file_handle++, filehandletable++) {
  110. if (filehandletable->Empty) break;
  111. }
  112. if (file_handle == TABLE_MAX) {
  113. Do_IO_Error(TOO_MANY_FILES, file_name);
  114. }
  115. /*
  116. ** Find the file in the FileTable (if it exists there).
  117. */
  118. index = Find_File_Index(file_name);
  119. filedata = &FileDataPtr[index];
  120. // Start out looking at the start drive.
  121. ibm_setdisk(*StartPath - 'A');
  122. /*
  123. ** If the file exists in the current directory, then by all
  124. ** means, use that file instead of any corresponding pack
  125. ** file representation. This is used as a means of
  126. ** overriding packed files (quite handy).
  127. */
  128. immediate = FALSE;
  129. /*
  130. ** Check the current directory by attempting to open with READ access.
  131. ** Only check if the file is not already cached and it is a packed file.
  132. */
  133. if ((index != ERROR) && (filedata->Flag & FILEF_PACKED) && (!filedata->Ptr))
  134. {
  135. int handle2;
  136. handle2 = FILEOPEN(file_name, MODE_OLDFILE);
  137. if (handle2 != FILEOPENERROR) {
  138. FILECLOSE(handle2);
  139. immediate = TRUE;
  140. }
  141. }
  142. /*
  143. ** Check to see if any WRITE permission is requested for the
  144. ** file. If so, delete the file from RAM and mark it as non-resident.
  145. **
  146. ** NOTE: If the file is packed, we CAN NOT alter it in ANY way.
  147. ** We will return an ERROR to the user, and hope he/she is
  148. ** smart enough to handle it.
  149. */
  150. if ((mode & WRITE) && index != ERROR && filedata->Ptr) {
  151. if(filedata->Flag & FILEF_PACKED) { // is file packed?
  152. Mem_Free(FileCacheHeap, filedata->Ptr);
  153. filedata->Ptr = NULL;
  154. } else { // file is packed
  155. Do_IO_Error(WRITING_RESIDENT, file_name);
  156. }
  157. }
  158. // If the file is cached already, then let us use it and leave.
  159. if (filedata->Ptr && index != ERROR) {
  160. /*
  161. ** File is resident. Initialize the local table.
  162. */
  163. filehandletable->Index = index;
  164. filehandletable->Empty = FALSE;
  165. filehandletable->Pos = 0L;
  166. filehandletable->Name = filedata->Name;
  167. filehandletable->Handle = 0; // Resident flag.
  168. filehandletable->Mode = mode;
  169. // If the cached file is packed and its parent is resident, then
  170. // add one to the parent open count.
  171. if (filedata->Flag & FILEF_PACKED) {
  172. parent = &FileDataPtr[filedata->Disk];
  173. }
  174. else {
  175. parent = NULL;
  176. }
  177. if (parent && (parent->Ptr == filedata->Ptr)) {
  178. filehandletable->Start = filedata->Start;
  179. parent->OpenCount++;
  180. } else {
  181. filehandletable->Start = 0;
  182. }
  183. // Mark the pointer in use so that it is never returned as the oldest
  184. // pointer location in memory.
  185. Mem_In_Use(filedata->Ptr);
  186. } else {
  187. /*
  188. ** At this time we determine if it is a packed file. If so, the
  189. ** opening process takes a dramatically different turn.
  190. */
  191. if (index != ERROR && (filedata->Flag & FILEF_PACKED) && !immediate && (mode & WRITE) == 0) {
  192. /*
  193. ** This is a packed file. Perform recursive open process to reach
  194. ** the parent packed file.
  195. */
  196. parent = &FileDataPtr[filedata->Disk];
  197. file_handle = Open_File(parent->Name, mode);
  198. // Reread the parents information.
  199. parent = &FileDataPtr[filedata->Disk];
  200. filehandletable = &FileHandleTable[file_handle];
  201. /*
  202. ** Process the packed file header. Update the start offset for every
  203. ** file ellaborated in the packed file.
  204. ** Exception: When a file is specified in the packed file and an
  205. ** entry exists for it in the file table, BUT the entry
  206. ** is not marked as packed, THEN ignore the packed version
  207. ** of the file.
  208. */
  209. if (FileData == FileDataPtr &&
  210. !(FileDataPtr[filehandletable->Index].Flag & FILEF_PROCESSED)) {
  211. long offset; // Offset of sub-file start.
  212. int i; // Sub-file FileData index.
  213. char name[FILENAMESIZE]; // Name of sub-file.
  214. FileDataType *cur=NULL; // Current entry in FileData.
  215. FileDataType *last=NULL; // Last entry in FileData.
  216. #if(DEBUGPRINT)
  217. Mono_Printf("Processing packed file '%s'\r", FileDataPtr[filedata->Disk].Name);
  218. #endif
  219. Read_File(file_handle, &offset, (unsigned long)sizeof(long));
  220. while (offset) {
  221. /*
  222. ** Due to the fact that the embedded file name is of arbitrary
  223. ** length, we must read it in a byte at a time. Reading stops
  224. ** when the terminating NULL is found.
  225. */
  226. i=0;
  227. do {
  228. Read_File(file_handle, &name[i], (unsigned long)sizeof(name[0]));
  229. } while (name[i++]);
  230. i = Find_File_Index(name);
  231. if (i != ERROR) {
  232. cur = &FileDataPtr[i];
  233. if ((cur->Flag & FILEF_PACKED) && cur->Disk == filehandletable->Index) {
  234. cur->Start = offset + FileDataPtr[ filehandletable->Index ].Start;
  235. if (last != NULL) {
  236. last->Size = cur->Start - last->Start;
  237. }
  238. last = cur;
  239. }
  240. } else {
  241. // ERROR: File specified in packed file has no table entry.
  242. // When this occurs, the embedded file is ignored.
  243. }
  244. Read_File(file_handle, &offset, (unsigned long)sizeof(long));
  245. }
  246. /*
  247. ** Fixup the size record of the last embedded file.
  248. */
  249. if (last != NULL) {
  250. last->Size = File_Size(file_handle) - last->Start;
  251. }
  252. FileDataPtr[filehandletable->Index].Flag |= FILEF_PROCESSED;
  253. }
  254. // if the parent is resident, the the child must have the same address.
  255. filedata->Ptr = parent->Ptr;
  256. /*
  257. ** Set starting position to start of embedded file.
  258. */
  259. filehandletable->Index = index;
  260. filehandletable->Name = filedata->Name;
  261. filehandletable->Start = filedata->Start;
  262. Seek_File(file_handle, NULL, SEEK_SET);
  263. // Attempt to cache the file if wanted, and room.
  264. Cache_File(index, file_handle);
  265. } else {
  266. /*
  267. ** Convert the Westwood access mode into the system specific mode.
  268. */
  269. local_mode = MODE_OLDFILE;
  270. switch (mode) {
  271. case READ:
  272. local_mode = MODE_OLDFILE;
  273. break;
  274. case READ | WRITE:
  275. #if(IBM)
  276. local_mode = MODE_READWRITE;
  277. #else
  278. local_mode = MODE_OLDFILE;
  279. #endif
  280. break;
  281. case WRITE:
  282. local_mode = MODE_NEWFILE;
  283. break;
  284. default:
  285. Do_IO_Error(BAD_OPEN_MODE, file_name);
  286. break;
  287. }
  288. /*
  289. ** The file is disk based. Perform the DOS open processing.
  290. */
  291. /*
  292. ** Make sure the file is available or the proper disk is inserted.
  293. ** The file MUST exist if READ access is requested. A mild attempt
  294. ** will be made to find the file if open for WRITE only.
  295. */
  296. if (mode & READ) {
  297. if (!File_Exists(file_name)) {
  298. CallingDOSInt--;
  299. return(ERROR); /* Ignore the error */
  300. }
  301. }
  302. /*
  303. ** Repetitively try to open the file until the error handler
  304. ** routine indicates otherwise.
  305. */
  306. handle = FILEOPENERROR;
  307. for (;;) {
  308. handle = Open_File_With_Recovery( file_name, local_mode );
  309. if (handle != FILEOPENERROR) {
  310. break;
  311. } else {
  312. CHANGEDIR( DataPath );
  313. CHANGEDIR( StartPath );
  314. if (!Do_Open_Error(COULD_NOT_OPEN, file_name)) {
  315. CallingDOSInt--;
  316. return(FILEOPENERROR);
  317. }
  318. }
  319. }
  320. /*
  321. ** A file that is read off of disk cannot be part of
  322. ** a pack file regardless of the bit settting.
  323. */
  324. if (immediate && index != ERROR) {
  325. filedata->Flag &= ~FILEF_PACKED;
  326. filedata->Size = 0;
  327. }
  328. if (index != ERROR) {
  329. // The true file size is needed.
  330. // Go back to beginning of file.
  331. filedata->Size = FILESEEK(handle, 0L, SEEK_END);
  332. FILESEEK(handle, 0L, SEEK_SET);
  333. }
  334. /*
  335. ** Initialize the local file handle table.
  336. */
  337. filehandletable->Index = index;
  338. filehandletable->Pos = 0L;
  339. filehandletable->Start = 0L;
  340. filehandletable->Empty = FALSE;
  341. filehandletable->Handle = handle;
  342. filehandletable->Mode = mode;
  343. if (index != ERROR) {
  344. filehandletable->Name = filedata->Name;
  345. } else {
  346. filehandletable->Name = NULL;
  347. }
  348. /*
  349. ** If the file should be made resident, then do it at this time.
  350. ** Perform all necessary adjustments to the file tables.
  351. */
  352. if ((mode & WRITE) == 0) {
  353. Cache_File(index, file_handle);
  354. }
  355. }
  356. }
  357. // If in the file table, increment the number of opens on this file.
  358. if (index != ERROR) {
  359. filedata->OpenCount++;
  360. }
  361. /*
  362. ** Return with valid file handle.
  363. */
  364. CallingDOSInt--;
  365. return(file_handle);
  366. }
  367. /***************************************************************************
  368. * CLOSE_FILE -- Closes a file previously opened with Open_File. *
  369. * *
  370. * Use this routine to close a file that was opened with Open_File. *
  371. * This is the only way to close a file that was opened with the *
  372. * Westwood file I/O system. *
  373. * *
  374. * INPUT: handle -- Westwood file handle. *
  375. * *
  376. * OUTPUT: none *
  377. * *
  378. * WARNINGS: none *
  379. * *
  380. * HISTORY: *
  381. * 11/11/1991 JLB : Created. *
  382. *=========================================================================*/
  383. VOID cdecl Close_File(int handle)
  384. {
  385. int index; // File data table index.
  386. BOOL flushed = FALSE; // If file flushed from cache, do change time stamp.
  387. BOOL stillopen;
  388. FileDataType *parent = NULL;
  389. FileDataType *filedata = NULL;
  390. VOID *memptr=NULL;
  391. #if(DEBUGPRINT)
  392. Mono_Printf("Close_File(%d)\r", handle);
  393. #endif
  394. /*
  395. ** Check for file handle validity.
  396. */
  397. if (!Is_Handle_Valid(handle, CLOSING_NON_HANDLE, NULL)) {
  398. return;
  399. }
  400. CallingDOSInt++;
  401. index = FileHandleTable[handle].Index;
  402. filedata = &FileDataPtr[index];
  403. // Remove the OpenCount on the file.
  404. if (index != ERROR) {
  405. filedata->OpenCount--;
  406. stillopen = filedata->OpenCount;
  407. // If this file packed in another, then decrement the parents open count.
  408. if (filedata->Flag & FILEF_PACKED) {
  409. parent = &FileDataPtr[filedata->Disk];
  410. // Do not dec OpenCount if the child was cached but not the parent.
  411. if (parent->Ptr == filedata->Ptr) {
  412. parent->OpenCount--;
  413. stillopen = parent->OpenCount;
  414. }
  415. }
  416. }
  417. // If the file was resident, then handle bookeeping.
  418. if (index != ERROR && filedata->Ptr) {
  419. // Get a pointer to the memory area for later use.
  420. memptr = filedata->Ptr;
  421. // If file has a parent, and it is the cached file...
  422. if (parent && (parent->Ptr == filedata->Ptr)) {
  423. // The PAK'd file is closed just by setting the pointer.
  424. if (!filedata->OpenCount) {
  425. filedata->Ptr = NULL;
  426. }
  427. // Uncache parent if no other sons are open and it should be flushed.
  428. if ((!parent->OpenCount) && (parent->Flag & FILEF_FLUSH)) {
  429. Mem_Free(FileCacheHeap, parent->Ptr);
  430. parent->Ptr = NULL;
  431. flushed = TRUE;
  432. }
  433. }
  434. // Else should the file be flushed? Only flush it if the flag says
  435. // so AND there are no other open counts on it.
  436. else if ((filedata->Flag & FILEF_FLUSH) && !filedata->OpenCount) {
  437. Mem_Free(FileCacheHeap, filedata->Ptr);
  438. filedata->Ptr = NULL;
  439. flushed = TRUE;
  440. }
  441. //
  442. // Set the time stamp on the close IF
  443. // the file was not flush AND
  444. // is not open AND
  445. // its time stamp should be changed by being a FILEF_KEEP|PROIORTY file.
  446. //
  447. if (!flushed && !stillopen && !(filedata->Flag & (FILEF_KEEP|FILEF_PRIORITY))) {
  448. Mem_Reference(memptr);
  449. }
  450. }
  451. // Just a simple close will do.
  452. else {
  453. FILECLOSE(FileHandleTable[handle].Handle);
  454. }
  455. // The WWS handle is no longer being used.
  456. FileHandleTable[handle].Empty = TRUE;
  457. CallingDOSInt--;
  458. }
  459. /***************************************************************************
  460. * READ_FILE -- Reads a block of data from a file. *
  461. * *
  462. * This routine is used to read a block of data from a previously *
  463. * opened file. *
  464. * *
  465. * INPUT: handle -- Westwood file handle returned from Open_File. *
  466. * *
  467. * buf -- Pointer to buffer to load the data into. *
  468. * *
  469. * bytes -- Number of bytes to load. *
  470. * *
  471. * OUTPUT: Returns with the number of bytes actually read from the file. *
  472. * If this number is less than the number requested, this *
  473. * indicates the end of the file. *
  474. * *
  475. * WARNINGS: none *
  476. * *
  477. * HISTORY: *
  478. * 11/13/1991 JLB : Created. *
  479. *=========================================================================*/
  480. long cdecl Read_File(int handle, VOID *buf, unsigned long bytes)
  481. {
  482. int doshandle; // DOS file handle.
  483. int fileindex; // File table index.
  484. long bytesread; // Accumulation of number of bytes read.
  485. FileDataType *filedata; // Pointer to the current FileData.
  486. #if(DEBUGPRINT)
  487. Mono_Printf("Read_File(%d, %08lx, %ld)\r", handle, buf, bytes);
  488. #endif
  489. /*
  490. ** The handle must be valid or else it is an error.
  491. */
  492. if (!buf || !bytes || !Is_Handle_Valid(handle, READING_NON_HANDLE, NULL)) {
  493. return(NULL);
  494. }
  495. CallingDOSInt++;
  496. /*
  497. ** Prepare some working variables.
  498. */
  499. doshandle = FileHandleTable[handle].Handle;
  500. fileindex = FileHandleTable[handle].Index;
  501. /*
  502. ** Limit the number of bytes to read according to the size of the
  503. ** file. If no file size is specified, then don't perform the check.
  504. */
  505. if (fileindex != ERROR) {
  506. filedata = &FileDataPtr[fileindex];
  507. if (filedata->Size) {
  508. bytes = MIN((unsigned long)filedata->Size - FileHandleTable[handle].Pos, bytes);
  509. }
  510. }
  511. /*
  512. ** Starts with no bytes being read in.
  513. */
  514. bytesread = 0;
  515. if (bytes) {
  516. if (doshandle) {
  517. /*
  518. ** Perform a DOS read of the file. Read the file in chunks.
  519. */
  520. while (bytes) {
  521. long number; // Number of bytes read.
  522. /*
  523. ** Read in a chunk of file data.
  524. */
  525. Hard_Error_Occured = 0;
  526. number = (long)FILEREAD(doshandle, buf, MIN(bytes, IO_CHUNK_SIZE));
  527. /*
  528. ** if a hard error occurs, read its copy on a different directory
  529. */
  530. if ( Hard_Error_Occured ) {
  531. number = Read_File_With_Recovery( handle, buf, (unsigned int)MIN(bytes, IO_CHUNK_SIZE));
  532. doshandle = FileHandleTable[handle].Handle;
  533. }
  534. /*
  535. ** Adjust the remaining bytes to read counter and adjust the count
  536. ** of the number of bytes actually read from the file.
  537. */
  538. bytes -= number;
  539. bytesread += number;
  540. /*
  541. ** Adjust the file position (manually).
  542. */
  543. FileHandleTable[handle].Pos += number;
  544. /*
  545. ** If less than requested bytes were read from the file, then
  546. ** we KNOW that the reading loop must terminate.
  547. ** Was there an error?
  548. */
  549. if (number < IO_CHUNK_SIZE) {
  550. break;
  551. }
  552. /*
  553. ** Adjust the destination pointer in anticipation of the next
  554. ** file read operation.
  555. */
  556. // this is a BCC bug
  557. // (char *)buf += number;
  558. buf = (char* ) buf + number;
  559. }
  560. } else {
  561. #if (DEBUGPRINT)
  562. Mono_Printf("Resident read '%s' %08lx[%ld].%ld\r", filedata->Name, (long)filedata->Start, FileHandleTable[handle].Pos, bytes);
  563. #endif
  564. /*
  565. ** Copy the block of the file into the specified buffer.
  566. */
  567. Mem_Copy((VOID*)((long)filedata->Ptr + FileHandleTable[handle].Start + FileHandleTable[handle].Pos), buf, bytes);
  568. bytesread = bytes;
  569. /*
  570. ** Adjust the file position (manually).
  571. */
  572. FileHandleTable[handle].Pos += bytes;
  573. }
  574. }
  575. /*
  576. ** Return with the number of bytes read in from the file.
  577. */
  578. CallingDOSInt--;
  579. return(bytesread);
  580. }
  581. /***************************************************************************
  582. * WRITE_FILE -- Writes a block of data to a file. *
  583. * *
  584. * This routine will write a block of data to a file. The file must *
  585. * have been previously opened with WRITE or READ|WRITE access. *
  586. * Writing cannot occur to a resident or packed file. *
  587. * *
  588. * INPUT: handle -- File handle as returned by Open_File. *
  589. * *
  590. * buf -- Pointer to the buffer that holds the data to be *
  591. * written out. *
  592. * *
  593. * bytes -- The number of bytes to write out. *
  594. * *
  595. * OUTPUT: Returns with the actual number of bytes written to the file. *
  596. * *
  597. * WARNINGS: If the value returned from this function is less than the *
  598. * number of bytes requested to be written, then this *
  599. * indicates some kind of error NOT caught by the file I/O *
  600. * system. *
  601. * *
  602. * HISTORY: *
  603. * 11/13/1991 JLB : Created. *
  604. *=========================================================================*/
  605. long cdecl Write_File(int handle, VOID const *buf, unsigned long bytes)
  606. {
  607. int doshandle; // DOS specific file handle.
  608. int fileindex; // FileData table index (if any).
  609. long byteswritten; // Accumulated number of bytes written.
  610. FileDataType *filedata; // Pointer to the current FileData.
  611. #if(DEBUGPRINT)
  612. Mono_Printf("Write_File(%d, %08lx, %ld)\r", handle, buf, bytes);
  613. #endif
  614. /*
  615. ** Check to make sure the file handle is valid.
  616. */
  617. if (!Is_Handle_Valid(handle, WRITING_NON_HANDLE, NULL)) {
  618. return(NULL);
  619. }
  620. doshandle = FileHandleTable[handle].Handle;
  621. fileindex = FileHandleTable[handle].Index;
  622. filedata = &FileDataPtr[fileindex];
  623. /*
  624. ** Writing to a resident file is an error condition.
  625. */
  626. if (!doshandle) {
  627. Do_IO_Error(WRITING_RESIDENT, filedata->Name);
  628. return(NULL);
  629. }
  630. CallingDOSInt++;
  631. /*
  632. ** Perform a DOS write of the data. This write is performed in blocks.
  633. */
  634. byteswritten = NULL;
  635. while (bytes) {
  636. long outbytes; // Number bytes written in one block.
  637. /*
  638. ** Write out one block of data.
  639. */
  640. outbytes = FILEWRITE(doshandle, (void*)buf, MIN((long)bytes, IO_CHUNK_SIZE));
  641. /*
  642. ** Reduce the bytes remaining to output counter and adjust the
  643. ** file position.
  644. */
  645. bytes -= outbytes;
  646. byteswritten += outbytes;
  647. FileHandleTable[handle].Pos += outbytes;
  648. /*
  649. ** If NO bytes were written out, then this is some kind of unknown
  650. ** error (possibly disk full?).
  651. */
  652. if (!outbytes) {
  653. break;
  654. }
  655. /*
  656. ** Possible adjustment to the file's size.
  657. */
  658. if (fileindex != ERROR) {
  659. filedata->Size = MAX(filedata->Size, FileHandleTable[handle].Pos);
  660. }
  661. /*
  662. ** Adjust the source pointer in anticipation of the next block write.
  663. */
  664. buf = (char*)(((long)buf) + outbytes);
  665. }
  666. /*
  667. ** Return with the actual number of bytes written.
  668. */
  669. CallingDOSInt--;
  670. return (byteswritten);
  671. }
  672. /***************************************************************************
  673. * SEEK_FILE -- Adjusts the position of the file pointer. *
  674. * *
  675. * This routine adjusts the position of the file pointer. Use this *
  676. * to control where the next Read_File or Write_File will occur. *
  677. * *
  678. * INPUT: handle -- File handle as returned by Open_File. *
  679. * *
  680. * offset -- Signed offset from indicated starting position. *
  681. * *
  682. * starting -- SEEK_CUR: Seeks from the current position. *
  683. * SEEK_SET: Seeks from the start of the file. *
  684. * SEEK_END: Seeks backward from the end. *
  685. * *
  686. * OUTPUT: Returns with the new file position. *
  687. * *
  688. * WARNINGS: Seeking past the end or before the beginning of the file *
  689. * is not allowed. *
  690. * *
  691. * HISTORY: *
  692. * 11/13/1991 JLB : Created. *
  693. *=========================================================================*/
  694. unsigned long cdecl Seek_File(int handle, long offset, int starting)
  695. {
  696. int doshandle; // DOS specific file handle.
  697. int fileindex; // FileData index (if any).
  698. FileDataType *filedata; // Pointer to the current FileData.
  699. #if(DEBUGPRINT)
  700. Mono_Printf("Seek_File(%d, %ld, %d)\r", handle, offset, starting);
  701. #endif
  702. /*
  703. ** Check to make sure that the file handle is valid.
  704. */
  705. if (!Is_Handle_Valid(handle, SEEKING_NON_HANDLE, NULL)) {
  706. return(NULL);
  707. }
  708. CallingDOSInt++;
  709. fileindex = FileHandleTable[handle].Index;
  710. doshandle = FileHandleTable[handle].Handle;
  711. filedata = &FileDataPtr[fileindex];
  712. if (!doshandle) {
  713. /*
  714. ** Resident file seek is merely an adjustment to the position value.
  715. */
  716. switch (starting) {
  717. /*
  718. ** Manually position from start of file.
  719. */
  720. case SEEK_SET:
  721. offset = MAX((long)0, offset); // Only positive offsets allowed.
  722. FileHandleTable[handle].Pos = NULL;
  723. break;
  724. /*
  725. ** Position is relative to end of file.
  726. */
  727. case SEEK_END:
  728. offset = MIN((long)0, offset); // Only negative offsets allowed.
  729. FileHandleTable[handle].Pos = filedata->Size;
  730. break;
  731. case SEEK_CUR:
  732. break;
  733. default:
  734. Do_IO_Error(SEEKING_BAD_OFFSET, FileHandleTable[handle].Name);
  735. break;
  736. }
  737. /*
  738. ** Perform the resident file position adjustment.
  739. */
  740. FileHandleTable[handle].Pos += offset;
  741. FileHandleTable[handle].Pos = MIN(FileHandleTable[handle].Pos, filedata->Size-1);
  742. FileHandleTable[handle].Pos = MAX(FileHandleTable[handle].Pos, (long)0);
  743. } else {
  744. /*
  745. ** Special handling for packed file seeks.
  746. */
  747. if (fileindex != ERROR && (filedata->Flag & FILEF_PACKED)) {
  748. switch (starting) {
  749. case SEEK_SET:
  750. FileHandleTable[handle].Pos = offset;
  751. break;
  752. case SEEK_END:
  753. // Only negative offsets allowed.
  754. // Do not allow seeking past the beginning.
  755. offset = MIN(0L, offset);
  756. if (-offset > filedata->Size) {
  757. Do_IO_Error(SEEKING_BAD_OFFSET, FileHandleTable[handle].Name);
  758. }
  759. FileHandleTable[handle].Pos = filedata->Size + offset;
  760. break;
  761. case SEEK_CUR:
  762. FileHandleTable[handle].Pos += offset;
  763. if (FileHandleTable[handle].Pos < 0) {
  764. FileHandleTable[handle].Pos = 0;
  765. }
  766. if (FileHandleTable[handle].Pos >= filedata->Size) {
  767. FileHandleTable[handle].Pos = filedata->Size;
  768. }
  769. break;
  770. default:
  771. Do_IO_Error(SEEKING_BAD_OFFSET, FileHandleTable[handle].Name);
  772. break;
  773. }
  774. FILESEEK(doshandle, FileHandleTable[handle].Pos+FileHandleTable[handle].Start, SEEK_SET);
  775. CallingDOSInt--;
  776. return (FileHandleTable[handle].Pos);
  777. }
  778. /*
  779. ** Perform the straight DOS seek.
  780. */
  781. FileHandleTable[handle].Pos = FILESEEK(doshandle, offset, starting);
  782. /*
  783. ** File position does not recognize packed offset.
  784. */
  785. if (fileindex != ERROR) {
  786. FileHandleTable[handle].Pos -= FileHandleTable[handle].Start;
  787. //SKB FileHandleTable[handle].Pos -= filedata->Start;
  788. }
  789. }
  790. /*
  791. ** Return with the current file position.
  792. */
  793. CallingDOSInt--;
  794. return (FileHandleTable[handle].Pos);
  795. }
  796. /***************************************************************************
  797. * FILE_EXISTS -- Searches for and FINDS the specified file. *
  798. * *
  799. * This routine will scan the available drives and return when the file *
  800. * is accessable. This routine is used when the programmer MUST be *
  801. * sure of a file's existance before proceeding. This process is *
  802. * automatically performed on a Open_File with READ access. *
  803. * *
  804. * INPUT: file_name -- Name of the file to check for. *
  805. * *
  806. * OUTPUT: Returns the disk number that the file exits on (A=1, B=2, etc) *
  807. * *
  808. * WARNINGS: none *
  809. * *
  810. * HISTORY: *
  811. * 11/14/1991 JLB : Created. *
  812. *=========================================================================*/
  813. BOOL cdecl File_Exists(char const *file_name)
  814. {
  815. /*
  816. ** If the filename is invalid then it errors out.
  817. */
  818. if (!file_name) return(FALSE);
  819. /*
  820. ** Continually search for the file (prompting as needed). Only return
  821. ** upon success or error function control reasons.
  822. */
  823. while (!Find_File(file_name)) {
  824. /*
  825. ** If the file could not be found then request for proper disk.
  826. */
  827. do {
  828. //struct find_t ffblk;
  829. //char path[MAXPATH];
  830. if (!Do_Open_Error(COULD_NOT_OPEN, file_name)) {
  831. return(FALSE);
  832. }
  833. ibm_setdisk(*DataPath - 'A');
  834. // Search for the volume ID so that the disk information get read in again.
  835. //_dos_findfirst("*.*", _A_VOLID, &ffblk);
  836. } while (CHANGEDIR( DataPath ));
  837. }
  838. return (TRUE);
  839. }
  840. /***************************************************************************
  841. * FILE_SIZE -- Determine the size of the specified file. *
  842. * *
  843. * This routine will return with the size of the file specified by the *
  844. * file handle passed in. *
  845. * *
  846. * INPUT: handle -- Westwood file handle. *
  847. * *
  848. * OUTPUT: Returns with the size of the file in bytes. *
  849. * *
  850. * WARNINGS: none *
  851. * *
  852. * HISTORY: *
  853. * 11/22/1991 JLB : Created. *
  854. *=========================================================================*/
  855. unsigned long cdecl File_Size(int handle)
  856. {
  857. FileDataType *filedata; // Pointer to the current FileData.
  858. if (FileHandleTable[handle].Index != ERROR) {
  859. filedata = &FileDataPtr[FileHandleTable[handle].Index];
  860. if (filedata->Size) {
  861. return(filedata->Size);
  862. }
  863. }
  864. return (filelength(FileHandleTable[handle].Handle));
  865. }
  866. /***************************************************************************
  867. * IS_HANDLE_VALID -- Determines validity of the specified file handle. *
  868. * *
  869. * Use this routine to determine if a file handle is valid or not. It *
  870. * checks to see if it falls within legal limits and does in fact *
  871. * reference an open file. This routine will call the error handler *
  872. * with the specified error number if the handle is not valid. *
  873. * *
  874. * INPUT: handle -- Handle to check for validity. *
  875. * *
  876. * error -- Error number to use when calling the IO_Error *
  877. * handler. *
  878. * *
  879. * name -- The file name (if known). *
  880. * *
  881. * OUTPUT: Returns with the file table index, if one exists for this *
  882. * file. *
  883. * *
  884. * WARNINGS: none *
  885. * *
  886. * HISTORY: *
  887. * 11/13/1991 JLB : Created. *
  888. *=========================================================================*/
  889. BOOL cdecl Is_Handle_Valid(int handle, FileErrorType error, char const *name)
  890. {
  891. /*
  892. ** The handle must be valid or else it is an error.
  893. */
  894. if (handle < 0 || handle >= TABLE_MAX) {
  895. /*
  896. ** ERROR: Invalid file handle.
  897. */
  898. Do_IO_Error(error, name);
  899. }
  900. /*
  901. ** An unused file handle causes an error.
  902. */
  903. if (FileHandleTable[handle].Empty) {
  904. Do_IO_Error(error, name);
  905. }
  906. return (TRUE);
  907. }
  908.