Guest User

Untitled

a guest
Oct 20th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. // ------------------
  2. // CConfig.cpp - VrEngine configuration file management
  3. // Author: EpicLegion (C) 2011
  4. // ------------------
  5.  
  6. #include "CConfig.hpp"
  7.  
  8. namespace VrEngine {
  9.  
  10. // Constructor/destructor
  11. CConfig::CConfig()
  12. {
  13. m_keyToType = new std::map<const std::string, ConfigType>;
  14. m_keyToPointer = new std::map<const std::string, void*>;
  15. }
  16.  
  17. CConfig::~CConfig()
  18. {
  19. // Iterate key-pointer map
  20. std::map<const std::string, void*>::iterator iter;
  21.  
  22. for(iter = m_keyToPointer->begin(); iter != m_keyToPointer->end(); iter++)
  23. {
  24. // Free
  25. if(iter->second != NULL)
  26. {
  27. delete iter->second;
  28. }
  29. }
  30.  
  31. // Delete maps
  32. delete m_keyToType;
  33. delete m_keyToPointer;
  34. }
  35.  
  36. // General stuff
  37. bool CConfig::RegisterNewCvar(const std::string& key, ConfigType type)
  38. {
  39. // Already exists?
  40. if(this->GetType(key) != CONFIG_NONE)
  41. {
  42. return false;
  43. }
  44.  
  45. // Invalid type?
  46. if(type == CONFIG_NONE)
  47. {
  48. return false;
  49. }
  50.  
  51. // Add to map
  52. (*m_keyToType)[key] = type;
  53. (*m_keyToPointer)[key] = NULL;
  54.  
  55. // Done
  56. return true;
  57. }
  58.  
  59. ConfigType CConfig::GetType(const std::string& key) const
  60. {
  61. // Find
  62. std::map<const std::string, ConfigType>::const_iterator iter;
  63.  
  64. iter = m_keyToType->find(key);
  65.  
  66. // Not found
  67. if(iter == m_keyToType->end())
  68. {
  69. return CONFIG_NONE;
  70. }
  71.  
  72. // Found
  73. return iter->second;
  74. }
  75.  
  76. // Getters
  77. Int32 CConfig::GetInt(const std::string& key, Int32 defaultValue) const
  78. {
  79. // Type check
  80. ConfigType type = this->GetType(key);
  81.  
  82. if(type != CONFIG_INT32 && type != CONFIG_UINT32)
  83. {
  84. return defaultValue;
  85. }
  86.  
  87. // Ptr
  88. Int32* ptr = (Int32*) (*m_keyToPointer)[key];
  89.  
  90. // Null?
  91. if(ptr == NULL)
  92. {
  93. return defaultValue;
  94. }
  95.  
  96. // Return
  97. return *ptr;
  98. }
  99.  
  100. const std::string& CConfig::GetString(const std::string& key, const std::string& defaultValue) const
  101. {
  102. // Oops
  103. if(this->GetType(key) != CONFIG_STRING)
  104. {
  105. return defaultValue;
  106. }
  107.  
  108. // Ptr
  109. std::string* ptr = (std::string*) (*m_keyToPointer)[key];
  110.  
  111. // Null?
  112. if(ptr == NULL)
  113. {
  114. return defaultValue;
  115. }
  116.  
  117. // Return
  118. return *ptr;
  119. }
  120.  
  121. bool CConfig::GetBool(const std::string& key, bool defaultValue) const
  122. {
  123. // Oops
  124. if(this->GetType(key) != CONFIG_BOOL)
  125. {
  126. return defaultValue;
  127. }
  128.  
  129. // Ptr
  130. bool* ptr = (bool*) (*m_keyToPointer)[key];
  131.  
  132. // Null?
  133. if(ptr == NULL)
  134. {
  135. return defaultValue;
  136. }
  137.  
  138. // Return
  139. return *ptr;
  140. }
  141.  
  142. float CConfig::GetFloat(const std::string& key, float defaultValue) const
  143. {
  144. // Oops
  145. if(this->GetType(key) != CONFIG_FLOAT)
  146. {
  147. return defaultValue;
  148. }
  149.  
  150. // Ptr
  151. float* ptr = (float*) (*m_keyToPointer)[key];
  152.  
  153. // Null?
  154. if(ptr == NULL)
  155. {
  156. return defaultValue;
  157. }
  158.  
  159. // Return
  160. return *ptr;
  161. }
  162.  
  163. // Setters
  164. void CConfig::SetInt(const std::string& key, Int32 value)
  165. {
  166. // Type check
  167. ConfigType type = this->GetType(key);
  168.  
  169. if(type != CONFIG_INT32 && type != CONFIG_UINT32)
  170. {
  171. return;
  172. }
  173.  
  174. if(type == CONFIG_UINT32 && value < 0)
  175. {
  176. return;
  177. }
  178.  
  179. // Ptr
  180. Int32* ptr = (Int32*) (*m_keyToPointer)[key];
  181.  
  182. // Null?
  183. if(ptr == NULL)
  184. {
  185. // Allocate memory
  186. ptr = new Int32;
  187.  
  188. // Set value
  189. *ptr = value;
  190.  
  191. // Set
  192. (*m_keyToPointer)[key] = (void*) ptr;
  193. }
  194. else
  195. {
  196. *ptr = value;
  197. }
  198. }
  199.  
  200. void CConfig::SetString(const std::string& key, const std::string& value)
  201. {
  202. // Oops
  203. if(this->GetType(key) != CONFIG_STRING)
  204. {
  205. return;
  206. }
  207.  
  208. // Cleanup
  209. std::string* ptr = (std::string*) (*m_keyToPointer)[key];
  210.  
  211. if(ptr != NULL)
  212. {
  213. delete ptr;
  214. ptr = NULL;
  215. }
  216.  
  217. // Allocate memory
  218. ptr = new std::string(value);
  219.  
  220. // Set
  221. (*m_keyToPointer)[key] = (void*) ptr;
  222. }
  223.  
  224. void CConfig::SetBool(const std::string& key, bool value)
  225. {
  226. // Oops
  227. if(this->GetType(key) != CONFIG_BOOL)
  228. {
  229. return;
  230. }
  231.  
  232. // Ptr
  233. bool* ptr = (bool*) (*m_keyToPointer)[key];
  234.  
  235. // Null?
  236. if(ptr == NULL)
  237. {
  238. // Allocate memory
  239. ptr = new bool;
  240.  
  241. // Set value
  242. *ptr = value;
  243.  
  244. // Set
  245. (*m_keyToPointer)[key] = (void*) ptr;
  246. }
  247. else
  248. {
  249. *ptr = value;
  250. }
  251. }
  252.  
  253. void CConfig::SetFloat(const std::string& key, float value)
  254. {
  255. // Oops
  256. if(this->GetType(key) != CONFIG_FLOAT)
  257. {
  258. return;
  259. }
  260.  
  261. // Ptr
  262. float* ptr = (float*) (*m_keyToPointer)[key];
  263.  
  264. // Null?
  265. if(ptr == NULL)
  266. {
  267. // Allocate memory
  268. ptr = new float;
  269.  
  270. // Set value
  271. *ptr = value;
  272.  
  273. // Set
  274. (*m_keyToPointer)[key] = (void*) ptr;
  275. }
  276. else
  277. {
  278. *ptr = value;
  279. }
  280. }
  281.  
  282. };
Add Comment
Please, Sign In to add comment