Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #ifndef ConfigFileReader_h
18 #define ConfigFileReader_h
19 
20 #include <stdlib.h>
21 #include <algorithm>
22 #include <fstream>
23 #include <map>
24 #include <vector>
25 
27 
28 {
29 protected:
30 
31  typedef std::map<std::string, std::string> ContentType;
32  typedef std::map<std::string, std::vector<std::string> > ListContentType;
33 
34  ContentType m_ConfigContent;
35  ListContentType m_ListContent;
36  std::map<std::string, unsigned int > m_ListSize;
37 
38  std::string Trim(std::string const& source, char const * delim = " \t\r\n")
39  {
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);
47  else
48  result.erase();
49  return result;
50  }
51 
52  std::string RemoveComment(std::string const& source, char const * delim = "#;")
53  {
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);
58  return Trim(result);
59  }
60 
61  std::string ListIndex(std::string const& section, unsigned int index) const
62  {
63  std::stringstream stream;
64  stream << section << "/" << index;
65  std::string result = stream.str();
66  std::transform(result.begin(), result.end(), result.begin(), ::tolower);
67  return result;
68  }
69 
70  std::string ContentIndex(std::string const& section, std::string const& entry) const
71 
72  {
73  std::string result = section + '/' + entry;
74  std::transform(result.begin(), result.end(), result.begin(), ::tolower);
75  return result;
76  }
77 
78  std::string ListSizeIndex(std::string const& section) const
79  {
80  std::string result = section;
81  std::transform(result.begin(), result.end(), result.begin(), ::tolower);
82  return result;
83  }
84 
85 public:
86 
87  ConfigFileReader(std::string const& configFile)
88  {
89  ReadFile (configFile);
90  }
91 
92  void ReadFile(std::string const& filePath)
93  {
94  std::ifstream file(filePath.c_str());
95  ReadStream(file);
96  file.close();
97  }
98 
99  void ReadStream (std::istream& stream)
100  {
101  std::string line;
102  std::string name;
103  std::string value;
104  std::string inSection;
105  bool inConfigSection = true;
106  int posEqual;
107  while (std::getline(stream,line)) {
108  line = RemoveComment(line, "#");
109 
110  if (! line.length()) continue;
111 
112  if (line[0] == '[') {
113  inConfigSection = true;
114  inSection = Trim(line.substr(1,line.find(']')-1));
115  continue;
116  }
117 
118  if(line[0] == '{') {
119  std::string address = Trim(line.substr(1,line.find('}')-1));
120  inSection = ListIndex(address, ListSize(address,0));
121  m_ListSize[ListSizeIndex(address)]++;
122  inConfigSection = false;
123  continue;
124  }
125 
126  if (inConfigSection)
127  {
128  posEqual=line.find('=');
129  name = Trim(line.substr(0,posEqual));
130  value = Trim(line.substr(posEqual+1));
131  m_ConfigContent[ContentIndex(inSection, name)]=value;
132  }
133  else
134  {
135  m_ListContent[inSection].push_back(Trim(line));
136  }
137  }
138  }
139 
140  std::string Value(std::string const& section, std::string const& entry) const
141  {
142  std::string index = ContentIndex(section,entry);
143  if (m_ConfigContent.find(index) == m_ConfigContent.end())
144  throw "Entry doesn't exist " + section + entry;
145  return m_ConfigContent.find(index)->second;
146  }
147 
148  std::string Value(const std::string & section, const std::string & entry, const std::string& standard)
149  {
150  try {
151  return Value(section, entry);
152  }
153  catch (const char *) {
154  m_ConfigContent[ContentIndex(section, entry)] = standard;
155  return standard;
156  }
157  }
158 
159  int IntValue(const std::string & section, const std::string & entry) const
160  {
161  int result;
162  std::stringstream stream (Value(section, entry));
163  stream >> result;
164  return result;
165  }
166 
167  int IntValue(const std::string & section, const std::string & entry, int standard)
168  {
169  try {
170  return IntValue(section, entry);
171  }
172  catch (const char *) {
173  std::stringstream stream;
174  stream << standard;
175  m_ConfigContent[ContentIndex(section, entry)] = stream.str();
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  throw "Entry doesn't exist " + section;
184  return m_ListContent.find(ListIndex(section,index))->second;
185  }
186 
187  unsigned int ListSize(std::string const& section) const
188  {
189  if (m_ListSize.find(ListSizeIndex(section)) == m_ListSize.end())
190  throw "Entry doesn't exist " + section;
191  return m_ListSize.find(ListSizeIndex(section))->second;
192  }
193 
194  unsigned int ListSize(std::string const& section, unsigned int standard)
195  {
196  try {
197  return ListSize(ListSizeIndex(section));
198  }
199  catch (...) {
200  m_ListSize[ListSizeIndex(section)] = standard;
201  return standard;
202  }
203  }
204 };
205 
206 #endif
std::vector< std::string > Vector(std::string const &section, unsigned int index) const
std::string Value(std::string const &section, std::string const &entry) const
static char * line
Definition: svm.cpp:2884
unsigned int ListSize(std::string const &section, unsigned int standard)
std::string RemoveComment(std::string const &source, char const *delim="#;")
std::string ContentIndex(std::string const &section, 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 &section, 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 &section, const std::string &entry, int standard)
int IntValue(const std::string &section, const std::string &entry) const
ConfigFileReader(std::string const &configFile)
std::string ListSizeIndex(std::string const &section) const
std::map< std::string, std::vector< std::string > > ListContentType
unsigned int ListSize(std::string const &section) const
void ReadFile(std::string const &filePath)
std::string ListIndex(std::string const &section, unsigned int index) const