Medical Imaging Interaction Toolkit  2026.06.00
Medical Imaging Interaction Toolkit
mitkConfigFileReader.h
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #ifndef mitkConfigFileReader_h
14 #define mitkConfigFileReader_h
15 
16 #include <stdlib.h>
17 #include <algorithm>
18 #include <fstream>
19 #include <map>
20 #include <vector>
21 #include <iostream>
22 
43 
44 {
45 protected:
46 
47  typedef std::map<std::string, std::string> ContentType;
48  typedef std::map<std::string, std::vector<std::string> > ListContentType;
49 
52  std::map<std::string, unsigned int > m_ListSize;
53 
54  std::string Trim(std::string const& source, char const * delim = " \t\r\n")
55  {
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);
63  else
64  result.erase();
65  return result;
66  }
67 
68  std::string RemoveComment(std::string const& source, char const * delim = "#;")
69  {
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);
74  return Trim(result);
75  }
76 
77  std::string ListIndex(std::string const& section, unsigned int index) const
78  {
79  std::stringstream stream;
80  stream << section << "/" << index;
81  std::string result = stream.str();
82  std::transform(result.begin(), result.end(), result.begin(), ::tolower);
83  return result;
84  }
85 
86  std::string ContentIndex(std::string const& section, std::string const& entry) const
87 
88  {
89  std::string result = section + '/' + entry;
90  std::transform(result.begin(), result.end(), result.begin(), ::tolower);
91  return result;
92  }
93 
94  std::string ListSizeIndex(std::string const& section) const
95  {
96  std::string result = section;
97  std::transform(result.begin(), result.end(), result.begin(), ::tolower);
98  return result;
99  }
100 
101 public:
102 
108  ConfigFileReader(std::string const& configFile)
109  {
110  ReadFile (configFile);
111  }
112 
118  void ReadFile(std::string const& filePath)
119  {
120  std::ifstream file(filePath.c_str());
121  ReadStream(file);
122  file.close();
123  }
124 
133  void ReadStream (std::istream& stream)
134  {
135  std::string line;
136  std::string name;
137  std::string value;
138  std::string inSection;
139  bool inConfigSection = true;
140  std::string::size_type posEqual;
141  while (std::getline(stream,line)) {
142  line = RemoveComment(line, "#");
143 
144  if (! line.length()) continue;
145 
146  if (line[0] == '[') {
147  inConfigSection = true;
148  inSection = Trim(line.substr(1,line.find(']')-1));
149  continue;
150  }
151 
152  if(line[0] == '{') {
153  std::string address = Trim(line.substr(1,line.find('}')-1));
154  inSection = ListIndex(address, ListSize(address,0));
155  m_ListSize[ListSizeIndex(address)]++;
156  inConfigSection = false;
157  continue;
158  }
159 
160  if (inConfigSection)
161  {
162  posEqual=line.find('=');
163  name = Trim(line.substr(0,posEqual));
164  value = Trim(line.substr(posEqual+1));
165  m_ConfigContent[ContentIndex(inSection, name)]=value;
166  }
167  else
168  {
169  m_ListContent[inSection].push_back(Trim(line));
170  }
171  }
172  }
173 
182  std::string Value(std::string const& section, std::string const& entry) const
183  {
184  std::string index = ContentIndex(section,entry);
185  if (m_ConfigContent.find(index) == m_ConfigContent.end())
186  throw std::string("Entry doesn't exist " + section +"::"+ entry);
187  std::cout << section << "::" << entry << m_ConfigContent.find(index)->second << std::endl;
188  return m_ConfigContent.find(index)->second;
189  }
190 
199  std::string Value(const std::string & section, const std::string & entry, const std::string& standard)
200  {
201  try {
202  return Value(section, entry);
203  }
204  catch (const std::string) {
205  m_ConfigContent[ContentIndex(section, entry)] = standard;
206  std::cout << section << "::" << entry << standard << " (default)" << std::endl;
207  return standard;
208  }
209  }
210 
219  int IntValue(const std::string & section, const std::string & entry) const
220  {
221  int result;
222  std::stringstream stream (Value(section, entry));
223  stream >> result;
224  return result;
225  }
226 
235  int IntValue(const std::string & section, const std::string & entry, int standard)
236  {
237  try {
238  return IntValue(section, entry);
239  }
240  catch (const std::string) {
241  std::stringstream stream;
242  stream << standard;
243  m_ConfigContent[ContentIndex(section, entry)] = stream.str();
244  std::cout << section << "::" << entry << stream.str() << "(default)" << std::endl;
245  return standard;
246  }
247  }
248 
257  std::vector<std::string> Vector(std::string const& section, unsigned int index) const
258  {
259  if (m_ListContent.find(ListIndex(section, index)) == m_ListContent.end())
260  {
261  throw std::string("Entry doesn't exist " + section);
262  }
263  return m_ListContent.find(ListIndex(section,index))->second;
264  }
265 
273  unsigned int ListSize(std::string const& section) const
274  {
275  if (m_ListSize.find(ListSizeIndex(section)) == m_ListSize.end())
276  {
277  throw std::string("Entry doesn't exist " + section);
278  }
279  return m_ListSize.find(ListSizeIndex(section))->second;
280  }
281 
289  unsigned int ListSize(std::string const& section, unsigned int standard)
290  {
291  try {
292  return ListSize(ListSizeIndex(section));
293  }
294  catch (...) {
295  m_ListSize[ListSizeIndex(section)] = standard;
296  return standard;
297  }
298  }
299 };
300 
301 #endif
Simple INI-style configuration file reader.
std::vector< std::string > Vector(std::string const &section, unsigned int index) const
Retrieve a list block's content lines by section name and index.
std::string Value(std::string const &section, std::string const &entry) const
Retrieve a string value from the configuration.
unsigned int ListSize(std::string const &section, 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 &section, 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 &section, const std::string &entry, int standard)
Retrieve an integer value, returning a default if the entry does not exist.
int IntValue(const std::string &section, 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 &section) const
ListContentType m_ListContent
std::map< std::string, std::vector< std::string > > ListContentType
std::string ContentIndex(std::string const &section, std::string const &entry) const
ContentType m_ConfigContent
std::string Value(const std::string &section, 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 &section) const
Get the number of list blocks with the given section name.