config.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*!
  2. *\file
  3. */
  4. // Config Class
  5. // Author: Charles Gruenwald III
  6. #include "config.hpp"
  7. #include <boost/algorithm/string.hpp>
  8. #include <cstdarg>
  9. #include <cstdio>
  10. namespace config
  11. {
  12. void Error(const char* format, ...)
  13. {
  14. fprintf(stderr, "\n*** Configuration error ***\n");
  15. va_list args;
  16. va_start(args, format);
  17. vfprintf(stderr, format, args);
  18. va_end(args);
  19. fprintf(stderr, "\n\n");
  20. exit(0);
  21. }
  22. bool Config::isLeaf(const String & path)
  23. {
  24. return !boost::find_first(path, "/");
  25. }
  26. //Configuration Management
  27. const Section & Config::getSection(const String & path)
  28. {
  29. return getSection_unsafe(path);
  30. }
  31. Section & Config::getSection_unsafe(const String & path)
  32. {
  33. //Handle the base case
  34. if(isLeaf(path))
  35. {
  36. if(!m_root.hasSection(path))
  37. m_root.addSubsection(path);
  38. return m_root.getSection_unsafe(path);
  39. }
  40. //split up the path on "/", and loop through each entry of this
  41. //split to obtain the actual section
  42. PathElementList path_elements;
  43. Config::splitPathElements(path, path_elements);
  44. Section * current = &m_root;
  45. for(PathElementList::iterator path_element = path_elements.begin();
  46. path_element != path_elements.end(); path_element++)
  47. {
  48. //add the section if it doesn't already exist
  49. if(!current->hasSection(*path_element))
  50. {
  51. current->addSubsection(*path_element);
  52. }
  53. //Find the current element name as a sub section of the current section
  54. current = &(current->getSection_unsafe(*path_element));
  55. }
  56. return *current;
  57. }
  58. //Small wrapper which sets the m_path variable appropriatly and calls the virtual loadConfig()
  59. void Config::load(const String & path)
  60. {
  61. m_path = path;
  62. loadConfig();
  63. }
  64. void Config::clear()
  65. {
  66. m_root.clear();
  67. }
  68. bool Config::hasKey(const String & path, UInt64 index)
  69. {
  70. //Handle the base case
  71. if(isLeaf(path))
  72. {
  73. if(m_root.hasKey(path, index))
  74. return true;
  75. else
  76. return false;
  77. }
  78. //Disect the path
  79. PathPair path_pair = Config::splitPath(path);
  80. Section & section = getSection_unsafe(path_pair.first);
  81. if(section.hasKey(path_pair.second, index))
  82. return true;
  83. else
  84. return false;
  85. }
  86. const Key & Config::getKey(const String & path, UInt64 index)
  87. {
  88. //Handle the base case
  89. if(isLeaf(path))
  90. {
  91. if(!m_root.hasKey(path, index))
  92. {
  93. if (index == UINT64_MAX)
  94. config::Error("Configuration value %s not found.", path.c_str());
  95. else
  96. config::Error("Configuration value %s[%i] not found.", path.c_str(), index);
  97. }
  98. else
  99. {
  100. return m_root.getKey(path, index);
  101. }
  102. }
  103. //Disect the path
  104. PathPair path_pair = Config::splitPath(path);
  105. Section & section = getSection_unsafe(path_pair.first);
  106. if(!section.hasKey(path_pair.second, index))
  107. {
  108. if (index == UINT64_MAX)
  109. config::Error("Configuration value %s not found.", path.c_str());
  110. else
  111. config::Error("Configuration value %s[%i] not found.", path.c_str(), index);
  112. }
  113. return section.getKey(path_pair.second, index);
  114. }
  115. const Section & Config::addSection(const String & path)
  116. {
  117. //Disect the path
  118. PathPair path_pair = Config::splitPath(path);
  119. Section &parent = getSection_unsafe(path_pair.first);
  120. return parent.addSubsection(path_pair.second);
  121. }
  122. std::pair<String,String> Config::splitPath(const String & path)
  123. {
  124. //Throw away path_elements, just return base and key/section
  125. std::vector<String> path_elements;
  126. return Config::splitPathElements(path, path_elements);
  127. }
  128. std::pair<String,String> Config::splitPathElements(const String & path, PathElementList & path_elements)
  129. {
  130. //split up the path on "/", the last entry is the name of the key
  131. //Everything up to the last "/" is the 'base_path' (which will specify a section)
  132. boost::split(path_elements, path, boost::is_any_of("/"));
  133. //Grab the appropriate pieces from the split
  134. String key_name = path_elements[path_elements.size() - 1];
  135. String base_path = "";
  136. if(path.rfind("/") != String::npos)
  137. base_path = path.substr(0, path.rfind("/"));
  138. return PathPair(base_path,key_name);
  139. }
  140. template <class V>
  141. const Key & Config::addKeyInternal(const String & path, const V & value, UInt64 index)
  142. {
  143. //Handle the base case
  144. if(isLeaf(path))
  145. return m_root.addKey(path, value, index);
  146. PathPair path_pair = Config::splitPath(path);
  147. Section &parent = getSection_unsafe(path_pair.first);
  148. return parent.addKey(path_pair.second, value, index);
  149. }
  150. //Convert the in-memory representation into a string
  151. String Config::showTree(const Section & current, int depth)
  152. {
  153. String result = "";
  154. String ret = "";
  155. String tabs = "";
  156. for(int i=0;i<depth;i++)
  157. tabs = tabs.append(" ");
  158. //First loop through all the subsections
  159. SectionList const & subsections = current.getSubsections();
  160. for(SectionList::const_iterator i = subsections.begin(); i != subsections.end(); i++)
  161. {
  162. Section const & subsection = *(i->second);
  163. result += tabs + "Section: " + i->second->getName() + "\n";
  164. //recurse
  165. result += showTree(subsection, depth+1);
  166. }
  167. //Now add all the keys of this section
  168. KeyList const & keys = current.getKeys();
  169. for(KeyList::const_iterator i = keys.begin(); i != keys.end();i++)
  170. {
  171. result += tabs + "Key: " + i->second->getName() + " - " + i->second->getString() + "\n";
  172. }
  173. return result;
  174. }
  175. void Config::set(const String & path, const String & new_value)
  176. {
  177. addKey(path, new_value);
  178. }
  179. void Config::set(const String & path, SInt64 new_value)
  180. {
  181. addKey(path, new_value);
  182. }
  183. void Config::set(const String & path, double new_value)
  184. {
  185. addKey(path, new_value);
  186. }
  187. //Below are the getters which also handle default values
  188. bool Config::getBoolArray(const String & path, UInt64 index)
  189. {
  190. return getKey(path, index).getBool();
  191. }
  192. SInt64 Config::getIntArray(const String & path, UInt64 index)
  193. {
  194. return getKey(path, index).getInt();
  195. }
  196. const String Config::getStringArray(const String & path, UInt64 index)
  197. {
  198. return getKey(path, index).getString();
  199. }
  200. double Config::getFloatArray(const String & path, UInt64 index)
  201. {
  202. return getKey(path,index).getFloat();
  203. }
  204. }//end of namespace config