Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/parser/ini_parser/ini_parser.h @ 9880

Last change on this file since 9880 was 9880, checked in by bensch, 18 years ago

new implementation of the IniParser
Now it is in Full stl-style, with iterators, and it does not have a strange internal state, that makes absolutely no sense

File size: 7.3 KB
Line 
1/*!
2 * @file ini_parser.h
3 * A small ini file parser
4 *
5 * Can be used to find a defined [Section] in an ini file and get the VarName = Value entries
6 */
7
8#ifndef _INI_PARSER_H
9#define _INI_PARSER_H
10
11#define PARSELINELENGHT     512       //!< how many chars to read at once
12
13#include <string>
14#include <list>
15
16//! ini-file parser
17/**
18 * This class can be used to load an initializer file and parse it's contents for variablename=value pairs.
19 */
20class IniParser
21{
22public:
23  ////////////////////////////////////
24  /// A Node for a Ini-Node. The base of all INI-elements.
25  class Node
26  {
27  public:
28    Node(const std::string& name, const std::string& comment = "");
29    virtual ~Node() {};
30    const std::string& name() const  { return _name; };
31    const std::string& comment() const  { return _comment; };
32    void setName(const std::string& name) { this->_name = name; };
33    void setComment(const std::string& comment) { this->_comment = comment; };
34
35    bool operator==(const std::string& name) const { return _name == name; };
36    bool operator==(const Node& node) const { return this->_name == node._name; };
37
38    virtual void debug() const = 0;
39
40  private:
41    std::string         _comment;  //!< A Comment that is appendet to the Top of this Node
42    std::string         _name;     //!< name of a given Node
43  };
44
45  //! a class for Entries in the Parser's File's Sections
46class Entry : public Node
47  {
48  public:
49    Entry(const std::string& name, const std::string& value = "", const std::string& comment = "");
50    const std::string& value() const { return _value; };
51    void setValue (const std::string& value) { _value = value; };
52
53    virtual void debug() const;
54
55  public:
56    typedef std::list<Entry>                    list;
57    typedef list::iterator                      iterator;
58    typedef list::const_iterator                const_iterator;
59
60  private:
61    std::string         _value;    //!< value of a given Entry
62  };
63
64  //! a clas for Sections in the Parser's file
65class Section : public Node
66  {
67  public:
68    Section(const std::string& sectionName, const std::string& comment = "");
69
70    Entry& addEntry(const std::string& entryName, const std::string& value = "", const std::string& comment = "");
71    bool editEntry(const std::string& entryName, const std::string& value, bool createMissing = true);
72    const std::string& getValue(const std::string& entryName, const std::string& defaultValue = "") const;
73
74    bool setEntryComment(const std::string& entryName, const std::string& comment);
75    const std::string& getEntryComment(const std::string& entryName) const;
76
77    const Entry::list& entries() const { return _entries; }
78    Entry* getEntry(const std::string& entryName);
79
80    Entry::const_iterator getEntryIt(const std::string& entryName) const;
81    Entry::iterator begin() { return _entries.begin(); };
82    Entry::const_iterator begin() const { return _entries.begin(); };
83    Entry::iterator end() { return _entries.end(); };
84    Entry::const_iterator end() const { return _entries.end(); };
85
86    void clear();
87
88    virtual void debug() const;
89
90  public:
91    typedef std::list<Section>                  list;
92    typedef list::iterator                      iterator;
93    typedef list::const_iterator                const_iterator;
94
95  private:
96    Entry::list     _entries;  //!< a list of entries for this section
97  };
98
99  //! A class for a INI-file.
100class Document : public Node
101  {
102  public:
103    Document(const std::string& fileName, const std::string& comment = "");
104
105    Section& addSection(const std::string& sectionName, const std::string& comment = "");
106    bool removeSection(const std::string& sectionName);
107    bool setSectionComment(const std::string& sectionName, const std::string& comment);
108
109    const Section::list& sections() const { return _sections; }
110    Section* getSection(const std::string& sectionName);
111
112    Section::const_iterator getSectionIt(const std::string& sectionName) const;
113    Section::iterator begin() { return _sections.begin(); };
114    Section::const_iterator begin() const { return _sections.begin(); };
115    Section::iterator end() { return _sections.end(); };
116    Section::const_iterator end() const { return _sections.end(); };
117
118    bool addEntry(const std::string& sectionName, const std::string& entryName, const std::string& value = "", const std::string& comment = "");
119    bool editEntry(const std::string& sectionName, const std::string& entryName, const std::string& value, bool createMissing = true);
120    const std::string& getValue(const std::string& sectionName, const std::string& entryName, const std::string& defaultValue = "") const;
121
122    bool setEntryComment(const std::string& sectionName, const std::string& entryName, const std::string& comment);
123    const std::string& getEntryComment(const std::string& sectionName, const std::string& entryName) const;
124
125    void clear();
126
127    virtual void debug() const;
128
129  private:
130    Section::list            _sections;        //!< a list of all stored Sections of the Parser.
131  };
132
133public:
134  IniParser (const std::string& filename = "");
135  virtual ~IniParser ();
136
137  /** @returns true if the file is opened, false otherwise */
138  bool isOpen() const { return !this->_document.sections().empty(); };
139  /** @returns the fileName we have opened. */
140  const std::string& getFileName() const { return this->_document.name(); };
141
142  /// Read and Write the File
143  bool readFile(const std::string& fileName, bool keepSettings = false);
144  bool writeFile(const std::string& fileName) const;
145
146  void setFileComment(const std::string& fileComment);
147  const std::string& getFileComment() const { return this->_document.comment(); };
148
149  /// Woring with Sections.
150  Section& addSection(const std::string& sectionName);
151  // iterate through sections with these Functions
152  Section* getSection(const std::string& sectionName) { return this->_document.getSection(sectionName); };
153  Section::const_iterator getSectionIt(const std::string& sectionName) const;
154
155  Section::iterator begin() { return this->_document.begin(); };
156  Section::const_iterator begin() const { return this->_document.begin(); };
157  Section::iterator end() { return this->_document.end(); };
158  Section::const_iterator end() const { return this->_document.end(); };
159
160  void setSectionComment(const std::string& sectionName, const std::string& comment);
161  const std::string& getSectionsComment(const std::string& sectionNane) const;
162
163  /// Working on Entries. (globally)
164  bool addEntry(const std::string& sectionName, const std::string& entryName, const std::string& value, const std::string& comment);
165  const std::string& getValue(const std::string& sectionNmae, const std::string& entryName, const std::string& defaultValue = "") const;
166  bool editEntry(const std::string& sectionName, const std::string& entryName, const std::string& value, bool createMissing = true);
167
168  void setEntryComment(const std::string& sectionName, const std::string& entryName, const std::string& comment);
169  const std::string& getEntryComment(const std::string& sectionName, const std::string& entryName) const;
170
171  // maintenance.
172  void debug() const;
173
174private:
175  void setNodeComment(Node* node, std::list<std::string>* comments);
176private:
177  std::string                      _fileName;
178  Document                         _document;
179
180  static const std::string         _emptyString;     //!< Just an Empty String that will be returned if nothing else is found.
181};
182
183#endif /* _INI_PARSER_H */
Note: See TracBrowser for help on using the repository browser.