simple_ini_tool.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #include "simple_ini_tool.h"
  2. #include <cassert>
  3. #include <cstdio>
  4. #include <cstdlib>
  5. #include <list>
  6. #include <string>
  7. #include <ctype.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. using namespace std;
  11. static inline bool IsSpace(char c)
  12. {
  13. return isspace(c) != 0 ; // !=0 消除VS的C4800警告
  14. }
  15. /** 将int转换成字符串,替代itoa函数,android中没有该函数 */
  16. static char* IntToStr(int v, char* str)
  17. {
  18. sprintf(str, "%d", v);
  19. return str;
  20. }
  21. /** 将文件以行的形式加载到内存中 */
  22. static list<string> LoadLines(const string& filename)
  23. {
  24. list<string> lines;
  25. FILE* file = fopen(filename.c_str(), "rt");
  26. if (file != NULL)
  27. {
  28. char line[1024];
  29. while(fgets(line, sizeof(line), file) != NULL)
  30. {
  31. lines.push_back(line);
  32. }
  33. fclose(file);
  34. }
  35. return lines;
  36. }
  37. /** 内存的数据写入文件 */
  38. static void SaveLines(const string& filename, const list<string>& lines)
  39. {
  40. #ifndef _WINDOWS
  41. printf("SaveLines =%s\n",filename.c_str());
  42. #endif
  43. FILE* file = fopen(filename.c_str(), "wt");
  44. if (file != NULL)
  45. {
  46. for (list<string>::const_iterator iter = lines.begin();
  47. iter != lines.end(); ++iter)
  48. {
  49. fputs(iter->c_str(), file);
  50. }
  51. fclose(file);
  52. }
  53. }
  54. template<typename T, typename F>
  55. static void Strip(T rawb, T rawe, T& b, T& e, F isStrip)
  56. {
  57. b = rawb;
  58. e = rawe;
  59. while (b != rawe)
  60. {
  61. if (!isStrip(*b)) break;
  62. ++b;
  63. }
  64. while (b != e)
  65. {
  66. /* 进入该分支,则表明b为!isStrip,当--e后,要么跳出循环,要么不为b */
  67. --e;
  68. if (!isStrip(*e))
  69. {
  70. ++e;
  71. break;
  72. }
  73. }
  74. }
  75. /** 判断是否是[xxx]结构 */
  76. static bool IsSection(const string& line, const string* section=NULL)
  77. {
  78. string::const_iterator b = line.begin();
  79. string::const_iterator e = line.end();
  80. Strip(line.begin(), line.end(), b, e, IsSpace);
  81. return e != b && *b == '[' && *(--e) == ']' && (section==NULL || string(++b, e) == *section);
  82. }
  83. class ListIter
  84. {
  85. public:
  86. typedef list<string> List;
  87. typedef list<string>::iterator Iter;
  88. };
  89. class ListCIter
  90. {
  91. public:
  92. typedef const list<string> List;
  93. typedef list<string>::const_iterator Iter;
  94. };
  95. template<typename T>
  96. static void FindSection(typename T::List& lines, const string& section, typename T::Iter& begin, typename T::Iter& end)
  97. {
  98. for (begin = lines.begin(); begin != lines.end(); ++begin)
  99. {
  100. if (IsSection(*begin, &section)) break;
  101. }
  102. end = begin;
  103. if (end != lines.end())
  104. {
  105. for (++end; end != lines.end(); ++end)
  106. {
  107. if (IsSection(*end)) break;
  108. }
  109. }
  110. }
  111. bool GetKeyValue(const string& line, string& key, string& value)
  112. {
  113. string::size_type m = line.find("=");
  114. if (m != string::npos)
  115. {
  116. string::const_iterator b = line.begin();
  117. string::const_iterator s = line.begin()+m;
  118. Strip(line.begin(), line.begin()+m, b, s, IsSpace);
  119. key = string(b, s);
  120. Strip(line.begin()+m+1, line.end(), b, s, IsSpace);
  121. value = string(b, s);
  122. return true;
  123. }
  124. else
  125. {
  126. return false;
  127. }
  128. }
  129. string IniFileRead(const string& filename, const string& section, const string& key, const string& defaultValue)
  130. {
  131. list<string> lines = LoadLines(filename);
  132. list<string>::const_iterator begin;
  133. list<string>::const_iterator end;
  134. FindSection<ListCIter>(lines, section, begin, end);
  135. for (list<string>::const_iterator iter=begin; iter != end; ++iter)
  136. {
  137. string k, v;
  138. if (GetKeyValue(*iter, k, v) && key == k) return v;
  139. }
  140. return defaultValue;
  141. }
  142. int IniFileRead(const std::string& filename, const std::string& section, std::list<std::string> &key, std::list<std::string> &value)
  143. {
  144. int Count = 0;
  145. list<string> lines = LoadLines(filename);
  146. list<string>::const_iterator begin;
  147. list<string>::const_iterator end;
  148. FindSection<ListCIter>(lines, section, begin, end);
  149. for (list<string>::const_iterator iter=begin; iter != end; ++iter)
  150. {
  151. string k, v;
  152. if (GetKeyValue(*iter, k, v))
  153. {
  154. Count++;
  155. key.insert(key.end(), k);
  156. value.insert(value.end(), v);
  157. }
  158. }
  159. return Count;
  160. }
  161. #define MAKE_SECTION_LINE(s) ("[" + (s) + "]\n")
  162. #define MAKE_KEY_VALUE_LINE(k,v) ((k) + " = " + (v) + "\n")
  163. void IniFileWrite(const string& filename, const string& section, const string& key, const string& value)
  164. {
  165. list<string> lines = LoadLines(filename);
  166. list<string>::iterator begin;
  167. list<string>::iterator end;
  168. FindSection<ListIter>(lines, section, begin, end);
  169. if (begin != end)
  170. {
  171. list<string>::iterator iter=begin;
  172. for (; iter != end; ++iter)
  173. {
  174. string k, v;
  175. if (GetKeyValue(*iter, k, v) && key == k)
  176. {
  177. *iter = MAKE_KEY_VALUE_LINE(key, value);
  178. break;
  179. }
  180. }
  181. if (iter == end)
  182. {
  183. lines.insert(iter, MAKE_KEY_VALUE_LINE(key, value));
  184. }
  185. }
  186. else
  187. {
  188. lines.push_back(MAKE_SECTION_LINE(section));
  189. lines.push_back(MAKE_KEY_VALUE_LINE(key, value));
  190. }
  191. SaveLines(filename, lines);
  192. }
  193. int IniFileRead(const string& filename, const string& section, const string& key, int defaultValue)
  194. {
  195. char str[48];
  196. return atoi(IniFileRead(filename, section, key, IntToStr(defaultValue, str)).c_str());
  197. }
  198. void IniFileWrite(const string& filename, const string& section, const string& key, int value)
  199. {
  200. char str[48];
  201. IniFileWrite(filename, section, key, IntToStr(value, str));
  202. }
  203. #ifdef TEST_SIMPLE_INI_TOOL
  204. #define CheckEqaul(a, b) ((a)!=(b))?(printf("CheckFailed %s == %s\n", #a, #b)):0
  205. int main()
  206. {
  207. const char* testFile = "test_simple_ini_tool.ini";
  208. // 清空文件
  209. fclose(fopen(testFile, "w"));
  210. // 写入数据
  211. IniFileWrite(testFile, "section0", "key0", "value0");
  212. IniFileWrite(testFile, "section0", "key1", 1);
  213. // 读取并检查写入的数据
  214. CheckEqaul(IniFileRead(testFile, "section0", "key0", "valuee"), "value0");
  215. CheckEqaul(IniFileRead(testFile, "section0", "key1", "valuee"), "1");
  216. CheckEqaul(IniFileRead(testFile, "section0", "key0", 1), 0);
  217. CheckEqaul(IniFileRead(testFile, "section0", "key1", 0), 1);
  218. // 读取未有的数据
  219. CheckEqaul(IniFileRead(testFile, "section0", "keyx", "valuee"), "valuee");
  220. CheckEqaul(IniFileRead(testFile, "sectionx", "key0", "valuee"), "valuee");
  221. CheckEqaul(IniFileRead(testFile, "section0", "keyx", 1), 1);
  222. CheckEqaul(IniFileRead(testFile, "sectionx", "key0", 1), 1);
  223. // 写入的二个section
  224. IniFileWrite(testFile, "section1", "key0", "value10");
  225. IniFileWrite(testFile, "section1", "key1", 11);
  226. // 读取并检查写入的数据
  227. CheckEqaul(IniFileRead(testFile, "section1", "key0", "valuee"), "value10");
  228. CheckEqaul(IniFileRead(testFile, "section1", "key1", "valuee"), "11");
  229. CheckEqaul(IniFileRead(testFile, "section1", "key0", 1), 0);
  230. CheckEqaul(IniFileRead(testFile, "section1", "key1", 0), 11);
  231. // 改写数据
  232. IniFileWrite(testFile, "section0", "key0", "value00");
  233. IniFileWrite(testFile, "section1", "key1", 111);
  234. // 检查数据
  235. CheckEqaul(IniFileRead(testFile, "section0", "key0", "valuee"), "value00");
  236. CheckEqaul(IniFileRead(testFile, "section1", "key1", 0), 111);
  237. return 0;
  238. }
  239. #endif //