Medical Imaging Interaction Toolkit  2024.12.99-0da743f6
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 
24 
25 {
26 protected:
27 
28  typedef std::map<std::string, std::string> ContentType;
29  typedef std::map<std::string, std::vector<std::string> > ListContentType;
30 
33  std::map<std::string, unsigned int > m_ListSize;
34 
35  std::string Trim(std::string const& source, char const * delim = " \t\r\n")
36  {
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);
44  else
45  result.erase();
46  return result;
47  }
48 
49  std::string RemoveComment(std::string const& source, char const * delim = "#;")
50  {
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);
55  return Trim(result);
56  }
57 
58  std::string ListIndex(std::string const& section, unsigned int index) const
59  {
60  std::stringstream stream;
61  stream << section << "/" << index;
62  std::string result = stream.str();
63  std::transform(result.begin(), result.end(), result.begin(), ::tolower);
64  return result;
65  }
66 
67  std::string ContentIndex(std::string const& section, std::string const& entry) const
68 
69  {
70  std::string result = section + '/' + entry;
71  std::transform(result.begin(), result.end(), result.begin(), ::tolower);
72  return result;
73  }
74 
75  std::string ListSizeIndex(std::string const& section) const
76  {
77  std::string result = section;
78  std::transform(result.begin(), result.end(), result.begin(), ::tolower);
79  return result;
80  }
81 
82 public:
83 
84  ConfigFileReader(std::string const& configFile)
85  {
86  ReadFile (configFile);
87  }
88 
89  void ReadFile(std::string const& filePath)
90  {
91  std::ifstream file(filePath.c_str());
92  ReadStream(file);
93  file.close();
94  }
95 
96  void ReadStream (std::istream& stream)
97  {
98  std::string line;
99  std::string name;
100  std::string value;
101  std::string inSection;
102  bool inConfigSection = true;
103  std::string::size_type posEqual;
104  while (std::getline(stream,line)) {
105  line = RemoveComment(line, "#");
106 
107  if (! line.length()) continue;
108 
109  if (line[0] == '[') {
110  inConfigSection = true;
111  inSection = Trim(line.substr(1,line.find(']')-1));
112  continue;
113  }
114 
115  if(line[0] == '{') {
116  std::string address = Trim(line.substr(1,line.find('}')-1));
117  inSection = ListIndex(address, ListSize(address,0));
118  m_ListSize[ListSizeIndex(address)]++;
119  inConfigSection = false;
120  continue;
121  }
122 
123  if (inConfigSection)
124  {
125  posEqual=line.find('=');
126  name = Trim(line.substr(0,posEqual));
127  value = Trim(line.substr(posEqual+1));
128  m_ConfigContent[ContentIndex(inSection, name)]=value;
129  }
130  else
131  {
132  m_ListContent[inSection].push_back(Trim(line));
133  }
134  }
135  }
136 
137  std::string Value(std::string const& section, std::string const& entry) const
138  {
139  std::string index = ContentIndex(section,entry);
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;
144  }
145 
146  std::string Value(const std::string & section, const std::string & entry, const std::string& standard)
147  {
148  try {
149  return Value(section, entry);
150  }
151  catch (const std::string) {
152  m_ConfigContent[ContentIndex(section, entry)] = standard;
153  std::cout << section << "::" << entry << standard << " (default)" << std::endl;
154  return standard;
155  }
156  }
157 
158  int IntValue(const std::string & section, const std::string & entry) const
159  {
160  int result;
161  std::stringstream stream (Value(section, entry));
162  stream >> result;
163  return result;
164  }
165 
166  int IntValue(const std::string & section, const std::string & entry, int standard)
167  {
168  try {
169  return IntValue(section, entry);
170  }
171  catch (const std::string) {
172  std::stringstream stream;
173  stream << standard;
174  m_ConfigContent[ContentIndex(section, entry)] = stream.str();
175  std::cout << section << "::" << entry << stream.str() << "(default)" << std::endl;
176  return standard;
177  }
178  }
179 
180  std::vector<std::string> Vector(std::string const& section, unsigned int index) const
181  {
182  if (m_ListContent.find(ListIndex(section, index)) == m_ListContent.end())
183  {
184  throw std::string("Entry doesn't exist " + section);
185  }
186  return m_ListContent.find(ListIndex(section,index))->second;
187  }
188 
189  unsigned int ListSize(std::string const& section) const
190  {
191  if (m_ListSize.find(ListSizeIndex(section)) == m_ListSize.end())
192  {
193  throw std::string("Entry doesn't exist " + section);
194  }
195  return m_ListSize.find(ListSizeIndex(section))->second;
196  }
197 
198  unsigned int ListSize(std::string const& section, unsigned int standard)
199  {
200  try {
201  return ListSize(ListSizeIndex(section));
202  }
203  catch (...) {
204  m_ListSize[ListSizeIndex(section)] = standard;
205  return standard;
206  }
207  }
208 };
209 
210 #endif
ConfigFileReader::ContentType
std::map< std::string, std::string > ContentType
Definition: mitkConfigFileReader.h:28
ConfigFileReader::ListSize
unsigned int ListSize(std::string const &section) const
Definition: mitkConfigFileReader.h:189
ConfigFileReader::IntValue
int IntValue(const std::string &section, const std::string &entry) const
Definition: mitkConfigFileReader.h:158
ConfigFileReader::ListContentType
std::map< std::string, std::vector< std::string > > ListContentType
Definition: mitkConfigFileReader.h:29
ConfigFileReader::m_ConfigContent
ContentType m_ConfigContent
Definition: mitkConfigFileReader.h:31
ConfigFileReader::ListIndex
std::string ListIndex(std::string const &section, unsigned int index) const
Definition: mitkConfigFileReader.h:58
ConfigFileReader::IntValue
int IntValue(const std::string &section, const std::string &entry, int standard)
Definition: mitkConfigFileReader.h:166
ConfigFileReader::ConfigFileReader
ConfigFileReader(std::string const &configFile)
Definition: mitkConfigFileReader.h:84
ConfigFileReader::RemoveComment
std::string RemoveComment(std::string const &source, char const *delim="#;")
Definition: mitkConfigFileReader.h:49
ConfigFileReader::ListSize
unsigned int ListSize(std::string const &section, unsigned int standard)
Definition: mitkConfigFileReader.h:198
ConfigFileReader::Vector
std::vector< std::string > Vector(std::string const &section, unsigned int index) const
Definition: mitkConfigFileReader.h:180
ConfigFileReader::ListSizeIndex
std::string ListSizeIndex(std::string const &section) const
Definition: mitkConfigFileReader.h:75
ConfigFileReader::ContentIndex
std::string ContentIndex(std::string const &section, std::string const &entry) const
Definition: mitkConfigFileReader.h:67
ConfigFileReader::ReadFile
void ReadFile(std::string const &filePath)
Definition: mitkConfigFileReader.h:89
ConfigFileReader::Value
std::string Value(std::string const &section, std::string const &entry) const
Definition: mitkConfigFileReader.h:137
ConfigFileReader::Trim
std::string Trim(std::string const &source, char const *delim=" \t\r\n")
Definition: mitkConfigFileReader.h:35
ConfigFileReader::m_ListSize
std::map< std::string, unsigned int > m_ListSize
Definition: mitkConfigFileReader.h:33
ConfigFileReader::Value
std::string Value(const std::string &section, const std::string &entry, const std::string &standard)
Definition: mitkConfigFileReader.h:146
ConfigFileReader::m_ListContent
ListContentType m_ListContent
Definition: mitkConfigFileReader.h:32
ConfigFileReader
Definition: mitkConfigFileReader.h:23
ConfigFileReader::ReadStream
void ReadStream(std::istream &stream)
Definition: mitkConfigFileReader.h:96