1
0

iniparser.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. const char* c_str = str.c_str();
  102. unsigned char utf8_bom[3] = { 0xEF, 0xBB, 0xBF };
  103. if (str.size() >= 3 && memcmp(c_str, utf8_bom, 3) == 0) {
  104. c_str += 3;
  105. }
  106. return LoadFromMem(c_str);
  107. }
  108. int IniParser::LoadFromMem(const char* data) {
  109. Unload();
  110. root_ = new IniNode;
  111. root_->type = IniNode::INI_NODE_TYPE_ROOT;
  112. std::stringstream ss;
  113. ss << data;
  114. std::string strLine;
  115. int line = 0;
  116. std::string::size_type pos;
  117. std::string content, comment, strDiv;
  118. IniNode* pScopeNode = root_;
  119. IniNode* pNewNode = NULL;
  120. while (std::getline(ss, strLine)) {
  121. ++line;
  122. content = ltrim(strLine);
  123. if (content.length() == 0) {
  124. // blank line
  125. strDiv += '\n';
  126. continue;
  127. }
  128. // trim_comment
  129. comment = "";
  130. pos = content.find_first_of(_comment);
  131. if (pos != std::string::npos) {
  132. comment = content.substr(pos);
  133. content = content.substr(0, pos);
  134. }
  135. content = rtrim(content);
  136. if (content.length() == 0) {
  137. strDiv += strLine;
  138. strDiv += '\n';
  139. continue;
  140. } else if (strDiv.length() != 0) {
  141. IniNode* pNode = new IniNode;
  142. pNode->type = IniNode::INI_NODE_TYPE_DIV;
  143. pNode->label = strDiv;
  144. pScopeNode->Add(pNode);
  145. strDiv = "";
  146. }
  147. if (content[0] == '[') {
  148. if (content[content.length()-1] == ']') {
  149. // section
  150. content = trim(content.substr(1, content.length()-2));
  151. pNewNode = new IniNode;
  152. pNewNode->type = IniNode::INI_NODE_TYPE_SECTION;
  153. pNewNode->label = content;
  154. root_->Add(pNewNode);
  155. pScopeNode = pNewNode;
  156. } else {
  157. // hlogw("format error, line:%d", line);
  158. continue; // ignore
  159. }
  160. } else {
  161. pos = content.find_first_of(_delim);
  162. if (pos != std::string::npos) {
  163. // key-value
  164. pNewNode = new IniNode;
  165. pNewNode->type = IniNode::INI_NODE_TYPE_KEY_VALUE;
  166. pNewNode->label = trim(content.substr(0, pos));
  167. pNewNode->value = trim(content.substr(pos+_delim.length()));
  168. pScopeNode->Add(pNewNode);
  169. } else {
  170. // hlogw("format error, line:%d", line);
  171. continue; // ignore
  172. }
  173. }
  174. if (comment.length() != 0) {
  175. // tail_comment
  176. IniNode* pNode = new IniNode;
  177. pNode->type = IniNode::INI_NODE_TYPE_SPAN;
  178. pNode->label = comment;
  179. pNewNode->Add(pNode);
  180. comment = "";
  181. }
  182. }
  183. // file end comment
  184. if (strDiv.length() != 0) {
  185. IniNode* pNode = new IniNode;
  186. pNode->type = IniNode::INI_NODE_TYPE_DIV;
  187. pNode->label = strDiv;
  188. root_->Add(pNode);
  189. }
  190. return 0;
  191. }
  192. void IniParser::DumpString(IniNode* pNode, std::string& str) {
  193. if (pNode == NULL) return;
  194. if (pNode->type != IniNode::INI_NODE_TYPE_SPAN) {
  195. if (str.length() > 0 && str[str.length()-1] != '\n') {
  196. str += '\n';
  197. }
  198. }
  199. switch (pNode->type) {
  200. case IniNode::INI_NODE_TYPE_SECTION: {
  201. str += '[';
  202. str += pNode->label;
  203. str += ']';
  204. }
  205. break;
  206. case IniNode::INI_NODE_TYPE_KEY_VALUE: {
  207. str += asprintf("%s %s %s", pNode->label.c_str(), _delim.c_str(), pNode->value.c_str());
  208. }
  209. break;
  210. case IniNode::INI_NODE_TYPE_DIV: {
  211. str += pNode->label;
  212. }
  213. break;
  214. case IniNode::INI_NODE_TYPE_SPAN: {
  215. str += '\t';
  216. str += pNode->label;
  217. }
  218. break;
  219. default:
  220. break;
  221. }
  222. for (auto p : pNode->children) {
  223. DumpString(p, str);
  224. }
  225. }
  226. std::string IniParser::DumpString() {
  227. std::string str;
  228. DumpString(root_, str);
  229. return str;
  230. }
  231. int IniParser::Save() {
  232. return SaveAs(_filepath.c_str());
  233. }
  234. int IniParser::SaveAs(const char* filepath) {
  235. std::string str = DumpString();
  236. if (str.length() == 0) {
  237. return 0;
  238. }
  239. HFile file;
  240. if (file.open(filepath, "w") != 0) {
  241. return ERR_SAVE_FILE;
  242. }
  243. file.write(str.c_str(), str.length());
  244. return 0;
  245. }
  246. std::string IniParser::GetValue(const std::string& key, const std::string& section) {
  247. if (root_ == NULL) return "";
  248. IniNode* pSection = root_;
  249. if (section.length() != 0) {
  250. pSection = root_->Get(section, IniNode::INI_NODE_TYPE_SECTION);
  251. if (pSection == NULL) return "";
  252. }
  253. IniNode* pKV = pSection->Get(key, IniNode::INI_NODE_TYPE_KEY_VALUE);
  254. if (pKV == NULL) return "";
  255. return pKV->value;
  256. }
  257. void IniParser::SetValue(const std::string& key, const std::string& value, const std::string& section) {
  258. if (root_ == NULL) {
  259. root_ = new IniNode;
  260. }
  261. IniNode* pSection = root_;
  262. if (section.length() != 0) {
  263. pSection = root_->Get(section, IniNode::INI_NODE_TYPE_SECTION);
  264. if (pSection == NULL) {
  265. pSection = new IniNode;
  266. pSection->type = IniNode::INI_NODE_TYPE_SECTION;
  267. pSection->label = section;
  268. root_->Add(pSection);
  269. }
  270. }
  271. IniNode* pKV = pSection->Get(key, IniNode::INI_NODE_TYPE_KEY_VALUE);
  272. if (pKV == NULL) {
  273. pKV = new IniNode;
  274. pKV->type = IniNode::INI_NODE_TYPE_KEY_VALUE;
  275. pKV->label = key;
  276. pSection->Add(pKV);
  277. }
  278. pKV->value = value;
  279. }
  280. template<>
  281. HV_EXPORT bool IniParser::Get(const std::string& key, const std::string& section, bool defvalue) {
  282. std::string str = GetValue(key, section);
  283. return str.empty() ? defvalue : getboolean(str.c_str());
  284. }
  285. template<>
  286. HV_EXPORT int IniParser::Get(const std::string& key, const std::string& section, int defvalue) {
  287. std::string str = GetValue(key, section);
  288. return str.empty() ? defvalue : atoi(str.c_str());
  289. }
  290. template<>
  291. HV_EXPORT float IniParser::Get(const std::string& key, const std::string& section, float defvalue) {
  292. std::string str = GetValue(key, section);
  293. return str.empty() ? defvalue : atof(str.c_str());
  294. }
  295. template<>
  296. HV_EXPORT void IniParser::Set(const std::string& key, const bool& value, const std::string& section) {
  297. SetValue(key, value ? "true" : "false", section);
  298. }
  299. template<>
  300. HV_EXPORT void IniParser::Set(const std::string& key, const int& value, const std::string& section) {
  301. SetValue(key, asprintf("%d", value), section);
  302. }
  303. template<>
  304. HV_EXPORT void IniParser::Set(const std::string& key, const float& value, const std::string& section) {
  305. SetValue(key, asprintf("%f", value), section);
  306. }