实现思路:读ini文件,用嵌套map存解析所得数据。保存时,再遍历嵌套map写入文件。
接口:
string ReadString(string section, string key, string def);
void WriteString(string section, string key, string value);
代码:
头文件:
View Code
#ifndef LINIFILE_H
#define LINIFILE_H
#include <map>
#include <string>
using namespace std;
class IniFile
{
typedef map<string, map<string, string> > IniSectionMap;
typedef map<string, string> IniKeyMap;
typedef map<string, map<string, string> >::iterator IniSectionIterator;
typedef map<string, string>::iterator IniKeyIterator;
public:
IniFile(string path);
~IniFile();
string ReadString(string section, string key, string def);
void WriteString(string section, string key, string value);
bool save();
private:
bool parseIniFile();
void print();
string path_;
IniSectionMap section_key_value_map_;
};
#endif
源文件:
View Code
#include <iostream>
#include <fstream>
#include <string.h>
#include "linifile.h"
IniFile::IniFile(string path)
{
path_ = path;
parseIniFile();
}
IniFile::~IniFile()
{
}
bool IniFile::parseIniFile()
{
fstream iniFile(path_.c_str(), ios::in);
if (!iniFile.is_open())
{
cerr << "error opening file" << endl;
return false;
}
//read from file line by line
bool bDone = false;
char szBuffer[4000];
string line;
string section;
IniKeyMap temp_key_map;
bool bInsert = false;
while(!bDone)
{
szBuffer[0] = ' ';
iniFile.getline(szBuffer, sizeof(szBuffer));
line = szBuffer;
if('[' == line[0])
{
if (bInsert)
{
section_key_value_map_.insert(make_pair(section, temp_key_map));
temp_key_map.clear();
section = "";
}
//line "[section]"
int nPos = line.find_first_of(']');
if(string::npos != nPos)
{
section = line.substr(1, nPos-1).c_str();
}
}
else
{
//line "key=value"
int nPos = line.find_first_of('=');
if(string::npos != nPos)
{
temp_key_map.insert(make_pair(line.substr(0, nPos), line.substr(nPos+1, line.size()-nPos-1)));
}
bInsert = true;
}
//check for exit
bDone = iniFile.eof() || iniFile.bad() || iniFile.fail();
}
//the last one
if (("" != section) && (0 != temp_key_map.size()))
{
section_key_value_map_.insert(make_pair(section, temp_key_map));
}
iniFile.close();
return true;
}
string IniFile::ReadString(string section, string key, string def)
{
cout << section_key_value_map_.size() << endl;
IniSectionIterator iter_section;
iter_section = section_key_value_map_.find(section);
if (section_key_value_map_.end() != iter_section)
{
IniKeyMap temp = iter_section->second;
IniKeyIterator iter_key;
iter_key = temp.find(key);
if (temp.end() != iter_key)
{
return iter_key->second;
}
}
return def;
}
void IniFile::WriteString(string section, string key, string value)
{
IniSectionIterator iter_section;
iter_section = section_key_value_map_.find(section);
if (section_key_value_map_.end() == iter_section)
{
IniKeyMap key_temp_map;
key_temp_map.insert(make_pair(key, value));
section_key_value_map_.insert(make_pair(section, key_temp_map));
}
else
{
map<string, string> temp = iter_section->second;
IniKeyIterator iter_key;
iter_key = temp.find(key);
if (temp.end() == iter_key)
{
(iter_section->second).insert(make_pair(key, value));
}
else
{
//update existing key
iter_key->second = value;
}
}
}
bool IniFile::save()
{
//open the INI file for writing
fstream iniFile(path_.c_str(), ios::out|ios::trunc);
if(!iniFile.is_open())
{
return false;
}
char szBuffer[4000];
IniSectionIterator iter_section;
IniKeyIterator iter_key;
for (iter_section = section_key_value_map_.begin(); iter_section != section_key_value_map_.end(); iter_section++)
{
//write line with section name
snprintf(szBuffer, sizeof(szBuffer), "[%s]n", (iter_section->first).c_str());
iniFile.write(szBuffer, strlen(szBuffer));
IniKeyMap key_temp_map = iter_section->second;
for (iter_key = key_temp_map.begin(); iter_key != key_temp_map.end(); iter_key++)
{
snprintf(szBuffer, sizeof(szBuffer), "%s=%sn", (iter_key->first).c_str(), (iter_key->second).c_str());
iniFile.write(szBuffer, strlen(szBuffer));
}
iniFile.write("n", 1);
}
iniFile.close();
return true;
}
//for test
void IniFile::print()
{
IniSectionIterator iter_section;
IniKeyIterator iter_key;
for (iter_section = section_key_value_map_.begin(); iter_section != section_key_value_map_.end(); iter_section++)
{
cout << "section: " << iter_section->first << endl;
IniKeyMap key_temp_map = iter_section->second;
for (iter_key = key_temp_map.begin(); iter_key != key_temp_map.end(); iter_key++)
{
cout << "key: " << iter_key->first << " | value: " << iter_key->second << endl;
}
cout << endl;
}
}
原文链接: https://www.cnblogs.com/leon-/archive/2012/09/02/2667786.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/61552
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!