| 1 |
|
| #ifndef TKR_SET_SV_H |
| 2 |
|
| #define TKR_SET_SV_H |
| 3 |
|
| |
| 4 |
|
| /* The set's ends in Perl: values out, dates in from with(), the words for |
| 5 |
|
| * a set that cannot go on, and the iterator's state on its code ref. */ |
| 6 |
|
| |
| 7 |
|
| static int32_t |
| 8 |
9535244 |
| tkr_set_nsec(const tkr_set *s) |
| 9 |
|
| { |
| 10 |
9535244 |
▶ | return s->has_start ? s->start.nsec : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| return s->has_start ? s->start.nsec : 0; |
9535242 |
2 |
9535244 |
|
| 11 |
|
| } |
| 12 |
|
| |
| 13 |
|
| static SV * |
| 14 |
9515239 |
| tkr_set_value_sv(pTHX_ const tkr_set *s, int64_t sec, const char *what) |
| 15 |
|
| { |
| 16 |
|
| time_keeper_instant t; |
| 17 |
|
| SV *sv; |
| 18 |
9515239 |
| t.sec = sec; |
| 19 |
9515239 |
| t.nsec = tkr_set_nsec(s); |
| 20 |
9515239 |
▶ | if (s->n_rules && (tkr_set_rules(s)[0].flags & TKR_F_RSCALE)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (s->n_rules && (tkr_set_rules(s)[0].flags & TKR_F_RSCALE)) |
9515238 |
1 |
9515239 |
| if (s->n_rules && (tkr_set_rules(s)[0].flags & TKR_F_RSCALE)) |
27 |
9515211 |
9515238 |
|
| 21 |
27 |
| sv = (tkr_tk(aTHX)->new_sv_cal)(aTHX_ &t, &s->zone, s->locale, tkr_set_rules(s)[0].calendar); |
| 22 |
|
| else |
| 23 |
9515212 |
| sv = (tkr_tk(aTHX)->new_sv)(aTHX_ &t, &s->zone, s->locale); |
| 24 |
9515239 |
▶ | if (!sv) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sv) |
0 |
9515239 |
9515239 |
|
| 25 |
0 |
| croak("%s: an occurrence outside the years Time::Keeper holds", what); |
| 26 |
9515239 |
| return sv; |
| 27 |
|
| } |
| 28 |
|
| |
| 29 |
|
| /* A whole day as a Time::Keeper::Date. */ |
| 30 |
|
| static SV * |
| 31 |
7 |
| tkr_date_sv(pTHX_ int64_t days, int locale) |
| 32 |
|
| { |
| 33 |
7 |
| dSP; |
| 34 |
7 |
| SV *got = NULL; |
| 35 |
|
| int n; |
| 36 |
|
| PERL_UNUSED_VAR(locale); |
| 37 |
7 |
| ENTER; |
| 38 |
7 |
| SAVETMPS; |
| 39 |
7 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
7 |
7 |
|
| 40 |
7 |
▶ | XPUSHs(sv_2mortal(newSVpvs("Time::Keeper::Date"))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVpvs("Time::Keeper::Date"))); |
0 |
7 |
7 |
|
| 41 |
7 |
▶ | XPUSHs(sv_2mortal(newSViv((IV)days))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSViv((IV)days))); |
0 |
7 |
7 |
|
| 42 |
7 |
| PUTBACK; |
| 43 |
7 |
| n = call_method("from_epoch_days", G_SCALAR); |
| 44 |
7 |
| SPAGAIN; |
| 45 |
7 |
▶ | if (n == 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == 1) { |
7 |
0 |
7 |
|
| 46 |
7 |
| SV *r = POPs; |
| 47 |
7 |
| got = SvREFCNT_inc_simple_NN(r); |
| 48 |
|
| } |
| 49 |
7 |
| PUTBACK; |
| 50 |
7 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
7 |
0 |
7 |
|
| 51 |
7 |
| LEAVE; |
| 52 |
7 |
▶ | return got ? got : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| return got ? got : &PL_sv_undef; |
7 |
0 |
7 |
|
| 53 |
|
| } |
| 54 |
|
| |
| 55 |
|
| /* A Time::Keeper::Date's day number. */ |
| 56 |
|
| static int |
| 57 |
3094 |
| tkr_date_days_of(pTHX_ SV *sv, int64_t *days) |
| 58 |
|
| { |
| 59 |
3094 |
| dSP; |
| 60 |
3094 |
| int n, ok = 0; |
| 61 |
3094 |
▶ | if (!SvROK(sv) || !sv_derived_from(sv, "Time::Keeper::Date")) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(sv) || !sv_derived_from(sv, "Time::Keeper::Date")) |
3094 |
0 |
3094 |
| if (!SvROK(sv) || !sv_derived_from(sv, "Time::Keeper::Date")) |
3093 |
1 |
3094 |
|
| 62 |
3093 |
| return 0; |
| 63 |
1 |
| ENTER; |
| 64 |
1 |
| SAVETMPS; |
| 65 |
1 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
1 |
1 |
|
| 66 |
1 |
▶ | XPUSHs(sv); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv); |
0 |
1 |
1 |
|
| 67 |
1 |
| PUTBACK; |
| 68 |
1 |
| n = call_method("epoch_days", G_SCALAR); |
| 69 |
1 |
| SPAGAIN; |
| 70 |
1 |
▶ | if (n == 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == 1) { |
1 |
0 |
1 |
|
| 71 |
1 |
| SV *r = POPs; |
| 72 |
1 |
▶ | if (SvOK(r)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(r)) { |
1 |
0 |
1 |
|
| 73 |
1 |
| *days = (int64_t)SvIV(r); |
| 74 |
1 |
| ok = 1; |
| 75 |
|
| } |
| 76 |
|
| } |
| 77 |
1 |
| PUTBACK; |
| 78 |
1 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
1 |
0 |
1 |
|
| 79 |
1 |
| LEAVE; |
| 80 |
1 |
| return ok; |
| 81 |
|
| } |
| 82 |
|
| |
| 83 |
|
| /* One value to with(): a date added or removed (a Time::Keeper value, a |
| 84 |
|
| * Time::Keeper::Date or a YYYY-MM-DD string for a whole day), or a rule. */ |
| 85 |
|
| static void |
| 86 |
4245 |
| tkr_with_one(pTHX_ tkr_set_parts *p, int ex, SV *v, const char *what) |
| 87 |
|
| { |
| 88 |
|
| time_keeper_instant in; |
| 89 |
|
| int64_t days; |
| 90 |
|
| STRLEN len; |
| 91 |
|
| const char *s; |
| 92 |
4245 |
▶ | if (ex == 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ex == 2) { |
317 |
3928 |
4245 |
|
| 93 |
|
| const tkr_rule *r; |
| 94 |
317 |
▶ | if (!v || !SvROK(v) || !sv_derived_from(v, "Time::Keeper::Recurrence") || sv_derived_from(v, "Time::Keeper::Recurrence::Set")) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (!v || !SvROK(v) || !sv_derived_from(v, "Time::Keeper::Recurrence") || sv_derived_from(v, "Time::Keeper::Recurrence::Set")) { |
317 |
0 |
317 |
| if (!v || !SvROK(v) || !sv_derived_from(v, "Time::Keeper::Recurrence") || sv_derived_from(v, "Time::Keeper::Recurrence::Set")) { |
317 |
0 |
317 |
| if (!v || !SvROK(v) || !sv_derived_from(v, "Time::Keeper::Recurrence") || sv_derived_from(v, "Time::Keeper::Recurrence::Set")) { |
316 |
1 |
317 |
| if (!v || !SvROK(v) || !sv_derived_from(v, "Time::Keeper::Recurrence") || sv_derived_from(v, "Time::Keeper::Recurrence::Set")) { |
0 |
316 |
316 |
|
| 95 |
1 |
| tkr_parts_free(p); |
| 96 |
1 |
| croak("%s: rule takes Time::Keeper::Recurrence rules", what); |
| 97 |
|
| } |
| 98 |
316 |
| r = tkr_rule_of(aTHX_ v, what); |
| 99 |
316 |
| tkr_parts_join_rule(aTHX_ p, r, what); |
| 100 |
313 |
| return; |
| 101 |
|
| } |
| 102 |
3928 |
▶ | if (v && SvROK(v) && tkr_date_days_of(aTHX_ v, &days)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (v && SvROK(v) && tkr_date_days_of(aTHX_ v, &days)) { |
3928 |
0 |
3928 |
| if (v && SvROK(v) && tkr_date_days_of(aTHX_ v, &days)) { |
3094 |
834 |
3928 |
| if (v && SvROK(v) && tkr_date_days_of(aTHX_ v, &days)) { |
1 |
3093 |
3094 |
|
| 103 |
1 |
| tkr_parts_add_date(aTHX_ p, ex, days * 86400, 1, what); |
| 104 |
1 |
| return; |
| 105 |
|
| } |
| 106 |
3927 |
▶ | if (v && SvROK(v) && (tkr_tk(aTHX)->from_sv)(aTHX_ v, &in, NULL, NULL) == TIME_KEEPER_OK) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (v && SvROK(v) && (tkr_tk(aTHX)->from_sv)(aTHX_ v, &in, NULL, NULL) == TIME_KEEPER_OK) { |
3927 |
0 |
3927 |
| if (v && SvROK(v) && (tkr_tk(aTHX)->from_sv)(aTHX_ v, &in, NULL, NULL) == TIME_KEEPER_OK) { |
3093 |
834 |
3927 |
| if (v && SvROK(v) && (tkr_tk(aTHX)->from_sv)(aTHX_ v, &in, NULL, NULL) == TIME_KEEPER_OK) { |
3093 |
0 |
3093 |
|
| 107 |
3093 |
| tkr_parts_add_raw(aTHX_ p, ex, in.sec, 0, what); |
| 108 |
3093 |
| return; |
| 109 |
|
| } |
| 110 |
834 |
▶ | if (v && SvOK(v) && !SvROK(v)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (v && SvOK(v) && !SvROK(v)) { |
834 |
0 |
834 |
| if (v && SvOK(v) && !SvROK(v)) { |
834 |
0 |
834 |
| if (v && SvOK(v) && !SvROK(v)) { |
834 |
0 |
834 |
|
| 111 |
|
| int64_t wall; |
| 112 |
|
| int kind; |
| 113 |
|
| char compact[9]; |
| 114 |
834 |
| s = SvPV(v, len); |
| 115 |
834 |
▶ | if (len == 10 && s[4] == '-' && s[7] == '-') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (len == 10 && s[4] == '-' && s[7] == '-') { |
833 |
1 |
834 |
| if (len == 10 && s[4] == '-' && s[7] == '-') { |
833 |
0 |
833 |
| if (len == 10 && s[4] == '-' && s[7] == '-') { |
833 |
0 |
833 |
|
| 116 |
833 |
| memcpy(compact, s, 4); memcpy(compact + 4, s + 5, 2); memcpy(compact + 6, s + 8, 2); |
| 117 |
833 |
▶ | if (tkr_ical_value(compact, 8, &wall, &kind)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tkr_ical_value(compact, 8, &wall, &kind)) { |
833 |
0 |
833 |
|
| 118 |
833 |
| tkr_parts_add_date(aTHX_ p, ex, wall, 1, what); |
| 119 |
833 |
| return; |
| 120 |
|
| } |
| 121 |
|
| } |
| 122 |
|
| } |
| 123 |
1 |
| tkr_parts_free(p); |
| 124 |
1 |
▶ | croak("%s: %s takes Time::Keeper values, Time::Keeper::Date values or YYYY-MM-DD strings for whole days", what, ex ? "exdate" : "rdate"); |
Branch: 1 branch
| Branch | True | False | Total |
| croak("%s: %s takes Time::Keeper values, Time::Keeper::Date values or YYYY-MM-DD strings for whole days", what, ex ? "exdate" : "rdate"); |
0 |
1 |
1 |
|
| 125 |
|
| } |
| 126 |
|
| |
| 127 |
|
| /* with()'s key and value pairs, into the parts. */ |
| 128 |
|
| static void |
| 129 |
1334 |
| tkr_with_args(pTHX_ tkr_set_parts *p, SV **st, I32 n, const char *what) |
| 130 |
|
| { |
| 131 |
|
| I32 i; |
| 132 |
1334 |
▶ | if (n % 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n % 2) { |
1 |
1333 |
1334 |
|
| 133 |
1 |
| tkr_parts_free(p); |
| 134 |
1 |
| croak("%s: the arguments are key and value pairs", what); |
| 135 |
|
| } |
| 136 |
3663 |
▶ | for (i = 0; i < n; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i += 2) { |
2336 |
1327 |
3663 |
|
| 137 |
2336 |
| const char *k = SvPV_nolen(st[i]); |
| 138 |
2336 |
| SV *v = st[i + 1]; |
| 139 |
|
| int ex; |
| 140 |
2336 |
▶ | if (strEQ(k, "rdate") || strEQ(k, "rdates")) ex = 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (strEQ(k, "rdate") || strEQ(k, "rdates")) ex = 0; |
1325 |
1011 |
2336 |
| if (strEQ(k, "rdate") || strEQ(k, "rdates")) ex = 0; |
0 |
1325 |
1325 |
|
| 141 |
1325 |
▶ | else if (strEQ(k, "exdate") || strEQ(k, "exdates")) ex = 1; |
Branch: 2 branches
| Branch | True | False | Total |
| else if (strEQ(k, "exdate") || strEQ(k, "exdates")) ex = 1; |
304 |
1021 |
1325 |
| else if (strEQ(k, "exdate") || strEQ(k, "exdates")) ex = 1; |
0 |
304 |
304 |
|
| 142 |
304 |
▶ | else if (strEQ(k, "rule") || strEQ(k, "rules")) ex = 2; |
Branch: 2 branches
| Branch | True | False | Total |
| else if (strEQ(k, "rule") || strEQ(k, "rules")) ex = 2; |
2 |
302 |
304 |
| else if (strEQ(k, "rule") || strEQ(k, "rules")) ex = 2; |
1 |
1 |
2 |
|
| 143 |
|
| else { |
| 144 |
1 |
| tkr_parts_free(p); |
| 145 |
1 |
| croak("%s: unknown argument '%s'", what, k); |
| 146 |
|
| } |
| 147 |
4342 |
▶ | if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) { |
2317 |
18 |
2335 |
| if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) { |
2007 |
310 |
2317 |
|
| 148 |
2007 |
| AV *av = (AV *)SvRV(v); |
| 149 |
2007 |
| I32 j, m = av_len(av) + 1; |
| 150 |
5924 |
▶ | for (j = 0; j < m; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < m; j++) { |
3917 |
2007 |
5924 |
|
| 151 |
3917 |
| SV **e = av_fetch(av, j, 0); |
| 152 |
3917 |
▶ | tkr_with_one(aTHX_ p, ex, e ? *e : &PL_sv_undef, what); |
Branch: 1 branch
| Branch | True | False | Total |
| tkr_with_one(aTHX_ p, ex, e ? *e : &PL_sv_undef, what); |
3917 |
0 |
3917 |
|
| 153 |
|
| } |
| 154 |
|
| } |
| 155 |
|
| else |
| 156 |
328 |
| tkr_with_one(aTHX_ p, ex, v, what); |
| 157 |
|
| } |
| 158 |
1327 |
| } |
| 159 |
|
| |
| 160 |
|
| static void |
| 161 |
40018 |
| tkr_set_rc_croak(pTHX_ int rc, const tkr_set *s, int64_t wall, const char *what) |
| 162 |
|
| { |
| 163 |
|
| tkr_rule probe; |
| 164 |
40018 |
| tkr_rule_init(&probe); |
| 165 |
40018 |
| probe.zone = s->zone; |
| 166 |
40018 |
▶ | if (s->n_rules && (tkr_set_rules(s)[0].flags & TKR_F_CRON)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (s->n_rules && (tkr_set_rules(s)[0].flags & TKR_F_CRON)) |
40018 |
0 |
40018 |
| if (s->n_rules && (tkr_set_rules(s)[0].flags & TKR_F_CRON)) |
2 |
40016 |
40018 |
|
| 167 |
2 |
| probe.flags |= TKR_F_CRON; |
| 168 |
40018 |
| tkr_rc_croak(aTHX_ rc, &probe, wall, what); |
| 169 |
40018 |
| } |
| 170 |
|
| |
| 171 |
|
| static int |
| 172 |
3 |
| tkr_setiter_mg_free(pTHX_ SV *sv, MAGIC *mg) |
| 173 |
|
| { |
| 174 |
3 |
| tkr_setiter *it = (tkr_setiter *)SvPVX(mg->mg_obj); |
| 175 |
|
| PERL_UNUSED_VAR(sv); |
| 176 |
3 |
| tkr_setiter_free(aTHX_ it); |
| 177 |
3 |
| return 0; |
| 178 |
|
| } |
| 179 |
|
| |
| 180 |
|
| static MGVTBL tkr_setiter_vtbl = { 0, 0, 0, 0, tkr_setiter_mg_free, 0, 0, 0 }; |
| 181 |
|
| |
| 182 |
|
| static tkr_setiter * |
| 183 |
11 |
| tkr_setiter_of(pTHX_ CV *cv, const char *what) |
| 184 |
|
| { |
| 185 |
|
| MAGIC *mg; |
| 186 |
11 |
▶ | for (mg = SvMAGIC((SV *)cv); mg; mg = mg->mg_moremagic) |
Branch: 1 branch
| Branch | True | False | Total |
| for (mg = SvMAGIC((SV *)cv); mg; mg = mg->mg_moremagic) |
11 |
0 |
11 |
|
| 187 |
11 |
▶ | if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &tkr_setiter_vtbl) |
Branch: 2 branches
| Branch | True | False | Total |
| if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &tkr_setiter_vtbl) |
11 |
0 |
11 |
| if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &tkr_setiter_vtbl) |
11 |
0 |
11 |
|
| 188 |
11 |
| return (tkr_setiter *)SvPVX(mg->mg_obj); |
| 189 |
0 |
| croak("%s: not an iterator", what); |
| 190 |
|
| return NULL; |
| 191 |
|
| } |
| 192 |
|
| |
| 193 |
|
| #endif |