Advertisement
Guest User

Untitled

a guest
Feb 5th, 2022
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "Config.h"
  3.  
  4. CConfig::CConfig()
  5. {
  6. }
  7.  
  8. CConfig::~CConfig()
  9. {
  10. Destroy();
  11. }
  12.  
  13. void CConfig::Destroy()
  14. {
  15. m_valueMap.clear();
  16. }
  17.  
  18. void CConfig::NextLine(FILE *fp)
  19. {
  20. int c;
  21.  
  22. while ((c = fgetc(fp)) != EOF)
  23. {
  24. if (c == '\n')
  25. return;
  26. }
  27. }
  28.  
  29. bool CConfig::GetWord(FILE *fp, char *tar)
  30. {
  31. int i = 0;
  32. int c;
  33.  
  34. int semicolon_mode = 0;
  35.  
  36. while ((c = fgetc(fp)) != EOF)
  37. {
  38. if (c == 13)
  39. continue;
  40.  
  41. if (semicolon_mode)
  42. {
  43. if (c == '"')
  44. {
  45. tar[i] = '\0';
  46. return true;
  47. }
  48.  
  49. tar[i++] = c;
  50. continue;
  51. }
  52. else
  53. {
  54. if (i == 0)
  55. {
  56. if (c == '"')
  57. {
  58. semicolon_mode = 1;
  59. continue;
  60. }
  61.  
  62. if (c == ' ' || c == '\t' || c == '\n')
  63. {
  64. continue;
  65. }
  66. }
  67.  
  68. if ((c == ' ' || c == '\t' || c == '\n'))
  69. {
  70. // Εά.
  71. tar[i] = '\0';
  72. return true;
  73. }
  74.  
  75. tar[i] = c;
  76. ++i;
  77. }
  78. }
  79.  
  80. return (i != 0);
  81. }
  82.  
  83. bool CConfig::GetLine(FILE* fp, char*dest)
  84. {
  85. int c;
  86. int i = 0;
  87. while ((c = fgetc(fp)) != EOF)
  88. {
  89. if (c == '\n')
  90. return true;
  91. dest[i] = c;
  92. ++i;
  93. }
  94. return true;
  95. }
  96.  
  97. bool CConfig::LoadFile(const char* filename)
  98. {
  99. char szTmp[256];
  100. char comment[256];
  101.  
  102. FILE * fp = fopen(filename, "rb");
  103.  
  104. if (fp == NULL)
  105. return false;
  106.  
  107. int mode = 0;
  108.  
  109. while (GetWord(fp, szTmp))
  110. {
  111. if (strcmp(szTmp, "//") == 0)
  112. {
  113. NextLine(fp);
  114. continue;
  115. }
  116.  
  117. switch (mode)
  118. {
  119. case 0:
  120. strlcpy(comment, szTmp, sizeof(comment));
  121. ++mode;
  122. break;
  123.  
  124. case 1:
  125. if (*szTmp == '=')
  126. ++mode;
  127. break;
  128.  
  129. case 2:
  130. mode = 0;
  131. m_valueMap.insert(TValueMap::value_type(comment, szTmp));
  132. break;
  133. }
  134.  
  135. // ITEM_ID_RANGE
  136. if (mode == 2 && strcmp(comment, "ITEM_ID_RANGE") == 0)
  137. {
  138. GetLine(fp, szTmp);
  139. m_valueMap.insert(TValueMap::value_type(comment, szTmp));
  140. mode = 0;
  141.  
  142. }
  143. // ITEM_ID_RANGE_END
  144. }
  145.  
  146.  
  147. // ΖΔΐΟ ΄έ΄Β ΊΞΊΠ.
  148. fclose(fp);
  149. return true;
  150. }
  151.  
  152. std::string * CConfig::Search(const char* key)
  153. {
  154. itertype(m_valueMap) i = m_valueMap.find(key);
  155.  
  156. if (i == m_valueMap.end())
  157. return NULL;
  158. else
  159. return (&i->second);
  160. }
  161.  
  162. bool CConfig::GetParam(const char*key, int index, DWORD *Param)
  163. {
  164. std::string * pstStr = Search(key);
  165. if (!pstStr)
  166. return false;
  167.  
  168. char szParam[5][32];
  169.  
  170. sscanf(pstStr->c_str(), "%s %s %s %s %s", szParam[0],szParam[1],szParam[2],szParam[3],szParam[4]);
  171.  
  172. str_to_number(*Param, szParam[index]);
  173.  
  174. sys_log(0, "GetParam %d", *Param);
  175. return true;
  176. }
  177. const char * CConfig::Get(const char* key)
  178. {
  179. std::string * pstStr = Search(key);
  180.  
  181. if (!pstStr)
  182. {
  183. static std::string stTemp = "";
  184. return stTemp.c_str();
  185. }
  186.  
  187. return pstStr->c_str();
  188. }
  189.  
  190.  
  191. bool CConfig::GetValue(const char * key, int* dest)
  192. {
  193. if (!Search(key))
  194. return false;
  195.  
  196. str_to_number(*dest, Get(key));
  197. return true;
  198. }
  199.  
  200. bool CConfig::GetValue(const char * key, float *dest)
  201. {
  202. if (!Search(key))
  203. return false;
  204.  
  205. str_to_number(*dest, Get(key));
  206. return true;
  207. }
  208.  
  209. bool CConfig::GetValue(const char * key, DWORD *dest)
  210. {
  211. if (!Search(key))
  212. return false;
  213.  
  214. str_to_number(*dest, Get(key));
  215. return true;
  216. }
  217.  
  218. bool CConfig::GetValue(const char * key, BYTE *dest)
  219. {
  220. if (!Search(key))
  221. return false;
  222.  
  223. *dest = *(BYTE *) Get(key);
  224. return true;
  225. }
  226.  
  227. bool CConfig::GetValue(const char * key, char *dest, size_t destSize)
  228. {
  229. if (!Search(key))
  230. return false;
  231.  
  232. strlcpy(dest, Get(key), destSize);
  233.  
  234. if (!*dest)
  235. return false;
  236.  
  237. return true;
  238. }
  239.  
  240. bool CConfig::GetTwoValue(const char* key, DWORD * dest1, DWORD *dest2)
  241. {
  242. if (!GetParam(key, 0, dest1))
  243. return false;
  244.  
  245. if (!GetParam(key, 1, dest2))
  246. return false;
  247.  
  248. return true;
  249. }
  250.  
  251.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement