1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/parser/RepoFileReader.cc
13 #include "zypp/base/Logger.h"
14 #include "zypp/base/String.h"
15 #include "zypp/base/Regex.h"
16 #include "zypp/base/InputStream.h"
17 #include "zypp/base/UserRequestException.h"
19 #include "zypp/parser/IniDict.h"
20 #include "zypp/parser/ServiceFileReader.h"
21 #include "zypp/ServiceInfo.h"
24 using zypp::parser::IniDict;
26 ///////////////////////////////////////////////////////////////////
28 { /////////////////////////////////////////////////////////////////
29 ///////////////////////////////////////////////////////////////////
31 { /////////////////////////////////////////////////////////////////
33 class ServiceFileReader::Impl
36 static void parseServices( const Pathname & file,
37 const ServiceFileReader::ProcessService & callback );
40 void ServiceFileReader::Impl::parseServices( const Pathname & file,
41 const ServiceFileReader::ProcessService & callback/*,
42 const ProgressData::ReceiverFnc &progress*/ )
45 if( is.stream().fail() )
47 ZYPP_THROW(Exception("Failed to open service file"));
50 parser::IniDict dict(is);
51 for ( parser::IniDict::section_const_iterator its = dict.sectionsBegin();
52 its != dict.sectionsEnd();
55 MIL << (*its) << endl;
57 ServiceInfo service(*its);
58 std::map<std::string,std::pair<std::string,ServiceInfo::RepoState>> repoStates; // <repo_NUM,< alias,RepoState >>
60 for ( IniDict::entry_const_iterator it = dict.entriesBegin(*its);
61 it != dict.entriesEnd(*its);
64 // MIL << (*it).first << endl;
65 if ( it->first == "name" )
66 service.setName( it->second );
67 else if ( it->first == "url" && ! it->second.empty() )
68 service.setUrl( Url (it->second) );
69 else if ( it->first == "enabled" )
70 service.setEnabled( str::strToTrue( it->second ) );
71 else if ( it->first == "autorefresh" )
72 service.setAutorefresh( str::strToTrue( it->second ) );
73 else if ( it->first == "type" )
74 service.setType( repo::ServiceType(it->second) );
75 else if ( it->first == "repostoenable" )
77 std::vector<std::string> aliases;
78 str::splitEscaped( it->second, std::back_inserter(aliases) );
79 for_( ait, aliases.begin(), aliases.end() )
81 service.addRepoToEnable( *ait );
84 else if ( it->first == "repostodisable" )
86 std::vector<std::string> aliases;
87 str::splitEscaped( it->second, std::back_inserter(aliases) );
88 for_( ait, aliases.begin(), aliases.end() )
90 service.addRepoToDisable( *ait );
93 else if ( str::startsWith( it->first, "repo_" ) )
95 static str::regex rxexpr( "([0-9]+)(_(.*))?" );
97 if ( str::regex_match( it->first.c_str()+5/*repo_*/, what, rxexpr ) )
99 std::string tag( what[1] );
100 if ( what.size() > 3 )
103 if ( what[3] == "enabled" )
104 repoStates[tag].second.enabled = str::strToBool( it->second, repoStates[tag].second.enabled );
105 else if ( what[3] == "autorefresh" )
106 repoStates[tag].second.autorefresh = str::strToBool( it->second, repoStates[tag].second.autorefresh );
107 else if ( what[3] == "priority" )
108 str::strtonum( it->second, repoStates[tag].second.priority );
110 ERR << "Unknown attribute " << it->first << " ignored" << endl;
115 repoStates[tag].first = it->second;
119 ERR << "Unknown attribute " << it->first << " ignored" << endl;
122 ERR << "Unknown attribute " << it->first << " ignored" << endl;
125 if ( ! repoStates.empty() )
127 ServiceInfo::RepoStates data;
128 for ( const auto & el : repoStates )
130 if ( el.second.first.empty() )
131 ERR << "Missing alias for repo_" << el.first << "; ignore entry" << endl;
133 data[el.second.first] = el.second.second;
135 if ( ! data.empty() )
136 service.setRepoStates( std::move(data) );
139 MIL << "Linking ServiceInfo with file " << file << endl;
140 service.setFilepath(file);
142 // add it to the list.
143 if ( !callback(service) )
144 ZYPP_THROW(AbortRequestException());
148 ///////////////////////////////////////////////////////////////////
150 // CLASS NAME : RepoFileReader
152 ///////////////////////////////////////////////////////////////////
154 ServiceFileReader::ServiceFileReader( const Pathname & repo_file,
155 const ProcessService & callback/*,
156 const ProgressData::ReceiverFnc &progress */)
158 Impl::parseServices(repo_file, callback/*, progress*/);
159 //MIL << "Done" << endl;
162 ServiceFileReader::~ServiceFileReader()
165 std::ostream & operator<<( std::ostream & str, const ServiceFileReader & obj )
170 /////////////////////////////////////////////////////////////////
171 } // namespace parser
172 ///////////////////////////////////////////////////////////////////
173 /////////////////////////////////////////////////////////////////
175 ///////////////////////////////////////////////////////////////////