Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 9880 in orxonox.OLD for trunk/src/lib/parser/ini_parser/ini_parser.h


Ignore:
Timestamp:
Oct 10, 2006, 3:42:31 PM (18 years ago)
Author:
bensch
Message:

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:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/parser/ini_parser/ini_parser.h

    r9869 r9880  
    1111#define PARSELINELENGHT     512       //!< how many chars to read at once
    1212
    13 #include "lib/util/filesys/file.h"
     13#include <string>
    1414#include <list>
    1515
     
    1818 * This class can be used to load an initializer file and parse it's contents for variablename=value pairs.
    1919 */
    20 class IniParser : public File
     20class IniParser
    2121{
     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
    2240  private:
    23     ////////////////////////////////////
    24     //! a struct for Entries in the Parser's File's Sections
    25     struct IniEntry
    26     {
    27       std::string         comment;  //!< A Comment that is appendet to the Top of this Entry.
    28       std::string         name;     //!< name of a given Entry
    29       std::string         value;    //!< value of a given Entry
    30     };
     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  };
    3144
    32     //! a struct for Sections in the Parser's file
    33     struct IniSection
    34     {
    35       std::string         comment;  //!< A Comment that is appendet to the Top of this Section.
    36       std::string         name;     //!< name of a given section
    37       std::list<IniEntry> entries;  //!< a list of entries for this section
    38     };
    39     ////////////////////////////////////
     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;
    4054
    4155  public:
    42     IniParser (const std::string& filename = "");
    43     virtual ~IniParser ();
    44 
    45     /** @returns true if the file is opened, false otherwise*/
    46     bool isOpen() const { return true; } ///HACK //(this->fileName.empty()) ? false : true; };
    47     /** @returns the fileName we have opened. */
    48     const std::string& getFileName() const { return this->fileName; };
    49 
    50     bool readFile(const std::string& fileName);
    51     bool writeFile(const std::string& fileName) const;
    52 
    53     void setFileComment(const std::string& fileComment);
    54     const std::string& getFileComment() const { return this->comment; };
    55 
    56     bool addSection(const std::string& sectionName);
    57     bool getSection(const std::string& sectionName);
    58     void setSectionComment(const std::string& comment, const std::string& sectionName);
    59     const std::string& getSectionComment(const std::string& sectionNane) const;
    60 
    61     // iterate through sections with these Functions
    62     void firstSection();
    63     const std::string& nextSection();
    64 
    65 
    66     bool addVar(const std::string& entryName, const std::string& value, const std::string& sectionName = "" );
    67     const std::string& getVar(const std::string& entryName, const std::string& sectionName, const std::string& defaultValue = "") const;
    68     bool editVar(const std::string& entryName, const std::string& value, const std::string& sectionName = "", bool createMissing = true);
    69     void setEntryComment(const std::string& comment, const std::string& entryName, const std::string& sectionName);
    70     const std::string& getEntryComment(const std::string& entryName, const std::string& sectionName) const;
    71 
    72     // iterate Through Variables with these Functions.
    73     void firstVar();
    74     bool nextVar();
    75 
    76 
    77     // retrieving functions when iterating.
    78     const std::string& getCurrentSection() const;
    79     const std::string& getCurrentName() const;
    80     const std::string& getCurrentValue() const;
    81 
    82 
    83     // maintenance.
    84     void debug() const;
    85 
     56    typedef std::list<Entry>                    list;
     57    typedef list::iterator                      iterator;
     58    typedef list::const_iterator                const_iterator;
    8659
    8760  private:
    88     void deleteSections();
    89     void setFileName(const std::string& fileName);
     61    std::string         _value;    //!< value of a given Entry
     62  };
    9063
    91     void setFileComment();
    92     void setSectionComment();
    93     void setEntryComment();
     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 = "");
    9469
    95     std::list<IniSection>::const_iterator getSectionIT(const std::string& sectionName) const;
    96     std::list<IniSection>::iterator getSectionIT(const std::string& sectionName);
     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;
    9773
    98     std::list<IniEntry>::const_iterator getEntryIT(const std::string& entryName, const std::string& sectionName = "") const;
    99     std::list<IniEntry>::iterator getEntryIT(const std::string& entryName, const std::string& sectionName = "");
     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;
    10094
    10195  private:
    102     std::string                      fileName;        //!< The name of the File that was parsed.
    103     std::string                      comment;         //!< A Comment for the header of this File.
    104     std::list<IniSection>            sections;        //!< a list of all stored Sections of the Parser
    105     std::list<IniSection>::iterator  currentSection;  //!< the current selected Section
    106     std::list<IniEntry>::iterator    currentEntry;    //!< the current selected entry (in currentSection)
     96    Entry::list     _entries;  //!< a list of entries for this section
     97  };
    10798
    108     std::list<std::string>           commentList;     //!< A list of Comments. (this is for temporary saving of Comments, that are inserted in front of Sections/Entries.)
     99  //! A class for a INI-file.
     100class Document : public Node
     101  {
     102  public:
     103    Document(const std::string& fileName, const std::string& comment = "");
    109104
     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);
    110108
    111     static const std::string         emptyString;
     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.
    112181};
    113182
Note: See TracChangeset for help on using the changeset viewer.