ORES.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 : ORES.CPP
  21. //Description : Resource library handling object
  22. #include <string.h>
  23. #include <ALL.h>
  24. #include <OSYS.h>
  25. #include <ORES.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. // <long> = offset of corresponding data unit in this file
  34. // .
  35. // .
  36. // <long> = the last index is a virtual index which is used to calculate
  37. // the size of the last data unit
  38. //
  39. // Data area
  40. // <char..> = data
  41. //
  42. //---------------------------------------------
  43. //
  44. // size of data unit = next offset - current offset
  45. //
  46. //---------------------------------------------//
  47. //---------- Begin of function Resource::init ----------//
  48. //
  49. // <char*> resName = name of the resource file (e.g. "GIF.RES")
  50. // <int> readAll = whether read all data into the buffer or read one each time
  51. // [int] useCommonBuf = whether use the common buffer to store the data or not
  52. // (default:0)
  53. //
  54. void Resource::init(char* resName, int readAll, int useCommonBuf)
  55. {
  56. if( init_flag )
  57. deinit();
  58. //-------------------------------------------//
  59. long dataSize;
  60. file_open(resName);
  61. read_all = readAll;
  62. use_common_buf = useCommonBuf;
  63. data_buf = NULL;
  64. data_buf_size = 0 ;
  65. rec_count = file_get_short();
  66. cur_rec_no = -1;
  67. //---------- Read in record index -------------//
  68. index_buf = (long*) mem_add( (rec_count+1) * sizeof(long) );
  69. // rec_count+1 is the last index pointer for calculating last record size
  70. file_read( index_buf, sizeof(long) * (rec_count+1) );
  71. //---------- Read in record data -------------//
  72. if( read_all )
  73. {
  74. dataSize = index_buf[rec_count] - index_buf[0];
  75. data_buf = mem_add( dataSize );
  76. file_read( data_buf, dataSize );
  77. file_close();
  78. }
  79. else
  80. {
  81. if( use_common_buf )
  82. data_buf = sys.common_data_buf;
  83. }
  84. init_flag = 1;
  85. }
  86. //----------- End of function Resource::init -------------//
  87. //---------- Begin of function Resource::deinit ---------//
  88. //
  89. void Resource::deinit()
  90. {
  91. if( init_flag )
  92. {
  93. if( index_buf )
  94. {
  95. mem_del(index_buf);
  96. index_buf = NULL;
  97. }
  98. if( data_buf && data_buf != sys.common_data_buf )
  99. {
  100. mem_del(data_buf);
  101. data_buf=NULL;
  102. }
  103. if( !read_all )
  104. file_close();
  105. init_flag=0;
  106. }
  107. }
  108. //----------- End of function Resource::deinit ------------//
  109. //---------- Begin of function Resource::read ----------//
  110. //
  111. // Read in data from the resource file and store in an the buffer of this class
  112. //
  113. // The index field of the current record in the database object is
  114. // used to locate the data in the resource file.
  115. //
  116. // Syntax : read(int recNo)
  117. //
  118. // [int] recNo = the record no of the data going to read
  119. // (default : current record no.)
  120. //
  121. // Return : <char*> data pointer
  122. // NULL if the record has not index to data
  123. //
  124. char* Resource::read(int recNo)
  125. {
  126. err_when( !init_flag );
  127. unsigned dataSize;
  128. if( recNo < 1 )
  129. recNo = cur_rec_no;
  130. err_when( recNo < 1 || recNo > rec_count );
  131. if( recNo < 1 || recNo > rec_count ) // when no in debug mode, err_when() will be removed
  132. return NULL;
  133. //------ all data pre-loaded to memory ------//
  134. if( read_all )
  135. return data_buf + index_buf[recNo-1] - index_buf[0];
  136. //------ all data NOT pre-loaded to memory -------//
  137. if( recNo == cur_rec_no && !use_common_buf )
  138. return data_buf;
  139. dataSize = index_buf[recNo] - index_buf[recNo-1];
  140. err_when( use_common_buf && dataSize > COMMON_DATA_BUF_SIZE );
  141. if( !use_common_buf && data_buf_size < dataSize )
  142. data_buf = mem_resize( data_buf, dataSize );
  143. data_buf_size = dataSize;
  144. //------------ read data ------------//
  145. file_seek( index_buf[recNo-1] );
  146. file_read( data_buf, dataSize );
  147. return data_buf;
  148. }
  149. //----------- End of function Resource::read -------------//
  150. //---------- Begin of function Resource::get_file ----------//
  151. //
  152. // Position the file pointer to the beginning of the data and
  153. // return the file stream
  154. //
  155. // <int> recNo = the record no of the data going to read
  156. // (default : current record no.)
  157. //
  158. // <int&> dataSIze = for returning the data size of the resource record
  159. //
  160. //
  161. // Return : <FILE*> the file stream
  162. // NULL if the record has not index to data/all data read into memory
  163. //
  164. File* Resource::get_file(int recNo, int& dataSize)
  165. {
  166. err_when( !init_flag || read_all ); // if read_all, all data in file has been read in and the file has been closed
  167. err_when( recNo < 1 || recNo > rec_count );
  168. if( recNo < 1 || recNo > rec_count ) // when no in debug mode, err_when() will be removed
  169. return NULL;
  170. file_seek( index_buf[recNo-1] );
  171. return this;
  172. }
  173. //----------- End of function Resource::get_file -------------//