2010-03-30 19:14:11 +00:00
|
|
|
#ifndef _INIFILE_H_
|
|
|
|
#define _INIFILE_H_
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
//
|
|
|
|
// IniFile
|
|
|
|
//
|
|
|
|
class IniSection : public std::map< std::string, std::string >
|
|
|
|
{
|
|
|
|
public:
|
2010-04-01 04:33:07 +00:00
|
|
|
void Set( const std::string& key, const std::string& val, const std::string& def = "" );
|
|
|
|
std::string Get( const std::string& key, const std::string& def = "" );
|
2010-03-30 19:14:11 +00:00
|
|
|
|
|
|
|
template <typename V, typename D>
|
2010-04-01 04:33:07 +00:00
|
|
|
void Set( const std::string& key, const V& val, const D& def = 0 )
|
2010-03-30 19:14:11 +00:00
|
|
|
{
|
|
|
|
if ( val != def )
|
|
|
|
{
|
|
|
|
std::ostringstream ss;
|
|
|
|
ss << long(val);
|
|
|
|
operator[](key) = ss.str();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-04-01 02:59:40 +00:00
|
|
|
iterator f = find(key);
|
2010-03-30 19:14:11 +00:00
|
|
|
if ( f != end() )
|
|
|
|
erase( f );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
template <typename V>
|
2010-04-01 04:33:07 +00:00
|
|
|
V Get( const std::string& key, const V& def = 0 )
|
2010-03-30 19:14:11 +00:00
|
|
|
{
|
|
|
|
const const_iterator f = find(key);
|
|
|
|
if ( f != end() )
|
|
|
|
if ( false == f->second.empty() )
|
|
|
|
{
|
|
|
|
std::istringstream ss(f->second);
|
|
|
|
int val;
|
|
|
|
ss >> val;
|
|
|
|
return V(val);
|
|
|
|
}
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class IniFile : public std::map< std::string, IniSection >
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef IniSection Section;
|
|
|
|
|
|
|
|
void Clean();
|
|
|
|
void Save( std::ostream& file );
|
|
|
|
void Load( std::istream& file );
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|