| 1 |
|
| #include "EXTERN.h" |
| 2 |
|
| #include "perl.h" |
| 3 |
|
| #include "XSUB.h" |
| 4 |
|
| #include <stdio.h> |
| 5 |
|
| #include <string.h> |
| 6 |
|
| |
| 7 |
|
| #define CONF_GET(conf, key, default) \ |
| 8 |
|
| ({ \ |
| 9 |
|
| SV** val = hv_fetch(conf, key, strlen(key), 0); \ |
| 10 |
|
| val ? SvUV(*val) : (default); \ |
| 11 |
|
| }) |
| 12 |
|
| |
| 13 |
|
| #ifndef newSVpvn_utf8 |
| 14 |
|
| #define newSVpvn_utf8(ptr, len, utf8) \ |
| 15 |
|
| ({ \ |
| 16 |
|
| SV* copy = newSVpvn((ptr), (len)); \ |
| 17 |
|
| if (utf8) { SvUTF8_on(copy); } \ |
| 18 |
|
| copy; \ |
| 19 |
|
| }) |
| 20 |
|
| #endif |
| 21 |
|
| |
| 22 |
|
| #define BUF_LEN 1024 |
| 23 |
|
| #define STATE_MASK 0x00FF |
| 24 |
|
| #define ACTION_MASK 0xFF00 |
| 25 |
|
| #define DBG(fmt, ...) if (opts->debug > 0) { fprintf(stderr, fmt, ##__VA_ARGS__); } |
| 26 |
|
| #define DUMP(s) do_sv_dump(0, Perl_debug_log, (s), 0, 1, 0, 0); |
| 27 |
|
| |
| 28 |
|
| typedef struct opts { |
| 29 |
|
| U32 nodot; |
| 30 |
|
| U32 nullsplit; |
| 31 |
|
| U32 dclone; |
| 32 |
|
| U32 debug; |
| 33 |
|
| } Opts; |
| 34 |
|
| |
| 35 |
|
| typedef enum input { |
| 36 |
|
| I_DT = 0x00, // . |
| 37 |
|
| I_LS = 0x01, // [ |
| 38 |
|
| I_RS = 0x02, // ] |
| 39 |
|
| I_LC = 0x03, // { |
| 40 |
|
| I_RC = 0x04, // } |
| 41 |
|
| I_DI = 0x05, // 0-9 digit |
| 42 |
|
| I_CH = 0x06, // any other char |
| 43 |
|
| I_EN = 0x07 // fake end-of-line char |
| 44 |
|
| } Input; |
| 45 |
|
| |
| 46 |
|
| typedef enum action { |
| 47 |
|
| A_EC = 0x0200, // eat char |
| 48 |
|
| A_ED = 0x0800, // eat digit |
| 49 |
|
| A_CH = 0x1000, // create hash (vivify) |
| 50 |
|
| A_CA = 0x2000, // create array (vivify) |
| 51 |
|
| A_CV = 0x4000, // put scalar |
| 52 |
|
| A_AA = 0x8000 // put scalar-or-array (auto-arrays) |
| 53 |
|
| } Action; |
| 54 |
|
| |
| 55 |
|
| static Input classes[] = { |
| 56 |
|
| I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, |
| 57 |
|
| I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, |
| 58 |
|
| I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, |
| 59 |
|
| I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, |
| 60 |
|
| I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, |
| 61 |
|
| I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_DT, I_CH, |
| 62 |
|
| I_DI, I_DI, I_DI, I_DI, I_DI, I_DI, I_DI, I_DI, |
| 63 |
|
| I_DI, I_DI, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, |
| 64 |
|
| I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, |
| 65 |
|
| I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, |
| 66 |
|
| I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, |
| 67 |
|
| I_CH, I_CH, I_CH, I_LS, I_CH, I_RS, I_CH, I_CH, |
| 68 |
|
| I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, |
| 69 |
|
| I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, |
| 70 |
|
| I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, |
| 71 |
|
| I_CH, I_CH, I_CH, I_LC, I_CH, I_RC, I_CH, I_CH, |
| 72 |
|
| I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, |
| 73 |
|
| I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, |
| 74 |
|
| I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, |
| 75 |
|
| I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, |
| 76 |
|
| I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, |
| 77 |
|
| I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, |
| 78 |
|
| I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, |
| 79 |
|
| I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, |
| 80 |
|
| I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, |
| 81 |
|
| I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, |
| 82 |
|
| I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, |
| 83 |
|
| I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, |
| 84 |
|
| I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, |
| 85 |
|
| I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, |
| 86 |
|
| I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, |
| 87 |
|
| I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH, I_CH |
| 88 |
|
| }; |
| 89 |
|
| |
| 90 |
|
| typedef enum state { |
| 91 |
|
| S_FN = 0x00, // first char in normal key |
| 92 |
|
| S_RN = 0x01, // reading normal key |
| 93 |
|
| S_FK = 0x02, // first char of key inside {} |
| 94 |
|
| S_RK = 0x03, // reading key inside {} |
| 95 |
|
| S_FI = 0x04, // first digit of index inside [], checking auto-array |
| 96 |
|
| S_RI = 0x05, // reading index inside [] |
| 97 |
|
| S_RC = 0x06, // reading controll . [ or { |
| 98 |
|
| S_EN = 0x07, // end state |
| 99 |
|
| S_E1 = 0x08, // error: delimeter not balanced |
| 100 |
|
| S_E2 = 0x09, // error: index should be a number |
| 101 |
|
| S_E3 = 0x0a, // error: zero-length key name |
| 102 |
|
| S_E4 = 0x0b, // error: unexpected controll char |
| 103 |
|
| S_E5 = 0x0c, // error: type mismatch - array found |
| 104 |
|
| S_E6 = 0x0d, // error: type mismatch - hash found |
| 105 |
|
| } State; |
| 106 |
|
| |
| 107 |
|
| static U32 machine[][8] = { |
| 108 |
|
| /* I_DT, I_LS, I_RS, I_LC, I_RC, I_DI, I_CH, I_EN |
| 109 |
|
| /*S_FN*/ { S_E3, S_E3, S_E3, S_E3, S_E3, S_RN|A_EC, S_RN|A_EC, S_E3 }, |
| 110 |
|
| /*S_RN*/ { S_FN|A_CH, S_FI|A_CA, S_RN|A_EC, S_FK|A_CH, S_RN|A_EC, S_RN|A_EC, S_RN|A_EC, S_EN|A_CV }, |
| 111 |
|
| /*S_FK*/ { S_E1, S_E1, S_E1, S_E1, S_E3, S_RK|A_EC, S_RK|A_EC, S_E1 }, |
| 112 |
|
| /*S_RK*/ { S_E1, S_E1, S_E1, S_E1, S_RC, S_RK|A_EC, S_RK|A_EC, S_E1 }, |
| 113 |
|
| /*S_FI*/ { S_E2, S_E1, S_EN|A_AA, S_E1, S_E1, S_RI|A_ED, S_E2, S_E1 }, |
| 114 |
|
| /*S_RI*/ { S_E2, S_E1, S_RC, S_E1, S_E1, S_RI|A_ED, S_E2, S_E1 }, |
| 115 |
|
| /*S_RC*/ { S_FN|A_CH, S_FI|A_CA, S_E4, S_FK|A_CH, S_E4, S_E4, S_E4, S_EN|A_CV } |
| 116 |
|
| }; |
| 117 |
|
| |
| 118 |
|
| static SV* |
| 119 |
132 |
| _dclone(SV* sv) |
| 120 |
|
| { |
| 121 |
|
| I32 count; |
| 122 |
|
| SV* res; |
| 123 |
132 |
| dSP; |
| 124 |
132 |
▶ | if (!SvROK(sv)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvROK(sv)) { |
122 |
10 |
132 |
|
| 125 |
122 |
| return sv_mortalcopy(sv); |
| 126 |
|
| } |
| 127 |
10 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
10 |
10 |
|
| 128 |
10 |
▶ | XPUSHs(sv); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv); |
0 |
10 |
10 |
|
| 129 |
10 |
| PUTBACK; |
| 130 |
10 |
| count = call_pv("Storable::dclone", G_SCALAR); |
| 131 |
10 |
▶ | if (count != 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (count != 1) { |
0 |
10 |
10 |
|
| 132 |
0 |
| croak("Storable::dclone call failed\n"); |
| 133 |
|
| } |
| 134 |
10 |
| SPAGAIN; |
| 135 |
10 |
| res = POPs; |
| 136 |
10 |
| PUTBACK; |
| 137 |
10 |
| return res; |
| 138 |
|
| } |
| 139 |
|
| |
| 140 |
|
| static SV* |
| 141 |
87 |
| _split(SV* val, Opts* opts) |
| 142 |
|
| { |
| 143 |
87 |
| char* beg = SvPVX(val); |
| 144 |
87 |
| char* end = SvEND(val); |
| 145 |
87 |
| char* zer = (char*) memchr(beg, '\0', SvCUR(val)); |
| 146 |
87 |
▶ | if (zer && zer < end) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (zer && zer < end) { |
8 |
79 |
87 |
| if (zer && zer < end) { |
8 |
0 |
8 |
|
| 147 |
8 |
| AV* arr = newAV(); |
| 148 |
|
| do { |
| 149 |
13 |
| av_push(arr, newSVpvn_utf8(beg, zer - beg, SvUTF8(val))); |
| 150 |
13 |
| beg = zer + 1; |
| 151 |
13 |
| zer = memchr(beg, '\0', end - beg); |
| 152 |
13 |
▶ | } while (zer && zer < end); |
Branch: 2 branches
| Branch | True | False | Total |
| } while (zer && zer < end); |
5 |
8 |
13 |
| } while (zer && zer < end); |
5 |
0 |
5 |
|
| 153 |
8 |
▶ | if (beg <= end) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (beg <= end) { |
8 |
0 |
8 |
|
| 154 |
8 |
| av_push(arr, newSVpvn_utf8(beg, end - beg, SvUTF8(val))); |
| 155 |
|
| } |
| 156 |
8 |
| val = newRV_noinc((SV*)arr); |
| 157 |
8 |
| sv_2mortal(val); |
| 158 |
|
| } |
| 159 |
87 |
| return val; |
| 160 |
|
| } |
| 161 |
|
| |
| 162 |
|
| static void |
| 163 |
10 |
| _copy_array(AV* tgt, AV* src) |
| 164 |
|
| { |
| 165 |
10 |
| U32 i = 0; |
| 166 |
10 |
| U32 l = av_len(src); // last index |
| 167 |
10 |
| av_fill(tgt, l); |
| 168 |
31 |
▶ | for (i = 0; i <= l; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= l; i++) { |
21 |
10 |
31 |
|
| 169 |
|
| // TODO: optimize copying |
| 170 |
21 |
| SV** el = av_fetch(src, i, 0); |
| 171 |
21 |
▶ | if (el) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (el) { |
21 |
0 |
21 |
|
| 172 |
21 |
| SvREFCNT_inc(*el); |
| 173 |
21 |
| av_store(tgt, i, *el); |
| 174 |
|
| } |
| 175 |
|
| } |
| 176 |
10 |
| } |
| 177 |
|
| |
| 178 |
|
| static void |
| 179 |
310 |
| _store(void* ptr, const char* part_key, U32 part_klen, U32 part_idx, SV* val, Opts* opts) |
| 180 |
|
| { |
| 181 |
310 |
▶ | if (SvTYPE((SV*)ptr) == SVt_PVHV) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvTYPE((SV*)ptr) == SVt_PVHV) { |
223 |
87 |
310 |
|
| 182 |
223 |
▶ | DBG("hv_store ptr %p part_key '%s' part_klen %u val %p (type %u)\n", ptr, part_key, part_klen, val, SvTYPE(val)); |
Branch: 1 branch
| Branch | True | False | Total |
| DBG("hv_store ptr %p part_key '%s' part_klen %u val %p (type %u)\n", ptr, part_key, part_klen, val, SvTYPE(val)); |
0 |
223 |
223 |
|
| 183 |
223 |
| hv_store((HV*)ptr, part_key, part_klen, val, 0); |
| 184 |
|
| } |
| 185 |
|
| else { |
| 186 |
87 |
▶ | DBG("av_store ptr %p part_idx %u val %p (type %u)\n", ptr, part_idx, val, SvTYPE(val)); |
Branch: 1 branch
| Branch | True | False | Total |
| DBG("av_store ptr %p part_idx %u val %p (type %u)\n", ptr, part_idx, val, SvTYPE(val)); |
0 |
87 |
87 |
|
| 187 |
87 |
| av_store((AV*)ptr, part_idx, val); |
| 188 |
|
| } |
| 189 |
310 |
| } |
| 190 |
|
| |
| 191 |
|
| static SV* |
| 192 |
280 |
| _next(void* ptr, const char* part_key, U32 part_klen, U32 part_idx, svtype type, Opts* opts) |
| 193 |
|
| { |
| 194 |
|
| SV** ref_ptr; |
| 195 |
|
| SV* next; |
| 196 |
280 |
▶ | if (SvTYPE((SV*)ptr) == SVt_PVHV) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvTYPE((SV*)ptr) == SVt_PVHV) { |
241 |
39 |
280 |
|
| 197 |
241 |
| ref_ptr = hv_fetch((HV*)ptr, part_key, part_klen, 0); |
| 198 |
|
| } |
| 199 |
|
| else { |
| 200 |
39 |
| ref_ptr = av_fetch((AV*)ptr, part_idx, 0); |
| 201 |
|
| } |
| 202 |
280 |
▶ | if (!ref_ptr) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ref_ptr) { |
123 |
157 |
280 |
|
| 203 |
123 |
▶ | next = type == SVt_PVHV ? (SV*)newHV() : (SV*)newAV(); |
Branch: 1 branch
| Branch | True | False | Total |
| next = type == SVt_PVHV ? (SV*)newHV() : (SV*)newAV(); |
65 |
58 |
123 |
|
| 204 |
123 |
| _store(ptr, part_key, part_klen, part_idx, newRV_noinc((SV*)next), opts); |
| 205 |
|
| } |
| 206 |
|
| else { |
| 207 |
157 |
▶ | if (SvROK(*ref_ptr) && SvTYPE(SvRV(*ref_ptr)) == type) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(*ref_ptr) && SvTYPE(SvRV(*ref_ptr)) == type) { |
157 |
0 |
157 |
| if (SvROK(*ref_ptr) && SvTYPE(SvRV(*ref_ptr)) == type) { |
156 |
1 |
157 |
|
| 208 |
156 |
| next = SvRV(*ref_ptr); |
| 209 |
|
| } |
| 210 |
|
| else { |
| 211 |
1 |
| return NULL; |
| 212 |
|
| } |
| 213 |
|
| } |
| 214 |
279 |
| return next; |
| 215 |
|
| } |
| 216 |
|
| |
| 217 |
|
| static void |
| 218 |
214 |
| _handle_pair(const unsigned char* key, U32 klen, SV* val, AV* err, Opts* opts, HV* ov) |
| 219 |
|
| { |
| 220 |
214 |
| U32 pos = 0; |
| 221 |
214 |
| U32 mv = 0; |
| 222 |
214 |
| U32 ac = 0; |
| 223 |
214 |
| Input inp = I_CH; |
| 224 |
214 |
| State st = S_FN; |
| 225 |
|
| |
| 226 |
214 |
| U32 part_idx = 0; |
| 227 |
214 |
| const unsigned char* part_key = key; |
| 228 |
214 |
| U32 part_klen = 0; |
| 229 |
|
| |
| 230 |
214 |
| void* ptr = ov; |
| 231 |
|
| |
| 232 |
214 |
▶ | DBG("key '%s' klen %u\n", key, klen); |
Branch: 1 branch
| Branch | True | False | Total |
| DBG("key '%s' klen %u\n", key, klen); |
0 |
214 |
214 |
|
| 233 |
|
| |
| 234 |
1612 |
▶ | for (pos = 0; pos <= klen && st < S_EN; pos++) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (pos = 0; pos <= klen && st < S_EN; pos++) { |
1432 |
180 |
1612 |
| for (pos = 0; pos <= klen && st < S_EN; pos++) { |
1398 |
34 |
1432 |
|
| 235 |
1398 |
▶ | DBG("chr %c %u\n", key[pos], key[pos]); |
Branch: 1 branch
| Branch | True | False | Total |
| DBG("chr %c %u\n", key[pos], key[pos]); |
0 |
1398 |
1398 |
|
| 236 |
1398 |
▶ | DBG("class %u\n", classes[key[pos]]); |
Branch: 1 branch
| Branch | True | False | Total |
| DBG("class %u\n", classes[key[pos]]); |
0 |
1398 |
1398 |
|
| 237 |
|
| |
| 238 |
1398 |
▶ | inp = pos == klen ? I_EN : classes[key[pos]]; |
Branch: 1 branch
| Branch | True | False | Total |
| inp = pos == klen ? I_EN : classes[key[pos]]; |
1218 |
180 |
1398 |
|
| 239 |
1398 |
▶ | if (inp == I_DT && opts->nodot) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (inp == I_DT && opts->nodot) { |
50 |
1348 |
1398 |
| if (inp == I_DT && opts->nodot) { |
8 |
42 |
50 |
|
| 240 |
8 |
| inp = I_CH; |
| 241 |
|
| } |
| 242 |
1398 |
| mv = machine[st][inp]; |
| 243 |
1398 |
▶ | DBG("st %u pos %u chr '%c(%u)' inp %u -> st %u\n", st, pos, key[pos], (int)key[pos], inp, mv & STATE_MASK); |
Branch: 1 branch
| Branch | True | False | Total |
| DBG("st %u pos %u chr '%c(%u)' inp %u -> st %u\n", st, pos, key[pos], (int)key[pos], inp, mv & STATE_MASK); |
0 |
1398 |
1398 |
|
| 244 |
1398 |
| st = mv & STATE_MASK; |
| 245 |
1398 |
| ac = mv & ACTION_MASK; |
| 246 |
|
| |
| 247 |
1398 |
| switch (ac) { |
| 248 |
589 |
| case A_EC: |
| 249 |
589 |
| part_klen++; |
| 250 |
589 |
| break; |
| 251 |
104 |
| case A_ED: |
| 252 |
104 |
| part_idx = part_idx * 10 + key[pos] - '0'; |
| 253 |
104 |
| break; |
| 254 |
156 |
| case A_CH: |
| 255 |
156 |
| ptr = _next(ptr, part_key, part_klen, part_idx, SVt_PVHV, opts); |
| 256 |
156 |
▶ | if (!ptr) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ptr) { |
1 |
155 |
156 |
|
| 257 |
1 |
| st = S_E5; |
| 258 |
|
| } |
| 259 |
156 |
| part_key = key + pos + 1; |
| 260 |
156 |
| part_klen = 0; |
| 261 |
156 |
| part_idx = 0; |
| 262 |
156 |
| break; |
| 263 |
124 |
| case A_CA: |
| 264 |
124 |
| ptr = _next(ptr, part_key, part_klen, part_idx, SVt_PVAV, opts); |
| 265 |
124 |
▶ | if (!ptr) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ptr) { |
0 |
124 |
124 |
|
| 266 |
0 |
| st = S_E6; |
| 267 |
|
| } |
| 268 |
124 |
| part_key = key + pos + 1; |
| 269 |
124 |
| part_klen = 0; |
| 270 |
124 |
| part_idx = 0; |
| 271 |
124 |
| break; |
| 272 |
178 |
| case A_CV: |
| 273 |
178 |
▶ | if (opts->nullsplit && SvPOK(val)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (opts->nullsplit && SvPOK(val)) { |
114 |
64 |
178 |
| if (opts->nullsplit && SvPOK(val)) { |
87 |
27 |
114 |
|
| 274 |
87 |
| val = _split(val, opts); |
| 275 |
|
| } |
| 276 |
178 |
| SvREFCNT_inc(val); |
| 277 |
178 |
| _store(ptr, part_key, part_klen, part_idx, val, opts); |
| 278 |
178 |
| break; |
| 279 |
19 |
| case A_AA: |
| 280 |
19 |
▶ | if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVAV) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVAV) { |
10 |
9 |
19 |
| if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVAV) { |
10 |
0 |
10 |
|
| 281 |
10 |
| _copy_array((AV*) ptr, (AV*) SvRV(val)); |
| 282 |
|
| } |
| 283 |
|
| else { |
| 284 |
9 |
| SvREFCNT_inc(val); |
| 285 |
9 |
| _store(ptr, part_key, part_klen, part_idx, val, opts); |
| 286 |
|
| } |
| 287 |
19 |
| break; |
| 288 |
|
| } |
| 289 |
|
| } |
| 290 |
214 |
▶ | DBG("final state %u\n\n", st); |
Branch: 1 branch
| Branch | True | False | Total |
| DBG("final state %u\n\n", st); |
0 |
214 |
214 |
|
| 291 |
|
| |
| 292 |
|
| // normal return |
| 293 |
214 |
▶ | if (st == S_EN) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (st == S_EN) { |
197 |
17 |
214 |
|
| 294 |
197 |
| return; |
| 295 |
|
| } |
| 296 |
|
| |
| 297 |
|
| // error handling if needed |
| 298 |
17 |
▶ | if (err) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (err) { |
17 |
0 |
17 |
|
| 299 |
|
| char msg[BUF_LEN]; |
| 300 |
17 |
| char* key_prefix = NULL; |
| 301 |
|
| # define ERR(fmt, ...) snprintf(msg, sizeof(msg), fmt, ##__VA_ARGS__); |
| 302 |
17 |
| switch (st) { |
| 303 |
0 |
| case S_EN: |
| 304 |
0 |
| break; |
| 305 |
5 |
| case S_E1: |
| 306 |
5 |
| ERR("Not balanced delimiter for %s", key); |
| 307 |
5 |
| break; |
| 308 |
2 |
| case S_E2: |
| 309 |
2 |
| ERR("Array index should be a number for %s", key); |
| 310 |
2 |
| break; |
| 311 |
5 |
| case S_E3: |
| 312 |
5 |
▶ | if (pos == 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pos == 1) { |
1 |
4 |
5 |
|
| 313 |
1 |
| ERR("Unexpected initial char '%c' for %s", key[0], key); |
| 314 |
|
| } |
| 315 |
|
| else { |
| 316 |
4 |
| ERR("Zero-length key name for %s", key); |
| 317 |
|
| } |
| 318 |
5 |
| break; |
| 319 |
4 |
| case S_E4: |
| 320 |
4 |
| ERR("Delimeter expected at %s for %s", key + pos, key); |
| 321 |
4 |
| break; |
| 322 |
1 |
| case S_E5: |
| 323 |
|
| case S_E6: |
| 324 |
1 |
| key_prefix = (char*) malloc(pos); |
| 325 |
1 |
| strncpy(key_prefix, key, pos - 1); |
| 326 |
1 |
| key_prefix[pos-1] = '\0'; |
| 327 |
1 |
▶ | ERR("Type mismatch: %s already used as %s for %s", key_prefix, (st == S_E5 ? "ArrayRef" : "HashRef"), key); |
Branch: 1 branch
| Branch | True | False | Total |
| ERR("Type mismatch: %s already used as %s for %s", key_prefix, (st == S_E5 ? "ArrayRef" : "HashRef"), key); |
1 |
0 |
1 |
|
| 328 |
1 |
| free(key_prefix); |
| 329 |
1 |
| break; |
| 330 |
0 |
| default: |
| 331 |
0 |
| ERR("Internal: unexpected final state %u for %s", st, key); |
| 332 |
0 |
| break; |
| 333 |
|
| } |
| 334 |
|
| # undef ERR |
| 335 |
17 |
| av_push(err, newSVpv((msg), 0)); |
| 336 |
|
| } |
| 337 |
|
| |
| 338 |
17 |
| return; |
| 339 |
|
| } |
| 340 |
|
| |
| 341 |
|
| |
| 342 |
|
| MODULE = CGI::Struct::XS PACKAGE = CGI::Struct::XS |
| 343 |
|
| |
| 344 |
|
| PROTOTYPES: DISABLE |
| 345 |
|
| |
| 346 |
|
| HV* |
| 347 |
|
| build_cgi_struct(HV* iv, ...) |
| 348 |
|
| PREINIT: |
| 349 |
31 |
| AV* err = NULL; |
| 350 |
31 |
| HV* conf = NULL; |
| 351 |
31 |
| Opts opts = { .nodot = 0, .nullsplit = 1, .dclone = 1, .debug = 0 }; |
| 352 |
31 |
| HE* pair = NULL; |
| 353 |
31 |
| char* key = NULL; |
| 354 |
31 |
| U32 klen = 0; |
| 355 |
31 |
| SV* val = NULL; |
| 356 |
|
| CODE: |
| 357 |
|
| /* prepare args */ |
| 358 |
31 |
▶ | if (items > 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 1) { |
31 |
0 |
31 |
|
| 359 |
31 |
| SV* const xsub_tmp_sv = ST(1); |
| 360 |
31 |
| SvGETMAGIC(xsub_tmp_sv); |
| 361 |
31 |
▶ | if (SvROK(xsub_tmp_sv) && SvTYPE(SvRV(xsub_tmp_sv)) == SVt_PVAV) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(xsub_tmp_sv) && SvTYPE(SvRV(xsub_tmp_sv)) == SVt_PVAV) { |
31 |
0 |
31 |
| if (SvROK(xsub_tmp_sv) && SvTYPE(SvRV(xsub_tmp_sv)) == SVt_PVAV) { |
31 |
0 |
31 |
|
| 362 |
31 |
| err = (AV*) SvRV(xsub_tmp_sv); |
| 363 |
|
| } |
| 364 |
|
| } |
| 365 |
31 |
▶ | if (items > 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 2) { |
14 |
17 |
31 |
|
| 366 |
14 |
| SV* const xsub_tmp_sv = ST(2); |
| 367 |
14 |
| SvGETMAGIC(xsub_tmp_sv); |
| 368 |
14 |
▶ | if (SvROK(xsub_tmp_sv) && SvTYPE(SvRV(xsub_tmp_sv)) == SVt_PVHV) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(xsub_tmp_sv) && SvTYPE(SvRV(xsub_tmp_sv)) == SVt_PVHV) { |
14 |
0 |
14 |
| if (SvROK(xsub_tmp_sv) && SvTYPE(SvRV(xsub_tmp_sv)) == SVt_PVHV) { |
14 |
0 |
14 |
|
| 369 |
14 |
| conf = (HV*) SvRV(xsub_tmp_sv); |
| 370 |
14 |
▶ | opts.nodot = CONF_GET(conf, "nodot", opts.nodot); |
Branch: 1 branch
| Branch | True | False | Total |
| opts.nodot = CONF_GET(conf, "nodot", opts.nodot); |
9 |
5 |
14 |
|
| 371 |
14 |
▶ | opts.nullsplit = CONF_GET(conf, "nullsplit", opts.nullsplit); |
Branch: 1 branch
| Branch | True | False | Total |
| opts.nullsplit = CONF_GET(conf, "nullsplit", opts.nullsplit); |
12 |
2 |
14 |
|
| 372 |
14 |
▶ | opts.dclone = CONF_GET(conf, "dclone", opts.dclone); |
Branch: 1 branch
| Branch | True | False | Total |
| opts.dclone = CONF_GET(conf, "dclone", opts.dclone); |
9 |
5 |
14 |
|
| 373 |
14 |
▶ | opts.debug = CONF_GET(conf, "debug", opts.debug); |
Branch: 1 branch
| Branch | True | False | Total |
| opts.debug = CONF_GET(conf, "debug", opts.debug); |
0 |
14 |
14 |
|
| 374 |
|
| } |
| 375 |
|
| } |
| 376 |
|
| |
| 377 |
|
| /* prepare output */ |
| 378 |
31 |
| RETVAL = newHV(); |
| 379 |
31 |
| sv_2mortal((SV*)RETVAL); |
| 380 |
|
| |
| 381 |
|
| /* main loop */ |
| 382 |
31 |
| hv_iterinit(iv); |
| 383 |
245 |
▶ | while (pair = hv_iternext(iv)) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (pair = hv_iternext(iv)) { |
214 |
31 |
245 |
|
| 384 |
214 |
| key = hv_iterkey(pair, &klen); |
| 385 |
214 |
| val = hv_iterval(iv, pair); |
| 386 |
214 |
▶ | if (opts.dclone) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (opts.dclone) { |
132 |
82 |
214 |
|
| 387 |
132 |
| val = _dclone(val); |
| 388 |
|
| } |
| 389 |
214 |
| _handle_pair(key, klen, val, err, &opts, RETVAL); |
| 390 |
|
| } |
| 391 |
|
| OUTPUT: |
| 392 |
|
| RETVAL |