OFILETXT.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 : OFILETXT.CPP
  21. //Description : Object Text file
  22. #include <stdlib.h>
  23. #include <ALL.h>
  24. #include <KEY.h>
  25. #include <OFILETXT.h>
  26. //---- marco function for advancing to next token, bypassing space, ',' and ':' ---//
  27. #define next_token() for( ; *data_ptr==' ' || *data_ptr==',' || *data_ptr==':' ; data_ptr++ )
  28. //-------- Begin of function FileTxt::FileTxt ----------//
  29. //
  30. // <char*> fileName = name of the file
  31. //
  32. FileTxt::FileTxt(char* fileName)
  33. {
  34. file_open(fileName);
  35. //-----------------------------------//
  36. file_length = File::file_size();
  37. data_buf = mem_add( file_length+1 );
  38. data_buf[file_length] = CHAR_EOF;
  39. data_ptr = data_buf;
  40. //-----------------------------------//
  41. file_read( data_buf, file_length );
  42. file_close();
  43. }
  44. //---------- End of function FileTxt::FileTxt ----------//
  45. //-------- Begin of function FileTxt::FileTxt ----------//
  46. //
  47. // Initialize this FileTxt structure with a file stream.
  48. //
  49. // <File*> filePtr = pointer to a file class for reading in the text
  50. // <int> dataSize = size of the data.
  51. //
  52. FileTxt::FileTxt(File* filePtr, int dataSize)
  53. {
  54. //-----------------------------------//
  55. file_length = dataSize;
  56. data_buf = mem_add( file_length+1 );
  57. data_buf[file_length] = CHAR_EOF;
  58. data_ptr = data_buf;
  59. //-----------------------------------//
  60. filePtr->file_read( data_buf, file_length );
  61. }
  62. //---------- End of function FileTxt::FileTxt ----------//
  63. //-------- Begin of function FileTxt::~FileTxt ----------//
  64. //
  65. FileTxt::~FileTxt()
  66. {
  67. mem_del( data_buf );
  68. }
  69. //---------- End of function FileTxt::~FileTxt ----------//
  70. //--------- Begin of function FileTxt::get_char ------//
  71. //
  72. // Get one character from the file stream
  73. //
  74. // [int] advancePointer = whether advance the text pointer after getting the char or not
  75. // set this to 0, if you only want to get a char for testing only
  76. // (default : 1)
  77. //
  78. // return : <char> the character gotten
  79. // if EOF, return NULL
  80. //
  81. char FileTxt::get_char(int advancePointer)
  82. {
  83. while( *data_ptr == CHAR_RETURN )
  84. next_line();
  85. if( *data_ptr == CHAR_EOF )
  86. return 0;
  87. char dataChar = *data_ptr;
  88. if( advancePointer )
  89. data_ptr++;
  90. return dataChar;
  91. }
  92. //----------- End of function FileTxt::get_char ------//
  93. //--------- Begin of function FileTxt::next_line ------//
  94. //
  95. // Description : move the pointer of the character buffer to the
  96. // first character (not space) of the next line
  97. //
  98. // Return : <int> 1 - jump to next line
  99. // 0 - end of file
  100. //
  101. char* FileTxt::next_line()
  102. {
  103. for( ; *data_ptr != CHAR_RETURN && *data_ptr != CHAR_EOF ; data_ptr++ );
  104. if( *data_ptr == CHAR_RETURN )
  105. data_ptr++;
  106. if( *data_ptr == CHAR_LINE_FEED )
  107. data_ptr++;
  108. next_token();
  109. if( *data_ptr == CHAR_EOF )
  110. return NULL;
  111. else
  112. return data_ptr;
  113. }
  114. //----------- End of function FileTxt::next_line ------//
  115. //----------- End of function FileTxt::locate_word ------//
  116. //
  117. // locate the specified word
  118. //
  119. // <char*> wordPtr = pointer to the word
  120. //
  121. // Return : the pointer to next word
  122. // NULL if not found, now pointed to EOF
  123. //
  124. char* FileTxt::locate_word(char* wordPtr)
  125. {
  126. for( ; *data_ptr != CHAR_EOF ; data_ptr++ )
  127. {
  128. if( *data_ptr == *wordPtr ) // match first character first
  129. {
  130. for( ; *data_ptr == *wordPtr && *wordPtr ; data_ptr++, wordPtr++ );
  131. if( *wordPtr == NULL )
  132. {
  133. next_token();
  134. return data_ptr;
  135. }
  136. }
  137. }
  138. return NULL;
  139. }
  140. //----------- End of function FileTxt::locate_word ------//
  141. //--------- Begin of function FileTxt::get_token ------//
  142. //
  143. // [int] advancePointer = whether we should advance the pointer
  144. // after getting the token
  145. // (default : 1)
  146. // It it is 0, the pointer will stay there
  147. //
  148. // Return : the pointer to token buffer
  149. // NULL if not found, now pointed to EOF
  150. //
  151. char* FileTxt::get_token(int advancePointer)
  152. {
  153. int i;
  154. char pc;
  155. char* savePtr, *tokenPtr = token_buf;
  156. if( !advancePointer )
  157. savePtr = data_ptr;
  158. next_token();
  159. while( *data_ptr == CHAR_RETURN ) // bypass all space lines
  160. {
  161. if( !next_line() ) // End of File
  162. return NULL;
  163. }
  164. //.........................................//
  165. for( i=0 ; ; i++,data_ptr++ )
  166. {
  167. pc = *data_ptr;
  168. if ( pc == ' ' || pc == '=' || pc == ',' || pc == ':' || pc==CHAR_RETURN )
  169. break;
  170. else if( pc == CHAR_EOF )
  171. {
  172. if( !advancePointer ) // don't advance the pointer after getting the token
  173. data_ptr = savePtr;
  174. return NULL;
  175. }
  176. if ( i<MAX_TOKEN_LEN )
  177. *tokenPtr++ = *data_ptr;
  178. }
  179. //................................//
  180. *tokenPtr = NULL;
  181. next_token();
  182. if( !advancePointer )
  183. data_ptr = savePtr;
  184. return token_buf;
  185. }
  186. //----------- End of function FileTxt::get_token ------//
  187. //----------- End of function FileTxt::get_num ------//
  188. //
  189. // Convert current token to number and return the number
  190. //
  191. double FileTxt::get_num()
  192. {
  193. return atof( get_token() );
  194. }
  195. //----------- End of function FileTxt::get_num ------//
  196. //--------- Begin of function FileTxt::read_line ------//
  197. //
  198. // Read a line from this text file to the given buffer
  199. //
  200. // <char*> textBuf = a pre-allocated text buffer with a len of bufLen+1
  201. // <int> bufLen = max no. of chars the buffer can store
  202. //
  203. void FileTxt::read_line(char* textBuf, int bufLen)
  204. {
  205. next_token(); // skip all leading space
  206. if( *data_ptr == CHAR_RETURN )
  207. next_line();
  208. //-----------------------------------------//
  209. int i;
  210. for( i=0 ; *data_ptr!=CHAR_RETURN && *data_ptr!=CHAR_EOF && i<bufLen ; i++ )
  211. *textBuf++ = *data_ptr++;
  212. *textBuf=NULL;
  213. next_line();
  214. }
  215. //----------- End of function FileTxt::read_line ------//
  216. //--------- Begin of function FileTxt::read_paragraph ------//
  217. //
  218. // Read a paragraph from the text file to the given buffer
  219. // It will read all coming text until it encounter PAGE BREAK or EOF character
  220. //
  221. // <char*> textPtr = a pre-allocated text buffer with a len of bufLen+1
  222. // <int> bufLen = max no. of chars the buffer can store
  223. //
  224. // return : <int> textReadLen = the length of text actually read into textPtr
  225. //
  226. int FileTxt::read_paragraph(char* textPtr, int bufLen)
  227. {
  228. next_token(); // skip all leading space
  229. if( *data_ptr == CHAR_RETURN )
  230. next_line();
  231. //-----------------------------------------//
  232. char ch;
  233. int textReadLen=0;
  234. while( *data_ptr != CHAR_EOF )
  235. {
  236. ch = *data_ptr++;
  237. if( ch==CHAR_PAGE_BREAK || ch=='~' )
  238. break;
  239. // RETURN + LINE_FEED = word wrap
  240. // LINE_FEED only = new line
  241. if( ch==CHAR_RETURN )
  242. {
  243. if( *data_ptr==CHAR_LINE_FEED )
  244. {
  245. data_ptr++;
  246. ch = ' '; // Convert RETURN + LINE_FEED to a space
  247. }
  248. }
  249. // ##### begin Gilbert 24/7 #######//
  250. if( ch== 0xb ) //KEY_CTRL_K )
  251. // ##### end Gilbert 24/7 #######//
  252. {
  253. ch = '\n'; // CTRL-K means new line (CTRL-J)
  254. if( *data_ptr == CHAR_RETURN ) // by pass RETURN + LINE_FEED which may be turned into space if not cancelled
  255. data_ptr++;
  256. if( *data_ptr == CHAR_LINE_FEED )
  257. data_ptr++;
  258. }
  259. *textPtr++ = ch;
  260. textReadLen++;
  261. if( textReadLen>=bufLen-1 ) // when in non-debug mode
  262. break;
  263. }
  264. *textPtr++ = NULL;
  265. textReadLen++;
  266. err_when( data_ptr-data_buf > file_length );
  267. return textReadLen;
  268. }
  269. //----------- End of function FileTxt::read_paragraph ------//