libzypp  13.10.6
ServiceFileReader.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #include <iostream>
13 #include "zypp/base/Logger.h"
14 #include "zypp/base/String.h"
15 #include "zypp/base/InputStream.h"
17 
18 #include "zypp/parser/IniDict.h"
20 #include "zypp/ServiceInfo.h"
21 
22 using std::endl;
24 
26 namespace zypp
27 {
28  namespace parser
30  {
31 
33  {
34  public:
35  static void parseServices( const Pathname & file,
36  const ServiceFileReader::ProcessService & callback );
37  };
38 
39  void ServiceFileReader::Impl::parseServices( const Pathname & file,
40  const ServiceFileReader::ProcessService & callback/*,
41  const ProgressData::ReceiverFnc &progress*/ )
42  {
43  InputStream is(file);
44  if( is.stream().fail() )
45  {
46  ZYPP_THROW(Exception("Failed to open service file"));
47  }
48 
49  parser::IniDict dict(is);
51  its != dict.sectionsEnd();
52  ++its )
53  {
54  MIL << (*its) << endl;
55 
56  ServiceInfo service(*its);
57 
58  for ( IniDict::entry_const_iterator it = dict.entriesBegin(*its);
59  it != dict.entriesEnd(*its);
60  ++it )
61  {
62  // MIL << (*it).first << endl;
63  if ( it->first == "name" )
64  service.setName( it->second );
65  else if ( it->first == "url" && ! it->second.empty() )
66  service.setUrl( Url (it->second) );
67  else if ( it->first == "enabled" )
68  service.setEnabled( str::strToTrue( it->second ) );
69  else if ( it->first == "autorefresh" )
70  service.setAutorefresh( str::strToTrue( it->second ) );
71  else if ( it->first == "type" )
72  service.setType( repo::ServiceType(it->second) );
73  else if ( it->first == "repostoenable" )
74  {
75  std::vector<std::string> aliases;
76  str::splitEscaped( it->second, std::back_inserter(aliases) );
77  for_( ait, aliases.begin(), aliases.end() )
78  {
79  service.addRepoToEnable( *ait );
80  }
81  }
82  else if ( it->first == "repostodisable" )
83  {
84  std::vector<std::string> aliases;
85  str::splitEscaped( it->second, std::back_inserter(aliases) );
86  for_( ait, aliases.begin(), aliases.end() )
87  {
88  service.addRepoToDisable( *ait );
89  }
90  }
91  else
92  ERR << "Unknown attribute " << it->first << " ignored" << endl;
93  }
94 
95  MIL << "Linking ServiceInfo with file " << file << endl;
96  service.setFilepath(file);
97 
98  // add it to the list.
99  if ( !callback(service) )
100  ZYPP_THROW(AbortRequestException());
101  }
102  }
103 
105  //
106  // CLASS NAME : RepoFileReader
107  //
109 
110  ServiceFileReader::ServiceFileReader( const Pathname & repo_file,
111  const ProcessService & callback/*,
112  const ProgressData::ReceiverFnc &progress */)
113  {
114  Impl::parseServices(repo_file, callback/*, progress*/);
115  //MIL << "Done" << endl;
116  }
117 
119  {}
120 
121  std::ostream & operator<<( std::ostream & str, const ServiceFileReader & obj )
122  {
123  return str;
124  }
125 
127  } // namespace parser
130 } // namespace zypp
#define MIL
Definition: Logger.h:47
MapKVIteratorTraits< SectionSet >::Key_const_iterator section_const_iterator
Definition: IniDict.h:46
Read service data from a .service file.
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:320
void setAutorefresh(bool autorefresh)
enable or disable autorefresh
Definition: RepoInfoBase.cc:94
ServiceFileReader(const Pathname &serviceFile, const ProcessService &callback)
Constructor.
friend std::ostream & operator<<(std::ostream &str, const ServiceFileReader &obj)
void setEnabled(bool enabled)
enable or disable the repository
Definition: RepoInfoBase.cc:89
void addRepoToEnable(const std::string &alias_r)
Add alias_r to the set of ReposToEnable.
Definition: ServiceInfo.cc:127
void setFilepath(const Pathname &filename)
set the path to the .repo file
Helper to create and pass std::istream.
Definition: InputStream.h:56
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition: Easy.h:27
void setType(const repo::ServiceType &type)
Set service type.
Definition: ServiceInfo.cc:106
static void parseServices(const Pathname &file, const ServiceFileReader::ProcessService &callback)
#define ERR
Definition: Logger.h:49
unsigned splitEscaped(const C_Str &line_r, _OutputIterator result_r, const C_Str &sepchars_r=" \t", bool withEmpty=false)
Split line_r into words with respect to escape delimeters.
Definition: String.h:446
Service type enumeration.
Definition: ServiceType.h:26
section_const_iterator sectionsBegin() const
Definition: IniDict.cc:94
entry_const_iterator entriesEnd(const std::string &section) const
Definition: IniDict.cc:82
function< bool(const ServiceInfo &)> ProcessService
Callback definition.
std::istream & stream() const
The std::istream.
Definition: InputStream.h:93
void setUrl(const Url &url)
Sets url for this service.
Definition: ServiceInfo.cc:102
Parses a INI file and offers its structure as a dictionary.
Definition: IniDict.h:40
bool strToTrue(const C_Str &str)
Parsing boolean from string.
Definition: String.cc:61
entry_const_iterator entriesBegin(const std::string &section) const
Definition: IniDict.cc:71
Base class for Exception.
Definition: Exception.h:143
void addRepoToDisable(const std::string &alias_r)
Add alias_r to the set of ReposToDisable.
Definition: ServiceInfo.cc:155
EntrySet::const_iterator entry_const_iterator
Definition: IniDict.h:47
void setName(const std::string &name)
set the repository name
section_const_iterator sectionsEnd() const
Definition: IniDict.cc:99
Url manipulation class.
Definition: Url.h:87