13 #ifndef ConfigFileReader_h 14 #define ConfigFileReader_h 35 std::string
Trim(std::string
const& source,
char const * delim =
" \t\r\n")
37 std::string result(source);
38 std::string::size_type index = result.find_last_not_of(delim);
39 if(index != std::string::npos)
40 result.erase(++index);
41 index = result.find_first_not_of(delim);
42 if(index != std::string::npos)
43 result.erase(0, index);
49 std::string
RemoveComment(std::string
const& source,
char const * delim =
"#;")
51 std::string result(source);
52 std::string::size_type index = result.find_first_of(delim);
53 if(index != std::string::npos)
54 result.erase(++index);
58 std::string
ListIndex(std::string
const& section,
unsigned int index)
const 60 std::stringstream stream;
61 stream << section <<
"/" << index;
62 std::string result = stream.str();
63 std::transform(result.begin(), result.end(), result.begin(), ::tolower);
67 std::string
ContentIndex(std::string
const& section, std::string
const& entry)
const 70 std::string result = section +
'/' + entry;
71 std::transform(result.begin(), result.end(), result.begin(), ::tolower);
77 std::string result = section;
78 std::transform(result.begin(), result.end(), result.begin(), ::tolower);
91 std::ifstream file(filePath.c_str());
101 std::string inSection;
102 bool inConfigSection =
true;
103 std::string::size_type posEqual;
104 while (std::getline(stream,line)) {
107 if (! line.length())
continue;
109 if (line[0] ==
'[') {
110 inConfigSection =
true;
111 inSection =
Trim(line.substr(1,line.find(
']')-1));
116 std::string address =
Trim(line.substr(1,line.find(
'}')-1));
119 inConfigSection =
false;
125 posEqual=line.find(
'=');
126 name =
Trim(line.substr(0,posEqual));
127 value =
Trim(line.substr(posEqual+1));
132 m_ListContent[inSection].push_back(
Trim(line));
137 std::string
Value(std::string
const& section, std::string
const& entry)
const 140 if (m_ConfigContent.find(index) == m_ConfigContent.end())
141 throw std::string(
"Entry doesn't exist " + section +
"::"+ entry);
142 std::cout << section <<
"::" << entry << m_ConfigContent.find(index)->second << std::endl;
143 return m_ConfigContent.find(index)->second;
146 std::string
Value(
const std::string & section,
const std::string & entry,
const std::string& standard)
149 return Value(section, entry);
151 catch (
const std::string) {
152 m_ConfigContent[
ContentIndex(section, entry)] = standard;
153 std::cout << section <<
"::" << entry << standard <<
" (default)" << std::endl;
158 int IntValue(
const std::string & section,
const std::string & entry)
const 161 std::stringstream stream (
Value(section, entry));
166 int IntValue(
const std::string & section,
const std::string & entry,
int standard)
171 catch (
const std::string) {
172 std::stringstream stream;
174 m_ConfigContent[
ContentIndex(section, entry)] = stream.str();
175 std::cout << section <<
"::" << entry << stream.str() <<
"(default)" << std::endl;
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())
184 throw std::string(
"Entry doesn't exist " + section);
186 return m_ListContent.find(
ListIndex(section,index))->second;
189 unsigned int ListSize(std::string
const& section)
const 191 if (m_ListSize.find(
ListSizeIndex(section)) == m_ListSize.end())
193 throw std::string(
"Entry doesn't exist " + section);
198 unsigned int ListSize(std::string
const& section,
unsigned int standard)
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
std::string ListSizeIndex(std::string const §ion) const
ListContentType m_ListContent
unsigned int ListSize(std::string const §ion) const
int IntValue(const std::string §ion, const std::string &entry) const
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::string Trim(std::string const &source, char const *delim=" \\)
std::string ListIndex(std::string const §ion, unsigned int index) const
std::map< std::string, std::string > ContentType
void ReadStream(std::istream &stream)
int IntValue(const std::string §ion, const std::string &entry, int standard)
ConfigFileReader(std::string const &configFile)
std::vector< std::string > Vector(std::string const §ion, unsigned int index) const
std::map< std::string, std::vector< std::string > > ListContentType
void ReadFile(std::string const &filePath)
std::string Value(std::string const §ion, std::string const &entry) const