configfile.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /****************************************************************************\
  19. * C O N F I D E N T I A L --- W E S T W O O D S T U D I O S *
  20. ******************************************************************************
  21. Project Name: Carpenter (The RedAlert ladder creator)
  22. File Name : configfile.cpp
  23. Author : Neal Kettler
  24. Start Date : June 9, 1997
  25. Last Update : June 17, 1997
  26. This class will read in a config file and store the key value pairs for
  27. later access. This is a fairly simple class, the config file is assumed
  28. to be of the form:
  29. #comment
  30. key = value
  31. The value can then be retrieved as a string or an integer. The key on
  32. the left is used for retrieval and it must be specified in uppercase
  33. for the 'get' functions. E.g. getString("KEY",valWstring);
  34. \***************************************************************************/
  35. #include <stdlib.h>
  36. #include <stdio.h>
  37. #include <ctype.h>
  38. #include "configfile.h"
  39. #include "wdebug.h"
  40. static uint32 Wstring_Hash(const Wstring &string);
  41. static char *Eat_Spaces(char *string);
  42. ConfigFile::ConfigFile() : Dictionary_(Wstring_Hash)
  43. { }
  44. ConfigFile::~ConfigFile()
  45. { }
  46. // Read and parse the config file. The key value pairs will be stored
  47. // for later access by the getString/getInt functions.
  48. bit8 ConfigFile::readFile(FILE *in)
  49. {
  50. char string[256];
  51. char sectionname[256]; // section name like '[user parameters]'
  52. Wstring key;
  53. Wstring value;
  54. char *cptr;
  55. memset(string,0,256);
  56. memset(sectionname,0,256);
  57. sectionList.clear();
  58. while (fgets(string,256,in))
  59. {
  60. cptr=Eat_Spaces(string);
  61. if ((*cptr==0)||(*cptr=='#')) // '#' signals a comment
  62. continue;
  63. if (*cptr=='[') // new section
  64. {
  65. key=cptr;
  66. key.truncate(']'); // remove after & including the ]
  67. key.cat("]"); // put the ] back
  68. strcpy(sectionname,key.get()); // set the current section name
  69. Wstring wssectionname;
  70. if (strlen(sectionname)==2) // clear section with a "[]"
  71. {
  72. sectionname[0]=0;
  73. wssectionname.set("");
  74. }
  75. else
  76. wssectionname.set(sectionname+1);
  77. wssectionname.truncate(']');
  78. sectionList.addTail(wssectionname);
  79. continue;
  80. }
  81. if (strchr(cptr,'=')==NULL) // All config entries must have a '='
  82. continue;
  83. key=cptr;
  84. key.truncate('=');
  85. key.removeSpaces(); // No spaces allowed in the key
  86. key.toUpper(); // make key all caps
  87. // Add the section name to the end of the key
  88. if (strlen(sectionname))
  89. key.cat(sectionname);
  90. cptr=Eat_Spaces(strchr(cptr,'=')+1); // Jump to after the '='
  91. value=cptr;
  92. value.truncate('\r');
  93. value.truncate('\n');
  94. value.truncate('#');
  95. // Remove trailing spaces
  96. while(isgraph(value.get()[strlen(value.get())-1])==0)
  97. value.get()[strlen(value.get())-1]=0;
  98. Critsec_.lock();
  99. Dictionary_.add(key,value);
  100. Critsec_.unlock();
  101. }
  102. return(TRUE);
  103. }
  104. //
  105. // Enum through the config strings. To start, index & offset should be 0
  106. // If retval is false you're done, ignore whatever's in key & value.
  107. //
  108. // Section specifies the configfile section. Set to NULL if you don't care.
  109. //
  110. bit8 ConfigFile::enumerate(int &index, int &offset, Wstring &key, Wstring &value, IN char *section) const
  111. {
  112. int seclen = strlen(section);
  113. while(1)
  114. {
  115. Critsec_.lock();
  116. if (Dictionary_.iterate(index,offset,key,value)==FALSE) // out of keys?
  117. {
  118. Critsec_.unlock();
  119. return(FALSE);
  120. }
  121. Critsec_.unlock();
  122. if (section==NULL) // no specified section, so any will do...
  123. break;
  124. if (strlen(section)+2 >= strlen(key.get())) // key should have form: X[section]
  125. continue;
  126. // Is this key part of our section?
  127. const char *keystr = key.get() + strlen(key.get())-seclen-1;
  128. if (strncmp(keystr,section,strlen(section))==0)
  129. break;
  130. }
  131. key.truncate('['); // remove the section name
  132. return(TRUE);
  133. }
  134. // Get a config entry as a string
  135. bit8 ConfigFile::getString(IN Wstring &_key, Wstring &value, IN char *section) const
  136. {
  137. Wstring key(_key);
  138. key.toUpper();
  139. if (section) // append section name to key
  140. {
  141. key+="[";
  142. key+=section;
  143. key+="]";
  144. }
  145. Critsec_.lock();
  146. bit8 retval=Dictionary_.getValue(key,value);
  147. Critsec_.unlock();
  148. if (retval==FALSE)
  149. {
  150. DBGMSG("Config entry missing: "<<key.get());
  151. }
  152. return(retval);
  153. }
  154. // Get a config entry as a string
  155. bit8 ConfigFile::getString(IN char *key,Wstring &value, IN char *section) const
  156. {
  157. Wstring sKey;
  158. sKey.set(key);
  159. return(getString(sKey,value,section));
  160. }
  161. // Get a config entry as an integer
  162. bit8 ConfigFile::getInt(IN Wstring &_key,sint32 &value, IN char *section) const
  163. {
  164. Wstring key(_key);
  165. key.toUpper();
  166. if (section) // append section name to key
  167. {
  168. key+="[";
  169. key+=section;
  170. key+="]";
  171. }
  172. Wstring svalue;
  173. Critsec_.lock();
  174. bit8 retval=Dictionary_.getValue(key,svalue);
  175. Critsec_.unlock();
  176. if (retval==FALSE)
  177. { DBGMSG("Config entry missing: "<<key.get()); }
  178. if (retval==FALSE)
  179. return(FALSE);
  180. value=atol(svalue.get());
  181. return(TRUE);
  182. }
  183. // Get a config entry as an integer
  184. bit8 ConfigFile::getInt(IN char *key,sint32 &value, IN char *section) const
  185. {
  186. Wstring sKey;
  187. sKey.set(key);
  188. return(getInt(sKey,value,section));
  189. }
  190. // Get a config entry as an integer
  191. bit8 ConfigFile::getInt(IN Wstring &_key,sint16 &value, IN char *section) const
  192. {
  193. Wstring key(_key);
  194. key.toUpper();
  195. if (section) // append section name to key
  196. {
  197. key+="[";
  198. key+=section;
  199. key+="]";
  200. }
  201. Wstring svalue;
  202. Critsec_.lock();
  203. bit8 retval=Dictionary_.getValue(key,svalue);
  204. Critsec_.unlock();
  205. if (retval==FALSE)
  206. { DBGMSG("Config entry missing: "<<key.get()); }
  207. if (retval==FALSE)
  208. return(FALSE);
  209. value=atoi(svalue.get());
  210. return(TRUE);
  211. }
  212. // Get a config entry as an integer
  213. bit8 ConfigFile::getInt(IN char *key,sint16 &value, IN char *section) const
  214. {
  215. Wstring sKey;
  216. sKey.set(key);
  217. return(getInt(sKey,value,section));
  218. }
  219. /************* MDC; Added functionality for updating and saving config files ************/
  220. // Remove an entry
  221. bit8 ConfigFile::removeEntry(IN Wstring &_key, IN char *section)
  222. {
  223. Wstring key(_key);
  224. key.toUpper();
  225. if (section) // append section name to key
  226. {
  227. key+="[";
  228. key+=section;
  229. key+="]";
  230. }
  231. Critsec_.lock();
  232. bit8 retval=Dictionary_.remove(key);
  233. Critsec_.unlock();
  234. if (retval==FALSE)
  235. { DBGMSG("Config entry missing: "<<key.get()); }
  236. if (retval==FALSE)
  237. return(FALSE);
  238. return(TRUE);
  239. }
  240. // Remove an entry
  241. bit8 ConfigFile::removeEntry(IN char *key, IN char *section)
  242. {
  243. Wstring sKey;
  244. sKey.set(key);
  245. return removeEntry(sKey, section);
  246. }
  247. // Set a config entry as a string
  248. bit8 ConfigFile::setString(IN Wstring &_key, IN Wstring &value, IN char *section)
  249. {
  250. Wstring key(_key);
  251. key.toUpper();
  252. if (section) // append section name to key
  253. {
  254. key+="[";
  255. key+=section;
  256. key+="]";
  257. }
  258. else
  259. {
  260. section = ""; // give it a default
  261. }
  262. Critsec_.lock();
  263. Dictionary_.remove(key);
  264. bit8 retval=Dictionary_.add(key,value);
  265. // Test for a new section
  266. Wstring test;
  267. int i;
  268. for (i=0; i<sectionList.length(); i++)
  269. {
  270. sectionList.get(test, i);
  271. if (!strcmp(test.get(), section))
  272. break;
  273. }
  274. if (i == sectionList.length())
  275. {
  276. // New section!
  277. //DBGMSG("New section " << section << ", " << sectionList.length() << " entries");
  278. test.set(section);
  279. sectionList.addTail(test);
  280. }
  281. Critsec_.unlock();
  282. if (retval==FALSE)
  283. {
  284. DBGMSG("Config could not set entry: "<<key.get());
  285. }
  286. return(retval);
  287. }
  288. // Set a config entry as a string
  289. bit8 ConfigFile::setString(IN char *key,IN Wstring &value, IN char *section)
  290. {
  291. Wstring sKey;
  292. sKey.set(key);
  293. return(setString(sKey,value,section));
  294. }
  295. // Set a config entry as an integer
  296. bit8 ConfigFile::setInt(IN Wstring &_key,IN sint32 &value, IN char *section)
  297. {
  298. Wstring key(_key);
  299. key.toUpper();
  300. if (section) // append section name to key
  301. {
  302. key+="[";
  303. key+=section;
  304. key+="]";
  305. }
  306. else
  307. {
  308. section = ""; // give it a default
  309. }
  310. Wstring svalue;
  311. svalue.setFormatted("%d", value);
  312. Critsec_.lock();
  313. Dictionary_.remove(key);
  314. bit8 retval=Dictionary_.add(key,svalue);
  315. // Test for a new section
  316. Wstring test;
  317. //DBGMSG("Testing " << sectionList.length() << " entries for " << section);
  318. int i;
  319. for (i=0; i<sectionList.length(); i++)
  320. {
  321. sectionList.get(test, i);
  322. //DBGMSG("Looking at " << test.get());
  323. if (!strcmp(test.get(), section))
  324. break;
  325. }
  326. if (i == sectionList.length() && section)
  327. {
  328. // New section!
  329. //DBGMSG("New section " << section << ", " << sectionList.length() << " entries");
  330. test.set(section);
  331. sectionList.addTail(test);
  332. }
  333. Critsec_.unlock();
  334. if (retval==FALSE)
  335. { DBGMSG("Config could not set entry: "<<key.get()); }
  336. if (retval==FALSE)
  337. return(FALSE);
  338. return(TRUE);
  339. }
  340. // Set a config entry as an integer
  341. bit8 ConfigFile::setInt(IN char *key,IN sint32 &value, IN char *section)
  342. {
  343. Wstring sKey;
  344. sKey.set(key);
  345. return(setInt(sKey,value,section));
  346. }
  347. // Write config file to disk. Does not preserve comments, etc.
  348. bit8 ConfigFile::writeFile(FILE *config)
  349. {
  350. if (!config)
  351. {
  352. ERRMSG("No FP on config file write!");
  353. return FALSE;
  354. }
  355. int index = 0;
  356. int offset = 0;
  357. Wstring key;
  358. Wstring value;
  359. Wstring section;
  360. //DBGMSG(sectionList.length() << " entries");
  361. for (int i=0; i<sectionList.length(); i++)
  362. {
  363. sectionList.get(section, i);
  364. //DBGMSG("Writing " << section.get());
  365. fprintf(config, "[%s]\n", section.get());
  366. index = 0;
  367. offset = 0;
  368. while (enumerate(index, offset, key, value, section.get())!=FALSE)
  369. {
  370. fprintf(config, "%s=%s\n", key.get(), value.get());
  371. }
  372. fprintf(config, "\n");
  373. }
  374. return TRUE;
  375. }
  376. /************* Static functions below **************/
  377. // Given a Wstring, return a 32 bit integer that has a good numeric
  378. // distributation for the purposes of indexing into a hash table.
  379. static uint32 Wstring_Hash(const Wstring &string)
  380. {
  381. uint32 retval=0;
  382. retval=string.length();
  383. for (uint32 i=0; i<string.length(); i++)
  384. {
  385. retval+=*(string.get()+i);
  386. retval+=i;
  387. retval=(retval<<8)^(retval>>24); // ROL 8
  388. }
  389. return(retval);
  390. }
  391. static char *Eat_Spaces(char *string)
  392. {
  393. char *retval=string;
  394. while (isspace(*retval))
  395. retval++;
  396. return(retval);
  397. }