KVPair.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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. // File: KVPair.cpp
  20. // Author: Matthew D. Campbell
  21. // Creation Date: 9/4/2002
  22. // Description: Key/Value Pair class
  23. // ---------------------------------------------------------------------------
  24. #include "misc.h"
  25. #include "KVPair.h"
  26. #include "debug.h"
  27. #include <stdio.h>
  28. std::string intToString(int val)
  29. {
  30. std::string s = "";
  31. bool neg = (val < 0);
  32. if (val < 0)
  33. {
  34. val = -val;
  35. }
  36. if (val == 0)
  37. return "0";
  38. char buf[2];
  39. buf[1] = 0;
  40. while (val)
  41. {
  42. buf[0] = '0' + val%10;
  43. val /= 10;
  44. s.insert(0, buf);
  45. }
  46. if (neg)
  47. s.insert(0, "-");
  48. return s;
  49. }
  50. static std::string trim(std::string s, const std::string& delim)
  51. {
  52. unsigned int i;
  53. i = s.find_first_not_of(delim);
  54. if (i != s.npos)
  55. {
  56. s = s.substr(i);
  57. }
  58. i = s.find_last_not_of(delim);
  59. if (i>=0 && i<s.npos)
  60. {
  61. s = s.substr(0, i+1);
  62. }
  63. return s;
  64. }
  65. static KeyValueMap parseIntoKVPairs( std::string s, const std::string& delim )
  66. {
  67. KeyValueMap m;
  68. bool done = false;
  69. std::string kv, k, v;
  70. while (!done)
  71. {
  72. int comma;
  73. comma = s.find_first_of(delim, 0);
  74. if (comma < 1)
  75. {
  76. done = true;
  77. kv = s;
  78. }
  79. else
  80. {
  81. kv = s.substr(0, comma);
  82. s = s.substr(comma+1);
  83. }
  84. s = trim(s, delim);
  85. kv = trim(kv, delim);
  86. if (!kv.empty())
  87. {
  88. int equals = kv.find_first_of('=', 0);
  89. if (equals < 1)
  90. {
  91. done = true;
  92. }
  93. else
  94. {
  95. k = trim(trim(kv.substr(0, equals), delim), " \t");
  96. v = trim(trim(kv.substr(equals+1), delim), " \t");
  97. if (!k.empty() && !v.empty())
  98. {
  99. m[k] = v;
  100. }
  101. }
  102. }
  103. }
  104. return m;
  105. }
  106. KVPairClass::KVPairClass( void )
  107. {}
  108. KVPairClass::KVPairClass( const std::string& in, const std::string& delim )
  109. {
  110. set( in, delim );
  111. }
  112. void KVPairClass::set( const std::string& in, const std::string& delim )
  113. {
  114. m_map = parseIntoKVPairs( in, delim );
  115. }
  116. void KVPairClass::readFromFile( const std::string& in, const std::string& delim )
  117. {
  118. m_map.clear();
  119. FILE *fp = fopen(in.c_str(), "rb");
  120. if (fp)
  121. {
  122. std::string s = "";
  123. fseek(fp, 0, SEEK_END);
  124. int len = ftell(fp);
  125. fseek(fp, 0, SEEK_SET);
  126. char *buf = new char[len + 1];
  127. memset(buf, 0, len+1);
  128. fread(buf, 1, len, fp);
  129. fclose(fp);
  130. s.append(buf);
  131. delete[] buf;
  132. set(s, delim);
  133. }
  134. }
  135. std::string KVPairClass::getStringVal( const std::string& key ) const
  136. {
  137. KeyValueMap::const_iterator it = m_map.find(key);
  138. if (it == m_map.end())
  139. {
  140. return "";
  141. }
  142. return it->second;
  143. }
  144. bool KVPairClass::getString( const std::string& key, std::string& val ) const
  145. {
  146. std::string tmp = getStringVal(key);
  147. if (tmp.empty())
  148. {
  149. return false;
  150. }
  151. val = tmp;
  152. return true;
  153. }
  154. bool KVPairClass::getInt( const std::string& key, int& val ) const
  155. {
  156. std::string tmp = getStringVal(key);
  157. if (tmp.empty())
  158. {
  159. return false;
  160. }
  161. val = atoi(tmp.c_str());
  162. return true;
  163. }
  164. bool KVPairClass::getUnsignedInt( const std::string& key, unsigned int& val ) const
  165. {
  166. std::string tmp = getStringVal(key);
  167. if (tmp.empty())
  168. {
  169. return false;
  170. }
  171. val = atoi(tmp.c_str());
  172. return true;
  173. }