RAWFILE.CPP 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348
  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. /* $Header: /CounterStrike/RAWFILE.CPP 1 3/03/97 10:25a Joe_bostic $ */
  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 : Westwood Library *
  24. * *
  25. * File Name : RAWFILE.CPP *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : August 8, 1994 *
  30. * *
  31. * Last Update : August 4, 1996 [JLB] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * RawFileClass::Bias -- Bias a file with a specific starting position and length. *
  36. * RawFileClass::Close -- Perform a closure of the file. *
  37. * RawFileClass::Create -- Creates an empty file. *
  38. * RawFileClass::Delete -- Deletes the file object from the disk. *
  39. * RawFileClass::Error -- Handles displaying a file error message. *
  40. * RawFileClass::Get_Date_Time -- Gets the date and time the file was last modified. *
  41. * RawFileClass::Is_Available -- Checks to see if the specified file is available to open. *
  42. * RawFileClass::Open -- Assigns name and opens file in one operation. *
  43. * RawFileClass::Open -- Opens the file object with the rights specified. *
  44. * RawFileClass::RawFileClass -- Simple constructor for a file object. *
  45. * RawFileClass::Raw_Seek -- Performs a seek on the unbiased file *
  46. * RawFileClass::Read -- Reads the specified number of bytes into a memory buffer. *
  47. * RawFileClass::Seek -- Reposition the file pointer as indicated. *
  48. * RawFileClass::Set_Date_Time -- Sets the date and time the file was last modified. *
  49. * RawFileClass::Set_Name -- Manually sets the name for a file object. *
  50. * RawFileClass::Size -- Determines size of file (in bytes). *
  51. * RawFileClass::Write -- Writes the specified data to the buffer specified. *
  52. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  53. #include <stdlib.h>
  54. #include <stdio.h>
  55. #include <string.h>
  56. #include <direct.h>
  57. #include <share.h>
  58. #include <stddef.h>
  59. #include "rawfile.h"
  60. #ifndef WIN32
  61. #include <fcntl.h>
  62. #include <io.h>
  63. #include <dos.h>
  64. extern short Hard_Error_Occured;
  65. #endif
  66. /***********************************************************************************************
  67. * RawFileClass::Error -- Handles displaying a file error message. *
  68. * *
  69. * Display an error message as indicated. If it is allowed to retry, then pressing a key *
  70. * will return from this function. Otherwise, it will exit the program with "exit()". *
  71. * *
  72. * INPUT: error -- The error number (same as the DOSERR.H error numbers). *
  73. * *
  74. * canretry -- Can this routine exit normally so that retrying can occur? If this is *
  75. * false, then the program WILL exit in this routine. *
  76. * *
  77. * filename -- Optional filename to report with this error. If no filename is *
  78. * supplied, then no filename is listed in the error message. *
  79. * *
  80. * OUTPUT: none, but this routine might not return at all if the "canretry" parameter is *
  81. * false or the player pressed ESC. *
  82. * *
  83. * WARNINGS: This routine may not return at all. It handles being in text mode as well as *
  84. * if in a graphic mode. *
  85. * *
  86. * HISTORY: *
  87. * 10/17/1994 JLB : Created. *
  88. *=============================================================================================*/
  89. void RawFileClass::Error(int , int , char const * )
  90. {
  91. }
  92. /***********************************************************************************************
  93. * RawFileClass::RawFileClass -- Simple constructor for a file object. *
  94. * *
  95. * This constructor is called when a file object is created with a supplied filename, but *
  96. * not opened at the same time. In this case, an assumption is made that the supplied *
  97. * filename is a constant string. A duplicate of the filename string is not created since *
  98. * it would be wasteful in that case. *
  99. * *
  100. * INPUT: filename -- The filename to assign to this file object. *
  101. * *
  102. * OUTPUT: none *
  103. * *
  104. * WARNINGS: none *
  105. * *
  106. * HISTORY: *
  107. * 10/17/1994 JLB : Created. *
  108. *=============================================================================================*/
  109. RawFileClass::RawFileClass(char const * filename) :
  110. Rights(0),
  111. BiasStart(0),
  112. BiasLength(-1),
  113. Handle(NULL_HANDLE),
  114. Filename(filename),
  115. Date(0),
  116. Time(0),
  117. Allocated(false)
  118. {
  119. }
  120. /***********************************************************************************************
  121. * RawFileClass::Set_Name -- Manually sets the name for a file object. *
  122. * *
  123. * This routine will set the name for the file object to the name specified. This name is *
  124. * duplicated in free store. This allows the supplied name to be a temporarily constructed *
  125. * text string. Setting the name in this fashion doesn't affect the closed or opened state *
  126. * of the file. *
  127. * *
  128. * INPUT: filename -- The filename to assign to this file object. *
  129. * *
  130. * OUTPUT: Returns with a pointer to the allocated copy of this filename. This pointer is *
  131. * guaranteed to remain valid for the duration of this file object or until the name *
  132. * is changed -- whichever is sooner. *
  133. * *
  134. * WARNINGS: Because of the allocation this routine must perform, memory could become *
  135. * fragmented. *
  136. * *
  137. * HISTORY: *
  138. * 10/17/1994 JLB : Created. *
  139. *=============================================================================================*/
  140. char const * RawFileClass::Set_Name(char const * filename)
  141. {
  142. if (Filename != NULL && Allocated) {
  143. free((char *)Filename);
  144. ((char *&)Filename) = 0;
  145. Allocated = false;
  146. }
  147. if (filename == NULL) return(NULL);
  148. Bias(0);
  149. ((char *&)Filename) = strdup(filename);
  150. if (Filename == NULL) {
  151. Error(ENOMEM, false, filename);
  152. }
  153. Allocated = true;
  154. return(Filename);
  155. }
  156. /***********************************************************************************************
  157. * RawFileClass::Open -- Assigns name and opens file in one operation. *
  158. * *
  159. * This routine will assign the specified filename to the file object and open it at the *
  160. * same time. If the file object was already open, then it will be closed first. If the *
  161. * file object was previously assigned a filename, then it will be replaced with the new *
  162. * name. Typically, this routine is used when an anonymous file object has been crated and *
  163. * now it needs to be assigned a name and opened. *
  164. * *
  165. * INPUT: filename -- The filename to assign to this file object. *
  166. * *
  167. * rights -- The open file access rights to use. *
  168. * *
  169. * OUTPUT: bool; Was the file opened? The return value of this is moot, since the open file *
  170. * is designed to never return unless it succeeded. *
  171. * *
  172. * WARNINGS: none *
  173. * *
  174. * HISTORY: *
  175. * 10/17/1994 JLB : Created. *
  176. *=============================================================================================*/
  177. int RawFileClass::Open(char const * filename, int rights)
  178. {
  179. Set_Name(filename);
  180. return(Open(rights));
  181. }
  182. /***********************************************************************************************
  183. * RawFileClass::Open -- Opens the file object with the rights specified. *
  184. * *
  185. * This routine is used to open the specified file object with the access rights indicated. *
  186. * This only works if the file has already been assigned a filename. It is guaranteed, by *
  187. * the error handler, that this routine will always return with success. *
  188. * *
  189. * INPUT: rights -- The file access rights to use when opening this file. This is a *
  190. * combination of READ and/or WRITE bit flags. *
  191. * *
  192. * OUTPUT: bool; Was the file opened successfully? This will always return true by reason of *
  193. * the error handler. *
  194. * *
  195. * WARNINGS: none *
  196. * *
  197. * HISTORY: *
  198. * 10/17/1994 JLB : Created. *
  199. *=============================================================================================*/
  200. int RawFileClass::Open(int rights)
  201. {
  202. Close();
  203. /*
  204. ** Verify that there is a filename associated with this file object. If not, then this is a
  205. ** big error condition.
  206. */
  207. if (Filename == NULL) {
  208. Error(ENOENT, false);
  209. }
  210. /*
  211. ** Record the access rights used for this open call. These rights will be used if the
  212. ** file object is duplicated.
  213. */
  214. Rights = rights;
  215. /*
  216. ** Repetitively try to open the file. Abort if a fatal error condition occurs.
  217. */
  218. for (;;) {
  219. /*
  220. ** Try to open the file according to the access rights specified.
  221. */
  222. #ifndef WIN32
  223. Hard_Error_Occured = 0;
  224. #endif
  225. switch (rights) {
  226. /*
  227. ** If the access rights are not recognized, then report this as
  228. ** an invalid access code.
  229. */
  230. default:
  231. errno = EINVAL;
  232. break;
  233. case READ:
  234. #ifdef WIN32
  235. Handle = CreateFile(Filename, GENERIC_READ, FILE_SHARE_READ,
  236. NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  237. #else
  238. _dos_open(Filename, O_RDONLY|SH_DENYNO, &Handle);
  239. #endif
  240. break;
  241. case WRITE:
  242. #ifdef WIN32
  243. Handle = CreateFile(Filename, GENERIC_WRITE, 0,
  244. NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  245. #else
  246. _dos_creat(Filename, 0, &Handle);
  247. #endif
  248. break;
  249. case READ|WRITE:
  250. #ifdef WIN32
  251. Handle = CreateFile(Filename, GENERIC_READ | GENERIC_WRITE, 0,
  252. NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  253. #else
  254. _dos_open(Filename, O_RDWR|O_CREAT|SH_DENYWR, &Handle);
  255. #endif
  256. break;
  257. }
  258. /*
  259. ** Biased files must be positioned past the bias start position.
  260. */
  261. if (BiasStart != 0 || BiasLength != -1) {
  262. Seek(0, SEEK_SET);
  263. }
  264. /*
  265. ** If the handle indicates the file is not open, then this is an error condition.
  266. ** For the case of the file cannot be found, then allow a retry. All other cases
  267. ** are fatal.
  268. */
  269. if (Handle == NULL_HANDLE) {
  270. #ifdef WIN32
  271. // return(false);
  272. Error(GetLastError(), false, Filename);
  273. // continue;
  274. #else
  275. /*
  276. ** If this flag is set, then some hard error occurred. Just assume that the error
  277. ** is probably a removed CD-ROM and allow a retry.
  278. */
  279. if (Hard_Error_Occured) {
  280. Error(Hard_Error_Occured, true, Filename);
  281. } else {
  282. if (errno == ENOENT) {
  283. Error(ENOENT, true, Filename);
  284. } else {
  285. Error(errno, false, Filename);
  286. }
  287. }
  288. continue;
  289. #endif
  290. }
  291. break;
  292. }
  293. return(true);
  294. }
  295. /***********************************************************************************************
  296. * RawFileClass::Is_Available -- Checks to see if the specified file is available to open. *
  297. * *
  298. * This routine will examine the disk system to see if the specified file can be opened *
  299. * or not. Use this routine before opening a file in order to make sure that is available *
  300. * or to perform other necessary actions. *
  301. * *
  302. * INPUT: force -- Should this routine keep retrying until the file becomes available? If *
  303. * in this case it doesn't become available, then the program will abort. *
  304. * *
  305. * OUTPUT: bool; Is the file available to be opened? *
  306. * *
  307. * WARNINGS: Depending on the parameter passed in, this routine may never return. *
  308. * *
  309. * HISTORY: *
  310. * 10/18/1994 JLB : Created. *
  311. *=============================================================================================*/
  312. int RawFileClass::Is_Available(int forced)
  313. {
  314. #ifndef WIN32
  315. bool open_failed;
  316. #endif
  317. if (Filename == NULL) return(false);
  318. /*
  319. ** If the file is already open, then is must have already passed the availability check.
  320. ** Return true in this case.
  321. */
  322. if (Is_Open()) return(true);
  323. /*
  324. ** If this is a forced check, then go through the normal open channels, since those
  325. ** channels ensure that the file must exist.
  326. */
  327. if (forced) {
  328. RawFileClass::Open(READ);
  329. RawFileClass::Close();
  330. return(true);
  331. }
  332. /*
  333. ** Perform a raw open of the file. If this open fails for ANY REASON, including a missing
  334. ** CD-ROM, this routine will return a failure condition. In all but the missing file
  335. ** condition, go through the normal error recover channels.
  336. */
  337. for (;;) {
  338. #ifdef WIN32
  339. Handle = CreateFile(Filename, GENERIC_READ, FILE_SHARE_READ,
  340. NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  341. if (Handle == NULL_HANDLE) {
  342. return(false);
  343. }
  344. break;
  345. #else
  346. Hard_Error_Occured = 0;
  347. open_failed = _dos_open(Filename, O_RDONLY|SH_DENYNO, &Handle);
  348. /*
  349. ** If DOS reports that everything is just fine except that the file is not present,
  350. ** then return with this fact. Any other case will fall through to the error handler
  351. ** routine.
  352. */
  353. if (open_failed && errno == ENOENT) return(false);
  354. /*
  355. ** If we got an access error it could be because there is no cd in
  356. ** the drive. Call the error handler but allow a continue if it
  357. ** returns.
  358. */
  359. if (open_failed && errno == EACCES) {
  360. Error(errno, true, Filename);
  361. continue;
  362. }
  363. /*
  364. ** If the file could not be found, then return with this information. If a more
  365. ** serious error occurred, then display the error message and abort.
  366. */
  367. if (Hard_Error_Occured) {
  368. Error(Hard_Error_Occured, true, Filename);
  369. continue;
  370. } else {
  371. if (open_failed) {
  372. /*
  373. ** An unhandled error condition is fatal. Display the error message and then
  374. ** abort.
  375. */
  376. Error(errno, false, Filename);
  377. }
  378. }
  379. if (!open_failed) break;
  380. #endif
  381. }
  382. /*
  383. ** Since the file could be opened, then close it and return that the file exists.
  384. */
  385. #ifdef WIN32
  386. if (!CloseHandle(Handle)) {
  387. Error(GetLastError(), false, Filename);
  388. }
  389. #else
  390. if (_dos_close(Handle)) {
  391. Error(errno, false, Filename);
  392. }
  393. #endif
  394. Handle = NULL_HANDLE;
  395. return(true);
  396. }
  397. /***********************************************************************************************
  398. * RawFileClass::Close -- Perform a closure of the file. *
  399. * *
  400. * Close the file object. In the rare case of an error, handle it as appropriate. *
  401. * *
  402. * INPUT: none *
  403. * *
  404. * OUTPUT: none *
  405. * *
  406. * WARNINGS: Some rare error conditions may cause this routine to abort the program. *
  407. * *
  408. * HISTORY: *
  409. * 10/18/1994 JLB : Created. *
  410. *=============================================================================================*/
  411. void RawFileClass::Close(void)
  412. {
  413. /*
  414. ** If the file is open, then close it. If the file is already closed, then just return. This
  415. ** isn't considered an error condition.
  416. */
  417. if (Is_Open()) {
  418. #ifdef WIN32
  419. /*
  420. ** Try to close the file. If there was an error (who knows what that could be), then
  421. ** call the error routine.
  422. */
  423. if (!CloseHandle(Handle)) {
  424. Error(GetLastError(), false, Filename);
  425. }
  426. #else
  427. for (;;) {
  428. /*
  429. ** Close the file. If there was an error in the close operation -- abort.
  430. */
  431. Hard_Error_Occured = 0;
  432. if (_dos_close(Handle)) {
  433. /*
  434. ** By definition, this error can only be a bad file handle. This a fatal condition
  435. ** of course, so abort with an error message.
  436. */
  437. Error(errno, false, Filename);
  438. }
  439. /*
  440. ** In the condition (if it is even possible) of a hard error occurring, then
  441. ** assume it is the case of missing media. Display an error message and try
  442. ** again if indicated.
  443. */
  444. if (Hard_Error_Occured) {
  445. Error(Hard_Error_Occured, true, Filename);
  446. continue;
  447. }
  448. break;
  449. }
  450. #endif
  451. /*
  452. ** At this point the file must have been closed. Mark the file as empty and return.
  453. */
  454. Handle = NULL_HANDLE;
  455. }
  456. }
  457. /***********************************************************************************************
  458. * RawFileClass::Read -- Reads the specified number of bytes into a memory buffer. *
  459. * *
  460. * This routine will read the specified number of bytes and place the data into the buffer *
  461. * indicated. It is legal to call this routine with a request for more bytes than are in *
  462. * the file. This condition can result in fewer bytes being read than requested. Determine *
  463. * this by examining the return value. *
  464. * *
  465. * INPUT: buffer -- Pointer to the buffer to read data into. If NULL is passed, no read *
  466. * is performed. *
  467. * *
  468. * size -- The number of bytes to read. If NULL is passed, then no read is *
  469. * performed. *
  470. * *
  471. * OUTPUT: Returns with the number of bytes read into the buffer. If this number is less *
  472. * than requested, it indicates that the file has been exhausted. *
  473. * *
  474. * WARNINGS: none *
  475. * *
  476. * HISTORY: *
  477. * 10/18/1994 JLB : Created. *
  478. *=============================================================================================*/
  479. long RawFileClass::Read(void * buffer, long size)
  480. {
  481. long bytesread = 0; // Running count of the number of bytes read into the buffer.
  482. int opened = false; // Was the file opened by this routine?
  483. /*
  484. ** If the file isn't opened, open it. This serves as a convenience
  485. ** for the programmer.
  486. */
  487. if (!Is_Open()) {
  488. /*
  489. ** The error check here is moot. Open will never return unless it succeeded.
  490. */
  491. if (!Open(READ)) {
  492. return(0);
  493. }
  494. opened = true;
  495. }
  496. /*
  497. ** A biased file has the requested read length limited to the bias length of
  498. ** the file.
  499. */
  500. if (BiasLength != -1) {
  501. int remainder = BiasLength - Seek(0);
  502. size = size < remainder ? size : remainder;
  503. }
  504. #ifdef WIN32
  505. long total = 0;
  506. while (size > 0) {
  507. bytesread = 0;
  508. SetErrorMode(SEM_FAILCRITICALERRORS);
  509. if (!ReadFile(Handle, buffer, size, &(unsigned long&)bytesread, NULL)) {
  510. size -= bytesread;
  511. total += bytesread;
  512. Error(GetLastError(), true, Filename);
  513. SetErrorMode(0);
  514. continue;
  515. }
  516. SetErrorMode(0);
  517. size -= bytesread;
  518. total += bytesread;
  519. if (bytesread == 0) break;
  520. }
  521. bytesread = total;
  522. #else
  523. int readresult;
  524. /*
  525. ** Read the file in convenient chunk sizes. When the actual number
  526. ** of bytes read does not match the desired, then assume that the file
  527. ** is exhausted and bail. This loop was adjusted to take into
  528. ** consideration the fact that "read" returns a SIGNED value whereas
  529. ** it takes an UNSIGNED value as the byte count.
  530. */
  531. while (size) {
  532. unsigned desired; // Bytes desired to be read this pass.
  533. unsigned actual; // Actual number of bytes read.
  534. /*
  535. ** Break the read request into chunks no bigger than the low level DOS read
  536. ** can handle.
  537. */
  538. desired = size < 32000L ? size : 32000L;
  539. Hard_Error_Occured = 0;
  540. readresult = _dos_read(Handle, buffer, desired, &actual);
  541. /*
  542. ** If a hard error occurred, then assume that it is the case of the CD-ROM or
  543. ** floppy media having been removed. Display the error and retry as directed.
  544. */
  545. if (Hard_Error_Occured) {
  546. Error(Hard_Error_Occured, true, Filename);
  547. continue; // Not technically needed, but to be consistent...
  548. } else {
  549. /*
  550. ** If negative one is returned from the read operation, then this indicates
  551. ** either a bad file number or invalid access. These are fatal conditions, so
  552. ** display the error and then abort.
  553. */
  554. if (readresult != 0) {
  555. Error(errno, false, Filename);
  556. } else {
  557. /*
  558. ** No error occurred during the read. Adjust the pointers and size counters and
  559. ** loop again if more data is needed to be read.
  560. */
  561. buffer = (char *)buffer + actual;
  562. bytesread += actual;
  563. size -= actual;
  564. if (actual != desired) break; // No more data?
  565. }
  566. }
  567. }
  568. #endif //WIN32
  569. /*
  570. ** Close the file if it was opened by this routine and return
  571. ** the actual number of bytes read into the buffer.
  572. */
  573. if (opened) Close();
  574. return(bytesread);
  575. }
  576. /***********************************************************************************************
  577. * RawFileClass::Write -- Writes the specified data to the buffer specified. *
  578. * *
  579. * This routine will write the data specified to the file. *
  580. * *
  581. * INPUT: buffer -- The buffer that holds the data to write. *
  582. * *
  583. * size -- The number of bytes to write to the file. *
  584. * *
  585. * OUTPUT: Returns with the number of bytes written to the file. This routine catches the *
  586. * case of a disk full condition, so this routine will always return with the number *
  587. * matching the size request. *
  588. * *
  589. * WARNINGS: A fatal file condition could cause this routine to never return. *
  590. * *
  591. * HISTORY: *
  592. * 10/18/1994 JLB : Created. *
  593. *=============================================================================================*/
  594. long RawFileClass::Write(void const * buffer, long size)
  595. {
  596. long bytesread = 0;
  597. int opened = false; // Was the file manually opened?
  598. /*
  599. ** Check to open status of the file. If the file is open, then merely write to
  600. ** it. Otherwise, open the file for writing and then close the file when the
  601. ** output is finished.
  602. */
  603. if (!Is_Open()) {
  604. if (!Open(WRITE)) {
  605. return(0);
  606. }
  607. opened = true;
  608. }
  609. #ifdef WIN32
  610. if (!WriteFile(Handle, buffer, size, &(unsigned long&)bytesread, NULL)) {
  611. Error(GetLastError(), false, Filename);
  612. }
  613. #else
  614. int writeresult;
  615. /*
  616. ** Write the data to the file in chunks no bigger than what the low level DOS write
  617. ** can handle.
  618. */
  619. while (size) {
  620. unsigned desired; // Bytes desired to be write this pass.
  621. unsigned actual; // Actual number of bytes written.
  622. Hard_Error_Occured = 0;
  623. // desired = (unsigned)MIN(size, Transfer_Block_Size());
  624. desired = size;
  625. writeresult = _dos_write(Handle, buffer, desired, &actual);
  626. /*
  627. ** If a hard error occurred, then assume it is the case of the media being
  628. ** removed. Print the error message an retry as directed.
  629. */
  630. if (Hard_Error_Occured) {
  631. Error(Hard_Error_Occured, true, Filename);
  632. continue; // Not technically needed, but to be consistent...
  633. } else {
  634. /*
  635. ** If negative one is returned by the DOS read, then this indicates a bad file
  636. ** handle or invalid access. Either condition is fatal -- display error condition
  637. ** and abort.
  638. */
  639. if (writeresult != 0) {
  640. Error(errno, false, Filename);
  641. } else {
  642. /*
  643. ** A successful write occurred. Update pointers and byte counter as appropriate.
  644. */
  645. buffer = (char *)buffer + actual;
  646. bytesread += actual;
  647. size -= actual;
  648. /*
  649. ** If the actual bytes written is less than requested, assume this is a case of
  650. ** the disk being full. Consider this a fatal error condition.
  651. */
  652. if (actual != desired) {
  653. Error(ENOSPC, false, Filename);
  654. }
  655. }
  656. }
  657. }
  658. #endif //WIN32
  659. /*
  660. ** Fixup the bias length if necessary.
  661. */
  662. if (BiasLength != -1) {
  663. if (Raw_Seek(0) > BiasStart+BiasLength) {
  664. BiasLength = Raw_Seek(0) - BiasStart;
  665. }
  666. }
  667. /*
  668. ** If this routine had to open the file, then close it before returning.
  669. */
  670. if (opened) {
  671. Close();
  672. }
  673. /*
  674. ** Return with the number of bytes written. This will always be the number of bytes
  675. ** requested, since the case of the disk being full is caught by this routine.
  676. */
  677. return(bytesread);
  678. }
  679. /***********************************************************************************************
  680. * RawFileClass::Seek -- Reposition the file pointer as indicated. *
  681. * *
  682. * Use this routine to move the filepointer to the position indicated. It can move either *
  683. * relative to current position or absolute from the beginning or ending of the file. This *
  684. * routine will only return if it successfully performed the seek. *
  685. * *
  686. * INPUT: pos -- The position to seek to. This is interpreted as relative to the position *
  687. * indicated by the "dir" parameter. *
  688. * *
  689. * dir -- The relative position to relate the seek to. This can be either SEEK_SET *
  690. * for the beginning of the file, SEEK_CUR for the current position, or *
  691. * SEEK_END for the end of the file. *
  692. * *
  693. * OUTPUT: This routine returns the position that the seek ended up at. *
  694. * *
  695. * WARNINGS: If there was a file error, then this routine might never return. *
  696. * *
  697. * HISTORY: *
  698. * 10/18/1994 JLB : Created. *
  699. *=============================================================================================*/
  700. long RawFileClass::Seek(long pos, int dir)
  701. {
  702. /*
  703. ** A file that is biased will have a seek operation modified so that the file appears to
  704. ** exist only within the bias range. All bytes outside of this range appear to be
  705. ** non-existant.
  706. */
  707. if (BiasLength != -1) {
  708. switch (dir) {
  709. case SEEK_SET:
  710. if (pos > BiasLength) {
  711. pos = BiasLength;
  712. }
  713. pos += BiasStart;
  714. break;
  715. case SEEK_CUR:
  716. break;
  717. case SEEK_END:
  718. dir = SEEK_SET;
  719. pos += BiasStart + BiasLength;
  720. // pos = (pos <= BiasStart+BiasLength) ? pos : BiasStart+BiasLength;
  721. // pos = (pos >= BiasStart) ? pos : BiasStart;
  722. break;
  723. }
  724. /*
  725. ** Perform the modified raw seek into the file.
  726. */
  727. long newpos = Raw_Seek(pos, dir) - BiasStart;
  728. /*
  729. ** Perform a final double check to make sure the file position fits with the bias range.
  730. */
  731. if (newpos < 0) {
  732. newpos = Raw_Seek(BiasStart, SEEK_SET) - BiasStart;
  733. }
  734. if (newpos > BiasLength) {
  735. newpos = Raw_Seek(BiasStart+BiasLength, SEEK_SET) - BiasStart;
  736. }
  737. return(newpos);
  738. }
  739. /*
  740. ** If the file is not biased in any fashion, then the normal seek logic will
  741. ** work just fine.
  742. */
  743. return(Raw_Seek(pos, dir));
  744. }
  745. /***********************************************************************************************
  746. * RawFileClass::Size -- Determines size of file (in bytes). *
  747. * *
  748. * Use this routine to determine the size of the file. The file must exist or this is an *
  749. * error condition. *
  750. * *
  751. * INPUT: none *
  752. * *
  753. * OUTPUT: Returns with the number of bytes in the file. *
  754. * *
  755. * WARNINGS: This routine handles error conditions and will not return unless the file *
  756. * exists and can successfully be queried for file length. *
  757. * *
  758. * HISTORY: *
  759. * 10/18/1994 JLB : Created. *
  760. *=============================================================================================*/
  761. long RawFileClass::Size(void)
  762. {
  763. long size = 0;
  764. /*
  765. ** A biased file already has its length determined.
  766. */
  767. if (BiasLength != -1) {
  768. return(BiasLength);
  769. }
  770. /*
  771. ** If the file is open, then proceed normally.
  772. */
  773. if (Is_Open()) {
  774. #ifdef WIN32
  775. size = GetFileSize(Handle, NULL);
  776. /*
  777. ** If there was in internal error, then call the error function.
  778. */
  779. if (size == 0xFFFFFFFF) {
  780. Error(GetLastError(), false, Filename);
  781. }
  782. #else
  783. /*
  784. ** Repetitively try to determine the file size until a fatal error condition or success
  785. ** is achieved.
  786. */
  787. for (;;) {
  788. Hard_Error_Occured = 0;
  789. size = filelength(Handle);
  790. /*
  791. ** If a hard error occurred, then assume it is the case of removed media. Display an
  792. ** error condition and allow retry.
  793. */
  794. if (Hard_Error_Occured) {
  795. Error(Hard_Error_Occured, true, Filename);
  796. continue;
  797. } else {
  798. if (size == -1) {
  799. Error(errno, false, Filename);
  800. }
  801. }
  802. break;
  803. }
  804. #endif
  805. } else {
  806. /*
  807. ** If the file wasn't open, then open the file and call this routine again. Count on
  808. ** the fact that the open function must succeed.
  809. */
  810. if (Open()) {
  811. size = Size();
  812. /*
  813. ** Since we needed to open the file we must remember to close the file when the
  814. ** size has been determined.
  815. */
  816. Close();
  817. }
  818. }
  819. BiasLength = size-BiasStart;
  820. return(BiasLength);
  821. }
  822. /***********************************************************************************************
  823. * RawFileClass::Create -- Creates an empty file. *
  824. * *
  825. * This routine will create an empty file from the file object. The file object's filename *
  826. * must already have been assigned before this routine will function. *
  827. * *
  828. * INPUT: none *
  829. * *
  830. * OUTPUT: bool; Was the file successfully created? This routine will always return true. *
  831. * *
  832. * WARNINGS: A fatal error condition could occur with this routine. Especially if the disk *
  833. * is full or a read-only media was selected. *
  834. * *
  835. * HISTORY: *
  836. * 10/18/1994 JLB : Created. *
  837. *=============================================================================================*/
  838. int RawFileClass::Create(void)
  839. {
  840. Close();
  841. if (Open(WRITE)) {
  842. /*
  843. ** A biased file must be at least as long as the bias offset. Seeking to the
  844. ** appropriate start offset has the effect of lengthening the file to the
  845. ** correct length.
  846. */
  847. if (BiasLength != -1) {
  848. Seek(0, SEEK_SET);
  849. }
  850. Close();
  851. return(true);
  852. }
  853. return(false);
  854. }
  855. /***********************************************************************************************
  856. * RawFileClass::Delete -- Deletes the file object from the disk. *
  857. * *
  858. * This routine will delete the file object from the disk. If the file object doesn't *
  859. * exist, then this routine will return as if it had succeeded (since the effect is the *
  860. * same). *
  861. * *
  862. * INPUT: none *
  863. * *
  864. * OUTPUT: bool; Was the file deleted? If the file was already missing, the this value will *
  865. * be false. *
  866. * *
  867. * WARNINGS: none *
  868. * *
  869. * HISTORY: *
  870. * 10/18/1994 JLB : Created. *
  871. *=============================================================================================*/
  872. int RawFileClass::Delete(void)
  873. {
  874. /*
  875. ** If the file was open, then it must be closed first.
  876. */
  877. Close();
  878. /*
  879. ** If there is no filename associated with this object, then this indicates a fatal error
  880. ** condition. Report this and abort.
  881. */
  882. if (!Filename) {
  883. Error(ENOENT, false);
  884. }
  885. /*
  886. ** Repetitively try to delete the file if possible. Either return with success, or
  887. ** abort the program with an error.
  888. */
  889. for (;;) {
  890. /*
  891. ** If the file is already missing, then return with this fact. No action is necessary.
  892. ** This can occur as this section loops if the file exists on a floppy and the floppy
  893. ** was removed, the file deleted on another machine, and then the floppy was
  894. ** reinserted. Admittedly, this is a rare case, but is handled here.
  895. */
  896. if (!Is_Available()) {
  897. return(false);
  898. }
  899. #ifdef WIN32
  900. if (!DeleteFile(Filename)) {
  901. Error(GetLastError(), false, Filename);
  902. return(false);
  903. }
  904. #else
  905. Hard_Error_Occured = 0;
  906. if (remove(Filename) == -1) {
  907. /*
  908. ** If a hard error occurred, then assume that the media has been removed. Display
  909. ** error message and retry as directed.
  910. */
  911. if (Hard_Error_Occured) {
  912. Error(Hard_Error_Occured, true, Filename);
  913. continue;
  914. }
  915. /*
  916. ** If at this point, DOS says the file doesn't exist, then just exit with this
  917. ** fact. It should have been caught earlier, but in any case, this is a legal
  918. ** condition.
  919. */
  920. if (errno == ENOENT) break;
  921. /*
  922. ** The only way it can reach this point is if DOS indicates that access is denied
  923. ** on the file. This occurs when trying to delete a file on a read-only media such
  924. ** as a CD-ROM. Report this as a fatal error and then abort.
  925. */
  926. Error(errno, false, Filename);
  927. }
  928. #endif
  929. break;
  930. }
  931. /*
  932. ** DOS reports that the file was successfully deleted. Return with this fact.
  933. */
  934. return(true);
  935. }
  936. /***********************************************************************************************
  937. * RawFileClass::Get_Date_Time -- Gets the date and time the file was last modified. *
  938. * *
  939. * Use this routine to get the date and time of the file. *
  940. * *
  941. * INPUT: none *
  942. * *
  943. * OUTPUT: Returns with the file date and time as a long. *
  944. * Use the YEAR(long), MONTH(),.... *
  945. * *
  946. * WARNINGS: none *
  947. * *
  948. * HISTORY: *
  949. * 11/14/1995 DRD : Created. *
  950. * 07/13/1996 JLB : Handles win32 method. *
  951. *=============================================================================================*/
  952. unsigned long RawFileClass::Get_Date_Time(void)
  953. {
  954. #ifdef WIN32
  955. BY_HANDLE_FILE_INFORMATION info;
  956. if (GetFileInformationByHandle(Handle, &info)) {
  957. WORD dosdate;
  958. WORD dostime;
  959. FileTimeToDosDateTime(&info.ftLastWriteTime, &dosdate, &dostime);
  960. return((dosdate << 16) | dostime);
  961. }
  962. return(0);
  963. #else
  964. unsigned short time;
  965. unsigned short date;
  966. unsigned long datetime = 0;
  967. //
  968. // If the file is open, then proceed normally.
  969. //
  970. if ( RawFileClass::Is_Open() ) {
  971. if ( _dos_getftime( Handle, &date, &time ) ) {
  972. //
  973. // return 0 indicating error with no date and time
  974. //
  975. return( datetime );
  976. }
  977. } else {
  978. //
  979. // If the file wasn't open, then see if the file exists.
  980. //
  981. if ( RawFileClass::Is_Available() ) {
  982. RawFileClass::Open();
  983. if ( _dos_getftime( Handle, &date, &time ) ) {
  984. RawFileClass::Close();
  985. //
  986. // return 0 indicating error with no date and time
  987. //
  988. return( datetime );
  989. }
  990. RawFileClass::Close();
  991. } else {
  992. //
  993. // return 0 indicating error with no date and time
  994. //
  995. return( datetime );
  996. }
  997. }
  998. //
  999. // combine the date and time as a long
  1000. //
  1001. datetime = (date << 16) + time;
  1002. return( datetime );
  1003. #endif
  1004. }
  1005. /***********************************************************************************************
  1006. * RawFileClass::Set_Date_Time -- Sets the date and time the file was last modified. *
  1007. * *
  1008. * Use this routine to set the date and time of the file. *
  1009. * *
  1010. * INPUT: the file date and time as a long *
  1011. * *
  1012. * OUTPUT: successful or not if the file date and time was changed. *
  1013. * *
  1014. * WARNINGS: none *
  1015. * *
  1016. * HISTORY: *
  1017. * 11/14/1995 DRD : Created. *
  1018. * 07/13/1996 JLB : Handles win 32 method *
  1019. *=============================================================================================*/
  1020. bool RawFileClass::Set_Date_Time(unsigned long datetime)
  1021. {
  1022. #ifdef WIN32
  1023. if (RawFileClass::Is_Open()) {
  1024. BY_HANDLE_FILE_INFORMATION info;
  1025. if (GetFileInformationByHandle(Handle, &info)) {
  1026. FILETIME filetime;
  1027. if (DosDateTimeToFileTime((WORD)(datetime >> 16), (WORD)(datetime & 0x0FFFF), &filetime)) {
  1028. return(SetFileTime(Handle, &info.ftCreationTime, &filetime, &filetime));
  1029. }
  1030. }
  1031. }
  1032. return(false);
  1033. #else
  1034. unsigned short time;
  1035. unsigned short date;
  1036. //
  1037. // If the file is open, then proceed normally.
  1038. //
  1039. if ( RawFileClass::Is_Open() ) {
  1040. //
  1041. // only set the date and time if open for READ only
  1042. //
  1043. if ( Rights == READ ) {
  1044. time = (unsigned short)(datetime & 0xFFFF);
  1045. date = (unsigned short)((datetime >> 16) & 0xFFFF);
  1046. if ( !_dos_setftime( Handle, date, time ) ) {
  1047. //
  1048. // return true indicating success
  1049. //
  1050. return( true );
  1051. }
  1052. }
  1053. } else {
  1054. //
  1055. // If the file wasn't open, then see if the file exists.
  1056. //
  1057. if ( RawFileClass::Is_Available() ) {
  1058. RawFileClass::Open();
  1059. time = (unsigned short)(datetime & 0xFFFF);
  1060. date = (unsigned short)((datetime >> 16) & 0xFFFF);
  1061. if ( !_dos_setftime( Handle, date, time ) ) {
  1062. RawFileClass::Close();
  1063. //
  1064. // return true indicating success
  1065. //
  1066. return( true );
  1067. }
  1068. RawFileClass::Close();
  1069. }
  1070. }
  1071. //
  1072. // return false indicating error
  1073. //
  1074. return( false );
  1075. #endif
  1076. }
  1077. /***********************************************************************************************
  1078. * RawFileClass::Bias -- Bias a file with a specific starting position and length. *
  1079. * *
  1080. * This will bias a file by giving it an artificial starting position and length. By *
  1081. * using this routine, it is possible to 'fool' the file into ignoring a header and *
  1082. * trailing extra data. An example of this would be a file inside of a mixfile. *
  1083. * *
  1084. * INPUT: start -- The starting offset that will now be considered the start of the *
  1085. * file. *
  1086. * *
  1087. * length -- The forced length of the file. For files that are opened for write, *
  1088. * this serves as the artificial constraint on the file's length. For *
  1089. * files opened for read, this limits the usable file size. *
  1090. * *
  1091. * OUTPUT: none *
  1092. * *
  1093. * WARNINGS: none *
  1094. * *
  1095. * HISTORY: *
  1096. * 06/02/1996 JLB : Created. *
  1097. *=============================================================================================*/
  1098. void RawFileClass::Bias(int start, int length)
  1099. {
  1100. if (start == 0) {
  1101. BiasStart = 0;
  1102. BiasLength = -1;
  1103. return;
  1104. }
  1105. BiasLength = RawFileClass::Size();
  1106. BiasStart += start;
  1107. if (length != -1) {
  1108. BiasLength = BiasLength < length ? BiasLength : length;
  1109. }
  1110. BiasLength = BiasLength > 0 ? BiasLength : 0;
  1111. /*
  1112. ** Move the current file offset to a legal position if necessary and the
  1113. ** file was open.
  1114. */
  1115. if (Is_Open()) {
  1116. RawFileClass::Seek(0, SEEK_SET);
  1117. }
  1118. }
  1119. /***********************************************************************************************
  1120. * RawFileClass::Raw_Seek -- Performs a seek on the unbiased file *
  1121. * *
  1122. * This will perform a seek on the file as if it were unbiased. This is in spite of any *
  1123. * bias setting the file may have. The ability to perform a raw seek in this fasion is *
  1124. * necessary to maintain the bias ability. *
  1125. * *
  1126. * INPUT: pos -- The position to seek the file relative to the "dir" parameter. *
  1127. * *
  1128. * dir -- The origin of the seek operation. *
  1129. * *
  1130. * OUTPUT: Returns with the new position of the seek operation. *
  1131. * *
  1132. * WARNINGS: none *
  1133. * *
  1134. * HISTORY: *
  1135. * 08/04/1996 JLB : Created. *
  1136. *=============================================================================================*/
  1137. long RawFileClass::Raw_Seek(long pos, int dir)
  1138. {
  1139. /*
  1140. ** If the file isn't opened, then this is a fatal error condition.
  1141. */
  1142. if (!Is_Open()) {
  1143. Error(EBADF, false, Filename);
  1144. }
  1145. #ifdef WIN32
  1146. switch (dir) {
  1147. case SEEK_SET:
  1148. dir = FILE_BEGIN;
  1149. break;
  1150. case SEEK_CUR:
  1151. dir = FILE_CURRENT;
  1152. break;
  1153. case SEEK_END:
  1154. dir = FILE_END;
  1155. break;
  1156. }
  1157. pos = SetFilePointer(Handle, pos, NULL, dir);
  1158. /*
  1159. ** If there was an error in the seek, then bail with an error condition.
  1160. */
  1161. if (pos == 0xFFFFFFFF) {
  1162. Error(GetLastError(), false, Filename);
  1163. }
  1164. #else
  1165. /*
  1166. ** Keep trying to seek until a non-retry condition occurs.
  1167. */
  1168. for (;;) {
  1169. /*
  1170. ** Perform the low level seek on the file.
  1171. */
  1172. Hard_Error_Occured = 0;
  1173. pos = lseek(Handle, pos, dir);
  1174. /*
  1175. ** If a hard error occurred, then assume that it is the case of removed media. Display
  1176. ** error message and retry.
  1177. */
  1178. if (Hard_Error_Occured) {
  1179. Error(Hard_Error_Occured, true, Filename);
  1180. continue;
  1181. } else {
  1182. /*
  1183. ** A negative one indicates a fatal error with the seek operation. Display error
  1184. ** condition and then abort.
  1185. */
  1186. if (pos == -1) {
  1187. Error(errno, false, Filename);
  1188. }
  1189. }
  1190. break;
  1191. }
  1192. #endif
  1193. /*
  1194. ** Return with the new position of the file. This will range between zero and the number of
  1195. ** bytes the file contains.
  1196. */
  1197. return(pos);
  1198. }