43 std::ostringstream ostr;
52 std::istringstream istr (val);
55 if (! (istr >> returnVal)) {
57 throw std::invalid_argument (
"Not a valid " +
58 (std::string)
typeid (T).name() +
" received !");
68 ConfigFile (
const std::string &filename,
char keysep =
'=',
char remsep =
'#') :
82 template<
typename ValueType> ValueType
value (
const std::string &key,
83 ValueType
const &defaultValue = ValueType())
const {
94 std::string
value (
const std::string &key,
95 std::string
const &defaultValue = std::string())
const {
112 size_t pos = line.find (
_remsep);
113 if (pos != line.npos) {
114 line.erase (pos, line.npos);
122 return (line.find_first_not_of (
' ') == line.npos);
128 std::string temp = line;
130 temp.erase (0, temp.find_first_not_of (
"\t "));
149 static void trim (std::string &str) {
151 str.erase (0, str.find_first_not_of (
"\t \""));
152 str.erase (str.find_last_not_of (
"\t \"") + 1);
158 const std::string &line)
const {
160 key = line.substr (0, sepPos);
167 const std::string &line)
const {
169 if (sepPos < line.npos) {
171 value = line.substr (sepPos + 1);
183 std::string temp = line;
185 temp.erase (0, temp.find_first_not_of (
"\t "));
186 size_t sepPos = temp.find (
_keysep);
188 std::string key,
value;
196 _contents.insert (std::pair<std::string, std::string> (key,
value));
201 parseLine (
const std::string &line,
size_t const lineNo) {
203 if (line.find (
_keysep) != line.npos) {
216 file.open (filename.c_str());
219 throw std::invalid_argument (
"File " + filename +
" couldn't be found!");
224 while (std::getline (file, line)) {
226 std::string temp = line;
246 std::string
ConfigFile::value (
const std::string &key, std::string
const &defaultValue)
const {
250 return std::string();
void extractValue(std::string &value, size_t const &sepPos, const std::string &line) const
std::string value(const std::string &key, std::string const &defaultValue=std::string()) const
void removeComment(std::string &line) const
bool onlyWhitespace(const std::string &line) const
bool validLine(const std::string &line) const
void extractKey(std::string &key, size_t const &sepPos, const std::string &line) const
ValueType value(const std::string &key, ValueType const &defaultValue=ValueType()) const
static void trim(std::string &str)
void parseLine(const std::string &line, size_t const lineNo)
ConfigFile(const std::string &filename, char keysep='=', char remsep='#')
bool keyExists(const std::string &key) const
std::map< std::string, std::string > _contents
void extractContents(const std::string &line)
void extractKeys(const std::string &filename)
static T string_to_T(std::string const &val)
static std::string T_to_string(T const &val)
Global namespace for Piduino.