libzypp  11.13.5
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 
29  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