iniparser.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. #include "iniparser.h"
  2. #include <list>
  3. #include <sstream>
  4. #include "hdef.h"
  5. #include "herr.h"
  6. #include "hstring.h"
  7. #include "hfile.h"
  8. #include "hbase.h"
  9. using namespace hv;
  10. /**********************************
  11. # div
  12. [section]
  13. key = value # span
  14. # div
  15. ***********************************/
  16. class IniNode {
  17. public:
  18. enum Type {
  19. INI_NODE_TYPE_UNKNOWN,
  20. INI_NODE_TYPE_ROOT,
  21. INI_NODE_TYPE_SECTION,
  22. INI_NODE_TYPE_KEY_VALUE,
  23. INI_NODE_TYPE_DIV,
  24. INI_NODE_TYPE_SPAN,
  25. } type;
  26. std::string label; // section|key|comment
  27. std::string value;
  28. std::list<IniNode*> children;
  29. virtual ~IniNode() {
  30. for (auto pNode : children) {
  31. if (pNode) {
  32. delete pNode;
  33. }
  34. }
  35. children.clear();
  36. }
  37. void Add(IniNode* pNode) {
  38. children.push_back(pNode);
  39. }
  40. void Del(IniNode* pNode) {
  41. for (auto iter = children.begin(); iter != children.end(); ++iter) {
  42. if ((*iter) == pNode) {
  43. delete (*iter);
  44. children.erase(iter);
  45. return;
  46. }
  47. }
  48. }
  49. IniNode* Get(const std::string& label, Type type = INI_NODE_TYPE_KEY_VALUE) {
  50. for (auto pNode : children) {
  51. if (pNode->type == type && pNode->label == label) {
  52. return pNode;
  53. }
  54. }
  55. return NULL;
  56. }
  57. };
  58. class IniSection : public IniNode {
  59. public:
  60. IniSection() : IniNode(), section(label) {
  61. type = INI_NODE_TYPE_SECTION;
  62. }
  63. std::string &section;
  64. };
  65. class IniKeyValue : public IniNode {
  66. public:
  67. IniKeyValue() : IniNode(), key(label) {
  68. type = INI_NODE_TYPE_KEY_VALUE;
  69. }
  70. std::string &key;
  71. };
  72. class IniComment : public IniNode {
  73. public:
  74. IniComment() : IniNode(), comment(label) {
  75. }
  76. std::string &comment;
  77. };
  78. IniParser::IniParser() {
  79. _comment = DEFAULT_INI_COMMENT;
  80. _delim = DEFAULT_INI_DELIM;
  81. root_ = NULL;
  82. }
  83. IniParser::~IniParser() {
  84. Unload();
  85. }
  86. int IniParser::Unload() {
  87. SAFE_DELETE(root_);
  88. return 0;
  89. }
  90. int IniParser::Reload() {
  91. return LoadFromFile(_filepath.c_str());
  92. }
  93. int IniParser::LoadFromFile(const char* filepath) {
  94. _filepath = filepath;
  95. HFile file;
  96. if (file.open(filepath, "r") != 0) {
  97. return ERR_OPEN_FILE;
  98. }
  99. std::string str;
  100. file.readall(str);
  101. return LoadFromMem(str.c_str());
  102. }
  103. int IniParser::LoadFromMem(const char* data) {
  104. Unload();
  105. root_ = new IniNode;
  106. root_->type = IniNode::INI_NODE_TYPE_ROOT;
  107. std::stringstream ss;
  108. ss << data;
  109. std::string strLine;
  110. int line = 0;
  111. std::string::size_type pos;
  112. std::string content, comment, strDiv;
  113. IniNode* pScopeNode = root_;
  114. IniNode* pNewNode = NULL;
  115. while (std::getline(ss, strLine)) {
  116. ++line;
  117. content = trimL(strLine);
  118. if (content.length() == 0) {
  119. // blank line
  120. strDiv += '\n';
  121. continue;
  122. }
  123. // trim_comment
  124. comment = "";
  125. pos = content.find_first_of(_comment);
  126. if (pos != std::string::npos) {
  127. comment = content.substr(pos);
  128. content = content.substr(0, pos);
  129. }
  130. content = trimR(content);
  131. if (content.length() == 0) {
  132. strDiv += strLine;
  133. strDiv += '\n';
  134. continue;
  135. } else if (strDiv.length() != 0) {
  136. IniNode* pNode = new IniNode;
  137. pNode->type = IniNode::INI_NODE_TYPE_DIV;
  138. pNode->label = strDiv;
  139. pScopeNode->Add(pNode);
  140. strDiv = "";
  141. }
  142. if (content[0] == '[') {
  143. if (content[content.length()-1] == ']') {
  144. // section
  145. content = trim(content.substr(1, content.length()-2));
  146. pNewNode = new IniNode;
  147. pNewNode->type = IniNode::INI_NODE_TYPE_SECTION;
  148. pNewNode->label = content;
  149. root_->Add(pNewNode);
  150. pScopeNode = pNewNode;
  151. } else {
  152. // hlogw("format error, line:%d", line);
  153. continue; // ignore
  154. }
  155. } else {
  156. pos = content.find_first_of(_delim);
  157. if (pos != std::string::npos) {
  158. // key-value
  159. pNewNode = new IniNode;
  160. pNewNode->type = IniNode::INI_NODE_TYPE_KEY_VALUE;
  161. pNewNode->label = trim(content.substr(0, pos));
  162. pNewNode->value = trim(content.substr(pos+_delim.length()));
  163. pScopeNode->Add(pNewNode);
  164. } else {
  165. // hlogw("format error, line:%d", line);
  166. continue; // ignore
  167. }
  168. }
  169. if (comment.length() != 0) {
  170. // tail_comment
  171. IniNode* pNode = new IniNode;
  172. pNode->type = IniNode::INI_NODE_TYPE_SPAN;
  173. pNode->label = comment;
  174. pNewNode->Add(pNode);
  175. comment = "";
  176. }
  177. }
  178. // file end comment
  179. if (strDiv.length() != 0) {
  180. IniNode* pNode = new IniNode;
  181. pNode->type = IniNode::INI_NODE_TYPE_DIV;
  182. pNode->label = strDiv;
  183. root_->Add(pNode);
  184. }
  185. return 0;
  186. }
  187. void IniParser::DumpString(IniNode* pNode, std::string& str) {
  188. if (pNode == NULL) return;
  189. if (pNode->type != IniNode::INI_NODE_TYPE_SPAN) {
  190. if (str.length() > 0 && str[str.length()-1] != '\n') {
  191. str += '\n';
  192. }
  193. }
  194. switch (pNode->type) {
  195. case IniNode::INI_NODE_TYPE_SECTION: {
  196. str += '[';
  197. str += pNode->label;
  198. str += ']';
  199. }
  200. break;
  201. case IniNode::INI_NODE_TYPE_KEY_VALUE: {
  202. str += asprintf("%s %s %s", pNode->label.c_str(), _delim.c_str(), pNode->value.c_str());
  203. }
  204. break;
  205. case IniNode::INI_NODE_TYPE_DIV: {
  206. str += pNode->label;
  207. }
  208. break;
  209. case IniNode::INI_NODE_TYPE_SPAN: {
  210. str += '\t';
  211. str += pNode->label;
  212. }
  213. break;
  214. default:
  215. break;
  216. }
  217. for (auto p : pNode->children) {
  218. DumpString(p, str);
  219. }
  220. }
  221. std::string IniParser::DumpString() {
  222. std::string str;
  223. DumpString(root_, str);
  224. return str;
  225. }
  226. int IniParser::Save() {
  227. return SaveAs(_filepath.c_str());
  228. }
  229. int IniParser::SaveAs(const char* filepath) {
  230. std::string str = DumpString();
  231. if (str.length() == 0) {
  232. return 0;
  233. }
  234. HFile file;
  235. if (file.open(filepath, "w") != 0) {
  236. return ERR_SAVE_FILE;
  237. }
  238. file.write(str.c_str(), str.length());
  239. return 0;
  240. }
  241. std::string IniParser::GetValue(const std::string& key, const std::string& section) {
  242. if (root_ == NULL) return "";
  243. IniNode* pSection = root_;
  244. if (section.length() != 0) {
  245. pSection = root_->Get(section, IniNode::INI_NODE_TYPE_SECTION);
  246. if (pSection == NULL) return "";
  247. }
  248. IniNode* pKV = pSection->Get(key, IniNode::INI_NODE_TYPE_KEY_VALUE);
  249. if (pKV == NULL) return "";
  250. return pKV->value;
  251. }
  252. void IniParser::SetValue(const std::string& key, const std::string& value, const std::string& section) {
  253. if (root_ == NULL) {
  254. root_ = new IniNode;
  255. }
  256. IniNode* pSection = root_;
  257. if (section.length() != 0) {
  258. pSection = root_->Get(section, IniNode::INI_NODE_TYPE_SECTION);
  259. if (pSection == NULL) {
  260. pSection = new IniNode;
  261. pSection->type = IniNode::INI_NODE_TYPE_SECTION;
  262. pSection->label = section;
  263. root_->Add(pSection);
  264. }
  265. }
  266. IniNode* pKV = pSection->Get(key, IniNode::INI_NODE_TYPE_KEY_VALUE);
  267. if (pKV == NULL) {
  268. pKV = new IniNode;
  269. pKV->type = IniNode::INI_NODE_TYPE_KEY_VALUE;
  270. pKV->label = key;
  271. pSection->Add(pKV);
  272. }
  273. pKV->value = value;
  274. }
  275. template<>
  276. HV_EXPORT bool IniParser::Get(const std::string& key, const std::string& section, bool defvalue) {
  277. std::string str = GetValue(key, section);
  278. return str.empty() ? defvalue : getboolean(str.c_str());
  279. }
  280. template<>
  281. HV_EXPORT int IniParser::Get(const std::string& key, const std::string& section, int defvalue) {
  282. std::string str = GetValue(key, section);
  283. return str.empty() ? defvalue : atoi(str.c_str());
  284. }
  285. template<>
  286. HV_EXPORT float IniParser::Get(const std::string& key, const std::string& section, float defvalue) {
  287. std::string str = GetValue(key, section);
  288. return str.empty() ? defvalue : atof(str.c_str());
  289. }
  290. template<>
  291. HV_EXPORT void IniParser::Set(const std::string& key, const bool& value, const std::string& section) {
  292. SetValue(key, value ? "true" : "false", section);
  293. }
  294. template<>
  295. HV_EXPORT void IniParser::Set(const std::string& key, const int& value, const std::string& section) {
  296. SetValue(key, asprintf("%d", value), section);
  297. }
  298. template<>
  299. HV_EXPORT void IniParser::Set(const std::string& key, const float& value, const std::string& section) {
  300. SetValue(key, asprintf("%f", value), section);
  301. }