ORESX.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 : ORESX.CPP
  21. //Description : Object of resource library with name index
  22. #include <string.h>
  23. #include <ALL.h>
  24. #include <OSYS.h>
  25. #include <ORESX.h>
  26. //--------- Format of RES file ------------//
  27. //
  28. // In the resource file, contain many data unit.
  29. //
  30. // <int> = no. of data records in this RES file
  31. //
  32. // Index area
  33. // char[8] = data record name
  34. // <long> = pointer of corresponding data unit in this file
  35. // .
  36. // .
  37. // char[9] = the last index is a virtual index which is used to calculate
  38. // <long> the size of the last data unit
  39. //
  40. //
  41. // Data area
  42. // <char..> = data
  43. //
  44. //---------------------------------------------
  45. //
  46. // size of data unit = next offset - current offset
  47. //
  48. //---------------------------------------------//
  49. //---------- Begin of function void ResourceIdx::init ---------//
  50. //
  51. // <char*> resName = name of the resource file (e.g. "GIF.RES")
  52. // <int> readAll = whether read all data into the buffer or read one each time
  53. // (default:0)
  54. // [int] useCommonBuf = whether use the common buffer to store the data or not
  55. // (default:0)
  56. //
  57. void ResourceIdx::init(char* resName, int readAll, int useCommonBuf)
  58. {
  59. long dataSize;
  60. if( init_flag ) // if a resource is already opened, close it first
  61. deinit();
  62. //------------- open resource file ----------//
  63. file_open( resName );
  64. //------------ Init vars ---------------------//
  65. read_all = readAll;
  66. use_common_buf = useCommonBuf;
  67. data_buf = NULL;
  68. data_buf_size = 0;
  69. rec_count = file_get_short();
  70. cur_rec_no = -1;
  71. user_data_buf = NULL;
  72. user_data_buf_size = 0;
  73. user_start_read_pos = 0;
  74. //---------- Read in record index -------------//
  75. index_buf = (ResIndex*) mem_add( (rec_count+1) * sizeof(ResIndex) );
  76. // rec_count+1 is the last index pointer for calculating last record size
  77. file_read( index_buf, sizeof(ResIndex) * (rec_count+1) );
  78. //---------- Read in record data -------------//
  79. if( read_all )
  80. {
  81. dataSize = index_buf[rec_count].pointer - index_buf[0].pointer;
  82. data_buf = mem_add( dataSize );
  83. file_read( data_buf, dataSize );
  84. file_close();
  85. }
  86. else
  87. {
  88. if( use_common_buf )
  89. data_buf = sys.common_data_buf;
  90. }
  91. init_flag = 1;
  92. }
  93. //----------- End of function ResourceIdx::init ------------//
  94. //---------- Begin of function ResourceIdx::deinit ---------//
  95. //
  96. void ResourceIdx::deinit()
  97. {
  98. if( init_flag )
  99. {
  100. if( index_buf )
  101. {
  102. mem_del(index_buf);
  103. index_buf = NULL;
  104. }
  105. if( data_buf && data_buf != sys.common_data_buf )
  106. {
  107. mem_del(data_buf);
  108. data_buf = NULL;
  109. }
  110. if( !read_all )
  111. file_close();
  112. init_flag=0;
  113. }
  114. }
  115. //----------- End of function ResourceIdx::~ResourceIdx ------------//
  116. //---------- Begin of function ResourceIdx::read_into_user_buf ----------//
  117. //
  118. // Read a block of data into the buffer specified by the user.
  119. //
  120. // <char*> dataName = name of the data going to read
  121. // <char*> userBuf = pointer to the user buffer
  122. // <int> userBufSize = size of the user buffer.
  123. // [int] userStartReadPos = the starting position of the data
  124. // to be read into the buffer.
  125. // (default:0)
  126. //
  127. // Return : <int> 1 - succeeded
  128. // 0 - failed
  129. //
  130. int ResourceIdx::read_into_user_buf(char* dataName, char* userBuf, int userBufSize, int userStartReadPos)
  131. {
  132. err_when( !init_flag || !dataName );
  133. int indexId = get_index(dataName);
  134. if( !indexId )
  135. return 0;
  136. //----- set the user buffer and read into the data -----//
  137. set_user_buf(userBuf, userBufSize, userStartReadPos);
  138. int rc = get_data(indexId)!=NULL; // ==NULL if actual data size > buffer size
  139. reset_user_buf();
  140. return rc;
  141. }
  142. //----------- End of function ResourceIdx::read_into_user_buf -------------//
  143. //---------- Begin of function ResourceIdx::read ----------//
  144. //
  145. // Read in data from the resource file and store in an the buffer of this class
  146. //
  147. // The index field of the current record in the database object is
  148. // used to locate the data in the resource file.
  149. //
  150. // Syntax : read(char* dataName)
  151. //
  152. // <char*> dataName = name of the data going to read
  153. //
  154. // Return : <char*> data pointer
  155. // NULL if the record has not index to data
  156. //
  157. char* ResourceIdx::read(char* dataName)
  158. {
  159. err_when( !init_flag || !dataName );
  160. int indexId = get_index(dataName);
  161. if( indexId )
  162. return get_data(indexId);
  163. else
  164. return NULL;
  165. }
  166. //----------- End of function ResourceIdx::read -------------//
  167. //---------- Begin of function ResourceIdx::get_index ----------//
  168. //
  169. // Get the index of the data in the resource file,
  170. // and later can retrieve the data using this index by calling get_data()
  171. //
  172. // <char*> dataName = name of the data going to get_index
  173. //
  174. // Return : <int> index of the data in the resource file
  175. // 0 if the name is not found in the resource file
  176. //
  177. int ResourceIdx::get_index(char* dataName)
  178. {
  179. err_when( !init_flag || !dataName );
  180. int i;
  181. for( i=0 ; i<rec_count ; i++ )
  182. {
  183. if( strcmp( index_buf[i].name, dataName ) == 0 )
  184. return i+1;
  185. }
  186. return 0;
  187. }
  188. //----------- End of function ResourceIdx::get_index -------------//
  189. //---------- Begin of function ResourceIdx::get_data ----------//
  190. //
  191. // Read in data from the resource file and store in an the buffer of this class
  192. //
  193. // The index field of the current record in the database object is
  194. // used to locate the data in the resource file.
  195. //
  196. // <int> indexId = index of the data pointing to the resource file
  197. //
  198. // Return : <char*> data pointer
  199. //
  200. char* ResourceIdx::get_data(int indexId)
  201. {
  202. err_when( !init_flag );
  203. unsigned dataSize;
  204. err_when( indexId < 1 || indexId > rec_count );
  205. indexId--; // make it start from 0, instead of starting from 1 for easy array accessing
  206. //------ all data pre-loaded to memory ------//
  207. if( read_all )
  208. return data_buf + index_buf[indexId].pointer - index_buf[0].pointer;
  209. //------ all data NOT pre-loaded to memory -------//
  210. if( indexId == cur_rec_no && !use_common_buf )
  211. return data_buf;
  212. dataSize = index_buf[indexId+1].pointer - index_buf[indexId].pointer;
  213. file_seek( index_buf[indexId].pointer );
  214. //--- if the user has custom assigned a buffer, read into that buffer ---//
  215. if( user_data_buf )
  216. {
  217. if( user_start_read_pos > 0 )
  218. {
  219. file_seek(user_start_read_pos,FILE_CURRENT); // skip the width and height info
  220. dataSize -= user_start_read_pos;
  221. }
  222. if( dataSize > user_data_buf_size )
  223. return NULL;
  224. file_read( user_data_buf, dataSize );
  225. return user_data_buf;
  226. }
  227. else
  228. {
  229. err_when( use_common_buf && dataSize > COMMON_DATA_BUF_SIZE );
  230. if( !use_common_buf && data_buf_size < dataSize )
  231. data_buf = mem_resize( data_buf, dataSize );
  232. data_buf_size = dataSize;
  233. file_read( data_buf, dataSize );
  234. return data_buf;
  235. }
  236. }
  237. //----------- End of function ResourceIdx::get_data -------------//
  238. //---------- Begin of function ResourceIdx::get_file ----------//
  239. //
  240. // Position the file pointer to the beginning of the data and
  241. // return the file stream
  242. //
  243. // Syntax : get_file(char* dataName)
  244. //
  245. // <char*> dataName = name of the data going to read
  246. // <int&> dataSIze = for returning the data size of the resource record
  247. //
  248. // Return : <FILE*> the file stream
  249. // NULL if the record has not index to data
  250. //
  251. File* ResourceIdx::get_file(char* dataName, int& dataSize)
  252. {
  253. err_when( !init_flag || !dataName || read_all);
  254. int i;
  255. //-------- Search for data name ----------//
  256. for( i=0 ; i<rec_count ; i++ )
  257. {
  258. if( strcmp( index_buf[i].name, dataName ) == 0 )
  259. {
  260. file_seek( index_buf[i].pointer );
  261. dataSize = index_buf[i+1].pointer - index_buf[i].pointer;
  262. return this;
  263. }
  264. }
  265. return NULL;
  266. }
  267. //----------- End of function ResourceIdx::get_file -------------//
  268. //---------- Begin of function ResourceIdx::get_file ----------//
  269. //
  270. // Position the file pointer to the beginning of the data and
  271. // return the file stream
  272. //
  273. // Syntax : get_file(char* dataName)
  274. //
  275. // <char*> dataName = name of the data going to read
  276. // <int&> dataSIze = for returning the data size of the resource record
  277. //
  278. // Return : <FILE*> the file stream
  279. // NULL if the record has not index to data
  280. //
  281. File* ResourceIdx::get_file(int bitmapId, int& dataSize)
  282. {
  283. err_when( !init_flag || !bitmapId || read_all );
  284. //-------- Search for data name ----------//
  285. file_seek( index_buf[bitmapId-1].pointer );
  286. dataSize = index_buf[bitmapId].pointer - index_buf[bitmapId-1].pointer;
  287. return this;
  288. }
  289. //----------- End of function ResourceIdx::get_file -------------//
  290. //---------- Begin of function ResourceIdx::set_user_buf ----------//
  291. //
  292. // Set custom user data buffer. When it is set, data will be loaded
  293. // into the user buffer instead of ResourceIdx's own buffer.
  294. //
  295. // <char*> userDataBuf - pointer user data buffer
  296. // <int> bufSize - size of the data bufferr
  297. // <int> userStartReadPos - the starting position of the data
  298. // to be read into the buffer.
  299. //
  300. void ResourceIdx::set_user_buf(char* userDataBuf, int bufSize, int userStartReadPos)
  301. {
  302. user_data_buf = userDataBuf;
  303. user_data_buf_size = bufSize;
  304. user_start_read_pos = userStartReadPos;
  305. }
  306. //----------- End of function ResourceIdx::set_user_buf -------------//
  307. //---------- Begin of function ResourceIdx::reset_user_buf ----------//
  308. //
  309. // Cancel using custom user data buffer.
  310. //
  311. void ResourceIdx::reset_user_buf()
  312. {
  313. user_data_buf = NULL;
  314. user_data_buf_size = 0;
  315. }
  316. //----------- End of function ResourceIdx::reset_user_buf -------------//
  317. //---------- Begin of function ResourceIdx::data_name ----------//
  318. char *ResourceIdx::data_name(int i)
  319. {
  320. if( i < 1 || i > rec_count)
  321. return NULL;
  322. return( index_buf[i-1].name );
  323. }
  324. //---------- End of function ResourceIdx::data_name ----------//