IniDict.cc

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00012 #include <iostream>
00013 #include "zypp/base/Logger.h"
00014 #include <map>
00015 #include <string>
00016 #include "zypp/parser/IniDict.h"
00017 
00018 using namespace std;
00020 namespace zypp
00021 { 
00022 
00023   namespace parser
00024   { 
00025 
00026     //
00027     //  CLASS NAME : IniDict
00028     //
00030 
00032     //
00033     //  METHOD NAME : IniDict::IniDict
00034     //  METHOD TYPE : Ctor
00035     //
00036     IniDict::IniDict( const InputStream &is,
00037                       const ProgressData::ReceiverFnc & progress )
00038     {
00039       read(is, progress );
00040     }
00041 
00042     IniDict::IniDict()
00043     {
00044     }
00045 
00046     void IniDict::read( const InputStream &is,
00047                         const ProgressData::ReceiverFnc & progress )
00048     {
00049       parse(is, progress );
00050     }
00051 
00053     //
00054     //  METHOD NAME : IniDict::~IniDict
00055     //  METHOD TYPE : Dtor
00056     //
00057     IniDict::~IniDict()
00058     {}
00059 
00060     void IniDict::consume( const std::string &section )
00061     {
00062       _dict[section]; // remember even empty sections
00063     }
00064 
00065     void IniDict::consume( const std::string &section, const std::string &key, const std::string &value )
00066     {
00067       _dict[section][key] = value;
00068     }
00069 
00070 
00071     IniDict::entry_const_iterator IniDict::entriesBegin(const std::string &section) const
00072     {
00073       SectionSet::const_iterator secit = _dict.find(section);
00074       if ( secit == _dict.end() )
00075       {
00076         return _empty_map.begin();
00077       }
00078 
00079       return (secit->second).begin();
00080     }
00081 
00082     IniDict::entry_const_iterator IniDict::entriesEnd(const std::string &section) const
00083     {
00084       SectionSet::const_iterator secit = _dict.find(section);
00085       if ( secit == _dict.end() )
00086       {
00087         return _empty_map.end();
00088       }
00089 
00090       return (secit->second).end();
00091     }
00092 
00093 
00094     IniDict::section_const_iterator IniDict::sectionsBegin() const
00095     {
00096       return make_map_key_begin( _dict );
00097     }
00098 
00099     IniDict::section_const_iterator IniDict::sectionsEnd() const
00100     {
00101       return make_map_key_end( _dict );
00102     }
00103 
00104     void IniDict::insertEntry( const std::string &section,
00105                                const std::string &key,
00106                                const std::string &value )
00107     {
00108       consume( section, key, value );
00109     }
00110 
00111     void IniDict::deleteSection( const std::string &section )
00112     {
00113       _dict.erase(section);
00114     }
00115 
00116     bool IniDict::hasSection( const std::string &section ) const
00117     {
00118       SectionSet::const_iterator secit = _dict.find(section);
00119       if ( secit == _dict.end() )
00120         return false;
00121       return true;
00122     }
00123 
00124     bool IniDict::hasEntry( const std::string &section,
00125                             const std::string &entry ) const
00126     {
00127       SectionSet::const_iterator secit = _dict.find(section);
00128       if ( secit == _dict.end() )
00129         return false;
00130 
00131       EntrySet::const_iterator entryit = (secit->second).find(entry);
00132       if ( entryit == (secit->second).end() )
00133         return false;
00134 
00135       return true;
00136     }
00137 
00138     /******************************************************************
00139     **
00140     **  FUNCTION NAME : operator<<
00141     **  FUNCTION TYPE : std::ostream &
00142     */
00143     std::ostream & operator<<( std::ostream & str, const IniDict & obj )
00144     {
00145       for ( IniDict::section_const_iterator si = obj.sectionsBegin();
00146             si != obj.sectionsEnd();
00147             ++si )
00148       {
00149         str << "[" << *si << "]" << endl;
00150         for ( IniDict::entry_const_iterator ei = obj.entriesBegin(*si);
00151               ei != obj.entriesEnd(*si);
00152               ++ei )
00153         {
00154           str << ei->first << " = " << ei->second << endl;
00155         }
00156         str << endl;
00157       }
00158       return str;
00159     }
00160 
00162   } // namespace parser
00165 } // namespace zypp

doxygen