File: Bare.xs

package info (click to toggle)
libxml-bare-perl 0.47-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,404 kB
  • sloc: xml: 15,819; perl: 2,176; ansic: 705; cpp: 41; makefile: 2
file content (505 lines) | stat: -rwxr-xr-x 12,649 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
// JEdit mode Line -> :folding=indent:mode=c++:indentSize=2:noTabs=true:tabSize=2:
#include "EXTERN.h"
#define PERL_IN_HV_C
#define PERL_HASH_INTERNAL_ACCESS

#include "perl.h"
#include "XSUB.h"
#include "parser.h"

struct nodec *root;

U32 vhash;
U32 chash;
U32 phash;
U32 ihash;
U32 cdhash;
U32 zhash;
U32 ahash;

struct nodec *curnode;
char *rootpos;

/* -------------------------------------------------------------------- */

/*
-- xml_dequote_string is a slightly adapted version of xml_dequote
-- from the XML::Quote package by Sergey Skvortsov
*/
static SV *xml_dequote_string(unsigned char *src, STRLEN src_len)
{
  SV *dstSV;
  unsigned char *src2;
  unsigned char *dst;
  unsigned char c, c1, c2, c3, c4;
  STRLEN src_len2, dst_len;

  src2 = src;
  src_len2 = src_len;
  dst_len = src_len;

  // calculate dequoted string length
  while (src_len >= 3) {
    c = *src++;
    src_len--;

    if ('&' != c) {
      continue;
    }

    /* We have "&", now look for:- & " ' < > */
    c = *src;
    c1 = *(src + 1);
    c2 = *(src + 2);
    if (c2 == ';' && c1 == 't' && (c == 'l' || c == 'g')) {
      dst_len -= 3;
      src += 3;
      src_len -= 3;
      continue;
    }

    if (src_len >= 4) {
      c3 = *(src + 3);
    } else {
      continue;
    }

    if (c == 'a' && c1 == 'm' && c2 == 'p' && c3 == ';') {
      dst_len -= 4;
      src += 4;
      src_len -= 4;
      continue;
    }

    if (src_len >= 5) {
      c4 = *(src + 4);
    } else {
      continue;
    }

    if (c4 == ';'
        && ((c == 'q' && c1 == 'u' && c2 == 'o' && c3 == 't') || (c == 'a' && c1 == 'p' && c2 == 'o' && c3 == 's'))) {
      dst_len -= 5;
      src += 5;
      src_len -= 5;
      continue;
    }                           //if
  }                             //while

  if (dst_len == src_len2) {
    // nothing to dequote
    dstSV = newSVpv(src2, dst_len);
    return dstSV;
  }

  /* We have someting to dequote, so make a SV to put it into */
  dstSV = newSV(dst_len);
  SvCUR_set(dstSV, dst_len);
  SvPOK_on(dstSV);
  dst = SvPVX(dstSV);

  while (src_len2 >= 3) {       // 3 is min length of quoted symbol
    c = *src2++;
    src_len2--;
    if ('&' != c) {
      *dst++ = c;
      continue;
    }
    c = *src2;
    c1 = *(src2 + 1);
    c2 = *(src2 + 2);

    // 1. test len=3: < >
    if (c1 == 't' && c2 == ';') {
      if (c == 'l') {
        *dst++ = '<';
        src2 += 3;
        src_len2 -= 3;
        continue;
      } else if (c == 'g') {
        *dst++ = '>';
      } else {
        *dst++ = '&';
        continue;
      }
      src2 += 3;
      src_len2 -= 3;
      continue;
    }                           //if lt | gt


    // 2. test len=4: &amp;
    if (src_len2 >= 4) {
      c3 = *(src2 + 3);
    } else {
      *dst++ = '&';
      continue;
    }

    if (c == 'a' && c1 == 'm' && c2 == 'p' && c3 == ';') {
      *dst++ = '&';
      src2 += 4;
      src_len2 -= 4;
      continue;
    }
    // 3. test len=5: &quot; &apos;
    if (src_len2 >= 5) {
      c4 = *(src2 + 4);
    } else {
      *dst++ = '&';
      continue;
    }

    if (c4 == ';') {
      if (c == 'q' && c1 == 'u' && c2 == 'o' && c3 == 't') {
        *dst++ = '"';
      } else if (c == 'a' && c1 == 'p' && c2 == 'o' && c3 == 's') {
        *dst++ = '\'';
      } else {
        *dst++ = '&';
        continue;
      }
      src2 += 5;
      src_len2 -= 5;
      continue;
    }                           //if ;

    *dst++ = '&';
  }                             //while


  while (src_len2-- > 0) {      // also copy trailing \0
    *dst++ = *src2++;
  }

  return dstSV;
}

