rawfile.cpp 59 KB

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