| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- #include "simple_ini_tool.h"
- #include <cassert>
- #include <cstdio>
- #include <cstdlib>
- #include <list>
- #include <string>
- #include <ctype.h>
- #include <stdio.h>
- #include <stdlib.h>
- using namespace std;
- static inline bool IsSpace(char c)
- {
- return isspace(c) != 0 ; // !=0 消除VS的C4800警告
- }
- /** 将int转换成字符串,替代itoa函数,android中没有该函数 */
- static char* IntToStr(int v, char* str)
- {
- sprintf(str, "%d", v);
- return str;
- }
- /** 将文件以行的形式加载到内存中 */
- static list<string> LoadLines(const string& filename)
- {
- list<string> lines;
- FILE* file = fopen(filename.c_str(), "rt");
- if (file != NULL)
- {
- char line[1024];
- while(fgets(line, sizeof(line), file) != NULL)
- {
- lines.push_back(line);
- }
- fclose(file);
- }
- return lines;
- }
- /** 内存的数据写入文件 */
- static void SaveLines(const string& filename, const list<string>& lines)
- {
- #ifndef _WINDOWS
- printf("SaveLines =%s\n",filename.c_str());
- #endif
- FILE* file = fopen(filename.c_str(), "wt");
- if (file != NULL)
- {
- for (list<string>::const_iterator iter = lines.begin();
- iter != lines.end(); ++iter)
- {
- fputs(iter->c_str(), file);
- }
- fclose(file);
- }
- }
- template<typename T, typename F>
- static void Strip(T rawb, T rawe, T& b, T& e, F isStrip)
- {
- b = rawb;
- e = rawe;
- while (b != rawe)
- {
- if (!isStrip(*b)) break;
- ++b;
- }
- while (b != e)
- {
- /* 进入该分支,则表明b为!isStrip,当--e后,要么跳出循环,要么不为b */
- --e;
- if (!isStrip(*e))
- {
- ++e;
- break;
- }
- }
- }
- /** 判断是否是[xxx]结构 */
- static bool IsSection(const string& line, const string* section=NULL)
- {
- string::const_iterator b = line.begin();
- string::const_iterator e = line.end();
- Strip(line.begin(), line.end(), b, e, IsSpace);
- return e != b && *b == '[' && *(--e) == ']' && (section==NULL || string(++b, e) == *section);
- }
- class ListIter
- {
- public:
- typedef list<string> List;
- typedef list<string>::iterator Iter;
- };
- class ListCIter
- {
- public:
- typedef const list<string> List;
- typedef list<string>::const_iterator Iter;
- };
- template<typename T>
- static void FindSection(typename T::List& lines, const string& section, typename T::Iter& begin, typename T::Iter& end)
- {
- for (begin = lines.begin(); begin != lines.end(); ++begin)
- {
- if (IsSection(*begin, §ion)) break;
- }
- end = begin;
- if (end != lines.end())
- {
- for (++end; end != lines.end(); ++end)
- {
- if (IsSection(*end)) break;
- }
- }
- }
- bool GetKeyValue(const string& line, string& key, string& value)
- {
- string::size_type m = line.find("=");
- if (m != string::npos)
- {
- string::const_iterator b = line.begin();
- string::const_iterator s = line.begin()+m;
- Strip(line.begin(), line.begin()+m, b, s, IsSpace);
- key = string(b, s);
- Strip(line.begin()+m+1, line.end(), b, s, IsSpace);
- value = string(b, s);
- return true;
- }
- else
- {
- return false;
- }
- }
- string IniFileRead(const string& filename, const string& section, const string& key, const string& defaultValue)
- {
- list<string> lines = LoadLines(filename);
- list<string>::const_iterator begin;
- list<string>::const_iterator end;
- FindSection<ListCIter>(lines, section, begin, end);
- for (list<string>::const_iterator iter=begin; iter != end; ++iter)
- {
- string k, v;
- if (GetKeyValue(*iter, k, v) && key == k) return v;
- }
- return defaultValue;
- }
- int IniFileRead(const std::string& filename, const std::string& section, std::list<std::string> &key, std::list<std::string> &value)
- {
- int Count = 0;
- list<string> lines = LoadLines(filename);
- list<string>::const_iterator begin;
- list<string>::const_iterator end;
- FindSection<ListCIter>(lines, section, begin, end);
- for (list<string>::const_iterator iter=begin; iter != end; ++iter)
- {
- string k, v;
- if (GetKeyValue(*iter, k, v))
- {
- Count++;
- key.insert(key.end(), k);
- value.insert(value.end(), v);
- }
- }
- return Count;
- }
- #define MAKE_SECTION_LINE(s) ("[" + (s) + "]\n")
- #define MAKE_KEY_VALUE_LINE(k,v) ((k) + " = " + (v) + "\n")
- void IniFileWrite(const string& filename, const string& section, const string& key, const string& value)
- {
- list<string> lines = LoadLines(filename);
- list<string>::iterator begin;
- list<string>::iterator end;
- FindSection<ListIter>(lines, section, begin, end);
- if (begin != end)
- {
- list<string>::iterator iter=begin;
- for (; iter != end; ++iter)
- {
- string k, v;
- if (GetKeyValue(*iter, k, v) && key == k)
- {
- *iter = MAKE_KEY_VALUE_LINE(key, value);
- break;
- }
- }
- if (iter == end)
- {
- lines.insert(iter, MAKE_KEY_VALUE_LINE(key, value));
- }
- }
- else
- {
- lines.push_back(MAKE_SECTION_LINE(section));
- lines.push_back(MAKE_KEY_VALUE_LINE(key, value));
- }
- SaveLines(filename, lines);
- }
- int IniFileRead(const string& filename, const string& section, const string& key, int defaultValue)
- {
- char str[48];
- return atoi(IniFileRead(filename, section, key, IntToStr(defaultValue, str)).c_str());
- }
- void IniFileWrite(const string& filename, const string& section, const string& key, int value)
- {
- char str[48];
- IniFileWrite(filename, section, key, IntToStr(value, str));
- }
- #ifdef TEST_SIMPLE_INI_TOOL
- #define CheckEqaul(a, b) ((a)!=(b))?(printf("CheckFailed %s == %s\n", #a, #b)):0
- int main()
- {
- const char* testFile = "test_simple_ini_tool.ini";
- // 清空文件
- fclose(fopen(testFile, "w"));
- // 写入数据
- IniFileWrite(testFile, "section0", "key0", "value0");
- IniFileWrite(testFile, "section0", "key1", 1);
- // 读取并检查写入的数据
- CheckEqaul(IniFileRead(testFile, "section0", "key0", "valuee"), "value0");
- CheckEqaul(IniFileRead(testFile, "section0", "key1", "valuee"), "1");
- CheckEqaul(IniFileRead(testFile, "section0", "key0", 1), 0);
- CheckEqaul(IniFileRead(testFile, "section0", "key1", 0), 1);
- // 读取未有的数据
- CheckEqaul(IniFileRead(testFile, "section0", "keyx", "valuee"), "valuee");
- CheckEqaul(IniFileRead(testFile, "sectionx", "key0", "valuee"), "valuee");
- CheckEqaul(IniFileRead(testFile, "section0", "keyx", 1), 1);
- CheckEqaul(IniFileRead(testFile, "sectionx", "key0", 1), 1);
- // 写入的二个section
- IniFileWrite(testFile, "section1", "key0", "value10");
- IniFileWrite(testFile, "section1", "key1", 11);
- // 读取并检查写入的数据
- CheckEqaul(IniFileRead(testFile, "section1", "key0", "valuee"), "value10");
- CheckEqaul(IniFileRead(testFile, "section1", "key1", "valuee"), "11");
- CheckEqaul(IniFileRead(testFile, "section1", "key0", 1), 0);
- CheckEqaul(IniFileRead(testFile, "section1", "key1", 0), 11);
- // 改写数据
- IniFileWrite(testFile, "section0", "key0", "value00");
- IniFileWrite(testFile, "section1", "key1", 111);
- // 检查数据
- CheckEqaul(IniFileRead(testFile, "section0", "key0", "valuee"), "value00");
- CheckEqaul(IniFileRead(testFile, "section1", "key1", 0), 111);
- return 0;
- }
- #endif //
|