/* -------------------------------------------------------------------- */

SV *node_val_unescaped(struct nodec * thisnode)
{
  SV *sv;

  if (curnode->type == NODE_TYPE_ESCAPED)
    sv = xml_dequote_string(curnode->value, curnode->vallen);
  else
    sv = newSVpvn(curnode->value, curnode->vallen);

  SvUTF8_on(sv);

  return sv;
}

/* -------------------------------------------------------------------- */

SV *cxml2obj()
{
  HV *output = newHV();
  SV *outputref = newRV_noinc((SV *) output);
  int i;
  struct attc *curatt;
  int numatts = curnode->numatt;
  SV *attval;
  SV *attatt;

  int length = curnode->numchildren;
  SV *svi = newSViv(curnode->pos);

  hv_store(output, "_pos", 4, svi, phash);
  hv_store(output, "_i", 2, newSViv(curnode->name - rootpos), ihash);
  hv_store(output, "_z", 2, newSViv(curnode->z), zhash);
  if (!length) {
    if (curnode->vallen) {
      SV *sv = node_val_unescaped(curnode);
      hv_store(output, "value", 5, sv, vhash);
      if (curnode->type & NODE_TYPE_CDATA) {
        SV *svi = newSViv(1);
        hv_store(output, "_cdata", 6, svi, cdhash);
      }
    }
    if (curnode->comlen) {
      SV *sv = newSVpvn(curnode->comment, curnode->comlen);
      SvUTF8_on(sv);
      hv_store(output, "comment", 7, sv, chash);
    }
  } else {
    if (curnode->vallen) {
      SV *sv = node_val_unescaped(curnode);
      hv_store(output, "value", 5, sv, vhash);
      if (curnode->type & NODE_TYPE_CDATA) {
        SV *svi = newSViv(1);
        hv_store(output, "_cdata", 6, svi, cdhash);
      }
    }
    if (curnode->comlen) {
      SV *sv = newSVpvn(curnode->comment, curnode->comlen);
      SvUTF8_on(sv);
      hv_store(output, "comment", 7, sv, chash);
    }

    curnode = curnode->firstchild;
    for (i = 0; i < length; i++) {
      SV *key = newSVpv(curnode->name, curnode->namelen);
      SvUTF8_on(key);
      HE *curh = hv_fetch_ent(output, key, 0, 0);

      if (curnode->namelen > 6) {
        if (!strncmp(curnode->name, "multi_", 6)) {
          SV *subkey = newSVpv(&curnode->name[6], curnode->namelen - 6);
          SvUTF8_on(subkey);
          HE *oldh = hv_fetch_ent(output, subkey, 0, 0);
          AV *newarray = newAV();
          SV *newarrayref = newRV_noinc((SV *) newarray);
          if (!oldh) {
            hv_store_ent(output, subkey, newarrayref, 0);
          } else {
            SV *old = HeVAL(oldh);
            if (SvTYPE(SvRV(old)) == SVt_PVHV) {        // check for hash ref
              SV *newref = newRV((SV *) SvRV(old));
              hv_delete_ent(output, subkey, 0, 0);
              hv_store_ent(output, subkey, newarrayref, 0);
              av_push(newarray, newref);
            }
          }
          SvREFCNT_dec(subkey); // no longer need the subkey
        }
      }

      if (!curh) {
        hv_store_ent(output, key, cxml2obj(), 0);
      } else {
        SV *cur = HeVAL(curh);
        if (SvTYPE(SvRV(cur)) == SVt_PVHV) {
          AV *newarray = newAV();
          SV *newarrayref = newRV_noinc((SV *) newarray);
          SV *newref = newRV((SV *) SvRV(cur));
          hv_delete_ent(output, key, 0, 0);
          hv_store_ent(output, key, newarrayref, 0);
          av_push(newarray, newref);
          av_push(newarray, cxml2obj());
        } else {
          AV *av = (AV *) SvRV(cur);
          av_push(av, cxml2obj());
        }
      }
      if (i != (length - 1))
        curnode = curnode->next;
      SvREFCNT_dec(key);        // no longer need the key
    }

    curnode = curnode->parent;
  }

  if (numatts) {
    curatt = curnode->firstatt;
    for (i = 0; i < numatts; i++) {
      HV *atth = newHV();
      SV *atthref = newRV_noinc((SV *) atth);
      hv_store(output, curatt->name, curatt->namelen, atthref, 0);

      attval = newSVpvn(curatt->value, curatt->vallen);
      SvUTF8_on(attval);
      hv_store(atth, "value", 5, attval, vhash);
      attatt = newSViv(1);
      hv_store(atth, "_att", 4, attatt, ahash);
      if (i != (numatts - 1))
        curatt = curatt->next;
    }
  }
  return outputref;
}

