ORESDB.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 : ORESDB.CPP
  21. //Description : Resource library with database index reading object
  22. #include <string.h>
  23. #include <OSYS.h>
  24. #include <ODB.h>
  25. #include <ORESDB.h>
  26. //-------------------------------------------------------//
  27. //
  28. // Resource library reading object
  29. //
  30. // Files required :
  31. //
  32. // - A resource file build by LIBDB.EXE, it is always in .RES extension
  33. // - A database file with Index Pointer field, the values of the field
  34. // are automatically calculated by LIBDB.EXE
  35. //
  36. //-------------------------------------------------------//
  37. //---------- Begin of function ResourceDb::init ---------//
  38. //
  39. // <char*> resName = name of the resource file (e.g. "GIF.RES")
  40. // <Database*> dbObj = name of the database (e.g. Database db("PFILE.DBF"))
  41. // <int> indexOffset = offset of the index field
  42. // [int] useCommonBuf = whether use the vga common buffer to store the data or not
  43. // (default:0)
  44. //
  45. void ResourceDb::init(char* resName, Database* dbObj, int indexOffset, int useCommonBuf)
  46. {
  47. deinit();
  48. file_open( resName );
  49. db_obj = dbObj;
  50. index_field_offset = indexOffset;
  51. use_common_buf = useCommonBuf;
  52. if( use_common_buf )
  53. data_buf = sys.common_data_buf;
  54. else
  55. data_buf = NULL;
  56. err_if( db_obj == NULL )
  57. err_now("db_obj is NULL");
  58. init_flag = 1;
  59. }
  60. //----------- End of function ResourceDb::init ------------//
  61. //---------- Begin of function ResourceDb::deinit ---------//
  62. //
  63. void ResourceDb::deinit()
  64. {
  65. if( init_flag )
  66. {
  67. if( !use_common_buf && data_buf )
  68. {
  69. mem_del(data_buf);
  70. data_buf = NULL;
  71. }
  72. if( !read_all )
  73. file_close();
  74. init_flag=0;
  75. }
  76. }
  77. //----------- End of function ResourceDb::deinit ----------//
  78. //---------- Begin of function ResourceDb::read ----------//
  79. //
  80. // Read in data from the resource file and store in an the buffer of this class
  81. //
  82. // The index field of the current record in the database object is
  83. // used to locate the data in the resource file.
  84. //
  85. // Syntax : read(int recNo)
  86. //
  87. // [int] recNo = the record no. in the database.
  88. // (default : current record no.)
  89. //
  90. // Return : <char*> data pointer
  91. // NULL if the record has not index to data
  92. //
  93. char* ResourceDb::read(int recNo)
  94. {
  95. err_when( !init_flag || !db_obj );
  96. long* indexFieldPtr;
  97. char* recPtr;
  98. if( (recPtr = db_obj->read(recNo)) == NULL )
  99. return NULL;
  100. indexFieldPtr = (long*) (recPtr+index_field_offset);
  101. if( memcmp( indexFieldPtr, " ", 4 ) == 0 )
  102. return NULL; // no sample screen for this file
  103. file_seek( *indexFieldPtr );
  104. data_buf_size = file_get_long();
  105. err_when( use_common_buf && data_buf_size > COMMON_DATA_BUF_SIZE );
  106. if( !use_common_buf )
  107. data_buf = mem_resize( data_buf, data_buf_size );
  108. file_read( data_buf, data_buf_size );
  109. return data_buf;
  110. }
  111. //----------- End of function ResourceDb::read -------------//
  112. //---------- Begin of function ResourceDb::get_file ----------//
  113. //
  114. // Position the file pointer to the beginning of the data and
  115. // return the file stream
  116. //
  117. // Syntax : get_file()
  118. //
  119. // Return : <FILE*> the file stream
  120. // NULL if the record has not index to data
  121. //
  122. File* ResourceDb::get_file()
  123. {
  124. err_when( !init_flag || !db_obj );
  125. long* indexFieldPtr;
  126. char emptyField[] = " ";
  127. char* recPtr;
  128. if( (recPtr = db_obj->read()) == NULL )
  129. return NULL;
  130. indexFieldPtr = (long*) (recPtr+index_field_offset);
  131. if( memcmp( indexFieldPtr, emptyField, 8 ) == 0 )
  132. return NULL; // no sample screen for this file
  133. file_seek( *indexFieldPtr + sizeof(long) );
  134. return this;
  135. }
  136. //----------- End of function ResourceDb::get_file -------------//
  137. //---------- Begin of function ResourceDb::init_imported ----------//
  138. //
  139. // If the whole database has been read into memory, then only no need to
  140. // tell ResourceDb the database name and the index offset
  141. //
  142. // <char*> resName = name of the resource file (e.g. "GIF.RES")
  143. // <int> readAll = whether read all data into the buffer or read one each time
  144. // [int] useCommonBuf = whether use the vga common buffer to store the data or not
  145. // (default:0)
  146. //
  147. void ResourceDb::init_imported(char* resName, int readAll, int useCommonBuf)
  148. {
  149. deinit();
  150. db_obj = NULL;
  151. index_field_offset = NULL;
  152. read_all = readAll;
  153. file_open( resName );
  154. if( read_all )
  155. {
  156. data_buf_size = file_size();
  157. data_buf = mem_add( data_buf_size );
  158. file_read( data_buf, data_buf_size );
  159. file_close();
  160. use_common_buf = 0; // don't use vga buffer if read all
  161. }
  162. else
  163. {
  164. use_common_buf = useCommonBuf;
  165. if( use_common_buf )
  166. data_buf = sys.common_data_buf;
  167. else
  168. data_buf = NULL;
  169. }
  170. init_flag = 1;
  171. }
  172. //----------- End of function ResourceDb::init_imported -------------//
  173. //---------- Begin of function ResourceDb::read_imported ----------//
  174. //
  175. // If ResourceDb is initialized using init_imported(),
  176. // then use read_imported to read the record
  177. //
  178. // <long> offset = offset to the data in the resource file
  179. //
  180. // Return : <char*> data pointer
  181. // NULL if the record has not index to data
  182. //
  183. char* ResourceDb::read_imported(long offset)
  184. {
  185. err_when( !init_flag );
  186. // #### begin Gilbert 4/10 ######//
  187. // err_if( offset<0 || offset>=data_buf_size )
  188. // err_here();
  189. // #### end Gilbert 4/10 ######//
  190. //-------- read from buffer ---------//
  191. // ##### begin Gilbert 4/10 #######//
  192. if( read_all )
  193. {
  194. err_when( offset<0 || offset>=data_buf_size );
  195. return data_buf + offset + sizeof(long); // by pass the long parameters which is the size of the data
  196. }
  197. // ##### end Gilbert 4/10 #######//
  198. //---------- read from file ---------//
  199. // ##### begin Gilbert 2/10 ######//
  200. err_when( offset >= file_size() );
  201. // ##### end Gilbert 2/10 ######//
  202. file_seek( offset );
  203. data_buf_size = file_get_long();
  204. err_when( use_common_buf && data_buf_size > COMMON_DATA_BUF_SIZE);
  205. if( !use_common_buf )
  206. data_buf = mem_resize( data_buf, data_buf_size );
  207. file_read( data_buf, data_buf_size );
  208. return data_buf;
  209. }
  210. //----------- End of function ResourceDb::read_imported -------------//