From: Duncan Mac-Vicar P Date: Mon, 7 May 2007 14:54:51 +0000 (+0000) Subject: r5206@piscola: dmacvicar | 2007-05-07 16:54:08 +0200 X-Git-Tag: BASE-SuSE-Linux-10_3-Branch~790 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=60fe0c5cd093eb3bb18aa288051bcb7323f55026;p=platform%2Fupstream%2Flibzypp.git r5206@piscola: dmacvicar | 2007-05-07 16:54:08 +0200 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 --- diff --git a/tests/parser/inifile/inidict_test.cc b/tests/parser/inifile/inidict_test.cc index eb623d0..a16ef6c 100644 --- a/tests/parser/inifile/inidict_test.cc +++ b/tests/parser/inifile/inidict_test.cc @@ -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; + } } } diff --git a/zypp/parser/IniDict.cc b/zypp/parser/IniDict.cc index 250df46..a65931c 100644 --- a/zypp/parser/IniDict.cc +++ b/zypp/parser/IniDict.cc @@ -54,13 +54,44 @@ namespace zypp void IniDict::consume( const std::string §ion, 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 §ion) 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 §ion) 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<< diff --git a/zypp/parser/IniDict.h b/zypp/parser/IniDict.h index 3455694..bdc296c 100644 --- a/zypp/parser/IniDict.h +++ b/zypp/parser/IniDict.h @@ -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 PropertySet; - typedef std::map ConfigSet; - typedef ConfigSet::const_iterator const_iterator; + typedef std::map EntrySet; + typedef std::map SectionSet; + typedef MapKVIteratorTraits::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 §ion) const; + entry_const_iterator entriesEnd(const std::string §ion) 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 §ion, 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; }; /////////////////////////////////////////////////////////////////// diff --git a/zypp/parser/IniParser.cc b/zypp/parser/IniParser.cc index 3aa6b89..40238f9 100644 --- a/zypp/parser/IniParser.cc +++ b/zypp/parser/IniParser.cc @@ -59,14 +59,6 @@ void IniParser::consume( const std::string §ion ) 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] ); } }