/* -------------------------------------------------------------------- */

SV *cxml2obj_simple()
{
  int i;
  struct attc *curatt;
  int numatts = curnode->numatt;
  SV *attval;
  SV *attatt;
  HV *output;
  SV *outputref;

  int length = curnode->numchildren;
  if ((length + numatts) == 0) {
    if (curnode->vallen)
      return node_val_unescaped(curnode);
    else
      return newSVpv("", 0);    // an empty tag has empty string content
  }

  output = newHV();
  outputref = newRV_noinc((SV *) output);

  if (length) {
    curnode = curnode->firstchild;
    for (i = 0; i < length; i++) {
      SV *key = newSVpv(curnode->name, curnode->namelen);
      SvUTF8_on(key);
      HE *curh = hv_fetch_ent(output, key, 0, 0);

      if (curnode->namelen > 6) {
        if (!strncmp(curnode->name, "multi_", 6)) {
          SV *subkey = newSVpv(&curnode->name[6], curnode->namelen - 6);
          SvUTF8_on(subkey);
          HE *oldh = hv_fetch_ent(output, subkey, 0, 0);
          AV *newarray = newAV();
          SV *newarrayref = newRV_noinc((SV *) newarray);
          if (!oldh) {
            hv_store_ent(output, subkey, newarrayref, 0);
          } else {
            SV *old = HeVAL(oldh);
            if (SvTYPE(SvRV(old)) == SVt_PVHV) {        // check for hash ref
              SV *newref = newRV((SV *) SvRV(old));
              hv_delete_ent(output, subkey, 0, 0);
              hv_store_ent(output, subkey, newarrayref, 0);
              av_push(newarray, newref);
            }
          }
          SvREFCNT_dec(subkey); // no longer need the subkey
        }
      }

      if (!curh) {
        hv_store_ent(output, key, cxml2obj_simple(), 0);
      } else {
        SV *cur = HeVAL(curh);
        if (SvROK(cur)) {
          if (SvTYPE(SvRV(cur)) == SVt_PVHV) {
            AV *newarray = newAV();
            SV *newarrayref = newRV_noinc((SV *) newarray);
            SV *newref = newRV((SV *) SvRV(cur));
            hv_delete_ent(output, key, 0, 0);
            hv_store_ent(output, key, newarrayref, 0);
            av_push(newarray, newref);
            av_push(newarray, cxml2obj_simple());
          } else {
            AV *av = (AV *) SvRV(cur);
            av_push(av, cxml2obj_simple());
          }
        } else {
          AV *newarray = newAV();
          SV *newarrayref = newRV_noinc((SV *) newarray);

          STRLEN len;
          char *ptr = SvPV(cur, len);
          SV *newsv = newSVpvn(ptr, len);
          SvUTF8_on(newsv);

          av_push(newarray, newsv);
          hv_delete_ent(output, key, 0, 0);
          hv_store_ent(output, key, newarrayref, 0);
          av_push(newarray, cxml2obj_simple());
        }
      }
      if (i != (length - 1))
        curnode = curnode->next;
      SvREFCNT_dec(key);        // no longer need the key
    }
    curnode = curnode->parent;
  } else {
    SV *sv = node_val_unescaped(curnode);
    hv_store(output, "content", 7, sv, vhash);
  }

  if (numatts) {
    curatt = curnode->firstatt;
    for (i = 0; i < numatts; i++) {
      attval = newSVpvn(curatt->value, curatt->vallen);
      SvUTF8_on(attval);
      hv_store(output, curatt->name, curatt->namelen, attval, 0);
      if (i != (numatts - 1))
        curatt = curatt->next;
    }
  }

  return outputref;
}

