CONFIGFILE.CPP 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. ** Command & Conquer Red Alert(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. static uint32 Wstring_Hash(Wstring &string);
  40. static char *Eat_Spaces(char *string);
  41. ConfigFile::ConfigFile() : dictionary(Wstring_Hash)
  42. { }
  43. ConfigFile::~ConfigFile()
  44. { }
  45. // Read and parse the config file. The key value pairs will be stored
  46. // for later access by the getString/getInt functions.
  47. bit8 ConfigFile::readFile(FILE *in)
  48. {
  49. char string[256];
  50. Wstring key;
  51. Wstring value;
  52. char *cptr;
  53. memset(string,0,256);
  54. while (fgets(string,256,in))
  55. {
  56. cptr=Eat_Spaces(string);
  57. if ((*cptr==0)||(*cptr=='#')) // '#' signals a comment
  58. continue;
  59. if (strchr(cptr,'=')==NULL) // All config entries must have a '='
  60. continue;
  61. key=cptr;
  62. key.truncate('=');
  63. key.removeSpaces(); // No spaces allowed in the key
  64. key.toUpper(); // make key all caps
  65. cptr=Eat_Spaces(strchr(cptr,'=')+1); // Jump to after the '='
  66. value=cptr;
  67. value.truncate('\r');
  68. value.truncate('\n');
  69. dictionary.add(key,value);
  70. }
  71. return(TRUE);
  72. }
  73. // Get a config entry as a string
  74. bit8 ConfigFile::getString(Wstring &key,Wstring &value)
  75. {
  76. return(dictionary.getValue(key,value));
  77. }
  78. // Get a config entry as a string
  79. bit8 ConfigFile::getString(char *key,Wstring &value)
  80. {
  81. Wstring sKey;
  82. sKey.set(key);
  83. return(getString(sKey,value));
  84. }
  85. // Get a config entry as an integer
  86. bit8 ConfigFile::getInt(Wstring &key,sint32 &value)
  87. {
  88. Wstring svalue;
  89. bit8 retval=dictionary.getValue(key,svalue);
  90. if (retval==FALSE)
  91. return(FALSE);
  92. value=atol(svalue.get());
  93. return(TRUE);
  94. }
  95. // Get a config entry as an integer
  96. bit8 ConfigFile::getInt(char *key,sint32 &value)
  97. {
  98. Wstring sKey;
  99. sKey.set(key);
  100. return(getInt(sKey,value));
  101. }
  102. // Get a config entry as an integer
  103. bit8 ConfigFile::getInt(Wstring &key,sint16 &value)
  104. {
  105. Wstring svalue;
  106. bit8 retval=dictionary.getValue(key,svalue);
  107. if (retval==FALSE)
  108. return(FALSE);
  109. value=atoi(svalue.get());
  110. return(TRUE);
  111. }
  112. // Get a config entry as an integer
  113. bit8 ConfigFile::getInt(char *key,sint16 &value)
  114. {
  115. Wstring sKey;
  116. sKey.set(key);
  117. return(getInt(sKey,value));
  118. }
  119. /************* Static functions below **************/
  120. // Given a Wstring, return a 32 bit integer that has a good numeric
  121. // distributation for the purposes of indexing into a hash table.
  122. static uint32 Wstring_Hash(Wstring &string)
  123. {
  124. uint32 retval=0;
  125. retval=string.length();
  126. for (uint32 i=0; i<string.length(); i++)
  127. {
  128. retval+=*(string.get()+i);
  129. retval+=i;
  130. retval=(retval<<8)^(retval>>24); // ROL 8
  131. }
  132. return(retval);
  133. }
  134. static char *Eat_Spaces(char *string)
  135. {
  136. char *retval=string;
  137. while (isspace(*retval))
  138. retval++;
  139. return(retval);
  140. }