10 _delimiter(
"="), _comment(
"#"), _include(
"+"), _sentry(
"EndConfigFile")
16 const std::string fileName,
const std::string delimiter,
17 const std::string comment,
const std::string inc,
const std::string sentry ) :
18 _delimiter(delimiter), _comment(comment), _include(inc), _sentry(sentry)
22 std::ifstream in( fileName.c_str() );
26 std::cerr<<
"File not found: "<<fileName<<std::endl;
37 const std::string fileName,
const std::string delimiter,
38 const std::string comment,
const std::string inc,
const std::string sentry )
49 if (comment !=
"")
_comment = comment;
51 if (sentry !=
"")
_sentry = sentry;
53 std::ifstream in( fileName.c_str() );
57 std::cerr<<
"File not found: "<<fileName<<std::endl;
74 std::string key2 = key;
81 std::string key2 = key;
86 std::cerr<<
"Key not found: "<<key2<<std::endl;
90 "ConfigFile error: key "+key2+
" not found");
102 MapCIt p = _contents.find(key);
104 if (p == _contents.end()) {
105 std::stringstream err;
106 err<<
"ConfigFile error: key '"<<key<<
"' not found";
107 throw err.str().c_str();
110 std::string
val =
get(key);
115 const std::string key,
116 const std::string defval) {
150 std::string whiteSpace =
" \n\t\v\r\f";
151 s.erase( 0, s.find_first_not_of(whiteSpace) );
152 s.erase( s.find_last_not_of(whiteSpace) + 1);
161 os << p->second << std::endl;
169 std::string f = p->first;
170 std::string s = p->second;
171 std::replace(f.begin(),f.end(),
'\n',
' ');
172 std::replace(s.begin(),s.end(),
'\n',
' ');
174 os << s << std::endl;
185 const std::string& sentry =
_sentry;
186 const std::string::size_type skip = delim.size();
188 std::string nextLine =
"";
191 while( is || nextLine.size() > 0 ) {
194 if( nextLine.size() > 0 ) {
198 std::getline( is, line );
202 line = line.substr( 0, line.find(comm) );
208 if (line.size() == 0)
continue;
211 if (line.find(inc) == 0) {
212 line.erase(0,inc.size());
213 std::stringstream ss(line);
214 std::string fileName;
222 if( sentry !=
"" && line.find(sentry) != std::string::npos )
return;
225 std::string::size_type delimPos = line.find( delim );
226 if( delimPos < std::string::npos ) {
228 std::string key = line.substr( 0, delimPos );
229 line.replace( 0, delimPos+skip,
"" );
234 bool terminate =
false;
235 while( !terminate && is ) {
236 std::getline( is, nextLine );
239 std::string nextLineCopy = nextLine;
241 if( nextLineCopy ==
"" )
continue;
243 nextLine = nextLine.substr( 0, nextLine.find(comm) );
244 if( nextLine.find(delim) != std::string::npos )
246 if( sentry !=
"" && nextLine.find(sentry) != std::string::npos )
249 nextLineCopy = nextLine;
251 if( nextLineCopy !=
"" ) line +=
"\n";
static void trim(std::string &s)
std::string getstr(const std::string key) const
ConvertibleString get(const std::string &key) const
void load(const std::string fileName)
void write(std::ostream &os) const
bool keyExists(const std::string &key) const
ConvertibleString & getNoCheck(const std::string &key)
void writeAsComment(std::ostream &os) const
std::map< std::string, ConvertibleString > _contents
std::map< std::string, ConvertibleString >::const_iterator MapCIt
void read(std::istream &is)
void remove(const std::string &key)