PageRenderTime 43ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/MTA10/utils/map2xml/CConfig.cpp

https://github.com/MrHankey/multitheftauto
C++ | 423 lines | 289 code | 26 blank | 108 comment | 133 complexity | deb1689175a446c98fcb130ac95b18b4 MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, LGPL-2.1
  1. /*****************************************************************************
  2. *
  3. * PROJECT: Multi Theft Auto v1.0
  4. * LICENSE: See LICENSE in the top level directory
  5. * FILE: utils/map2xml/CConfig.cpp
  6. * PURPOSE: Config reader class
  7. * DEVELOPERS: Christian Myhre Lundheim <>
  8. *
  9. * Multi Theft Auto is available from http://www.multitheftauto.com/
  10. *
  11. *****************************************************************************/
  12. #include "CConfig.h"
  13. CConfig::CConfig ( const char* szFileName )
  14. {
  15. // Set the file name
  16. m_szFileName = new char [ strlen ( szFileName ) + 1 ];
  17. strcpy ( m_szFileName, szFileName );
  18. // Set the pointer to the file
  19. m_fp = fopen ( szFileName, "r" );
  20. }
  21. CConfig::~CConfig ()
  22. {
  23. // Close the pointer to the file
  24. if ( m_fp )
  25. {
  26. fclose ( m_fp );
  27. }
  28. // Delete the filename
  29. delete [] m_szFileName;
  30. }
  31. bool CConfig::GetEntry ( char* szEntry, char* szReturnText, int iInstance )
  32. {
  33. char szTemp[256];
  34. memset ( szTemp, '\0', 256 );
  35. // If the file exists
  36. if ( m_fp ) {
  37. char szInput[256];
  38. memset ( szInput, '\0', 256 );
  39. int iHits = 1;
  40. // Itterate the lines of the file
  41. while ( !feof ( m_fp ) ) {
  42. // Grab the current line
  43. if ( fgets ( szInput, 256, m_fp ) ) {
  44. // Check for a comment
  45. if ( szInput[0] == '#' ) continue;
  46. // Check for the new line character
  47. else if ( szInput[0] == '\n' ) continue;
  48. else {
  49. char szCurrentEntry[256];
  50. memset ( szCurrentEntry, '\0', 256 );
  51. int i = 0;
  52. // Itterate the characters of the line till the end of the line or a comment and store it in a temp variable
  53. while ( szInput[i] != ' ' && szInput[i] != '\0' && szInput[i] != '\n' && szInput[i] != '#' ) {
  54. if ( szInput[i] != ' ' && szInput[i] != '\0' && szInput[i] != '\n' && szInput[i] != '#' ) {
  55. szCurrentEntry[i] = szInput[i];
  56. }
  57. else {
  58. szCurrentEntry[i] = '\0';
  59. }
  60. i++;
  61. }
  62. // If this is one of the entries we are looking for
  63. if ( stricmp ( szCurrentEntry, szEntry ) == 0 ) {
  64. // If it is the specific entry we are looking for
  65. if (iHits == iInstance) {
  66. // Check for comments or end of line
  67. if ( ( szInput[i] == '\0' ) || ( szInput[i] == '\n' ) || ( szInput[i] == '#' ) ) {
  68. // Reset the file pointer to the start of the file
  69. rewind ( m_fp );
  70. return false;
  71. }
  72. i++;
  73. if ( ( szInput[i] == '\0' ) || ( szInput[i] == '\n' ) || ( szInput[i] == '#' ) ) {
  74. // Reset the file pointer to the start of the file
  75. rewind ( m_fp );
  76. return false;
  77. }
  78. else {
  79. int j = 0;
  80. // Copy the temp variable to our buffer
  81. while ( szInput[i] != '#' && szInput[i] != '\n' && szInput[i] != '\0' ) {
  82. szTemp[j] = szInput[i];
  83. i++;
  84. j++;
  85. }
  86. // Clear the temp variable
  87. szTemp[strlen ( szTemp )] = '\0';
  88. // Copy the buffer to the return buffer
  89. strcpy ( szReturnText, szTemp );
  90. // Reset the file pointer to the start of the file
  91. rewind ( m_fp );
  92. return true;
  93. }
  94. }
  95. else {
  96. // Increment the counter for the amount of correct entry hits
  97. iHits++;
  98. }
  99. }
  100. }
  101. }
  102. }
  103. // Reset the file pointer to the start of the file
  104. rewind ( m_fp );
  105. }
  106. else
  107. {
  108. // Print the file error
  109. perror ( m_szFileName );
  110. }
  111. return false;
  112. }
  113. void CConfig::GetPreviousEntry ( char* szEntry, char* szReturnText, int iInstance )
  114. {
  115. char szTemp[256];
  116. memset ( szTemp, '\0', 256 );
  117. // If the config file exists
  118. if ( m_fp ) {
  119. char szInput[256];
  120. memset ( szInput, '\0', 256 );
  121. int iHits = 1;
  122. // Itterate the lines of the config file
  123. while ( !feof ( m_fp ) ) {
  124. // Grab the current line of the file
  125. if ( fgets ( szInput, 256, m_fp ) ) {
  126. // Check for comments
  127. if ( szInput[0] == '#' ) continue;
  128. // Check for new line
  129. else if ( szInput[0] == '\n' ) continue;
  130. else {
  131. char szCurrentEntry[256];
  132. memset ( szCurrentEntry, '\0', 256 );
  133. int i = 0;
  134. // Itterate the characters of the file
  135. while ( szInput[i] != ' ' && szInput[i] != '\0' && szInput[i] != '\n' && szInput[i] != '#' ) {
  136. // Check for comments and end of line
  137. if ( szInput[i] != ' ' && szInput[i] != '\0' && szInput[i] != '\n' && szInput[i] != '#' ) {
  138. szCurrentEntry[i] = szInput[i];
  139. }
  140. else {
  141. szCurrentEntry[i] = '\0';
  142. }
  143. i++;
  144. }
  145. // If this an entry we are looking for
  146. if ( stricmp ( szCurrentEntry, szEntry ) == 0 ) {
  147. // If this is the specific entry we are looking for
  148. if ( iHits == iInstance ) {
  149. if ( szTemp[0] != '\0' ) {
  150. // Copy the result to the return buffer
  151. strcpy ( szReturnText, szTemp );
  152. break;
  153. }
  154. else {
  155. // Copy Unknown to the return buffer
  156. strcpy ( szReturnText, "Unknown" );
  157. break;
  158. }
  159. break;
  160. }
  161. else {
  162. iHits++;
  163. }
  164. }
  165. else {
  166. // Clear the temp buffer
  167. memset ( szTemp, '\0', 256 );
  168. strcpy ( szTemp, szInput );
  169. }
  170. }
  171. }
  172. }
  173. // Reset the file pointer to the start of the file
  174. rewind ( m_fp );
  175. }
  176. else
  177. {
  178. // Print the file error
  179. perror( m_szFileName );
  180. }
  181. }
  182. int CConfig::GetNumberOfLines ()
  183. {
  184. // Set the line count to -1
  185. int iLines = -1;
  186. // If the file exists
  187. if ( m_fp ) {
  188. // Set the line count to 0
  189. iLines++;
  190. char szInput[128] = { '\0' };
  191. // Itterate the lines of the file
  192. while ( !feof ( m_fp ) ) {
  193. // Grab the line of the file
  194. fgets ( szInput, 128, m_fp );
  195. // Increment the line counter
  196. iLines++;
  197. }
  198. // Reset the file pointer to the start of the file
  199. rewind ( m_fp );
  200. }
  201. else
  202. {
  203. // Print the file error
  204. perror ( m_szFileName );
  205. }
  206. // Return the number of lines found
  207. return iLines;
  208. }
  209. int CConfig::GetNumberOfEntries ()
  210. {
  211. // Set the number of entries to -1
  212. int iEntries = -1;
  213. // If the file exists
  214. if ( m_fp ) {
  215. // Set the number of entries to 0
  216. iEntries++;
  217. char szInput[128] = { '\0' };
  218. // Itterate the lines of the file
  219. while ( !feof ( m_fp ) ) {
  220. // Grab the current line
  221. fgets ( szInput, 128, m_fp );
  222. // If this is not a comment or a new line
  223. if ( ( szInput[0] != '#' ) && ( szInput[0] != '\n' ) ) {
  224. // Increment the entry counter
  225. iEntries++;
  226. }
  227. }
  228. // Reset the file pointer to the start of the file
  229. rewind ( m_fp );
  230. }
  231. else
  232. {
  233. // Print the file error
  234. perror ( m_szFileName );
  235. }
  236. // Return the number of entries
  237. return iEntries;
  238. }
  239. int CConfig::GetNumberOfSpecificEntries ( char* szEntry )
  240. {
  241. // Set the number of specific entries to -1
  242. int iEntries = -1;
  243. // If the file exists
  244. if ( m_fp ) {
  245. // Set the number of specific entries to 0
  246. iEntries++;
  247. char szInput[128] = { '\0' };
  248. // Itterate the lines of the file
  249. while ( !feof ( m_fp ) ) {
  250. // Grab the current line
  251. fgets ( szInput, 128, m_fp );
  252. // Check for comments or new lines
  253. if ( ( szInput[0] != '#' ) && ( szInput[0] != '\n' ) ) {
  254. char* szCurrentEntry = strtok ( szInput, " " );
  255. // If this is an entry we are looking for
  256. if ( stricmp ( szCurrentEntry, szEntry ) == 0 ) {
  257. // Increment the number of specific entries counter
  258. iEntries++;
  259. }
  260. }
  261. }
  262. // Reset the file pointer to the start of the file
  263. rewind ( m_fp );
  264. }
  265. else
  266. {
  267. // Print the file error
  268. perror ( m_szFileName );
  269. }
  270. // Return the number of specific entries
  271. return iEntries;
  272. }
  273. int CConfig::GetNumberOfEntryProperties ( char* szEntry, int iInstance )
  274. {
  275. // Set the number of specific entry properties to 0
  276. int iProperties = 0;
  277. // If the file exists
  278. if ( m_fp ) {
  279. char szInput[256] = { '\0' };
  280. int iHits = 1;
  281. // Itterate the lines of the file
  282. while ( !feof ( m_fp ) ) {
  283. // Grab the current line
  284. if ( fgets ( szInput, 256, m_fp ) ) {
  285. // Check for comments
  286. if ( szInput[0] == '#' ) continue;
  287. // Check for a new line
  288. else if ( szInput[0] == '\n' ) continue;
  289. else {
  290. char* szCurrentEntry = strtok(szInput, " ");
  291. // If this is an entry we are looking for
  292. if ( stricmp ( szCurrentEntry, szEntry ) == 0 ) {
  293. // If this is the specific entry we are looking for
  294. if ( iHits == iInstance ) {
  295. char* szNextToken = strtok(NULL, " ");
  296. // Check there are some properties
  297. if ( szNextToken != NULL ) {
  298. iProperties++;
  299. // Itterate the properties
  300. while ( szNextToken != NULL ) {
  301. szNextToken = strtok ( NULL, " " );
  302. if ( szNextToken != NULL ) {
  303. // Check for comments
  304. if ( szNextToken[0] == '#' ) break;
  305. else {
  306. // Increment the property counter
  307. iProperties++;
  308. }
  309. }
  310. }
  311. }
  312. break;
  313. }
  314. else {
  315. // Increment the hits counter
  316. iHits++;
  317. }
  318. }
  319. }
  320. }
  321. }
  322. // Reset the file pointer to the start of the file
  323. rewind ( m_fp );
  324. }
  325. else
  326. {
  327. // Print the file error
  328. perror ( m_szFileName );
  329. }
  330. // Return the number of properties
  331. return iProperties;
  332. }
  333. void CConfig::GetLine ( int iLines, char* szReturnText )
  334. {
  335. int i = 0;
  336. // If the file exists
  337. if ( m_fp ) {
  338. i++;
  339. char szInput[256];
  340. memset ( szInput, '\0', 256 );
  341. // Itterate the lines of the file
  342. while ( !feof ( m_fp ) ) {
  343. // Grab the current line
  344. if ( fgets ( szInput, 256, m_fp ) ) {
  345. // If this is the line we are looking for
  346. if ( i == iLines )
  347. {
  348. // Copy the line to the return buffer
  349. strcpy ( szReturnText, szInput );
  350. break;
  351. }
  352. // Increment the line counter
  353. i++;
  354. }
  355. }
  356. // Reset the file pointer to the start of the file
  357. rewind ( m_fp );
  358. }
  359. else
  360. {
  361. // Print the file error
  362. perror ( m_szFileName );
  363. }
  364. }
  365. int CConfig::WriteToConfig ( char* szInput )
  366. {
  367. // Set the file pointer to write mode
  368. m_fp = freopen ( m_szFileName, "at", m_fp );
  369. // If the file exists
  370. if ( m_fp ) {
  371. // Print a new line
  372. fprintf ( m_fp, "\n" );
  373. // Print the line to the end of the file
  374. fprintf ( m_fp, szInput );
  375. }
  376. else
  377. {
  378. // Print the file error
  379. perror ( m_szFileName );
  380. }
  381. // Set the file pointer to read mode for the rest of the object
  382. m_fp = freopen ( m_szFileName, "r", m_fp );
  383. return 0;
  384. }
  385. void CConfig::SetFileName ( char* szFileName )
  386. {
  387. strcpy ( m_szFileName, szFileName );
  388. }
  389. void CConfig::GetFileName (char* szFileName )
  390. {
  391. strcpy ( szFileName, m_szFileName );
  392. }
  393. bool CConfig::DoesFileExist ()
  394. {
  395. // Check the file exists
  396. return m_fp != NULL;
  397. }