/* -------------------------------------------------------------------- */

// *INDENT-OFF*
// Indent and XS declarations do not mix well :-(

MODULE = XML::Bare         PACKAGE = XML::Bare

SV *
xml2obj()
  CODE:
    if( root->err ) RETVAL = newSViv( root->err );
    else {
      curnode = root;
      RETVAL = cxml2obj();
    }
  OUTPUT:
    RETVAL
    
SV *
xml2obj_simple()
  CODE:
    PERL_HASH(vhash, "content", 7);
    curnode = root;
    RETVAL = cxml2obj_simple();
  OUTPUT:
    RETVAL

void
c_parse(text)
  char * text
  CODE:
    rootpos = text;
    PERL_HASH(vhash, "value", 5);
    PERL_HASH(ahash, "_att", 4);
    PERL_HASH(chash, "comment", 7);
    PERL_HASH(phash, "_pos", 4);
    PERL_HASH(ihash, "_i", 2 );
    PERL_HASH(zhash, "_z", 2 );
    PERL_HASH(cdhash, "_cdata", 6 );
    root = parserc_parse( text );

void
c_parsefile(filename)
  char * filename
  CODE:
    char *data;
    unsigned long len;
    FILE *handle;
    
    PERL_HASH(vhash, "value", 5);
    PERL_HASH(ahash, "_att", 4);
    PERL_HASH(chash, "comment", 7);
    PERL_HASH(phash, "_pos", 4);
    PERL_HASH(ihash, "_i", 2 );
    PERL_HASH(zhash, "_z", 2 );
    PERL_HASH(cdhash, "_cdata", 6 );
    
    handle = fopen(filename,"r");
    
    fseek( handle, 0, SEEK_END );
    
    len = ftell( handle );
    
    fseek( handle, 0, SEEK_SET );
    data = (char *) malloc( len );
    rootpos = data;
    fread( data, 1, len, handle );
    fclose( handle );
    root = parserc_parse( data );
    free( data );

SV *
get_root()
  CODE:
    RETVAL = newSVuv( PTR2UV( root ) );
  OUTPUT:
    RETVAL

void
free_tree_c( rootsv )
  SV *rootsv
  CODE:
    struct nodec *rootnode;
    rootnode = INT2PTR( struct nodec *, SvUV( rootsv ) );
    del_nodec( rootnode );