backup
[platform/upstream/libzypp.git] / devel / devel.ma / Parse.cc
1 #include <iostream>
2 #include <list>
3 #include <vector>
4 #include <string>
5 #include <iterator>
6
7 #include <zypp/base/Logger.h>
8 #include <zypp/base/Exception.h>
9 #include <zypp/base/String.h>
10 #include <zypp/base/PtrTypes.h>
11
12 #include <zypp/parser/tagfile/Parser.h>
13 #include <zypp/Package.h>
14 #include <zypp/CapSet.h>
15 #include <zypp/CapFactory.h>
16 #include <zypp/detail/PackageImpl.h>
17
18 #include <zypp/NVRA.h>
19
20 using std::endl;
21
22 ///////////////////////////////////////////////////////////////////
23 namespace zypp
24 { /////////////////////////////////////////////////////////////////
25   ///////////////////////////////////////////////////////////////////
26   namespace parser
27   { /////////////////////////////////////////////////////////////////
28     ///////////////////////////////////////////////////////////////////
29     namespace tagfile
30     { /////////////////////////////////////////////////////////////////
31
32
33       struct PackagesParser : public Parser
34       {
35         std::list<Package::Ptr> result;
36
37         shared_ptr<detail::PackageImpl> pkgImpl;
38         NVRAD nvrad;
39
40         bool pkgPending() const
41         { return pkgImpl; }
42
43         void collectPkg( const shared_ptr<detail::PackageImpl> & nextPkg_r
44                          = shared_ptr<detail::PackageImpl>() )
45         {
46           if ( pkgPending() )
47             {
48               result.push_back( detail::makeResolvableFromImpl( nvrad, pkgImpl ) );
49             }
50           pkgImpl = nextPkg_r;
51         }
52
53         void newPkg()
54         {
55           collectPkg( shared_ptr<detail::PackageImpl>(new detail::PackageImpl) );
56         }
57
58         void collectDeps( const std::list<std::string> & depstr_r, CapSet & capset )
59         {
60           for ( std::list<std::string>::const_iterator it = depstr_r.begin();
61                 it != depstr_r.end(); ++it )
62             {
63               capset.insert( CapFactory().parse( ResTraits<Package>::kind, *it ) );
64             }
65         }
66
67         /* Consume SingleTag data. */
68         virtual void consume( const STag & stag_r )
69         {
70           if ( stag_r.stag.isPlain( "Pkg" ) )
71             {
72               std::vector<std::string> words;
73               str::split( stag_r.value, std::back_inserter(words) );
74
75               if ( str::split( stag_r.value, std::back_inserter(words) ) != 4 )
76                 ZYPP_THROW( ParseException( "Pkg" ) );
77
78               newPkg();
79               nvrad = NVRAD( words[0], Edition(words[1],words[2]), Arch(words[4]) );
80             }
81         }
82
83         /* Consume MulitTag data. */
84         virtual void consume( const MTag & mtag_r )
85         {
86           if ( ! pkgPending() )
87             return;
88
89           if ( mtag_r.stag.isPlain( "Prv" ) )
90             {
91               collectDeps( mtag_r.value, nvrad.provides );
92             }
93           else if ( mtag_r.stag.isPlain( "Prq" ) )
94             {
95               collectDeps( mtag_r.value, nvrad.prerequires );
96             }
97           else if ( mtag_r.stag.isPlain( "Req" ) )
98             {
99               collectDeps( mtag_r.value, nvrad.requires );
100             }
101           else if ( mtag_r.stag.isPlain( "Con" ) )
102             {
103               collectDeps( mtag_r.value, nvrad.conflicts );
104             }
105           else if ( mtag_r.stag.isPlain( "Obs" ) )
106             {
107               collectDeps( mtag_r.value, nvrad.obsoletes );
108             }
109         }
110
111         virtual void parseEnd()
112         { collectPkg(); }
113       };
114
115       /////////////////////////////////////////////////////////////////
116     } // namespace tagfile
117     ///////////////////////////////////////////////////////////////////
118     /////////////////////////////////////////////////////////////////
119   } // namespace parser
120   ///////////////////////////////////////////////////////////////////
121   /////////////////////////////////////////////////////////////////
122 } // namespace zypp
123 ///////////////////////////////////////////////////////////////////
124 ////////////////////////////////////////////////////////////////////////////
125 //
126 //  Types
127 //
128 ////////////////////////////////////////////////////////////////////////////
129
130 using std::endl;
131 using namespace zypp;
132 using namespace zypp::parser::tagfile;
133
134 ////////////////////////////////////////////////////////////////////////////
135 //
136 //  Just for the stats
137 //
138 ////////////////////////////////////////////////////////////////////////////
139 struct Measure
140 {
141   time_t _begin;
142   Measure()
143   : _begin( time(NULL) )
144   {
145     USR << "START MEASURE..." << endl;
146   }
147   ~Measure()
148   {
149     USR << "DURATION: " << (time(NULL)-_begin) << " sec." << endl;
150   }
151 };
152 ////////////////////////////////////////////////////////////////////////////
153
154
155 using namespace std;
156 ////////////////////////////////////////////////////////////////////////////
157 //
158 //  Main
159 //
160 ////////////////////////////////////////////////////////////////////////////
161 int main( int argc, char* argv[] )
162 {
163   INT << "===[START]==========================================" << endl;
164   string infile( "p" );
165   if (argc >= 2 )
166     infile = argv[1];
167
168   PackagesParser p;
169   p.parse( infile );
170
171   SEC << p.result.size() << endl;
172   MIL << *p.result.front() << endl;
173   MIL << p.result.front()->deps() << endl;
174
175   INT << "===[END]============================================" << endl;
176   return 0;
177 }