1
0

iniparser.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #include "iniparser.h"
  2. #include "hdef.h"
  3. #include "herr.h"
  4. #include "hstring.h"
  5. #include "hfile.h"
  6. #include "hbase.h"
  7. #include <sstream>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. IniParser::IniParser() {
  12. _comment = DEFAULT_INI_COMMENT;
  13. _delim = DEFAULT_INI_DELIM;
  14. root_ = NULL;
  15. }
  16. IniParser::~IniParser() {
  17. Unload();
  18. }
  19. int IniParser::Unload() {
  20. SAFE_DELETE(root_);
  21. return 0;
  22. }
  23. int IniParser::LoadFromFile(const char* filepath) {
  24. _filepath = filepath;
  25. HFile file;
  26. if (file.open(filepath, "r") != 0) {
  27. return ERR_OPEN_FILE;
  28. }
  29. std::string str;
  30. file.readall(str);
  31. return LoadFromMem(str.c_str());
  32. }
  33. int IniParser::LoadFromMem(const char* data) {
  34. Unload();
  35. root_ = new IniNode;
  36. root_->type = IniNode::INI_NODE_TYPE_ROOT;
  37. std::stringstream ss;
  38. ss << data;
  39. std::string strLine;
  40. int line = 0;
  41. string::size_type pos;
  42. string content;
  43. string comment;
  44. string strDiv;
  45. IniNode* pScopeNode = root_;
  46. IniNode* pNewNode = NULL;
  47. while (std::getline(ss, strLine)) {
  48. ++line;
  49. content = trimL(strLine);
  50. if (content.length() == 0) {
  51. // blank line
  52. strDiv += '\n';
  53. continue;
  54. }
  55. // trim_comment
  56. comment = "";
  57. pos = content.find_first_of(_comment);
  58. if (pos != string::npos) {
  59. comment = content.substr(pos);
  60. content = content.substr(0, pos);
  61. }
  62. content = trimR(content);
  63. if (content.length() == 0) {
  64. strDiv += strLine;
  65. strDiv += '\n';
  66. continue;
  67. } else if (strDiv.length() != 0) {
  68. IniNode* pNode = new IniNode;
  69. pNode->type = IniNode::INI_NODE_TYPE_DIV;
  70. pNode->label = strDiv;
  71. pScopeNode->Add(pNode);
  72. strDiv = "";
  73. }
  74. if (content[0] == '[') {
  75. if (content[content.length()-1] == ']') {
  76. // section
  77. content = trim(content.substr(1, content.length()-2));
  78. pNewNode = new IniNode;
  79. pNewNode->type = IniNode::INI_NODE_TYPE_SECTION;
  80. pNewNode->label = content;
  81. root_->Add(pNewNode);
  82. pScopeNode = pNewNode;
  83. } else {
  84. // hlogw("format error, line:%d", line);
  85. continue; // ignore
  86. }
  87. } else {
  88. pos = content.find_first_of(_delim);
  89. if (pos != string::npos) {
  90. // key-value
  91. pNewNode = new IniNode;
  92. pNewNode->type = IniNode::INI_NODE_TYPE_KEY_VALUE;
  93. pNewNode->label = trim(content.substr(0, pos));
  94. pNewNode->value = trim(content.substr(pos+_delim.length()));
  95. pScopeNode->Add(pNewNode);
  96. } else {
  97. // hlogw("format error, line:%d", line);
  98. continue; // ignore
  99. }
  100. }
  101. if (comment.length() != 0) {
  102. // tail_comment
  103. IniNode* pNode = new IniNode;
  104. pNode->type = IniNode::INI_NODE_TYPE_SPAN;
  105. pNode->label = comment;
  106. pNewNode->Add(pNode);
  107. comment = "";
  108. }
  109. }
  110. // file end comment
  111. if (strDiv.length() != 0) {
  112. IniNode* pNode = new IniNode;
  113. pNode->type = IniNode::INI_NODE_TYPE_DIV;
  114. pNode->label = strDiv;
  115. root_->Add(pNode);
  116. }
  117. return 0;
  118. }
  119. void IniParser::DumpString(IniNode* pNode, string& str) {
  120. if (pNode == NULL) return;
  121. if (pNode->type != IniNode::INI_NODE_TYPE_SPAN) {
  122. if (str.length() > 0 && str[str.length()-1] != '\n') {
  123. str += '\n';
  124. }
  125. }
  126. switch (pNode->type) {
  127. case IniNode::INI_NODE_TYPE_SECTION: {
  128. str += '[';
  129. str += pNode->label;
  130. str += ']';
  131. }
  132. break;
  133. case IniNode::INI_NODE_TYPE_KEY_VALUE: {
  134. str += asprintf("%s %s %s", pNode->label.c_str(), _delim.c_str(), pNode->value.c_str());
  135. }
  136. break;
  137. case IniNode::INI_NODE_TYPE_DIV: {
  138. str += pNode->label;
  139. }
  140. break;
  141. case IniNode::INI_NODE_TYPE_SPAN: {
  142. str += '\t';
  143. str += pNode->label;
  144. }
  145. break;
  146. default:
  147. break;
  148. }
  149. for (auto p : pNode->children) {
  150. DumpString(p, str);
  151. }
  152. }
  153. string IniParser::DumpString() {
  154. string str;
  155. DumpString(root_, str);
  156. return str;
  157. }
  158. int IniParser::Save() {
  159. return SaveAs(_filepath.c_str());
  160. }
  161. int IniParser::SaveAs(const char* filepath) {
  162. string str = DumpString();
  163. if (str.length() == 0) {
  164. return 0;
  165. }
  166. HFile file;
  167. if (file.open(filepath, "w") != 0) {
  168. return ERR_SAVE_FILE;
  169. }
  170. file.write(str.c_str(), str.length());
  171. return 0;
  172. }
  173. string IniParser::GetValue(const string& key, const string& section) {
  174. if (root_ == NULL) return "";
  175. IniNode* pSection = root_;
  176. if (section.length() != 0) {
  177. pSection = root_->Get(section, IniNode::INI_NODE_TYPE_SECTION);
  178. if (pSection == NULL) return "";
  179. }
  180. IniNode* pKV = pSection->Get(key, IniNode::INI_NODE_TYPE_KEY_VALUE);
  181. if (pKV == NULL) return "";
  182. return pKV->value;
  183. }
  184. void IniParser::SetValue(const string& key, const string& value, const string& section) {
  185. if (root_ == NULL) {
  186. root_ = new IniNode;
  187. }
  188. IniNode* pSection = root_;
  189. if (section.length() != 0) {
  190. pSection = root_->Get(section, IniNode::INI_NODE_TYPE_SECTION);
  191. if (pSection == NULL) {
  192. pSection = new IniNode;
  193. pSection->type = IniNode::INI_NODE_TYPE_SECTION;
  194. pSection->label = section;
  195. root_->Add(pSection);
  196. }
  197. }
  198. IniNode* pKV = pSection->Get(key, IniNode::INI_NODE_TYPE_KEY_VALUE);
  199. if (pKV == NULL) {
  200. pKV = new IniNode;
  201. pKV->type = IniNode::INI_NODE_TYPE_KEY_VALUE;
  202. pKV->label = key;
  203. pSection->Add(pKV);
  204. }
  205. pKV->value = value;
  206. }
  207. template<>
  208. bool IniParser::Get(const string& key, const string& section, bool defvalue) {
  209. string str = GetValue(key, section);
  210. return str.empty() ? defvalue : getboolean(str.c_str());
  211. }
  212. template<>
  213. int IniParser::Get(const string& key, const string& section, int defvalue) {
  214. string str = GetValue(key, section);
  215. return str.empty() ? defvalue : atoi(str.c_str());
  216. }
  217. template<>
  218. float IniParser::Get(const string& key, const string& section, float defvalue) {
  219. string str = GetValue(key, section);
  220. return str.empty() ? defvalue : atof(str.c_str());
  221. }
  222. template<>
  223. void IniParser::Set(const string& key, const bool& value, const string& section) {
  224. SetValue(key, value ? "true" : "false", section);
  225. }
  226. template<>
  227. void IniParser::Set(const string& key, const int& value, const string& section) {
  228. SetValue(key, asprintf("%d", value), section);
  229. }
  230. template<>
  231. void IniParser::Set(const string& key, const float& value, const string& section) {
  232. SetValue(key, asprintf("%f", value), section);
  233. }