182 lines
3.6 KiB
C++
182 lines
3.6 KiB
C++
|
|
#include <string>
|
|
#include <algorithm>
|
|
#include <fstream>
|
|
#include "inifile.h"
|
|
|
|
// trim from start
|
|
static inline std::string <rim(std::string &s)
|
|
{
|
|
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
|
|
return s;
|
|
}
|
|
|
|
// trim from end
|
|
static inline std::string &rtrim(std::string &s)
|
|
{
|
|
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
|
|
return s;
|
|
}
|
|
|
|
// trim from both ends
|
|
static inline std::string &trim(std::string &s)
|
|
{
|
|
return ltrim(rtrim(s));
|
|
}
|
|
|
|
|
|
IniItem::IniItem(std::string _name)
|
|
{
|
|
name = _name;
|
|
value = "";
|
|
}
|
|
|
|
IniItem::IniItem(std::string _name, std::string _value)
|
|
{
|
|
name = _name;
|
|
value = _value;
|
|
}
|
|
|
|
void IniItem::SetValue(std::string _value)
|
|
{
|
|
value = _value;
|
|
}
|
|
|
|
IniGroup::IniGroup(std::string _name)
|
|
{
|
|
name = _name;
|
|
}
|
|
|
|
IniGroup::~IniGroup()
|
|
{
|
|
IniItemList::iterator it;
|
|
for (it = items.begin(); it != items.end(); ++it) {
|
|
IniItem *item = *it;
|
|
delete item;
|
|
}
|
|
}
|
|
|
|
IniItem *IniGroup::GetItem(std::string name)
|
|
{
|
|
IniItemList::iterator it;
|
|
for (it = items.begin(); it != items.end(); ++it) {
|
|
IniItem *item = *it;
|
|
if (item->name == name) return item;
|
|
}
|
|
|
|
items.push_back(new IniItem(name));
|
|
return items.back();
|
|
}
|
|
|
|
IniItem *IniGroup::GetItem(std::string name, std::string default_value)
|
|
{
|
|
IniItemList::iterator it;
|
|
for (it = items.begin(); it != items.end(); ++it) {
|
|
IniItem *item = *it;
|
|
if (item->name == name) return item;
|
|
}
|
|
|
|
items.push_back(new IniItem(name, default_value));
|
|
return items.back();
|
|
}
|
|
|
|
IniFile::~IniFile()
|
|
{
|
|
IniGroupList::iterator it;
|
|
for (it = groups.begin(); it != groups.end(); ++it) {
|
|
IniGroup *group = *it;
|
|
delete group;
|
|
}
|
|
}
|
|
|
|
IniGroup *IniFile::GetGroup(std::string name)
|
|
{
|
|
IniGroupList::iterator it;
|
|
for (it = groups.begin(); it != groups.end(); ++it) {
|
|
IniGroup *group = *it;
|
|
if (group->name == name) return group;
|
|
}
|
|
|
|
groups.push_back(new IniGroup(name));
|
|
return groups.back();
|
|
}
|
|
|
|
void IniFile::RemoveGroup(std::string name)
|
|
{
|
|
IniGroupList::iterator it;
|
|
for (it = groups.begin(); it != groups.end();) {
|
|
IniGroup *group = *it;
|
|
if (group->name == name) {
|
|
delete group;
|
|
it = groups.erase(it);
|
|
} else {
|
|
++it;
|
|
}
|
|
}
|
|
}
|
|
|
|
void IniFile::LoadFromDisk(std::string filename)
|
|
{
|
|
// Clear all information first...
|
|
IniGroupList::iterator it;
|
|
for (it = groups.begin(); it != groups.end(); ++it) {
|
|
IniGroup *group = *it;
|
|
delete group;
|
|
}
|
|
|
|
std::ifstream file;
|
|
file.open(filename.c_str());
|
|
|
|
IniGroup *group = NULL;
|
|
while (file.good()) {
|
|
char tmp[1024];
|
|
file.getline(tmp, sizeof tmp);
|
|
std::string line = tmp;
|
|
|
|
// Blank line
|
|
if (line.length() == 0) continue;
|
|
// Comment
|
|
if (line[0] == '#' || line[0] == ';') continue;
|
|
// Group header
|
|
if (line[0] == '[' && line[line.length() - 1] == ']') {
|
|
group = GetGroup(line.substr(1, line.length() - 2));
|
|
} else {
|
|
if (group == NULL) continue;
|
|
|
|
size_t loc = line.find('=');
|
|
if (loc != std::string::npos) {
|
|
std::string name = line.substr(0, loc);
|
|
std::string value = line.substr(loc + 1);
|
|
group->SetValue(trim(name), trim(value));
|
|
}
|
|
}
|
|
}
|
|
|
|
file.close();
|
|
}
|
|
|
|
void IniFile::SaveToDisk(std::string filename)
|
|
{
|
|
std::ofstream file;
|
|
file.open(filename.c_str());
|
|
|
|
IniGroupList::iterator it;
|
|
for (it = groups.begin(); it != groups.end(); ++it) {
|
|
IniGroup *group = *it;
|
|
|
|
file << std::endl;
|
|
file << "[" << group->name << "]" << std::endl;
|
|
|
|
IniItemList::iterator it2;
|
|
for (it2 = group->items.begin(); it2 != group->items.end(); ++it2) {
|
|
IniItem *item = *it2;
|
|
|
|
file << item->name << " = " << item->value << std::endl;
|
|
}
|
|
|
|
file << std::endl;
|
|
}
|
|
|
|
file.close();
|
|
}
|