99 lines
1.8 KiB
C++
99 lines
1.8 KiB
C++
#ifndef INIFILE_H
|
|
#define INIFILE_H
|
|
|
|
#include <string>
|
|
#include <sstream>
|
|
#include <list>
|
|
|
|
template <typename T>
|
|
std::string ss_conv_from(T value)
|
|
{
|
|
std::ostringstream temp_ss;
|
|
temp_ss << value;
|
|
return temp_ss.str();
|
|
}
|
|
|
|
template <typename T>
|
|
T ss_conv_to(std::string value)
|
|
{
|
|
std::istringstream temp_ss(value);
|
|
T tmp;
|
|
temp_ss >> tmp;
|
|
return tmp;
|
|
}
|
|
|
|
struct IniItem
|
|
{
|
|
std::string name;
|
|
std::string value;
|
|
|
|
IniItem(std::string name);
|
|
IniItem(std::string name, std::string value);
|
|
|
|
void SetValue(std::string value);
|
|
};
|
|
|
|
typedef std::list<IniItem *> IniItemList;
|
|
|
|
struct IniGroup
|
|
{
|
|
std::string name;
|
|
IniItemList items;
|
|
|
|
IniGroup(std::string name);
|
|
~IniGroup();
|
|
|
|
IniItem *GetItem(std::string name);
|
|
IniItem *GetItem(std::string name, std::string default_value);
|
|
|
|
std::string GetValue(std::string name, std::string default_value)
|
|
{
|
|
IniItem *item = GetItem(name, default_value);
|
|
return item->value;
|
|
}
|
|
|
|
void SetValue(std::string name, std::string value)
|
|
{
|
|
IniItem *item = GetItem(name);
|
|
item->value = value;
|
|
}
|
|
|
|
std::string GetValue(std::string name, const char *default_value)
|
|
{
|
|
IniItem *item = GetItem(name, default_value);
|
|
return item->value;
|
|
}
|
|
|
|
template <typename T>
|
|
T GetValue(std::string name, T default_value)
|
|
{
|
|
std::string value = ss_conv_from(default_value);
|
|
IniItem *item = GetItem(name, value);
|
|
return ss_conv_to<T>(item->value);
|
|
}
|
|
|
|
template <typename T>
|
|
void SetValue(std::string name, T value)
|
|
{
|
|
IniItem *item = GetItem(name);
|
|
item->value = ss_conv_from(value);
|
|
}
|
|
};
|
|
|
|
typedef std::list<IniGroup *> IniGroupList;
|
|
|
|
struct IniFile
|
|
{
|
|
IniGroupList groups;
|
|
|
|
~IniFile();
|
|
|
|
IniGroup *GetGroup(std::string name);
|
|
void RemoveGroup(std::string name);
|
|
|
|
void LoadFromDisk(std::string filename);
|
|
void SaveToDisk(std::string filename);
|
|
};
|
|
|
|
#endif // INIFILE_H
|