1
// JEdit mode Line -> :folding=indent:mode=c++:indentSize=2:noTabs=true:tabSize=2:
4
#define PERL_HASH_INTERNAL_ACCESS
5
#define BLIND_PV(a,b) SV *sv;sv=newSV(0);SvUPGRADE(sv,SVt_PV);SvPV_set(sv,a);SvCUR_set(sv,b);SvPOK_only_UTF8(sv);
19
struct nodec *curnode;
22
SV *cxml2obj(pTHX_ int a) {
24
SV *outputref = newRV( (SV *) output );
27
int numatts = curnode->numatt;
31
int length = curnode->numchildren;
32
SV *svi = newSViv( curnode->pos );
37
hv_store( output, "_pos", 4, svi, phash );
38
hv_store( output, "_i", 2, newSViv( curnode->name - rootpos ), ihash );
40
if( curnode->vallen ) {
41
SV * sv = newSVpvn( curnode->value, curnode->vallen );
42
//BLIND_PV( curnode->value, curnode->vallen );
43
hv_store( output, "value", 5, sv, vhash );
45
if( curnode->comlen ) {
46
SV * sv = newSVpvn( curnode->comment, curnode->comlen );
47
hv_store( output, "comment", 7, sv, chash );
51
if( curnode->vallen ) {
52
SV *sv = newSVpvn( curnode->value, curnode->vallen );
53
//BLIND_PV( curnode->value, curnode->vallen );
54
hv_store( output, "value", 5, sv, vhash );
56
if( curnode->comlen ) {
57
SV *sv = newSVpvn( curnode->comment, curnode->comlen );
58
hv_store( output, "comment", 7, sv, chash );
61
curnode = curnode->firstchild;
62
for( i = 0; i < length; i++ ) {
63
SV *namesv = newSVpvn( curnode->name, curnode->namelen );
65
SV **cur = hv_fetch( output, curnode->name, curnode->namelen, 0 );
67
if( curnode->namelen > 6 ) {
68
if( !strncmp( curnode->name, "multi_", 6 ) ) {
69
char *subname = &curnode->name[6];
70
int subnamelen = curnode->namelen-6;
71
SV **old = hv_fetch( output, subname, subnamelen, 0 );
72
AV *newarray = newAV();
73
SV *newarrayref = newRV( (SV *) newarray );
75
hv_store( output, subname, subnamelen, newarrayref, 0 );
78
if( SvTYPE( SvRV(*old) ) == SVt_PVHV ) { // check for hash ref
79
SV *newref = newRV( (SV *) SvRV(*old) );
80
hv_delete( output, subname, subnamelen, 0 );
81
hv_store( output, subname, subnamelen, newarrayref, 0 );
82
av_push( newarray, newref );
89
SV *ob = cxml2obj( aTHX_ 0 );
90
hv_store( output, curnode->name, curnode->namelen, ob, 0 );
93
if( SvTYPE( SvRV(*cur) ) == SVt_PVHV ) {
94
AV *newarray = newAV();
95
SV *newarrayref = newRV( (SV *) newarray );
96
SV *newref = newRV( (SV *) SvRV( *cur ) );
97
hv_delete( output, curnode->name, curnode->namelen, 0 );
98
hv_store( output, curnode->name, curnode->namelen, newarrayref, 0 );
99
av_push( newarray, newref );
100
av_push( newarray, cxml2obj( aTHX_ 0 ) );
103
AV *av = (AV *) SvRV( *cur );
104
av_push( av, cxml2obj( aTHX_ 0) );
107
if( i != ( length - 1 ) ) curnode = curnode->next;
110
curnode = curnode->parent;
114
curatt = curnode->firstatt;
115
for( i = 0; i < numatts; i++ ) {
117
SV *atthref = newRV( (SV *) atth );
118
hv_store( output, curatt->name, curatt->namelen, atthref, 0 );
120
attval = newSVpvn( curatt->value, curatt->vallen );
121
hv_store( atth, "value", 5, attval, vhash );
122
attatt = newSViv( 1 );
123
hv_store( atth, "att", 3, attatt, 0 );
124
if( i != ( numatts - 1 ) ) curatt = curatt->next;
130
SV *cxml2obj_simple(pTHX_ int a) {
134
int numatts = curnode->numatt;
138
int length = curnode->numchildren;
139
if( ( length + numatts ) == 0 ) {
140
if( curnode->vallen ) {
141
SV * sv = newSVpvn( curnode->value, curnode->vallen );
147
HV *output = newHV();
148
SV *outputref = newRV( (SV *) output );
151
curnode = curnode->firstchild;
152
for( i = 0; i < length; i++ ) {
153
SV *namesv = newSVpvn( curnode->name, curnode->namelen );
155
SV **cur = hv_fetch( output, curnode->name, curnode->namelen, 0 );
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 );
165
hv_store( output, subname, subnamelen, newarrayref, 0 );
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 );
179
SV *ob = cxml2obj_simple( aTHX_ 0 );
180
hv_store( output, curnode->name, curnode->namelen, ob, 0 );
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
av_push( newarray, newref );
191
av_push( newarray, cxml2obj_simple( aTHX_ 0 ) );
194
AV *av = (AV *) SvRV( *cur );
195
av_push( av, cxml2obj_simple( aTHX_ 0) );
199
AV *newarray = newAV();
200
SV *newarrayref = newRV( (SV *) newarray );
203
char *ptr = SvPV(*cur, len);
204
SV *newsv = newSVpvn( ptr, len );
206
av_push( newarray, newsv );
207
hv_delete( output, curnode->name, curnode->namelen, 0 );
208
hv_store( output, curnode->name, curnode->namelen, newarrayref, 0 );
209
av_push( newarray, cxml2obj_simple( aTHX_ 0 ) );
212
if( i != ( length - 1 ) ) curnode = curnode->next;
214
curnode = curnode->parent;
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;
230
struct parserc *parser = 0;
232
MODULE = XML::Bare PACKAGE = XML::Bare
239
curnode = parser->pcurnode;
241
if( curnode->err ) RETVAL = newSViv( curnode->err );
242
else RETVAL = cxml2obj(aTHX_ 0);
249
curnode = parser->pcurnode;
251
//if( curnode->err ) RETVAL = 0;
253
RETVAL = cxml2obj_simple(aTHX_ 0);
264
//printf("About to parse\n");
266
PERL_HASH(vhash, "value", 5);
267
PERL_HASH(chash, "comment", 7);
268
PERL_HASH(phash, "_pos", 4);
269
PERL_HASH(ihash, "_i", 2 );
270
parser = (struct parserc *) malloc( sizeof( struct parserc ) );
271
root = parserc_parse( parser, text );
273
//printf("Returned from parser\n");
276
//root = parser->pcurnode;
277
//this is wrong because parsing may halt in the middle; root= above is correct
280
//printf("C Parsing done\b");
284
c_parsefile(filename)
287
PERL_HASH(vhash, "value", 5);
288
PERL_HASH(chash, "comment", 7);
289
PERL_HASH(phash, "_pos", 4);
290
PERL_HASH(ihash, "_i", 2 );
294
handle = fopen(filename,"r");
296
fseek( handle, 0, SEEK_END );
298
len = ftell( handle );
300
fseek( handle, 0, SEEK_SET );
301
data = (char *) malloc( len );
303
fread( data, 1, len, handle );
305
parser = (struct parserc *) malloc( sizeof( struct parserc ) );
306
root = parserc_parse( parser, data );
307
//root = parser->pcurnode;
312
RETVAL = newSVuv( PTR2UV( root ) );
317
free_tree_c( rootsv )
320
struct nodec *rootnode;
321
rootnode = INT2PTR( struct nodec *, SvUV( rootsv ) );
322
del_nodec( rootnode );
b'\\ No newline at end of file'