4fe2f00815a3c0cdfc8edd0efa53a717f83b9300
[platform/upstream/libzypp.git] / zypp / parser / ServiceFileReader.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/parser/RepoFileReader.cc
10  *
11 */
12 #include <iostream>
13 #include "zypp/base/Logger.h"
14 #include "zypp/base/String.h"
15 #include "zypp/base/InputStream.h"
16 #include "zypp/base/UserRequestException.h"
17
18 #include "zypp/parser/IniDict.h"
19 #include "zypp/parser/ServiceFileReader.h"
20 #include "zypp/ServiceInfo.h"
21
22 using std::endl;
23 using zypp::parser::IniDict;
24
25 ///////////////////////////////////////////////////////////////////
26 namespace zypp
27 { /////////////////////////////////////////////////////////////////
28   ///////////////////////////////////////////////////////////////////
29   namespace parser
30   { /////////////////////////////////////////////////////////////////
31
32     class ServiceFileReader::Impl
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);
50       for ( parser::IniDict::section_const_iterator its = dict.sectionsBegin();
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" )
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 == "catalogstoenable" )
72           {
73             std::vector<std::string> aliases;
74             str::splitEscaped( it->second, std::back_inserter(aliases) );
75             for_( ait, aliases.begin(), aliases.end() )
76             {
77               service.addCatalogToEnable( *ait );
78             }
79           }
80           else
81             ERR << "Unknown attribute " << it->second << " ignored" << endl;
82         }
83
84         MIL << "Linking ServiceInfo with file " << file << endl;
85         service.setFilepath(file);
86
87         // add it to the list.
88         if ( !callback(service) )
89           ZYPP_THROW(AbortRequestException());
90       }
91     }
92
93     ///////////////////////////////////////////////////////////////////
94     //
95     //  CLASS NAME : RepoFileReader
96     //
97     ///////////////////////////////////////////////////////////////////
98
99     ServiceFileReader::ServiceFileReader( const Pathname & repo_file,
100                                     const ProcessService & callback/*,
101                                     const ProgressData::ReceiverFnc &progress */)
102     {
103       Impl::parseServices(repo_file, callback/*, progress*/);
104       //MIL << "Done" << endl;
105     }
106
107     ServiceFileReader::~ServiceFileReader()
108     {}
109
110     std::ostream & operator<<( std::ostream & str, const ServiceFileReader & obj )
111     {
112       return str;
113     }
114
115     /////////////////////////////////////////////////////////////////
116   } // namespace parser
117   ///////////////////////////////////////////////////////////////////
118   /////////////////////////////////////////////////////////////////
119 } // namespace zypp
120 ///////////////////////////////////////////////////////////////////