~ubuntu-branches/ubuntu/wily/libxml-bare-perl/wily

« back to all changes in this revision

Viewing changes to Bare.xs

  • Committer: Bazaar Package Importer
  • Author(s): Antonio Radici, Antonio Radici, gregor herrmann
  • Date: 2009-03-22 10:49:56 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090322104956-a5hx63ijvvmy4ao6
Tags: 0.43-1
[ Antonio Radici ]
* New upstream release
* debian/copyright:
  + added copyright for src/bench/xmlio_testread.cpp
  + removed repack.sh references because there are no licensing issues
  + set the proper license for the upstream source (it is dual licensed)
* debian/control:
  + upgrade to Standards-Version 3.8.1, no changes required
* removed debian/repack.sh
* debian/watch:
  + removed versionmangle and the reference to repack

[ gregor herrmann ]
* debian/control: add missing full stop to long description, thanks to
  Rhonda for spotting.

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
#include "XSUB.h"
9
9
#include "parser.h"
10
10
 
11
 
 
12
11
struct nodec *root;
13
12
 
14
13
U32 vhash;
15
14
U32 chash;
16
15
U32 phash;
17
16
U32 ihash;
 
17
U32 cdhash;
 
18
U32 zhash;
 
19
U32 ahash;
18
20
 
19
21
struct nodec *curnode;
20
22
char *rootpos;
21
23
  
22
 
