1、适用于C++项目,适用于Windows和Linux平台,依赖STL标准库。
2、用于C++读取INI文件,写入INI文件,以及查找相应键值对的快速操作。
3、在项目中需要此功能时,只需要引入两个文件,即:INIParser.h和INIParser.cpp。
4、main.c文件有例程,同时附带makefile,以及测试使用的conf.ini和生成的test.ini文件。
INIParser.h
15 | ININode(string root, string key, string value) |
29 | void InsertElement(string key, string value) |
31 | sub_node.insert(pair<string, string>(key, value)); |
33 | map<string, string> sub_node; |
39 | int ReadINI(string path); |
40 | string GetValue(string root, string key); |
41 | vector<ININode>::size_type GetSize(){return map_ini.size();} |
42 | vector<ININode>::size_type SetValue(string root, string key, string value); |
43 | int WriteINI(string path); |
44 | void Clear(){map_ini.clear();} |
46 | map<string, SubNode> map_ini; |
49 | #endif // INI_PARSER_H |
INIParser.cpp
001 | #include "INIParser.h" |
004 | string &TrimString(string &str) |
006 | string::size_type pos = 0; |
007 | while(str.npos != (pos = str.find(" "))) |
008 | str = str.replace(pos, pos+1, ""); |
013 | int INIParser::ReadINI(string path) |
015 | ifstream in_conf_file(path.c_str()); |
016 | if(!in_conf_file) return 0; |
017 | string str_line = ""; |
018 | string str_root = ""; |
019 | vector<ININode> vec_ini; |
020 | while(getline(in_conf_file, str_line)) |
022 | string::size_type left_pos = 0; |
023 | string::size_type right_pos = 0; |
024 | string::size_type equal_div_pos = 0; |
026 | string str_value = ""; |
027 | if((str_line.npos != (left_pos = str_line.find("["))) && (str_line.npos != (right_pos = str_line.find("]")))) |
030 | str_root = str_line.substr(left_pos+1, right_pos-1); |
034 | if(str_line.npos != (equal_div_pos = str_line.find("="))) |
036 | str_key = str_line.substr(0, equal_div_pos); |
037 | str_value = str_line.substr(equal_div_pos+1, str_line.size()-1); |
038 | str_key = TrimString(str_key); |
039 | str_value = TrimString(str_value); |
043 | if((!str_root.empty()) && (!str_key.empty()) && (!str_value.empty())) |
045 | ININode ini_node(str_root, str_key, str_value); |
046 | vec_ini.push_back(ini_node); |
050 | in_conf_file.close(); |
051 | in_conf_file.clear(); |
054 | map<string, string> map_tmp; |
055 | for(vector<ININode>::iterator itr = vec_ini.begin(); itr != vec_ini.end(); ++itr) |
057 | map_tmp.insert(pair<string, string>(itr->root, "")); |
061 | for(map<string, string>::iterator itr = map_tmp.begin(); itr != map_tmp.end(); ++itr) |
064 | for(vector<ININode>::iterator sub_itr = vec_ini.begin(); sub_itr != vec_ini.end(); ++sub_itr) |
066 | if(sub_itr->root == itr->first) |
069 | sn.InsertElement(sub_itr->key, sub_itr->value); |
072 | map_ini.insert(pair<string, SubNode>(itr->first, sn)); |
078 | string INIParser::GetValue(string root, string key) |
080 | map<string, SubNode>::iterator itr = map_ini.find(root); |
081 | map<string, string>::iterator sub_itr = itr->second.sub_node.find(key); |
082 | if(!(sub_itr->second).empty()) |
083 | return sub_itr->second; |
088 | int INIParser::WriteINI(string path) |
090 | ofstream out_conf_file(path.c_str()); |
095 | for(map<string, SubNode>::iterator itr = map_ini.begin(); itr != map_ini.end(); ++itr) |
098 | out_conf_file << "[" << itr->first << "]" << endl; |
099 | for(map<string, string>::iterator sub_itr = itr->second.sub_node.begin(); sub_itr != itr->second.sub_node.end(); ++sub_itr) |
102 | out_conf_file << sub_itr->first << "=" << sub_itr->second << endl; |
106 | out_conf_file.close(); |
107 | out_conf_file.clear(); |
112 | vector<ININode>::size_type INIParser::SetValue(string root, string key, string value) |
114 | map<string, SubNode>::iterator itr = map_ini.find(root); |
115 | if(map_ini.end() != itr) |
116 | itr->second.sub_node.insert(pair<string, string>(key, value)); |
120 | sn.InsertElement(key, value); |
121 | map_ini.insert(pair<string, SubNode>(root, sn)); |
123 | return map_ini.size(); |
测试文件(main.cpp)
07 | #include "INIParser.h" |
14 | ini_parser.ReadINI("conf.ini"); |
15 | cout << ini_parser.GetValue("default", "server_port") << endl; |
17 | cout << ini_parser.GetSize() << endl; |
19 | ini_parser.SetValue("class1","name1","Tom"); |
20 | ini_parser.SetValue("class2","name2","Lucy"); |
21 | ini_parser.WriteINI("test.ini"); |
编译:
读取以下INI文件(conf.ini)
结果如下:
01 | D:\workspace\cpp\INIParser 的目录 |
03 | 2014/07/30 09:52 <DIR> . |
04 | 2014/07/30 09:52 <DIR> .. |
05 | 2014/07/29 09:20 80 conf.ini |
06 | 2014/07/29 18:43 4,002 INIParser.cpp |
07 | 2014/07/29 18:38 1,012 INIParser.h |
08 | 2014/07/29 18:33 540 main.cpp |
09 | 2014/07/29 18:56 226 makefile |
10 | 2014/07/30 09:52 135,176 test.exe |
11 | 2014/07/29 18:56 39 test.ini |
13 | 2 个目录 96,918,614,016 可用字节 |
15 | D:\workspace\cpp\INIParser>test |
19 | D:\workspace\cpp\INIParser> |
转载自:
http://www.oschina.net/code/snippet_553781_37613