Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: completely doxygened and finalized IniParser

File size: 9.9 KB
RevLine 
[2064]1/*!
[5014]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 */
[3224]7#ifndef _INI_PARSER_H
8#define _INI_PARSER_H
[2064]9
[9880]10#include <string>
[5933]11#include <list>
[2064]12
[2141]13//! ini-file parser
14/**
[5014]15 * This class can be used to load an initializer file and parse it's contents for variablename=value pairs.
16 */
[9880]17class IniParser
[4597]18{
[9880]19public:
20  ////////////////////////////////////
[9881]21  /// A class for a Ini-Node. The base of all INI-elements.
[9880]22  class Node
23  {
24  public:
25    Node(const std::string& name, const std::string& comment = "");
[9881]26    //! Simple destructor
[9880]27    virtual ~Node() {};
[9881]28    /** @returns the name of the Node */
[9880]29    const std::string& name() const  { return _name; };
[9881]30    /** @returns the Comment of the Node */
[9880]31    const std::string& comment() const  { return _comment; };
[9881]32    /** @param name the name to set for this node */
[9880]33    void setName(const std::string& name) { this->_name = name; };
[9881]34    /** @param comment the Comment to set for this node */
[9880]35    void setComment(const std::string& comment) { this->_comment = comment; };
36
[9881]37    /** @param name the name to compare against this nodes name @returns true on match */
[9880]38    bool operator==(const std::string& name) const { return _name == name; };
39
[9881]40    /** @brief displays some debug information about the node */
[9880]41    virtual void debug() const = 0;
42
[5014]43  private:
[9880]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  };
[5945]47
[9880]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 = "");
[9881]53    /** @returns the Value of the Entry */
[9880]54    const std::string& value() const { return _value; };
[9881]55    /** @param value sets the value of the Entry */
[9880]56    void setValue (const std::string& value) { _value = value; };
[4482]57
[9880]58    virtual void debug() const;
59
[5014]60  public:
[9881]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.
[4482]64
[9880]65  private:
[9881]66    std::string                    _value;         //!< value of a given Entry
[9880]67  };
[5945]68
[9880]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 = "");
[4597]74
[9880]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;
[5945]78
[9880]79    bool setEntryComment(const std::string& entryName, const std::string& comment);
80    const std::string& getEntryComment(const std::string& entryName) const;
[5015]81
[9881]82    /** @returns the List of Entries */
[9880]83    const Entry::list& entries() const { return _entries; }
84    Entry* getEntry(const std::string& entryName);
[5014]85
[9880]86    Entry::const_iterator getEntryIt(const std::string& entryName) const;
[9881]87    /** @returns an Iterator pointing to the beginning of the entries. */
[9880]88    Entry::iterator begin() { return _entries.begin(); };
[9881]89    /** @returns a constant Iterator pointing to the beginning of the entries */
[9880]90    Entry::const_iterator begin() const { return _entries.begin(); };
[9881]91    /** @returns an Iterator pointing to the end of the entries */
[9880]92    Entry::iterator end() { return _entries.end(); };
[9881]93    /** @returns a constant Iterator pointing to the end of the entries */
[9880]94    Entry::const_iterator end() const { return _entries.end(); };
[5014]95
[9880]96    void clear();
[5015]97
[9880]98    virtual void debug() const;
[5014]99
[9880]100  public:
[9881]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.
[5945]104
[9880]105  private:
[9881]106    Entry::list                     _entries;        //!< a list of entries for this section
[9880]107  };
[5014]108
[9880]109  //! A class for a INI-file.
110class Document : public Node
111  {
112  public:
113    Document(const std::string& fileName, const std::string& comment = "");
[5945]114
[9880]115    Section& addSection(const std::string& sectionName, const std::string& comment = "");
116    bool removeSection(const std::string& sectionName);
[9883]117    bool setSectionsComment(const std::string& sectionName, const std::string& comment);
118    const std::string& getSectionsComment(const std::string& sectionName) const;
[5014]119
[9881]120    /** @returns list of all sections */
[9880]121    const Section::list& sections() const { return _sections; }
122    Section* getSection(const std::string& sectionName);
[5934]123
[9880]124    Section::const_iterator getSectionIt(const std::string& sectionName) const;
[9881]125    /** @returns an Iterator poining to the beginning of the Sections List */
[9880]126    Section::iterator begin() { return _sections.begin(); };
[9881]127    /** @returns a constant Iterator poining to the beginning of the Sections List */
[9880]128    Section::const_iterator begin() const { return _sections.begin(); };
[9881]129    /** @returns an Iterator poining to the end of the Sections List */
[9880]130    Section::iterator end() { return _sections.end(); };
[9881]131    /** @returns a constant Iterator poining to the end of the Sections List */
[9880]132    Section::const_iterator end() const { return _sections.end(); };
[5014]133
[9880]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;
[5946]137
[9880]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;
[5951]140
[9880]141    void clear();
[5945]142
[9880]143    virtual void debug() const;
144
[5014]145  private:
[9881]146    Section::list       _sections;        //!< a list of all stored Sections of the Parser.
[9880]147  };
[5945]148
[9880]149public:
150  IniParser (const std::string& filename = "");
151  virtual ~IniParser ();
[7221]152
[9880]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(); };
[7221]157
[9880]158  /// Read and Write the File
159  bool readFile(const std::string& fileName, bool keepSettings = false);
[9881]160  bool writeFile(const std::string& fileName = "") const;
[9880]161
[9883]162  //! @param fileComment the comment of the Document */
163  void setFileComment(const std::string& fileComment) { this->_document.setComment(fileComment); };
[9881]164  /** @returns comments for the File. */
[9880]165  const std::string& getFileComment() const { return this->_document.comment(); };
166
[9883]167  /// Working with Sections.
[9880]168  Section& addSection(const std::string& sectionName);
169  // iterate through sections with these Functions
[9883]170  //! @see Section::getSection()
[9880]171  Section* getSection(const std::string& sectionName) { return this->_document.getSection(sectionName); };
[9883]172  //! @see Document::getSectionIt()
173  Section::const_iterator getSectionIt(const std::string& sectionName) const { return this->_document.getSectionIt(sectionName); };
[9880]174
[9883]175  //! @see Document::begin()
[9880]176  Section::iterator begin() { return this->_document.begin(); };
[9883]177  //! @see Document::begin()
[9880]178  Section::const_iterator begin() const { return this->_document.begin(); };
[9883]179  //! @see Document::end()
[9880]180  Section::iterator end() { return this->_document.end(); };
[9883]181  //! @see Document::end()
[9880]182  Section::const_iterator end() const { return this->_document.end(); };
[9883]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); };
[9880]187
188  /// Working on Entries. (globally)
[9883]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); };
[9880]204
205  // maintenance.
206  void debug() const;
207
208private:
209  void setNodeComment(Node* node, std::list<std::string>* comments);
210private:
[9881]211  std::string                      _fileName;        //!< The name of the File that is parsed here.
212  Document                         _document;        //!< The Document inside of the file.
[9880]213
214  static const std::string         _emptyString;     //!< Just an Empty String that will be returned if nothing else is found.
[2064]215};
216
[3224]217#endif /* _INI_PARSER_H */
Note: See TracBrowser for help on using the repository browser.