| 1 |
|
| #include "EXTERN.h" |
| 2 |
|
| #include "perl.h" |
| 3 |
|
| #include "XSUB.h" |
| 4 |
|
| |
| 5 |
|
| #include "ppport.h" |
| 6 |
|
| |
| 7 |
|
| #include "vparse.h" |
| 8 |
|
| |
| 9 |
|
| // hv_store to array, create if not exists - from XML::Fast 0.11 |
| 10 |
|
| #define hv_store_aa( hv, kv, kl, sv ) \ |
| 11 |
|
| STMT_START { \ |
| 12 |
|
| SV **exists; \ |
| 13 |
|
| if( ( exists = hv_fetch(hv, kv, kl, 0) ) && SvROK(*exists) && (SvTYPE( SvRV(*exists) ) == SVt_PVAV) ) { \ |
| 14 |
|
| AV *av = (AV *) SvRV( *exists ); \ |
| 15 |
|
| av_push( av, sv ); \ |
| 16 |
|
| } \ |
| 17 |
|
| else { \ |
| 18 |
|
| AV *av = newAV(); \ |
| 19 |
|
| av_push( av, sv ); \ |
| 20 |
|
| (void) hv_store( hv, kv, kl, newRV_noinc( (SV *) av ), 0 ); \ |
| 21 |
|
| } \ |
| 22 |
|
| } STMT_END |
| 23 |
|
| |
| 24 |
|
| #define str_u(val) (!val ? newSV(0) : is_utf8 ? newSVpvn_utf8((val), strlen(val), 1) : newSVpvn((val), strlen(val))) |
| 25 |
|
| |
| 26 |
|
| |
| 27 |
52 |
| static HV *_card2perl(struct vparse_card *card, int is_utf8, int barekeys) |
| 28 |
|
| { |
| 29 |
|
| struct vparse_card *sub; |
| 30 |
|
| struct vparse_entry *entry; |
| 31 |
52 |
| HV *res = newHV(); |
| 32 |
52 |
| HV *prophash = newHV(); |
| 33 |
|
| |
| 34 |
52 |
▶ | if (card->type) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (card->type) { |
27 |
25 |
52 |
|
| 35 |
27 |
▶ | hv_store(res, "type", 4, str_u(card->type), 0); |
Branch: 2 branches
| Branch | True | False | Total |
| hv_store(res, "type", 4, str_u(card->type), 0); |
0 |
27 |
27 |
| hv_store(res, "type", 4, str_u(card->type), 0); |
26 |
1 |
27 |
|
| 36 |
27 |
| hv_store(res, "properties", 10, newRV_noinc( (SV *) prophash), 0); |
| 37 |
|
| } |
| 38 |
|
| |
| 39 |
52 |
▶ | if (card->objects) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (card->objects) { |
27 |
25 |
52 |
|
| 40 |
27 |
| AV *objarray = newAV(); |
| 41 |
27 |
| hv_store(res, "objects", 7, newRV_noinc( (SV *) objarray), 0); |
| 42 |
54 |
▶ | for (sub = card->objects; sub; sub = sub->next) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (sub = card->objects; sub; sub = sub->next) { |
27 |
27 |
54 |
|
| 43 |
27 |
| HV *child = _card2perl(sub, is_utf8, barekeys); |
| 44 |
27 |
| av_push(objarray, newRV_noinc( (SV *) child)); |
| 45 |
|
| } |
| 46 |
|
| } |
| 47 |
|
| |
| 48 |
339 |
▶ | for (entry = card->properties; entry; entry = entry->next) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (entry = card->properties; entry; entry = entry->next) { |
287 |
52 |
339 |
|
| 49 |
287 |
| HV *item = newHV(); |
| 50 |
|
| |
| 51 |
287 |
▶ | if (entry->group) |
Branch: 1 branch
| Branch | True | False | Total |
| if (entry->group) |
36 |
251 |
287 |
|
| 52 |
36 |
▶ | hv_store(item, "group", 5, str_u(entry->group), 0); |
Branch: 2 branches
| Branch | True | False | Total |
| hv_store(item, "group", 5, str_u(entry->group), 0); |
0 |
36 |
36 |
| hv_store(item, "group", 5, str_u(entry->group), 0); |
36 |
0 |
36 |
|
| 53 |
|
| |
| 54 |
287 |
▶ | hv_store(item, "name", 4, str_u(entry->name), 0); |
Branch: 2 branches
| Branch | True | False | Total |
| hv_store(item, "name", 4, str_u(entry->name), 0); |
0 |
287 |
287 |
| hv_store(item, "name", 4, str_u(entry->name), 0); |
286 |
1 |
287 |
|
| 55 |
287 |
▶ | if (entry->multivalue) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (entry->multivalue) { |
52 |
235 |
287 |
|
| 56 |
52 |
| AV *av = newAV(); |
| 57 |
|
| struct vparse_list *list; |
| 58 |
318 |
▶ | for (list = entry->v.values; list; list = list->next) |
Branch: 1 branch
| Branch | True | False | Total |
| for (list = entry->v.values; list; list = list->next) |
266 |
52 |
318 |
|
| 59 |
266 |
▶ | av_push(av, str_u(list->s)); |
Branch: 2 branches
| Branch | True | False | Total |
| av_push(av, str_u(list->s)); |
0 |
266 |
266 |
| av_push(av, str_u(list->s)); |
266 |
0 |
266 |
|
| 60 |
52 |
| hv_store(item, "values", 6, newRV_noinc( (SV *) av), 0); |
| 61 |
|
| } |
| 62 |
|
| else { |
| 63 |
235 |
▶ | hv_store(item, "value", 5, str_u(entry->v.value), 0); |
Branch: 2 branches
| Branch | True | False | Total |
| hv_store(item, "value", 5, str_u(entry->v.value), 0); |
0 |
235 |
235 |
| hv_store(item, "value", 5, str_u(entry->v.value), 0); |
234 |
1 |
235 |
|
| 64 |
|
| } |
| 65 |
|
| |
| 66 |
287 |
▶ | if (entry->params) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (entry->params) { |
126 |
161 |
287 |
|
| 67 |
|
| struct vparse_param *param; |
| 68 |
126 |
| HV *prop = newHV(); |
| 69 |
354 |
▶ | for (param = entry->params; param; param = param->next) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (param = entry->params; param; param = param->next) { |
228 |
126 |
354 |
|
| 70 |
228 |
▶ | if (param->value) |
Branch: 1 branch
| Branch | True | False | Total |
| if (param->value) |
228 |
0 |
228 |
|
| 71 |
228 |
▶ | hv_store_aa(prop, param->name, strlen(param->name), str_u(param->value)); |
Branch: 7 branches
| Branch | True | False | Total |
| hv_store_aa(prop, param->name, strlen(param->name), str_u(param->value)); |
58 |
170 |
228 |
| hv_store_aa(prop, param->name, strlen(param->name), str_u(param->value)); |
58 |
0 |
58 |
| hv_store_aa(prop, param->name, strlen(param->name), str_u(param->value)); |
58 |
0 |
58 |
| hv_store_aa(prop, param->name, strlen(param->name), str_u(param->value)); |
0 |
58 |
58 |
| hv_store_aa(prop, param->name, strlen(param->name), str_u(param->value)); |
58 |
0 |
58 |
| hv_store_aa(prop, param->name, strlen(param->name), str_u(param->value)); |
0 |
170 |
170 |
| hv_store_aa(prop, param->name, strlen(param->name), str_u(param->value)); |
170 |
0 |
170 |
|
| 72 |
|
| else |
| 73 |
0 |
▶ | hv_store_aa(prop, "type", 4, str_u(param->name)); |
Branch: 7 branches
| Branch | True | False | Total |
| hv_store_aa(prop, "type", 4, str_u(param->name)); |
0 |
0 |
0 |
| hv_store_aa(prop, "type", 4, str_u(param->name)); |
0 |
0 |
0 |
| hv_store_aa(prop, "type", 4, str_u(param->name)); |
0 |
0 |
0 |
| hv_store_aa(prop, "type", 4, str_u(param->name)); |
0 |
0 |
0 |
| hv_store_aa(prop, "type", 4, str_u(param->name)); |
0 |
0 |
0 |
| hv_store_aa(prop, "type", 4, str_u(param->name)); |
0 |
0 |
0 |
| hv_store_aa(prop, "type", 4, str_u(param->name)); |
0 |
0 |
0 |
|
| 74 |
|
| } |
| 75 |
126 |
| hv_store(item, "params", 6, newRV_noinc( (SV *) prop), 0); |
| 76 |
|
| } |
| 77 |
287 |
▶ | hv_store_aa(prophash, entry->name, strlen(entry->name), newRV_noinc( (SV *) item)); |
Branch: 3 branches
| Branch | True | False | Total |
| hv_store_aa(prophash, entry->name, strlen(entry->name), newRV_noinc( (SV *) item)); |
42 |
245 |
287 |
| hv_store_aa(prophash, entry->name, strlen(entry->name), newRV_noinc( (SV *) item)); |
42 |
0 |
42 |
| hv_store_aa(prophash, entry->name, strlen(entry->name), newRV_noinc( (SV *) item)); |
42 |
0 |
42 |
|
| 78 |
|
| } |
| 79 |
|
| |
| 80 |
52 |
| return res; |
| 81 |
|
| } |
| 82 |
|
| |
| 83 |
6 |
| static void _die_error(struct vparse_state *state, int err) |
| 84 |
|
| { |
| 85 |
|
| struct vparse_errorpos pos; |
| 86 |
6 |
| const char *src = state->base; |
| 87 |
|
| |
| 88 |
6 |
| vparse_fillpos(state, &pos); |
| 89 |
|
| /* everything points into src now */ |
| 90 |
6 |
| vparse_free(state); |
| 91 |
|
| |
| 92 |
6 |
▶ | if (pos.startpos <= 60) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pos.startpos <= 60) { |
6 |
0 |
6 |
|
| 93 |
6 |
| int len = pos.errorpos - pos.startpos; |
| 94 |
6 |
| croak("error %s at line %d char %d: %.*s ---> %.*s <---", |
| 95 |
|
| vparse_errstr(err), pos.errorline, pos.errorchar, |
| 96 |
|
| pos.startpos, src, len, src + pos.startpos); |
| 97 |
|
| } |
| 98 |
0 |
▶ | if (pos.errorpos - pos.startpos < 40) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pos.errorpos - pos.startpos < 40) { |
0 |
0 |
0 |
|
| 99 |
0 |
| int len = pos.errorpos - pos.startpos; |
| 100 |
0 |
| croak("error %s at line %d char %d: ... %.*s ---> %.*s <---", |
| 101 |
|
| vparse_errstr(err), pos.errorline, pos.errorchar, |
| 102 |
|
| 40 - len, src + pos.errorpos - 40, |
| 103 |
|
| len, src + pos.startpos); |
| 104 |
|
| } |
| 105 |
0 |
| croak("error %s at line %d char %d: %.*s ... %.*s <--- (started at line %d char %d)", |
| 106 |
|
| vparse_errstr(err), pos.errorline, pos.errorchar, |
| 107 |
|
| 20, src + pos.startpos, |
| 108 |
|
| 20, src + pos.errorpos - 20, |
| 109 |
|
| pos.startline, pos.startchar); |
| 110 |
|
| } |
| 111 |
|
| |
| 112 |
53 |
| static struct vparse_list *_get_keys(SV **key) |
| 113 |
|
| { |
| 114 |
53 |
| struct vparse_list *item = NULL; |
| 115 |
53 |
| struct vparse_list **valp = &item; |
| 116 |
|
| |
| 117 |
53 |
▶ | if (SvTYPE(SvRV(*key)) == SVt_PVAV) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvTYPE(SvRV(*key)) == SVt_PVAV) { |
53 |
0 |
53 |
|
| 118 |
53 |
| AV *av = (AV *) SvRV( *key ); |
| 119 |
53 |
| I32 len = 0, avlen = av_len(av) + 1; |
| 120 |
|
| SV **val; |
| 121 |
159 |
▶ | for (len = 0; len < avlen; len++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (len = 0; len < avlen; len++) { |
106 |
53 |
159 |
|
| 122 |
106 |
| val = av_fetch(av, len, 0); |
| 123 |
106 |
▶ | if (SvOK(*val) && SvPOK(*val)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvOK(*val) && SvPOK(*val)) { |
106 |
0 |
106 |
| if (SvOK(*val) && SvPOK(*val)) { |
106 |
0 |
106 |
|
| 124 |
106 |
| struct vparse_list *item = malloc(sizeof(struct vparse_list)); |
| 125 |
106 |
| item->s = strdup(SvPV_nolen(*val)); |
| 126 |
106 |
| item->next = NULL; |
| 127 |
106 |
| *valp = item; |
| 128 |
106 |
| valp = &item->next; |
| 129 |
|
| } |
| 130 |
|
| } |
| 131 |
|
| } |
| 132 |
|
| |
| 133 |
53 |
| return item; |
| 134 |
|
| } |
| 135 |
|
| |
| 136 |
|
| MODULE = Text::VCardFast PACKAGE = Text::VCardFast |
| 137 |
|
| |
| 138 |
|
| SV* |
| 139 |
|
| _vcard2hash(src, conf) |
| 140 |
|
| const char *src; |
| 141 |
|
| HV *conf; |
| 142 |
|
| PROTOTYPE: $$ |
| 143 |
|
| CODE: |
| 144 |
|
| HV *hash; |
| 145 |
|
| struct vparse_state parser; |
| 146 |
31 |
| struct vparse_list *multival = NULL; |
| 147 |
31 |
| struct vparse_list *multiparam = NULL; |
| 148 |
31 |
| int is_utf8 = 0; |
| 149 |
31 |
| int barekeys = 0; |
| 150 |
31 |
| int only_one = 0; |
| 151 |
|
| int r; |
| 152 |
|
| SV **key; |
| 153 |
|
| |
| 154 |
31 |
▶ | if ((key = hv_fetch(conf, "multival", 8, 0)) && SvTRUE(*key)) |
Branch: 2 branches
| Branch | True | False | Total |
| if ((key = hv_fetch(conf, "multival", 8, 0)) && SvTRUE(*key)) |
29 |
2 |
31 |
| if ((key = hv_fetch(conf, "multival", 8, 0)) && SvTRUE(*key)) |
29 |
0 |
29 |
|
| 155 |
29 |
| multival = _get_keys(key); |
| 156 |
|
| |
| 157 |
31 |
▶ | if ((key = hv_fetch(conf, "multiparam", 10, 0)) && SvTRUE(*key)) |
Branch: 2 branches
| Branch | True | False | Total |
| if ((key = hv_fetch(conf, "multiparam", 10, 0)) && SvTRUE(*key)) |
24 |
7 |
31 |
| if ((key = hv_fetch(conf, "multiparam", 10, 0)) && SvTRUE(*key)) |
24 |
0 |
24 |
|
| 158 |
24 |
| multiparam = _get_keys(key); |
| 159 |
|
| |
| 160 |
31 |
▶ | if ((key = hv_fetch(conf, "is_utf8", 7, 0)) && SvTRUE(*key)) |
Branch: 2 branches
| Branch | True | False | Total |
| if ((key = hv_fetch(conf, "is_utf8", 7, 0)) && SvTRUE(*key)) |
24 |
7 |
31 |
| if ((key = hv_fetch(conf, "is_utf8", 7, 0)) && SvTRUE(*key)) |
24 |
0 |
24 |
|
| 161 |
24 |
| is_utf8 = 1; |
| 162 |
|
| |
| 163 |
31 |
▶ | if ((key = hv_fetch(conf, "barekeys", 8, 0)) && SvTRUE(*key)) |
Branch: 2 branches
| Branch | True | False | Total |
| if ((key = hv_fetch(conf, "barekeys", 8, 0)) && SvTRUE(*key)) |
0 |
31 |
31 |
| if ((key = hv_fetch(conf, "barekeys", 8, 0)) && SvTRUE(*key)) |
0 |
0 |
0 |
|
| 164 |
0 |
| barekeys = 1; |
| 165 |
|
| |
| 166 |
31 |
▶ | if ((key = hv_fetch(conf, "only_one", 8, 0)) && SvTRUE(*key)) |
Branch: 2 branches
| Branch | True | False | Total |
| if ((key = hv_fetch(conf, "only_one", 8, 0)) && SvTRUE(*key)) |
1 |
30 |
31 |
| if ((key = hv_fetch(conf, "only_one", 8, 0)) && SvTRUE(*key)) |
1 |
0 |
1 |
|
| 167 |
1 |
| only_one = 1; |
| 168 |
|
| |
| 169 |
31 |
| memset(&parser, 0, sizeof(struct vparse_state)); |
| 170 |
31 |
| parser.base = src; |
| 171 |
31 |
| parser.multival = multival; |
| 172 |
31 |
| parser.multiparam = multiparam; |
| 173 |
31 |
| parser.barekeys = barekeys; |
| 174 |
|
| |
| 175 |
31 |
| r = vparse_parse(&parser, only_one); |
| 176 |
31 |
▶ | if (r) _die_error(&parser, r); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) _die_error(&parser, r); |
6 |
25 |
31 |
|
| 177 |
|
| |
| 178 |
25 |
| hash = _card2perl(parser.card, is_utf8, barekeys); |
| 179 |
|
| |
| 180 |
25 |
| vparse_free(&parser); |
| 181 |
|
| |
| 182 |
25 |
| RETVAL = newRV_noinc( (SV *) hash); |
| 183 |
|
| OUTPUT: |
| 184 |
|
| RETVAL |
| 185 |
|
| |