17 #ifndef ConfigFileReader_h
18 #define ConfigFileReader_h
38 std::string
Trim(std::string
const& source,
char const * delim =
" \t\r\n")
40 std::string result(source);
41 std::string::size_type index = result.find_last_not_of(delim);
42 if(index != std::string::npos)
43 result.erase(++index);
44 index = result.find_first_not_of(delim);
45 if(index != std::string::npos)
46 result.erase(0, index);
52 std::string
RemoveComment(std::string
const& source,
char const * delim =
"#;")
54 std::string result(source);
55 std::string::size_type index = result.find_first_of(delim);
56 if(index != std::string::npos)
57 result.erase(++index);
61 std::string
ListIndex(std::string
const& section,
unsigned int index)
const
63 std::stringstream stream;
64 stream << section <<
"/" << index;
65 std::string result = stream.str();
66 std::transform(result.begin(), result.end(), result.begin(), ::tolower);
70 std::string
ContentIndex(std::string
const& section, std::string
const& entry)
const
73 std::string result = section +
'/' + entry;
74 std::transform(result.begin(), result.end(), result.begin(), ::tolower);
80 std::string result = section;
81 std::transform(result.begin(), result.end(), result.begin(), ::tolower);
94 std::ifstream file(filePath.c_str());
104 std::string inSection;
105 bool inConfigSection =
true;
107 while (std::getline(stream,line)) {
110 if (! line.length())
continue;
112 if (line[0] ==
'[') {
113 inConfigSection =
true;
114 inSection =
Trim(line.substr(1,line.find(
']')-1));
119 std::string address =
Trim(line.substr(1,line.find(
'}')-1));
122 inConfigSection =
false;
128 posEqual=line.find(
'=');
129 name =
Trim(line.substr(0,posEqual));
130 value =
Trim(line.substr(posEqual+1));
135 m_ListContent[inSection].push_back(
Trim(line));
140 std::string
Value(std::string
const& section, std::string
const& entry)
const
143 if (m_ConfigContent.find(index) == m_ConfigContent.end())
144 throw "Entry doesn't exist " + section + entry;
145 return m_ConfigContent.find(index)->second;
148 std::string
Value(
const std::string & section,
const std::string & entry,
const std::string& standard)
151 return Value(section, entry);
153 catch (
const char *) {
154 m_ConfigContent[
ContentIndex(section, entry)] = standard;
159 int IntValue(
const std::string & section,
const std::string & entry)
const
162 std::stringstream stream (
Value(section, entry));
167 int IntValue(
const std::string & section,
const std::string & entry,
int standard)
172 catch (
const char *) {
173 std::stringstream stream;
175 m_ConfigContent[
ContentIndex(section, entry)] = stream.str();
180 std::vector<std::string>
Vector(std::string
const& section,
unsigned int index)
const
182 if (m_ListContent.find(
ListIndex(section, index)) == m_ListContent.end())
183 throw "Entry doesn't exist " + section;
184 return m_ListContent.find(
ListIndex(section,index))->second;
187 unsigned int ListSize(std::string
const& section)
const
189 if (m_ListSize.find(
ListSizeIndex(section)) == m_ListSize.end())
190 throw "Entry doesn't exist " + section;
194 unsigned int ListSize(std::string
const& section,
unsigned int standard)
std::vector< std::string > Vector(std::string const §ion, unsigned int index) const
std::string Value(std::string const §ion, std::string const &entry) const
unsigned int ListSize(std::string const §ion, unsigned int standard)
std::string RemoveComment(std::string const &source, char const *delim="#;")
std::string ContentIndex(std::string const §ion, std::string const &entry) const
ListContentType m_ListContent
std::string Trim(std::string const &source, char const *delim=" \t\r\n")
std::map< std::string, unsigned int > m_ListSize
std::string Value(const std::string §ion, const std::string &entry, const std::string &standard)
ContentType m_ConfigContent
std::map< std::string, std::string > ContentType
void ReadStream(std::istream &stream)
int IntValue(const std::string §ion, const std::string &entry, int standard)
int IntValue(const std::string §ion, const std::string &entry) const
ConfigFileReader(std::string const &configFile)
std::string ListSizeIndex(std::string const §ion) const
std::map< std::string, std::vector< std::string > > ListContentType
unsigned int ListSize(std::string const §ion) const
void ReadFile(std::string const &filePath)
std::string ListIndex(std::string const §ion, unsigned int index) const