~thopiekar/zypper/libzypp-manual-import

« back to all changes in this revision

Viewing changes to devel/devel.ma/ExPure.cc

  • Committer: Thomas-Karl Pietrowski
  • Date: 2014-01-29 22:44:28 UTC
  • Revision ID: thopiekar@googlemail.com-20140129224428-gpcqnsdakby362n8
firstĀ import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <libxml/xmlreader.h>
 
2
 
 
3
#include <iostream>
 
4
 
 
5
#include <zypp/base/LogControl.h>
 
6
#include <zypp/base/LogTools.h>
 
7
#include <zypp/base/Function.h>
 
8
#include <zypp/base/GzStream.h>
 
9
#include <zypp/parser/yum/YUMParser.h>
 
10
#include <zypp/Pathname.h>
 
11
 
 
12
using namespace std;
 
13
using namespace zypp;
 
14
using namespace zypp::parser::yum;
 
15
 
 
16
#include "zypp/parser/yum/YUMParser.h"
 
17
 
 
18
///////////////////////////////////////////////////////////////////
 
19
 
 
20
template<class _Cl>
 
21
  void ti( const _Cl & c )
 
22
  {
 
23
    SEC << __PRETTY_FUNCTION__ << endl;
 
24
  }
 
25
///////////////////////////////////////////////////////////////////
 
26
 
 
27
template<class _Parser>
 
28
  bool consume( const typename _Parser::value_type & node_r )
 
29
  {
 
30
    //DBG << node_r << endl;
 
31
    return true;
 
32
  }
 
33
 
 
34
template<class _Parser>
 
35
  void parseXmlFile( const Pathname & file_r,
 
36
                     function<bool(const typename _Parser::value_type &)> consume_r
 
37
                     = consume<_Parser> )
 
38
  {
 
39
    Measure x( "    zparse "+file_r.asString() );
 
40
    ifgzstream istr( file_r.asString().c_str() );
 
41
    if ( ! istr )
 
42
      {
 
43
        ZYPP_THROW( Exception( "Bad stream" ) );
 
44
      }
 
45
 
 
46
    for( _Parser parser( istr, "" ); ! parser.atEnd(); ++parser )
 
47
      {
 
48
        if ( consume_r && ! consume_r( *parser ) )
 
49
          {
 
50
            DBG << "abort parseXmlFile " << file_r << endl;
 
51
            return;
 
52
          }
 
53
      }
 
54
  }
 
55
 
 
56
bool consumeRepomd( const YUMRepomdParser::value_type & node_r )
 
57
{
 
58
  DBG << node_r << endl;
 
59
  return true;
 
60
}
 
61
 
 
62
void zparse( const Pathname & repodata_r )
 
63
{
 
64
  Measure x( "ZPARSE" );
 
65
  parseXmlFile<YUMRepomdParser>  ( repodata_r / "repomd.xml", consumeRepomd );
 
66
  parseXmlFile<YUMPrimaryParser> ( repodata_r / "primary.xml" );
 
67
  parseXmlFile<YUMOtherParser>   ( repodata_r / "other.xml" );
 
68
  parseXmlFile<YUMFileListParser>( repodata_r / "filelists.xml" );
 
69
  //parseXmlFile<YUMPatchesParser> ( repodata_r / "patches.xml" );
 
70
}
 
71
 
 
72
///////////////////////////////////////////////////////////////////
 
73
 
 
74
/**
 
75
 * processNode:
 
76
 * @reader: the xmlReader
 
77
 *
 
78
 * Dump information about the current node
 
79
 */
 
80
template<class _ParserValueType>
 
81
static void
 
