25 #ifndef DOZERG_CONFIGURATION_H_20070821 26 #define DOZERG_CONFIGURATION_H_20070821 34 #include "template.hh" 35 #include "tools/string.hh" 86 typedef std::map<std::string, std::string> container_type;
88 static const int kFormatEqual = 0;
89 static const int kFormatSpace = 1;
90 static const int kFormatColon = 2;
108 bool load(
const std::string & file_name,
int format = kFormatSpace){
109 std::string abs_file = tools::AbsFilename(file_name);
110 std::ifstream inf(abs_file.c_str());
114 for(std::string line;!inf.eof();){
115 std::getline(inf, line);
116 parseFormat(line.substr(0, line.find_first_of(
"#")), format);
118 conf_file_ = abs_file;
127 std::string
getString(
const std::string & key,
const std::string & strdefault =
"")
const{
128 container_type::const_iterator wh = content_.find(key);
129 if(wh == content_.end())
142 std::string
getFilepath(
const std::string & key,
const std::string & strdefault =
"")
const{
143 const std::string v =
getString(key, strdefault);
144 return (v.empty() ? v : tools::AbsFilename(v));
169 int getInt(
const std::string & key,
int ndefault = 0,
170 int min = std::numeric_limits<int>::min(),
171 int max = std::numeric_limits<int>::max())
const 173 return getInt<int>(key, ndefault, min, max);
175 template<
typename Int>
176 Int getInt(
const std::string & key, Int ndefault = 0,
177 Int min = std::numeric_limits<Int>::min(),
178 Int max = std::numeric_limits<Int>::max())
const 180 container_type::const_iterator wh = content_.find(key);
182 if(wh != content_.end()){
184 ret = atoi(wh->second.c_str());
186 std::istringstream iss(wh->second);
189 if(ret && wh->second.size() > 0){
190 char c = *wh->second.rbegin();
191 if((
'b' == c ||
'B' == c)
192 && wh->second.size() > 1)
193 c = *(wh->second.rbegin() + 1);
217 return (ret <= min ? min : (ret >= max ? max : ret));
227 oss<<
"{conf_file_="<<conf_file_
230 for(container_type::const_iterator i = content_.begin();i != content_.end();++i, ++s){
233 oss<<
"\""<<i->first<<
"\"=\""<<i->second;
239 void parseFormat(
const std::string & line,
int format){
242 const std::string sep = (format == kFormatEqual ?
"=" 243 : (format == kFormatSpace ?
" \t" 244 : (format == kFormatColon ?
":" : std::string())));
246 const size_t i = line.find_first_of(sep);
247 content_[tools::Trim(line.substr(0, i))]
248 = tools::Trim((std::string::npos == i ? std::string() : line.substr(i + 1)));
252 std::string conf_file_;
253 container_type content_;
void clear()
Clear all attributes loaded.
Definition: configuration.hh:100
Definition: to_string.hh:43
std::string toString() const
Get a readable description of this object.
Definition: configuration.hh:225
Definition: template.hh:23
std::string getConfName() const
Get config file name.
Definition: configuration.hh:95
std::string getFilepath(const std::string &key, const std::string &strdefault="") const
Get file name value of an attribute.
Definition: configuration.hh:142
std::string getString(const std::string &key, const std::string &strdefault="") const
Get string value of an attribute.
Definition: configuration.hh:127
Parse config file and get attributes.
Definition: configuration.hh:84
bool load(const std::string &file_name, int format=kFormatSpace)
Parse and load content of a config file.
Definition: configuration.hh:108