Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

many doxygen tags

File size: 8.8 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#include <string>
12#include <list>
13
14//! ini-file parser
15/**
16 * This class can be used to load an initializer file and parse it's contents for variablename=value pairs.
17 */
18class IniParser
19{
20public:
21  ////////////////////////////////////
22  /// A class for a Ini-Node. The base of all INI-elements.
23  class Node
24  {
25  public:
26    Node(const std::string& name, const std::string& comment = "");
27    //! Simple destructor
28    virtual ~Node() {};
29    /** @returns the name of the Node */
30    const std::string& name() const  { return _name; };
31    /** @returns the Comment of the Node */
32    const std::string& comment() const  { return _comment; };
33    /** @param name the name to set for this node */
34    void setName(const std::string& name) { this->_name = name; };
35    /** @param comment the Comment to set for this node */
36    void setComment(const std::string& comment) { this->_comment = comment; };
37
38    /** @param name the name to compare against this nodes name @returns true on match */
39    bool operator==(const std::string& name) const { return _name == name; };
40
41    /** @brief displays some debug information about the node */
42    virtual void debug() const = 0;
43
44  private:
45    std::string         _comment;  //!< A Comment that is appendet to the Top of this Node
46    std::string         _name;     //!< name of a given Node
47  };
48
49  //! a class for Entries in the Parser's File's Sections
50class Entry : public Node
51  {
52  public:
53    Entry(const std::string& name, const std::string& value = "", const std::string& comment = "");
54    /** @returns the Value of the Entry */
55    const std::string& value() const { return _value; };
56    /** @param value sets the value of the Entry */
57    void setValue (const std::string& value) { _value = value; };
58
59    virtual void debug() const;
60
61  public:
62    typedef std::list<Entry>       list;           //!< A Type definition for lists of Entries.
63    typedef list::iterator         iterator;       //!< A Type definition for iterators of Entries.
64    typedef list::const_iterator   const_iterator; //!< A Type definition for constant iterators of Entries.
65
66  private:
67    std::string                    _value;         //!< value of a given Entry
68  };
69
70  //! a clas for Sections in the Parser's file
71class Section : public Node
72  {
73  public:
74    Section(const std::string& sectionName, const std::string& comment = "");
75
76    Entry& addEntry(const std::string& entryName, const std::string& value = "", const std::string& comment = "");
77    bool editEntry(const std::string& entryName, const std::string& value, bool createMissing = true);
78    const std::string& getValue(const std::string& entryName, const std::string& defaultValue = "") const;
79
80    bool setEntryComment(const std::string& entryName, const std::string& comment);
81    const std::string& getEntryComment(const std::string& entryName) const;
82
83    /** @returns the List of Entries */
84    const Entry::list& entries() const { return _entries; }
85    Entry* getEntry(const std::string& entryName);
86
87    Entry::const_iterator getEntryIt(const std::string& entryName) const;
88    /** @returns an Iterator pointing to the beginning of the entries. */
89    Entry::iterator begin() { return _entries.begin(); };
90    /** @returns a constant Iterator pointing to the beginning of the entries */
91    Entry::const_iterator begin() const { return _entries.begin(); };
92    /** @returns an Iterator pointing to the end of the entries */
93    Entry::iterator end() { return _entries.end(); };
94    /** @returns a constant Iterator pointing to the end of the entries */
95    Entry::const_iterator end() const { return _entries.end(); };
96
97    void clear();
98
99    virtual void debug() const;
100
101  public:
102    typedef std::list<Section>      list;            //!< A Type definition for lists of Sections
103    typedef list::iterator          iterator;        //!< A Type definition for iterators of Sectionlists.
104    typedef list::const_iterator    const_iterator;  //!< A Type definition for constant iterators of Sectionlists.
105
106  private:
107    Entry::list                     _entries;        //!< a list of entries for this section
108  };
109
110  //! A class for a INI-file.
111class Document : public Node
112  {
113  public:
114    Document(const std::string& fileName, const std::string& comment = "");
115
116    Section& addSection(const std::string& sectionName, const std::string& comment = "");
117    bool removeSection(const std::string& sectionName);
118    bool setSectionComment(const std::string& sectionName, const std::string& comment);
119
120    /** @returns list of all sections */
121    const Section::list& sections() const { return _sections; }
122    Section* getSection(const std::string& sectionName);
123
124    Section::const_iterator getSectionIt(const std::string& sectionName) const;
125    /** @returns an Iterator poining to the beginning of the Sections List */
126    Section::iterator begin() { return _sections.begin(); };
127    /** @returns a constant Iterator poining to the beginning of the Sections List */
128    Section::const_iterator begin() const { return _sections.begin(); };
129    /** @returns an Iterator poining to the end of the Sections List */
130    Section::iterator end() { return _sections.end(); };
131    /** @returns a constant Iterator poining to the end of the Sections List */
132    Section::const_iterator end() const { return _sections.end(); };
133
134    bool addEntry(const std::string& sectionName, const std::string& entryName, const std::string& value = "", const std::string& comment = "");
135    bool editEntry(const std::string& sectionName, const std::string& entryName, const std::string& value, bool createMissing = true);
136    const std::string& getValue(const std::string& sectionName, const std::string& entryName, const std::string& defaultValue = "") const;
137
138    bool setEntryComment(const std::string& sectionName, const std::string& entryName, const std::string& comment);
139    const std::string& getEntryComment(const std::string& sectionName, const std::string& entryName) const;
140
141    void clear();
142
143    virtual void debug() const;
144
145  private:
146    Section::list       _sections;        //!< a list of all stored Sections of the Parser.
147  };
148
149public:
150  IniParser (const std::string& filename = "");
151  virtual ~IniParser ();
152
153  /** @returns true if the file is opened, false otherwise */
154  bool isOpen() const { return !this->_document.sections().empty(); };
155  /** @returns the fileName we have opened. */
156  const std::string& getFileName() const { return this->_document.name(); };
157
158  /// Read and Write the File
159  bool readFile(const std::string& fileName, bool keepSettings = false);
160  bool writeFile(const std::string& fileName = "") const;
161
162  void setFileComment(const std::string& fileComment);
163  /** @returns comments for the File. */
164  const std::string& getFileComment() const { return this->_document.comment(); };
165
166  /// Woring with Sections.
167  Section& addSection(const std::string& sectionName);
168  // iterate through sections with these Functions
169  //! see Section::getSection()
170  Section* getSection(const std::string& sectionName) { return this->_document.getSection(sectionName); };
171  Section::const_iterator getSectionIt(const std::string& sectionName) const;
172
173  //! see Section::begin()
174  Section::iterator begin() { return this->_document.begin(); };
175  //! see Section::begin()
176  Section::const_iterator begin() const { return this->_document.begin(); };
177    //! see Section::end()
178  Section::iterator end() { return this->_document.end(); };
179    //! see Section::end()
180  Section::const_iterator end() const { return this->_document.end(); };
181
182  void setSectionComment(const std::string& sectionName, const std::string& comment);
183  const std::string& getSectionsComment(const std::string& sectionNane) const;
184
185  /// Working on Entries. (globally)
186  bool addEntry(const std::string& sectionName, const std::string& entryName, const std::string& value, const std::string& comment);
187  const std::string& getValue(const std::string& sectionNmae, const std::string& entryName, const std::string& defaultValue = "") const;
188  bool editEntry(const std::string& sectionName, const std::string& entryName, const std::string& value, bool createMissing = true);
189
190  void setEntryComment(const std::string& sectionName, const std::string& entryName, const std::string& comment);
191  const std::string& getEntryComment(const std::string& sectionName, const std::string& entryName) const;
192
193  // maintenance.
194  void debug() const;
195
196private:
197  void setNodeComment(Node* node, std::list<std::string>* comments);
198private:
199  std::string                      _fileName;        //!< The name of the File that is parsed here.
200  Document                         _document;        //!< The Document inside of the file.
201
202  static const std::string         _emptyString;     //!< Just an Empty String that will be returned if nothing else is found.
203};
204
205#endif /* _INI_PARSER_H */
Note: See TracBrowser for help on using the repository browser.