libzypp  10.5.0
ServiceFileReader.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 "zypp/base/String.h"
00015 #include "zypp/base/InputStream.h"
00016 #include "zypp/base/UserRequestException.h"
00017 
00018 #include "zypp/parser/IniDict.h"
00019 #include "zypp/parser/ServiceFileReader.h"
00020 #include "zypp/ServiceInfo.h"
00021 
00022 using std::endl;
00023 using zypp::parser::IniDict;
00024 
00026 namespace zypp
00027 { 
00028 
00029   namespace parser
00030   { 
00031 
00032     class ServiceFileReader::Impl
00033     {
00034     public:
00035       static void parseServices( const Pathname & file,
00036           const ServiceFileReader::ProcessService & callback );
00037     };
00038 
00039     void ServiceFileReader::Impl::parseServices( const Pathname & file,
00040                                   const ServiceFileReader::ProcessService & callback/*,
00041                                   const ProgressData::ReceiverFnc &progress*/ )
00042     {
00043       InputStream is(file);
00044       if( is.stream().fail() )
00045       {
00046         ZYPP_THROW(Exception("Failed to open service file"));
00047       }
00048 
00049       parser::IniDict dict(is);
00050       for ( parser::IniDict::section_const_iterator its = dict.sectionsBegin();
00051             its != dict.sectionsEnd();
00052             ++its )
00053       {
00054         MIL << (*its) << endl;
00055 
00056         ServiceInfo service(*its);
00057 
00058         for ( IniDict::entry_const_iterator it = dict.entriesBegin(*its);
00059               it != dict.entriesEnd(*its);
00060               ++it )
00061         {
00062           // MIL << (*it).first << endl;
00063           if ( it->first == "name" )
00064             service.setName( it->second );
00065           else if ( it->first == "url" && ! it->second.empty() )
00066             service.setUrl( Url (it->second) );
00067           else if ( it->first == "enabled" )
00068             service.setEnabled( str::strToTrue( it->second ) );
00069           else if ( it->first == "autorefresh" )
00070             service.setAutorefresh( str::strToTrue( it->second ) );
00071           else if ( it->first == "type" )
00072             service.setType( repo::ServiceType(it->second) );
00073           else if ( it->first == "repostoenable" )
00074           {
00075             std::vector<std::string> aliases;
00076             str::splitEscaped( it->second, std::back_inserter(aliases) );
00077             for_( ait, aliases.begin(), aliases.end() )
00078             {
00079               service.addRepoToEnable( *ait );
00080             }
00081           }
00082           else if ( it->first == "repostodisable" )
00083           {
00084             std::vector<std::string> aliases;
00085             str::splitEscaped( it->second, std::back_inserter(aliases) );
00086             for_( ait, aliases.begin(), aliases.end() )
00087             {
00088               service.addRepoToDisable( *ait );
00089             }
00090           }
00091           else
00092             ERR << "Unknown attribute " << it->first << " ignored" << endl;
00093         }
00094 
00095         MIL << "Linking ServiceInfo with file " << file << endl;
00096         service.setFilepath(file);
00097 
00098         // add it to the list.
00099         if ( !callback(service) )
00100           ZYPP_THROW(AbortRequestException());
00101       }
00102     }
00103 
00105     //
00106     //  CLASS NAME : RepoFileReader
00107     //
00109 
00110     ServiceFileReader::ServiceFileReader( const Pathname & repo_file,
00111                                     const ProcessService & callback/*,
00112                                     const ProgressData::ReceiverFnc &progress */)
00113     {
00114       Impl::parseServices(repo_file, callback/*, progress*/);
00115       //MIL << "Done" << endl;
00116     }
00117 
00118     ServiceFileReader::~ServiceFileReader()
00119     {}
00120 
00121     std::ostream & operator<<( std::ostream & str, const ServiceFileReader & obj )
00122     {
00123       return str;
00124     }
00125 
00127   } // namespace parser
00130 } // namespace zypp