82
processNode(xmlTextReaderPtr reader, const _ParserValueType & stp ) {
 
83
    const xmlChar *name, *value;
 
84
 
 
85
    name = xmlTextReaderConstName(reader);
 
86
    if (name == NULL)
 
87
        name = BAD_CAST "--";
 
88
 
 
89
    value = xmlTextReaderConstValue(reader);
 
90
    string t;
 
91
    if ( value )
 
92
      {
 
93
        t = (const char *)value;
 
94
      }
 
95
    return;
 
96
    printf("%d %d %s %d %d",
 
97
            xmlTextReaderDepth(reader),
 
98
            xmlTextReaderNodeType(reader),
 
99
            name,
 
100
            xmlTextReaderIsEmptyElement(reader),
 
101
            xmlTextReaderHasValue(reader));
 
102
    if (value == NULL)
 
103
        printf("\n");
 
104
    else {
 
105
        if (xmlStrlen(value) > 40)
 
106
            printf(" %.40s...\n", value);
 
107
        else
 
108
            printf(" %s\n", value);
 
109
    }
 
110
}
 
111
 
 
112
 
 
113
/**
 
114
 * streamFile:
 
115
 * @filename: the file name to parse
 
116
 *
 
117
 * Parse and print information about an XML file.
 
118
 */
 
119
template<class _Parser>
 
120
static void
 
121
streamFile(const char *filename) {
 
122
    Measure x( string("    lparse ")+filename );
 
123
    xmlTextReaderPtr reader;
 
124
    int ret;
 
125
 
 
126
    typename _Parser::value_type stp;
 
127
 
 
128
    reader = xmlReaderForFile(filename, NULL, 0);
 
129
    if (reader != NULL) {
 
130
        ret = xmlTextReaderRead(reader);
 
131
        while (ret == 1) {
 
132
            stp = new typename _Parser::value_type::element_type;
 
133
            processNode(reader, stp);
 
134
            ret = xmlTextReaderRead(reader);
 
135
        }
 
136
        xmlFreeTextReader(reader);
 
137
        if (ret != 0) {
 
138
          ZYPP_THROW( Exception( string("Failed to parse ") + filename ) );
 
139
        }
 
140
    } else {
 
141
      ZYPP_THROW( Exception( string("Unable to open ") + filename ) );
 
142
    }
 
143
}
 
144
 
 
145
void lparse( const Pathname & repodata_r )
 
146
{
 
147
  Measure x( "LPARSE" );
 
148
    /*
 
149
     * this initialize the library and check potential ABI mismatches
 
150
     * between the version it was compiled for and the actual shared
 
151
     * library used.
 
152
     */
 
153
    LIBXML_TEST_VERSION
 
154
 
 
155
    streamFile<YUMRepomdParser>  ( (repodata_r / "repomd.xml").asString().c_str() );
 
156
    streamFile<YUMPrimaryParser> ( (repodata_r / "primary.xml").asString().c_str() );
 
157
    streamFile<YUMOtherParser>   ( (repodata_r / "other.xml").asString().c_str() );
 
158
    streamFile<YUMFileListParser>( (repodata_r / "filelists.xml").asString().c_str() );
 
159
    //streamFile<YUMPatchesParser> ( (repodata_r / "patches.xml").asString().c_str() );
 
160
 
 
161
    /*
 
162
     * Cleanup function for the XML library.
 
163
     */
 
164
    xmlCleanupParser();
 
165
 
 
166
    /*
 
167
     * this is to debug memory for regression tests
 
168
     */
 
169
    xmlMemoryDump();
 
170
}
 
171
 
 
172
/******************************************************************
 
173
**
 
174
**      FUNCTION NAME : main
 
175
**      FUNCTION TYPE : int
 
176
*/
 
177
int main( int argc, char * argv[] )
 
178
{
 
179
  INT << "===[START]==========================================" << endl;
 
180
 
 
181
  Pathname repodata( "/Local/PATCHES/repodata" );
 
182
  repodata = "/Local/FACTORY/repodata";
 
183
  lparse( repodata );
 
184
  zparse( repodata );
 
185
 
 
186
  INT << "===[END]============================================" << endl << endl;
 
187
  return 0;
 
188
}
 
189