SV *cxml2obj(pTHX_ int a) {
 
24
SV *cxml2obj() {
23
25
  HV *output = newHV();
24
 
  SV *outputref = newRV( (SV *) output );
 
26
  SV *outputref = newRV_noinc( (SV *) output );
25
27
  int i;
26
28
  struct attc *curatt;
27
29
  int numatts = curnode->numatt;
31
33
  int length = curnode->numchildren;
32
34
  SV *svi = newSViv( curnode->pos );
33
35
  
34
 
  #ifdef DEBUG
35
 
  printf("[");
36
 
  #endif
37
36
  hv_store( output, "_pos", 4, svi, phash );
38
37
  hv_store( output, "_i", 2, newSViv( curnode->name - rootpos ), ihash );
 
38
  hv_store( output, "_z", 2, newSViv( curnode->z ), zhash );
39
39
  if( !length ) {
40
40
    if( curnode->vallen ) {
41
41
      SV * sv = newSVpvn( curnode->value, curnode->vallen );
42
 
      //BLIND_PV( curnode->value, curnode->vallen );
43
42
      hv_store( output, "value", 5, sv, vhash );
 
43
      if( curnode->type ) {
 
44
        SV *svi = newSViv( 1 );
 
45
        hv_store( output, "_cdata", 6, svi, cdhash );
 
46
      }
44
47
    }
45
48
    if( curnode->comlen ) {
46
49
      SV * sv = newSVpvn( curnode->comment, curnode->comlen );
50
53
  else {
51
54
    if( curnode->vallen ) {
52
55
      SV *sv = newSVpvn( curnode->value, curnode->vallen );
53
 
      //BLIND_PV( curnode->value, curnode->vallen );
54
56
      hv_store( output, "value", 5, sv, vhash );
 
57
      if( curnode->type ) {
 
58
        SV *svi = newSViv( 1 );
 
59
        hv_store( output, "_cdata", 6, svi, cdhash );
 
60
      }
55
61
    }
56
62
    if( curnode->comlen ) {
57
63
      SV *sv = newSVpvn( curnode->comment, curnode->comlen );
60
66
    
61
67
    curnode = curnode->firstchild;
62
68
    for( i = 0; i < length; i++ ) {
63
 
      SV *namesv = newSVpvn( curnode->name, curnode->namelen );
64
 
      
65
69
      SV **cur = hv_fetch( output, curnode->name, curnode->namelen, 0 );
66
70
      
67
71
      if( curnode->namelen > 6 ) {
70
74
          int subnamelen = curnode->namelen-6;
71
75
          SV **old = hv_fetch( output, subname, subnamelen, 0 );
72
76
          AV *newarray = newAV();
73
 
          SV *newarrayref = newRV( (SV *) newarray );
 
77
          SV *newarrayref = newRV_noinc( (SV *) newarray );
74
78
          if( !old ) {
75
79
            hv_store( output, subname, subnamelen, newarrayref, 0 );
76
80
          }
86
90
      }
87
91
      
88
92
      if( !cur ) {
89
 
        SV *ob = cxml2obj( aTHX_ 0 );
 
93
        SV *ob = cxml2obj();
90
94
        hv_store( output, curnode->name, curnode->namelen, ob, 0 );
91
95
      }
92
96
      else {
93
97
        if( SvTYPE( SvRV(*cur) ) == SVt_PVHV ) {
94
98
          AV *newarray = newAV();
95
 
          SV *newarrayref = newRV( (SV *) newarray );
 
99
          SV *newarrayref = newRV_noinc( (SV *) newarray );
 
100
          
96
101
          SV *newref = newRV( (SV *) SvRV( *cur ) );
 
102
          
97
103
          hv_delete( output, curnode->name, curnode->namelen, 0 );
98
104
          hv_store( output, curnode->name, curnode->namelen, newarrayref, 0 );
99
105
          av_push( newarray, newref );
100
 
          av_push( newarray, cxml2obj( aTHX_ 0 ) );
 
106
          SV *ob = cxml2obj();
 
107
          av_push( newarray, ob );
101
108
        }
102
109
        else {
103
110
          AV *av = (AV *) SvRV( *cur );
104
 
          av_push( av, cxml2obj( aTHX_ 0) );
 
111
          SV *ob = cxml2obj();
 
112
          av_push( av, ob );
105
113
        }
106
114
      }
107
115
      if( i != ( length - 1 ) ) curnode = curnode->next;
114
122
    curatt = curnode->firstatt;
115
123
    for( i = 0; i < numatts; i++ ) {
116
124
      HV *atth = newHV();
117
 
      SV *atthref = newRV( (SV *) atth );
 
125
      SV *atthref = newRV_noinc( (SV *) atth );
118
126
      hv_store( output, curatt->name, curatt->namelen, atthref, 0 );
119
127
      
120
128
      attval = newSVpvn( curatt->value, curatt->vallen );
121
129
      hv_store( atth, "value", 5, attval, vhash );
122
130
      attatt = newSViv( 1 );
123
 
      hv_store( atth, "att", 3, attatt, 0 );
 
131
      hv_store( atth, "_att", 4, attatt, ahash );
124
132
      if( i != ( numatts - 1 ) ) curatt = curatt->next;
125
133
    }
126
134
  }
127
135
  return outputref;
128
136
}
129
137
 
130
 
SV *cxml2obj_simple(pTHX_ int a) {
131
 
  SV *outputref;
 
138
SV *cxml2obj_simple() {
132
139
  int i;
133
140
  struct attc *curatt;
134
141
  int numatts = curnode->numatt;
141
148
      SV * sv = newSVpvn( curnode->value, curnode->vallen );
142
149
      return sv;
143
150
    }
144
 
    return 0;
 
151
    return newSViv( 1 ); //&PL_sv_undef;
145
152
  }
146
 
  {
147
 
    HV *output = newHV();
148
 
    SV *outputref = newRV( (SV *) output );
149
 
    
150
 
    if( length ) {
151
 
      curnode = curnode->firstchild;
152
 
      for( i = 0; i < length; i++ ) {
153
 
        SV *namesv = newSVpvn( curnode->name, curnode->namelen );
154
 
        
155
 
        SV **cur = hv_fetch( output, curnode->name, curnode->namelen, 0 );
156
 
        
157
 
        if( curnode->namelen > 6 ) {
158
 
          if( !strncmp( curnode->name, "multi_", 6 ) ) {
159
 
            char *subname = &curnode->name[6];
160
 
            int subnamelen = curnode->namelen-6;
161
 
            SV **old = hv_fetch( output, subname, subnamelen, 0 );
162
 
            AV *newarray = newAV();
163
 
            SV *newarrayref = newRV( (SV *) newarray );
164
 
            if( !old ) {
 
153
  
 
154
  HV *output = newHV();
 
155
  SV *outputref = newRV( (SV *) output );
 
156
  
 
157
  if( length ) {
 
158
    curnode = curnode->firstchild;
 
159
    for( i = 0; i < length; i++ ) {
 
160
      SV *namesv = newSVpvn( curnode->name, curnode->namelen );
 
161
      
 
162
      SV **cur = hv_fetch( output, curnode->name, curnode->namelen, 0 );
 
163
      
 
164
      if( curnode->namelen > 6 ) {
 
165
        if( !strncmp( curnode->name, "multi_", 6 ) ) {
 
166
          char *subname = &curnode->name[6];
 
167
          int subnamelen = curnode->namelen-6;
 
168
          SV **old = hv_fetch( output, subname, subnamelen, 0 );
 
169
          AV *newarray = newAV();
 
170
          SV *newarrayref = newRV( (SV *) newarray );
 
171
          if( !old ) {
 
172
            hv_store( output, subname, subnamelen, newarrayref, 0 );
 
173
          }
 
174
          else {
 
175
            if( SvTYPE( SvRV(*old) ) == SVt_PVHV ) { // check for hash ref
 
176
              SV *newref = newRV( (SV *) SvRV(*old) );
 
177
              hv_delete( output, subname, subnamelen, 0 );
165
178
              hv_store( output, subname, subnamelen, newarrayref, 0 );
166
 
            }
167
 
            else {
168
 
              if( SvTYPE( SvRV(*old) ) == SVt_PVHV ) { // check for hash ref
169
 
                SV *newref = newRV( (SV *) SvRV(*old) );
170
 
                hv_delete( output, subname, subnamelen, 0 );
171
 
                hv_store( output, subname, subnamelen, newarrayref, 0 );
172
 
                av_push( newarray, newref );
173
 
              }
174
 
            }
175
 
          }
176
 
        }
177
 
          
178
 
        if( !cur ) {
179
 
          SV *ob = cxml2obj_simple( aTHX_ 0 );
180
 
          hv_store( output, curnode->name, curnode->namelen, ob, 0 );
181
 
        }
182
 
        else {
183
 
          if( SvROK( *cur ) ) {
184
 
            if( SvTYPE( SvRV(*cur) ) == SVt_PVHV ) {
185
 
              AV *newarray = newAV();
186
 
              SV *newarrayref = newRV( (SV *) newarray );
187
 
              SV *newref = newRV( (SV *) SvRV( *cur ) );
188
 
              hv_delete( output, curnode->name, curnode->namelen, 0 );
189
 
              hv_store( output, curnode->name, curnode->namelen, newarrayref, 0 );
190
179
              av_push( newarray, newref );
191
 
              av_push( newarray, cxml2obj_simple( aTHX_ 0 ) );
192
 
            }
193
 
            else {
194
 
              AV *av = (AV *) SvRV( *cur );
195
 
              av_push( av, cxml2obj_simple( aTHX_ 0) );
196
180
            }
197
181
          }
198
 
          else {
 
182
        }
 
183
      }
 
184
        
 
185
      if( !cur ) {
 
186
        SV *ob = cxml2obj_simple();
 
187
        hv_store( output, curnode->name, curnode->namelen, ob, 0 );
 
188
      }
 
189
      else {
 
190
        if( SvROK( *cur ) ) {
 
191
          if( SvTYPE( SvRV(*cur) ) == SVt_PVHV ) {
199
192
            AV *newarray = newAV();
200
193
            SV *newarrayref = newRV( (SV *) newarray );
201
 
            
202
 
            STRLEN len;
203
 
            char *ptr = SvPV(*cur, len);
204
 
            SV *newsv = newSVpvn( ptr, len );
205
 
            
206
 
            av_push( newarray, newsv );
 
194
            SV *newref = newRV( (SV *) SvRV( *cur ) );
207
195
            hv_delete( output, curnode->name, curnode->namelen, 0 );
208
196
            hv_store( output, curnode->name, curnode->namelen, newarrayref, 0 );
209
 
            av_push( newarray, cxml2obj_simple( aTHX_ 0 ) );
210
 
          }
211
 
        }
212
 
        if( i != ( length - 1 ) ) curnode = curnode->next;
213
 
      }
214
 
      curnode = curnode->parent;
215
 
    }
216
 
    
217
 
    if( numatts ) {
218
 
      curatt = curnode->firstatt;
219
 
      for( i = 0; i < numatts; i++ ) {
220
 
        attval = newSVpvn( curatt->value, curatt->vallen );
221
 
        hv_store( output, curatt->name, curatt->namelen, attval, 0 );
222
 
        if( i != ( numatts - 1 ) ) curatt = curatt->next;
223
 
      }
224
 
    }
225
 
    
226
 
    return outputref;
227
 
  }
 
197
            av_push( newarray, newref );
 
198
            av_push( newarray, cxml2obj_simple() );
 
199
          }
 
200
          else {
 
201
            AV *av = (AV *) SvRV( *cur );
 
202
            av_push( av, cxml2obj_simple() );
 
203
          }
 
204
        }
 
205
        else {
 
206
          AV *newarray = newAV();
 
207
          SV *newarrayref = newRV( (SV *) newarray );
 
208
          
 
209
          STRLEN len;
 
210
          char *ptr = SvPV(*cur, len);
 
211
          SV *newsv = newSVpvn( ptr, len );
 
212
          
 
213
          av_push( newarray, newsv );
 
214
          hv_delete( output, curnode->name, curnode->namelen, 0 );
 
215
          hv_store( output, curnode->name, curnode->namelen, newarrayref, 0 );
 
216
          av_push( newarray, cxml2obj_simple() );
 
217
        }
 
218
      }
 
219
      if( i != ( length - 1 ) ) curnode = curnode->next;
 
220
    }
 
221
    curnode = curnode->parent;
 
222
  }
 
223
  
 
224
  if( numatts ) {
 
225
    curatt = curnode->firstatt;
 
226
    for( i = 0; i < numatts; i++ ) {
 
227
      attval = newSVpvn( curatt->value, curatt->vallen );
 
228
      hv_store( output, curatt->name, curatt->namelen, attval, 0 );
 
229
      if( i != ( numatts - 1 ) ) curatt = curatt->next;
 
230
    }
 
231
  }
 
232
  
 
233
  return outputref;
228
234
}
229
235
 
230
236
struct parserc *parser = 0;
231
237
 
232
238
MODULE = XML::Bare         PACKAGE = XML::Bare
233
239
 
234
 
 
235
 
 
236
240
SV *
237
241
xml2obj()
238
242
  CODE:
239
243
    curnode = parser->pcurnode;
240
 
    //curnode = root;
241
244
    if( curnode->err ) RETVAL = newSViv( curnode->err );
242
 
    else RETVAL = cxml2obj(aTHX_ 0);
 
245
    else RETVAL = cxml2obj();
243
246
  OUTPUT:
244
247
    RETVAL
245
248
    
247
250
xml2obj_simple()
248
251
  CODE:
249
252
    curnode = parser->pcurnode;
250
 
    //curnode = root;
251
 
    //if( curnode->err ) RETVAL = 0;
252
 
    //else
253
 
    RETVAL = cxml2obj_simple(aTHX_ 0);
 
253
    RETVAL = cxml2obj_simple();
254
254
  OUTPUT:
255
255
    RETVAL
256
256
 
258
258
c_parse(text)
259
259
  char * text
260
260
  CODE:
261
 
    
262
261
    rootpos = text;
263
 
    #ifdef DEBUG
264
 
    //printf("About to parse\n");
265
 
    #endif
266
262
    PERL_HASH(vhash, "value", 5);
 
263
    PERL_HASH(ahash, "_att", 4);
267
264
    PERL_HASH(chash, "comment", 7);
268
265
    PERL_HASH(phash, "_pos", 4);
269
266
    PERL_HASH(ihash, "_i", 2 );
 
267
    PERL_HASH(zhash, "_z", 2 );
 
268
    PERL_HASH(cdhash, "_cdata", 6 );
270
269
    parser = (struct parserc *) malloc( sizeof( struct parserc ) );
271
270
    root = parserc_parse( parser, text );
272
 
    #ifdef DEBUG
273
 
    //printf("Returned from parser\n");
274
 
    #endif
275
 
    
276
 
    //root = parser->pcurnode;
277
 
    //this is wrong because parsing may halt in the middle; root= above is correct
278
 
    
279
 
    #ifdef DEBUG
280
 
    //printf("C Parsing done\b");
281
 
    #endif
282
271
    
283
272
void
284
273
c_parsefile(filename)
285
274
  char * filename
286
275
  CODE:
287
276
    PERL_HASH(vhash, "value", 5);
 
277
    PERL_HASH(ahash, "_att", 4);
288
278
    PERL_HASH(chash, "comment", 7);
289
279
    PERL_HASH(phash, "_pos", 4);
290
280
    PERL_HASH(ihash, "_i", 2 );
 
281
    PERL_HASH(zhash, "_z", 2 );
 
282
    PERL_HASH(cdhash, "_cdata", 6 );
291
283
    char *data;
292
284
    unsigned long len;
293
285
    FILE *handle;
304
296
    fclose( handle );
305
297
    parser = (struct parserc *) malloc( sizeof( struct parserc ) );
306
298
    root = parserc_parse( parser, data );
307
 
    //root = parser->pcurnode;
308
299
 
309
300
SV *
310
301
get_root()