OFILE.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. * Seven Kingdoms: Ancient Adversaries
  3. *
  4. * Copyright 1997,1998 Enlight Software Ltd.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. //Filename : OFILE.CPP
  21. //Description : Object File
  22. #include <windows.h>
  23. #include <stdio.h>
  24. #include <OBOX.h>
  25. #include <ALL.h>
  26. #include <OFILE.h>
  27. //--------- Define static variables -----------//
  28. static char *path_array[] = { "" }; // multiple search path
  29. //-------- Begin of function File::file_open ----------//
  30. //
  31. // Open an existing file for reading
  32. // If the file is not found, call err.run()
  33. //
  34. // <char*> fileName = name of the file
  35. // [int] handleError = handle error
  36. // ( if 1, when error happen, err.run() will be called
  37. // immediately )
  38. // ( default : 1 )
  39. // [int] allowVarySize = allow the writing size and the read size to be different
  40. // ( default : 0 )
  41. //
  42. // return : 1-success, 0-fail
  43. //
  44. int File::file_open(char* fileName, int handleError, int allowVarySize)
  45. {
  46. if( strlen(fileName) > MAX_PATH )
  47. err.run( "File : file name is too long." );
  48. if( file_handle != INVALID_HANDLE_VALUE )
  49. file_close();
  50. strcpy( file_name, fileName );
  51. handle_error = handleError;
  52. allow_vary_size = allowVarySize;
  53. //--------- search all path to open the file ------//
  54. for( int i=0 ; i<sizeof(path_array)/sizeof(path_array[0]) ; i++ )
  55. {
  56. char filePath[MAX_PATH];
  57. strcpy( filePath, path_array[i] );
  58. strcat( filePath, fileName );
  59. file_handle = CreateFile(filePath,
  60. GENERIC_READ,
  61. FILE_SHARE_READ,
  62. (LPSECURITY_ATTRIBUTES) NULL,
  63. OPEN_EXISTING,
  64. FILE_ATTRIBUTE_NORMAL,
  65. (HANDLE) NULL);
  66. if( file_handle != INVALID_HANDLE_VALUE)
  67. return TRUE;
  68. }
  69. err.run( "Error opening file %s.", file_name );
  70. return FALSE;
  71. }
  72. //---------- End of function File::file_open ----------//
  73. //-------- Begin of function File::file_create ----------//
  74. //
  75. // Create a new file for writing (reading is also permitted)
  76. //
  77. // <char*> fileName = name of the file
  78. // [int] handleError = handle error
  79. // ( if 1, when error happen, err.run() will be called
  80. // immediately )
  81. // ( default : 1 )
  82. // [int] allowVarySize = allow the writing size and the read size to be different
  83. // ( default : 0 )
  84. //
  85. //
  86. // return : 1-success, 0-fail
  87. //
  88. int File::file_create(char* fileName, int handleError, int allowVarySize)
  89. {
  90. if( strlen(fileName) > MAX_PATH )
  91. err.run( "File : file name is too long." );
  92. strcpy( file_name, fileName );
  93. handle_error = handleError;
  94. allow_vary_size = allowVarySize;
  95. // cannot use creat() because it can't specify Binary file type
  96. file_handle = CreateFile(fileName,
  97. GENERIC_WRITE,
  98. 0,
  99. (LPSECURITY_ATTRIBUTES) NULL,
  100. CREATE_ALWAYS,
  101. FILE_ATTRIBUTE_NORMAL,
  102. (HANDLE) NULL);
  103. if( file_handle == INVALID_HANDLE_VALUE)
  104. err.run( "Error creating file %s", file_name );
  105. return TRUE;
  106. }
  107. //---------- End of function File::file_create ----------//
  108. //### begin alex 24/7 ###//
  109. //-------- Begin of function File::file_append ----------//
  110. //
  111. // Open an existing file for reading
  112. // If the file is not found, call err.run()
  113. //
  114. // <char*> fileName = name of the file
  115. // [int] handleError = handle error
  116. // ( if 1, when error happen, err.run() will be called
  117. // immediately )
  118. // ( default : 1 )
  119. // [int] allowVarySize = allow the writing size and the read size to be different
  120. // ( default : 0 )
  121. //
  122. // return : 1-success, 0-fail
  123. //
  124. int File::file_append(char* fileName, int handleError, int allowVarySize)
  125. {
  126. if( strlen(fileName) > MAX_PATH )
  127. err.run( "File : file name is too long." );
  128. if( file_handle != INVALID_HANDLE_VALUE )
  129. file_close();
  130. strcpy( file_name, fileName );
  131. handle_error = handleError;
  132. allow_vary_size = allowVarySize;
  133. //--------- search all path to open the file ------//
  134. for( int i=0 ; i<sizeof(path_array)/sizeof(path_array[0]) ; i++ )
  135. {
  136. char filePath[MAX_PATH];
  137. strcpy( filePath, path_array[i] );
  138. strcat( filePath, fileName );
  139. file_handle = CreateFile(filePath,
  140. GENERIC_WRITE,
  141. FILE_SHARE_WRITE,
  142. (LPSECURITY_ATTRIBUTES) NULL,
  143. OPEN_EXISTING,
  144. FILE_ATTRIBUTE_NORMAL,
  145. (HANDLE) NULL);
  146. if( file_handle != INVALID_HANDLE_VALUE)
  147. return TRUE;
  148. }
  149. err.run( "Error opening file %s.", file_name );
  150. return FALSE;
  151. }
  152. //---------- End of function File::file_append ----------//
  153. //#### end alex 24/7 ####//
  154. //-------- Begin of function File::file_close ----------//
  155. //
  156. void File::file_close()
  157. {
  158. if( file_handle != INVALID_HANDLE_VALUE )
  159. {
  160. CloseHandle(file_handle);
  161. file_handle = INVALID_HANDLE_VALUE;
  162. }
  163. }
  164. //---------- End of function File::file_close ----------//
  165. //-------- Begin of function File::~File ----------//
  166. //
  167. File::~File()
  168. {
  169. file_close();
  170. }
  171. //---------- End of function File::~File ----------//
  172. //-------- Begin of function File::file_write ----------//
  173. //
  174. // Write a block of data to the file
  175. //
  176. // <void*> dataBuf = pointer to data buffer to be written to the file
  177. // <unsigned> dataSize = length of the data (must < 64K)
  178. //
  179. // return : 1-success, 0-fail
  180. //
  181. int File::file_write(void* dataBuf, unsigned dataSize)
  182. {
  183. err_when( file_handle == INVALID_HANDLE_VALUE ); // not initialized
  184. if( allow_vary_size ) // allow the writing size and the read size to be different
  185. {
  186. if( dataSize > 0xFFFF )
  187. file_put_unsigned_short(0); // if exceed the unsigned short limit, don't write it
  188. else
  189. file_put_unsigned_short( dataSize );
  190. }
  191. DWORD actualWritten;
  192. int rc = WriteFile( file_handle, dataBuf, dataSize, &actualWritten, NULL);
  193. if( !rc && handle_error )
  194. err.run( "Error writing file %s", file_name );
  195. return rc;
  196. }
  197. //---------- End of function File::file_write ----------//
  198. //-------- Begin of function File::file_read ----------//
  199. //
  200. // Read a block of data from the file
  201. //
  202. // <void*> dataBuf = pointer to data buffer to be written to the file
  203. // <unsigned> dataSize = length of the data (must < 64K)
  204. //
  205. // return : 1-success, 0-fail
  206. //
  207. int File::file_read(void* dataBuf, unsigned dataSize)
  208. {
  209. #define MAX_READ_SIZE 0xFFF0
  210. err_when( file_handle == INVALID_HANDLE_VALUE ); // not initialized
  211. int curPos=file_pos();
  212. start_read:
  213. unsigned readSize=dataSize, writtenSize=dataSize;
  214. if( allow_vary_size ) // allow the writing size and the read size to be different
  215. {
  216. writtenSize = file_get_unsigned_short();
  217. if( writtenSize ) // writtenSize==0, if the size > 0xFFFF
  218. readSize = min(dataSize, writtenSize); // the read size is the minimum of the written size and the supposed read size
  219. }
  220. DWORD actualRead;
  221. int rc=ReadFile( file_handle, dataBuf, dataSize, &actualRead, NULL);
  222. //-------- if the data size has been reduced ----------//
  223. if( readSize < writtenSize )
  224. file_seek( writtenSize-readSize, FILE_CURRENT );
  225. //---- if the data size has been increased, reset the unread area ---//
  226. if( readSize < dataSize )
  227. memset( (char*)dataBuf+readSize, 0, dataSize-readSize );
  228. //----- if reading error, popup box and ask for retry -----//
  229. if( !rc && handle_error )
  230. {
  231. char msgStr[100];
  232. sprintf( msgStr, "Error reading file %s, Retry ?", file_name );
  233. if( box.ask(msgStr) )
  234. {
  235. file_seek( curPos );
  236. goto start_read;
  237. }
  238. }
  239. return rc;
  240. }
  241. //---------- End of function File::file_read ----------//
  242. //-------- Begin of function File::file_put_short ----------//
  243. //
  244. // Put a short integer to the file
  245. //
  246. // <short int> = the short integer
  247. //
  248. // return : 1-success, 0-fail
  249. //
  250. int File::file_put_short(short value)
  251. {
  252. err_when( file_handle == INVALID_HANDLE_VALUE ); // not initialized
  253. DWORD actualWritten;
  254. if( WriteFile( file_handle, &value, sizeof(short), &actualWritten, NULL ) )
  255. return TRUE;
  256. else
  257. {
  258. if( handle_error )
  259. err.run( "Error writing file %s", file_name );
  260. return FALSE;
  261. }
  262. }
  263. //---------- End of function File::file_put_short ----------//
  264. //-------- Begin of function File::file_get_short ----------//
  265. //
  266. // Get a short integer from the file
  267. //
  268. // return : the short integer
  269. //
  270. short File::file_get_short()
  271. {
  272. err_when( file_handle == INVALID_HANDLE_VALUE ); // not initialized
  273. DWORD actualRead;
  274. short value;
  275. if( ReadFile( file_handle, &value, sizeof(short), &actualRead, NULL ) )
  276. return value;
  277. else
  278. {
  279. if( handle_error )
  280. err.run( "Error reading file %s", file_name );
  281. return FALSE;
  282. }
  283. }
  284. //---------- End of function File::file_get_short ----------//
  285. //-------- Begin of function File::file_put_unsigned_short ----------//
  286. //
  287. // Put a unsigned short integer to the file
  288. //
  289. // <unsigned short> = the short integer
  290. //
  291. // return : 1-success, 0-fail
  292. //
  293. int File::file_put_unsigned_short(unsigned short value)
  294. {
  295. err_when( file_handle == INVALID_HANDLE_VALUE ); // not initialized
  296. DWORD actualWritten;
  297. if( WriteFile( file_handle, &value, sizeof(unsigned short), &actualWritten, NULL ) )
  298. return TRUE;
  299. else
  300. {
  301. if( handle_error )
  302. err.run( "Error writing file %s", file_name );
  303. return FALSE;
  304. }
  305. }
  306. //---------- End of function File::file_put_unsigned_short ----------//
  307. //-------- Begin of function File::file_get_unsigned_short ----------//
  308. //
  309. // Get a short integer from the file
  310. //
  311. // return : the short integer
  312. //
  313. unsigned short File::file_get_unsigned_short()
  314. {
  315. err_when( file_handle == INVALID_HANDLE_VALUE ); // not initialized
  316. DWORD actualRead;
  317. unsigned short value;
  318. if( ReadFile( file_handle, &value, sizeof(unsigned short), &actualRead, NULL ) )
  319. return value;
  320. else
  321. {
  322. if( handle_error )
  323. err.run( "Error reading file %s", file_name );
  324. return 0;
  325. }
  326. }
  327. //---------- End of function File::file_get_unsigned_short ----------//
  328. //-------- Begin of function File::file_put_long ----------//
  329. //
  330. // Put a long integer to the file
  331. //
  332. // <long int> = the long integer
  333. //
  334. // return : 1-success, 0-fail
  335. //
  336. int File::file_put_long(long value)
  337. {
  338. err_when( file_handle == INVALID_HANDLE_VALUE ); // not initialized
  339. DWORD actualWritten;
  340. if( WriteFile( file_handle, &value, sizeof(long), &actualWritten, NULL ) )
  341. return TRUE;
  342. else
  343. {
  344. if( handle_error )
  345. err.run( "Error writing file %s", file_name );
  346. return FALSE;
  347. }
  348. }
  349. //---------- End of function File::file_put_long ----------//
  350. //-------- Begin of function File::file_get_long ----------//
  351. //
  352. // Get a long integer from the file
  353. //
  354. // return : the long integer
  355. //
  356. long File::file_get_long()
  357. {
  358. err_when( file_handle == INVALID_HANDLE_VALUE ); // not initialized
  359. DWORD actualRead;
  360. long value;
  361. if( ReadFile( file_handle, &value, sizeof(long), &actualRead, NULL ) )
  362. return value;
  363. else
  364. {
  365. if( handle_error )
  366. err.run( "Error reading file %s", file_name );
  367. return FALSE;
  368. }
  369. }
  370. //---------- End of function File::file_get_long ----------//
  371. //---------- Start of function File::file_seek ---------//
  372. //
  373. // <long> offset = seek offset
  374. // [int] whence = FILE_BEGIN, FILE_CURRENT, FILE_END
  375. // (default : FILE_BEGIN)
  376. //
  377. // return : the offset of the pointer's new position, measured in
  378. // bytes from the file beginning.
  379. //
  380. long File::file_seek(long offset, int whence)
  381. {
  382. if( whence == -1 )
  383. whence = FILE_BEGIN;
  384. return SetFilePointer( file_handle, offset, NULL, whence );
  385. }
  386. //------------ End of function File::file_seek ----------//
  387. //---------- Start of function File::file_pos ---------//
  388. //
  389. // Get the position of current file pointer
  390. //
  391. // return : the position of current file pointer
  392. //
  393. long File::file_pos()
  394. {
  395. return SetFilePointer( file_handle, 0, NULL, FILE_CURRENT );
  396. }
  397. //------------ End of function File::file_pos ----------//
  398. //---------- Start of function File::file_size ---------//
  399. long File::file_size()
  400. {
  401. return GetFileSize(file_handle, NULL);
  402. }
  403. //------------ End of function File::file_size ----------//