r5206@piscola: dmacvicar | 2007-05-07 16:54:08 +0200
authorDuncan Mac-Vicar P <dmacvicar@suse.de>
Mon, 7 May 2007 14:54:51 +0000 (14:54 +0000)
committerDuncan Mac-Vicar P <dmacvicar@suse.de>
Mon, 7 May 2007 14:54:51 +0000 (14:54 +0000)
 Ini* stuff is what I was looking for

 I will add comments support in a near future, but for now this is what I need.

 Only todo: add typed lookups:

 getBoolEntry, getIntEntry, etc

tests/parser/inifile/inidict_test.cc
zypp/parser/IniDict.cc
zypp/parser/IniDict.h
zypp/parser/IniParser.cc

index eb623d0..a16ef6c 100644 (file)
@@ -28,9 +28,14 @@ void ini_read_test(const string &dir)
 
   //MIL << dict["homedmacvicar"]["type"] << endl;
 
-  for ( IniDict::const_iterator it = dict.begin(); it != dict.end(); ++it )
+  for ( IniDict::section_const_iterator it = dict.sectionsBegin(); it != dict.sectionsEnd(); ++it )
   {
-    MIL << (*it).first << endl;
+    MIL << (*it) << endl;
+    
+    for ( IniDict::entry_const_iterator it2 = dict.entriesBegin(*it); it2 != dict.entriesEnd(*it); ++it2 )
+    {
+      MIL << "  - " << (*it2).first << " | " << (*it2).second << endl;
+    }
   }
 }
 
index 250df46..a65931c 100644 (file)
@@ -54,13 +54,44 @@ namespace zypp
     void IniDict::consume( const std::string &section, const std::string &key, const std::string &value )
     {
       //MIL << endl;
-      PropertySet keys = _dict[section];
-      keys[key] = value;
-      _dict[section] = keys;
+      _dict[section][key] = value;
       //MIL << this->size() << endl;
     }
 
 
+    IniDict::entry_const_iterator IniDict::entriesBegin(const std::string &section) const
+    {
+      SectionSet::const_iterator secit = _dict.find(section);
+      if ( secit == _dict.end() )
+      {
+        return _empty_map.begin();
+      }
+      
+      return (secit->second).begin();
+    }
+    
+    IniDict::entry_const_iterator IniDict::entriesEnd(const std::string &section) const
+    {
+      SectionSet::const_iterator secit = _dict.find(section);
+      if ( secit == _dict.end() )
+      {
+        return _empty_map.end();
+      }
+      
+      return (secit->second).end();
+    }
+    
+    
+    IniDict::section_const_iterator IniDict::sectionsBegin() const
+    {
+      return make_map_key_begin( _dict );
+    }
+    
+    IniDict::section_const_iterator IniDict::sectionsEnd() const
+    {
+      return make_map_key_end( _dict );
+    }
+      
     /******************************************************************
     **
     ** FUNCTION NAME : operator<<
index 3455694..bdc296c 100644 (file)
@@ -18,6 +18,7 @@
 
 #include "zypp/base/PtrTypes.h"
 #include "zypp/base/InputStream.h"
+#include "zypp/base/Iterator.h"
 #include "zypp/parser/IniParser.h"
 
 ///////////////////////////////////////////////////////////////////
@@ -40,18 +41,55 @@ namespace zypp
     {
       friend std::ostream & operator<<( std::ostream & str, const IniDict & obj );
     public:
-      typedef std::map<std::string, std::string> PropertySet;
-      typedef std::map<std::string, PropertySet> ConfigSet;
-      typedef ConfigSet::const_iterator const_iterator;
+      typedef std::map<std::string, std::string> EntrySet;
+      typedef std::map<std::string, EntrySet> SectionSet;
+      typedef MapKVIteratorTraits<SectionSet>::Key_const_iterator section_const_iterator;
+      typedef EntrySet::const_iterator entry_const_iterator;
       
-      const_iterator begin() const { return _dict.begin(); }
-      const_iterator end() const { return _dict.end(); }
-      /** Default ctor */
+      /**
+       * \name Section Iterators
+       * Iterate trough ini file sections
+       * \code
+       * for ( IniDict::section_const_iterator it = dict.sectionsBegin(); 
+       *       it != dict.sectionsEnd();
+       *       ++it )
+       * {
+       *   MIL << (*it) << endl;
+       * }
+       * \endcode
+       */
+       //@{
+      section_const_iterator sectionsBegin() const;
+      section_const_iterator sectionsEnd() const;
+      //@}
+      
+      /**
+       * \name Entries Iterators
+       * Iterate trough ini file entries in a section
+       * \code
+       * for ( IniDict::entry_const_iterator it = dict.entriesBegin("updates"); 
+       *       it != dict.entriesEnd("updates");
+       *       ++it )
+       * {
+       *   MIL << (*it).first << endl;
+       * }
+       * \endcode
+       */
+       
+      //@{
+      entry_const_iterator entriesBegin(const std::string &section) const;
+      entry_const_iterator entriesEnd(const std::string &section) const;
+      //@{
+      
+      /**
+       * Creates a dictionary from a InputStream
+       * containing a ini structured file
+       */
       IniDict( const InputStream &is );
+      
       /** Dtor */
       ~IniDict();
 
-      
     public:
 
       /** Called when a section is found. */
@@ -60,7 +98,14 @@ namespace zypp
       virtual void consume( const std::string &section, const std::string &key, const std::string &value );
 
     private:
-      ConfigSet _dict;
+      SectionSet _dict;
+      
+      /**
+       * empty map used to simulate
+       * iteration in non existant
+       * sections
+       */
+      EntrySet _empty_map;
     };
     ///////////////////////////////////////////////////////////////////
 
index 3aa6b89..40238f9 100644 (file)
@@ -59,14 +59,6 @@ void IniParser::consume( const std::string &section )
 void IniParser::endParse()
 {}
 
-void dumpRegexpResults( const boost::smatch &what )
-{
-  for ( unsigned int k=0; k < what.size(); k++)
-  {
-    DBG << "[match "<< k << "] [" << what[k] << "]" << std::endl;
-  }
-}
-
 ///////////////////////////////////////////////////////////////////
 //
 //     METHOD NAME : IniParser::parse
@@ -98,7 +90,7 @@ void IniParser::parse( const InputStream & input_r )
         boost::smatch what;
         if(boost::regex_match(trimmed, what, rxSection, boost::match_extra))
         {
-          //dumpRegexpResults(what);
+          //DBG << what << endl;
           std::string section = what[1];
           consume(section);
           section.swap(_current_section);
@@ -109,7 +101,7 @@ void IniParser::parse( const InputStream & input_r )
         boost::smatch what;
         if(boost::regex_match(trimmed, what, rxKeyValue, boost::match_extra))
         {
-          //dumpRegexpResults(what);
+          //DBG << what << endl;
           consume( _current_section, what[1], what[2] );
         }
       }