ODB.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 : ODB.DBF
  21. //Description : Object Database handling, it read DBF files
  22. #include <string.h>
  23. #include <ALL.h>
  24. #include <ODB.h>
  25. //-------- Begin of function Database constructor ------//
  26. //
  27. // [char*] fileName = the name of the DBF file to be opened
  28. // [int] bufferAll = read the whole database into memory or not
  29. //
  30. Database::Database(char* dbFileName, int bufferAll)
  31. {
  32. dbf_buf = NULL;
  33. rec_buf = NULL;
  34. last_read_recno = -1;
  35. if( dbFileName )
  36. open( dbFileName, bufferAll );
  37. }
  38. //----------- End of function Database constructor ------//
  39. //-------- Begin of function Database destructor ------//
  40. Database::~Database()
  41. {
  42. Database::close();
  43. }
  44. //----------- End of function Database destructor ------//
  45. //-------- Begin of function Database::open --------//
  46. //
  47. // Database::open( char* fileName )
  48. //
  49. // <char*> fileName = the name of the DBF file to be opened
  50. // [int] bufferAll = read the whole database into memory or not
  51. // (default : 0)
  52. //
  53. // return 1 : opened successfully
  54. // 0 : opening failure
  55. //
  56. void Database::open( char* fileName, int bufferAll )
  57. {
  58. close(); // if there is a opened file attached to current database, close it first
  59. file_open(fileName);
  60. file_read( &dbf_header, sizeof(DbfHeader) );
  61. //..........................................//
  62. if( bufferAll ) // read the whole database into memory or not
  63. {
  64. dbf_buf = mem_add( dbf_header.rec_size * dbf_header.last_rec );
  65. file_seek( 1 + dbf_header.data_offset );
  66. file_read( dbf_buf, dbf_header.rec_size*dbf_header.last_rec );
  67. file_close();
  68. dbf_buf_allocated = 1; // we allocated the buffer
  69. }
  70. else
  71. rec_buf = mem_add( dbf_header.rec_size );
  72. cur_recno = 1;
  73. }
  74. //--------- End of function Database::open ---------//
  75. //-------- Begin of function Database::open_from_buf --------//
  76. //
  77. // Open the database from an buffer with the database pre-read in
  78. //
  79. // <char*> dataPtr = the pointer to the pre-read database in the memory
  80. //
  81. void Database::open_from_buf(char* dataPtr)
  82. {
  83. close(); // if there is a open_from_bufed file attached to current database, close it first
  84. //------- set data pointers ----------//
  85. memcpy( &dbf_header, dataPtr, sizeof(DbfHeader) );
  86. dbf_buf = dataPtr + 1 + dbf_header.data_offset;
  87. dbf_buf_allocated = 0; // we didn't allocate the buffer, so don't bother to free it in deinit()
  88. cur_recno = 1;
  89. }
  90. //--------- End of function Database::open_from_buf ---------//
  91. //--------- Begin of function Database::read --------//
  92. //
  93. // Database::read( long recNum )
  94. //
  95. // [recNum] = the record number of the record to be read
  96. // (default : current record no.)
  97. //
  98. // return : <char*> success, the pointer to the reading buffer
  99. // <NULL> fail
  100. //
  101. char* Database::read( long recNo )
  102. {
  103. if( recNo <= 0 )
  104. recNo = cur_recno;
  105. if( recNo < 1 || recNo > dbf_header.last_rec )
  106. return NULL;
  107. if( dbf_buf ) // the whole database has been read into memory
  108. {
  109. return dbf_buf + dbf_header.rec_size * (recNo-1);
  110. }
  111. else // only a portion of the database is read into the memory at a time
  112. {
  113. if( recNo == last_read_recno ) // the record to be read is the same as one in buffer, simply return it
  114. return rec_buf;
  115. file_seek( 1+dbf_header.data_offset + dbf_header.rec_size * (recNo-1) );
  116. file_read( rec_buf, dbf_header.rec_size );
  117. last_read_recno = recNo;
  118. return rec_buf;
  119. }
  120. }
  121. //----------- End of function Database::read ---------//
  122. //---------- Begin of function Database::close -------//
  123. //
  124. // Database::close()
  125. //
  126. void Database::close()
  127. {
  128. if( rec_buf )
  129. {
  130. mem_del(rec_buf);
  131. rec_buf = NULL;
  132. }
  133. if( dbf_buf && dbf_buf_allocated )
  134. {
  135. mem_del( dbf_buf );
  136. dbf_buf = NULL;
  137. }
  138. }
  139. //----------- End of function Database::close ----------//