nsnake
Classic snake game for the terminal
INI.hpp
1 #ifndef INI_H_DEFINED
2 #define INI_H_DEFINED
3 
4 #include <cassert>
5 #include <map>
6 #include <list>
7 #include <stdexcept>
8 #include <string>
9 #include <cstring>
10 #include <iostream>
11 #include <fstream>
12 
48 namespace INI {
49 
71  struct Level
72  {
74  Level() :
75  parent(NULL),
76  depth(0)
77  { }
78 
80  Level(Level* p) :
81  parent(p),
82  depth(0)
83  { }
84 
88 
90  size_t depth;
91 
92  typedef std::map<std::string, std::string> ValueMap;
93  typedef std::map<std::string, Level> SectionMap;
94  typedef std::list<ValueMap::const_iterator> Values;
95  typedef std::list<SectionMap::const_iterator> Sections;
96 
107  ValueMap values;
108 
120  SectionMap sections;
121 
124 
127 
129  const std::string& operator[](const std::string& name)
130  {
131  return this->values[name];
132  }
133 
135  Level& operator()(const std::string& name)
136  {
137  return this->sections[name];
138  }
139 
144  void addGroup(std::string name);
145 
151  void addKey(std::string name, std::string value);
152  };
153 
156  class Parser
157  {
158 
159  public:
163  Parser();
164 
166  Parser(std::string filename);
167 
170  Parser(std::istream& stream);
171 
177  void dump(std::ostream& stream);
178 
184  Level& top();
185 
187  const std::string& operator[](const std::string& name)
188  {
189  return this->top()[name];
190  }
191 
193  Level& operator()(const std::string& name)
194  {
195  return this->top()(name);
196  }
197 
208  void create();
209 
211  void saveAs(std::string filename);
212 
213  private:
214  void dump(std::ostream& s, const Level& l, const std::string& sname);
215 
216  void parse(Level& l);
217 
227  void parseLevelLine(std::string& sname, size_t& depth);
228 
231  void raise_error(std::string msg);
232 
233  Level top_level;
234 
235  std::ifstream input_file;
236 
237  std::istream* input;
238 
239  std::string line_;
240 
242  size_t lines;
243  };
244 }
245 
246 #endif // INI_H_DEFINED
247 
Contains a "scope" of the INI file.
Definition: INI.hpp:71
Level & operator()(const std::string &name)
Access another Level within this Level.
Definition: INI.hpp:135
Level()
Create the topmost Level.
Definition: INI.hpp:74
size_t depth
Counter of how many nested levels this one is.
Definition: INI.hpp:90
void addGroup(std::string name)
Creates a new child group with #name.
Definition: INI.cpp:4
SectionMap sections
All the Levels inside this Level.
Definition: INI.hpp:120
Loads, reads and parses the contents of an INI file (or string).
Definition: INI.hpp:156
Values ordered_values
All values in the original order of the INI file.
Definition: INI.hpp:123
const std::string & operator[](const std::string &name)
Access a key within this Level.
Definition: INI.hpp:129
void addKey(std::string name, std::string value)
Creates a new key #name with #value.
Definition: INI.cpp:27
ValueMap values
All the key values inside this Level.
Definition: INI.hpp:107
const std::string & operator[](const std::string &name)
Shortcut to access a key within the top level.
Definition: INI.hpp:187
Level(Level *p)
Create a level with parent #p.
Definition: INI.hpp:80
Level & operator()(const std::string &name)
Shortcut to access a Level within the top level.
Definition: INI.hpp:193
Sections ordered_sections
All Sections in the original order of the INI file.
Definition: INI.hpp:126
Level * parent
The parent Level of this one.
Definition: INI.hpp:87
Simple module that contains everything needed to load and parse a file with the INI configuration for...
Definition: INI.hpp:48