13 #ifndef mitkConfigFileReader_h
14 #define mitkConfigFileReader_h
54 std::string
Trim(std::string
const& source,
char const * delim =
" \t\r\n")
56 std::string result(source);
57 std::string::size_type index = result.find_last_not_of(delim);
58 if(index != std::string::npos)
59 result.erase(++index);
60 index = result.find_first_not_of(delim);
61 if(index != std::string::npos)
62 result.erase(0, index);
68 std::string
RemoveComment(std::string
const& source,
char const * delim =
"#;")
70 std::string result(source);
71 std::string::size_type index = result.find_first_of(delim);
72 if(index != std::string::npos)
73 result.erase(++index);
77 std::string
ListIndex(std::string
const& section,
unsigned int index)
const
79 std::stringstream stream;
80 stream << section <<
"/" << index;
81 std::string result = stream.str();
82 std::transform(result.begin(), result.end(), result.begin(), ::tolower);
86 std::string
ContentIndex(std::string
const& section, std::string
const& entry)
const
89 std::string result = section +
'/' + entry;
90 std::transform(result.begin(), result.end(), result.begin(), ::tolower);
96 std::string result = section;
97 std::transform(result.begin(), result.end(), result.begin(), ::tolower);
120 std::ifstream file(filePath.c_str());
138 std::string inSection;
139 bool inConfigSection =
true;
140 std::string::size_type posEqual;
141 while (std::getline(stream,line)) {
144 if (! line.length())
continue;
146 if (line[0] ==
'[') {
147 inConfigSection =
true;
148 inSection =
Trim(line.substr(1,line.find(
']')-1));
153 std::string address =
Trim(line.substr(1,line.find(
'}')-1));
156 inConfigSection =
false;
162 posEqual=line.find(
'=');
163 name =
Trim(line.substr(0,posEqual));
164 value =
Trim(line.substr(posEqual+1));
182 std::string
Value(std::string
const& section, std::string
const& entry)
const
186 throw std::string(
"Entry doesn't exist " + section +
"::"+ entry);
187 std::cout << section <<
"::" << entry <<
m_ConfigContent.find(index)->second << std::endl;
199 std::string
Value(
const std::string & section,
const std::string & entry,
const std::string& standard)
202 return Value(section, entry);
204 catch (
const std::string) {
206 std::cout << section <<
"::" << entry << standard <<
" (default)" << std::endl;
219 int IntValue(
const std::string & section,
const std::string & entry)
const
222 std::stringstream stream (
Value(section, entry));
235 int IntValue(
const std::string & section,
const std::string & entry,
int standard)
240 catch (
const std::string) {
241 std::stringstream stream;
244 std::cout << section <<
"::" << entry << stream.str() <<
"(default)" << std::endl;
257 std::vector<std::string>
Vector(std::string
const& section,
unsigned int index)
const
261 throw std::string(
"Entry doesn't exist " + section);
273 unsigned int ListSize(std::string
const& section)
const
277 throw std::string(
"Entry doesn't exist " + section);
289 unsigned int ListSize(std::string
const& section,
unsigned int standard)
Simple INI-style configuration file reader.
std::vector< std::string > Vector(std::string const §ion, unsigned int index) const
Retrieve a list block's content lines by section name and index.
std::string Value(std::string const §ion, std::string const &entry) const
Retrieve a string value from the configuration.
unsigned int ListSize(std::string const §ion, unsigned int standard)
Get the number of list blocks, returning a default if none exist.
ConfigFileReader(std::string const &configFile)
Construct a ConfigFileReader and immediately parse the given file.
std::string Trim(std::string const &source, char const *delim=" \t\r\n")
std::string ListIndex(std::string const §ion, unsigned int index) const
void ReadStream(std::istream &stream)
Parse configuration data from an input stream.
std::string RemoveComment(std::string const &source, char const *delim="#;")
std::map< std::string, std::string > ContentType
std::map< std::string, unsigned int > m_ListSize
int IntValue(const std::string §ion, const std::string &entry, int standard)
Retrieve an integer value, returning a default if the entry does not exist.
int IntValue(const std::string §ion, const std::string &entry) const
Retrieve an integer value from the configuration.
void ReadFile(std::string const &filePath)
Read and parse a configuration file from disk.
std::string ListSizeIndex(std::string const §ion) const
ListContentType m_ListContent
std::map< std::string, std::vector< std::string > > ListContentType
std::string ContentIndex(std::string const §ion, std::string const &entry) const
ContentType m_ConfigContent
std::string Value(const std::string §ion, const std::string &entry, const std::string &standard)
Retrieve a string value, returning a default if the entry does not exist.
unsigned int ListSize(std::string const §ion) const
Get the number of list blocks with the given section name.