Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/test/src/lib/parser/ini_parser/ini_parser.h @ 10761

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

trunk: completely doxygened and finalized IniParser

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