include/tkr/tkr_ical.h

filter: Statementstmt 95.8% 394 / 411 Branchbran 80.8% 364 / 450 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 88.0% 758 / 861 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/tkr/tkr_set.h ↑ Summary include/tkr/tkr_obj.h »
1 #ifndef TKR_ICAL_H
2 #define TKR_ICAL_H
3  
4 /* iCalendar text in and out: the recurrence properties of one component
5 * (DTSTART, RRULE, RDATE, EXDATE, EXRULE), folded lines unfolded, a TZID
6 * read as an IANA name or a Windows name, VALUE=DATE for whole days, a
7 * PERIOD refused. Everything else in the text is ignored. The writer gives
8 * the lines back strictly: CRLF ends, folded at 75 octets, RRULE, RDATE and
9 * EXDATE but never EXRULE, UNTIL and UTC values with Z. The frozen forms
10 * of a rule and a set (FREEZE, Storable) are text too, and defined here:
11 * a rule is one line, a set is a header line and its iCalendar lines with
12 * the EXRULEs kept and every cron written as an X-TKR-CRON line. */
13  
14 #define TKR_ICAL_FOLD 75
15  
16 typedef struct tkr_ical_line {
17 const char *name, *params, *value;
18 size_t nlen, plen, vlen;
19 } tkr_ical_line;
20  
21 /* The text with its folds removed, in a new buffer freed with the scope. */
22 static char *
23 150 tkr_ical_unfold(pTHX_ const char *s, size_t len, size_t *outlen)
24 {
25 char *buf, *w;
26 size_t i;
27 150 Newx(buf, len + 1, char);
28 150 w = buf;
29 15274 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 15124 150 15274
30 15124 ▶ if (s[i] == '\r' && i + 2 < len && s[i + 1] == '\n' && (s[i + 2] == ' ' || s[i + 2] == '\t')) { i += 2; continue; }
Branch: 5 branches
BranchTrueFalseTotal
if (s[i] == '\r' && i + 2 < len && s[i + 1] == '\n' && (s[i + 2] == ' ' || s[i + 2] == '\t')) { i += 2; continue; } 360 14764 15124
if (s[i] == '\r' && i + 2 < len && s[i + 1] == '\n' && (s[i + 2] == ' ' || s[i + 2] == '\t')) { i += 2; continue; } 213 147 360
if (s[i] == '\r' && i + 2 < len && s[i + 1] == '\n' && (s[i + 2] == ' ' || s[i + 2] == '\t')) { i += 2; continue; } 213 0 213
if (s[i] == '\r' && i + 2 < len && s[i + 1] == '\n' && (s[i + 2] == ' ' || s[i + 2] == '\t')) { i += 2; continue; } 192 21 213
if (s[i] == '\r' && i + 2 < len && s[i + 1] == '\n' && (s[i + 2] == ' ' || s[i + 2] == '\t')) { i += 2; continue; } 0 192 192
31 15103 ▶ if (s[i] == '\n' && i + 1 < len && (s[i + 1] == ' ' || s[i + 1] == '\t')) { i += 1; continue; }
Branch: 4 branches
BranchTrueFalseTotal
if (s[i] == '\n' && i + 1 < len && (s[i + 1] == ' ' || s[i + 1] == '\t')) { i += 1; continue; } 350 14753 15103
if (s[i] == '\n' && i + 1 < len && (s[i + 1] == ' ' || s[i + 1] == '\t')) { i += 1; continue; } 202 148 350
if (s[i] == '\n' && i + 1 < len && (s[i + 1] == ' ' || s[i + 1] == '\t')) { i += 1; continue; } 202 0 202
if (s[i] == '\n' && i + 1 < len && (s[i + 1] == ' ' || s[i + 1] == '\t')) { i += 1; continue; } 1 201 202
32 15102 *w++ = s[i];
33 }
34 150 *w = 0;
35 150 *outlen = (size_t)(w - buf);
36 150 return buf;
37 }
38  
39 /* The next content line: name, parameters (between the first ; and the :
40 * outside quotes) and value. 0 at the end. */
41 static int
42 950 tkr_ical_next_line(const char *s, size_t len, size_t *pos, tkr_ical_line *l)
43 {
44 950 size_t i = *pos, start, colon = 0, semi = 0;
45 950 int quoted = 0;
46 2280 ▶ while (i < len && (s[i] == '\r' || s[i] == '\n')) i++;
Branch: 3 branches
BranchTrueFalseTotal
while (i < len && (s[i] == '\r' || s[i] == '\n')) i++; 1996 284 2280
while (i < len && (s[i] == '\r' || s[i] == '\n')) i++; 655 1341 1996
while (i < len && (s[i] == '\r' || s[i] == '\n')) i++; 675 666 1341
47 950 ▶ if (i >= len) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= len) return 0; 284 666 950
48 666 start = i;
49 29246 ▶ while (i < len && s[i] != '\r' && s[i] != '\n') {
Branch: 3 branches
BranchTrueFalseTotal
while (i < len && s[i] != '\r' && s[i] != '\n') { 29244 2 29246
while (i < len && s[i] != '\r' && s[i] != '\n') { 28584 660 29244
while (i < len && s[i] != '\r' && s[i] != '\n') { 28580 4 28584
50 28580 ▶ if (s[i] == '"') quoted = !quoted;
Branch: 1 branch
BranchTrueFalseTotal
if (s[i] == '"') quoted = !quoted; 8 28572 28580
51 28572 ▶ else if (!quoted && s[i] == ':' && !colon) colon = i;
Branch: 3 branches
BranchTrueFalseTotal
else if (!quoted && s[i] == ':' && !colon) colon = i; 28528 44 28572
else if (!quoted && s[i] == ':' && !colon) colon = i; 668 27860 28528
else if (!quoted && s[i] == ':' && !colon) colon = i; 666 2 668
52 27906 ▶ else if (!quoted && s[i] == ';' && !semi && !colon) semi = i;
Branch: 4 branches
BranchTrueFalseTotal
else if (!quoted && s[i] == ';' && !semi && !colon) semi = i; 27862 44 27906
else if (!quoted && s[i] == ';' && !semi && !colon) semi = i; 843 27019 27862
else if (!quoted && s[i] == ';' && !semi && !colon) semi = i; 843 0 843
else if (!quoted && s[i] == ';' && !semi && !colon) semi = i; 301 542 843
53 28580 i++;
54 }
55 666 *pos = i;
56 666 l->name = s + start;
57 666 ▶ if (!colon) {
Branch: 1 branch
BranchTrueFalseTotal
if (!colon) { 0 666 666
58 0 ▶ l->nlen = semi ? semi - start : i - start;
Branch: 1 branch
BranchTrueFalseTotal
l->nlen = semi ? semi - start : i - start; 0 0 0
59 0 l->params = l->value = s + i;
60 0 l->plen = l->vlen = 0;
61 0 return 1;
62 }
63 666 ▶ l->nlen = semi ? semi - start : colon - start;
Branch: 1 branch
BranchTrueFalseTotal
l->nlen = semi ? semi - start : colon - start; 301 365 666
64 666 ▶ l->params = semi ? s + semi + 1 : s + colon;
Branch: 1 branch
BranchTrueFalseTotal
l->params = semi ? s + semi + 1 : s + colon; 301 365 666
65 666 ▶ l->plen = semi ? colon - semi - 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
l->plen = semi ? colon - semi - 1 : 0; 301 365 666
66 666 l->value = s + colon + 1;
67 666 l->vlen = i - colon - 1;
68 666 return 1;
69 }
70  
71 /* A parameter's value, quotes removed; 0 when absent. */
72 static int
73 175 tkr_ical_param(const tkr_ical_line *l, const char *name, const char **v, size_t *vlen)
74 {
75 175 size_t i = 0, nlen = strlen(name);
76 205 ▶ while (i < l->plen) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < l->plen) { 167 38 205
77 167 size_t ns = i, ne, vs, ve;
78 167 int quoted = 0;
79 869 ▶ while (i < l->plen && l->params[i] != '=' && l->params[i] != ';') i++;
Branch: 3 branches
BranchTrueFalseTotal
while (i < l->plen && l->params[i] != '=' && l->params[i] != ';') i++; 869 0 869
while (i < l->plen && l->params[i] != '=' && l->params[i] != ';') i++; 702 167 869
while (i < l->plen && l->params[i] != '=' && l->params[i] != ';') i++; 702 0 702
80 167 ne = i;
81 167 ▶ if (i < l->plen && l->params[i] == '=') i++;
Branch: 2 branches
BranchTrueFalseTotal
if (i < l->plen && l->params[i] == '=') i++; 167 0 167
if (i < l->plen && l->params[i] == '=') i++; 167 0 167
82 167 vs = i;
83 2433 ▶ while (i < l->plen && (quoted || l->params[i] != ';')) {
Branch: 3 branches
BranchTrueFalseTotal
while (i < l->plen && (quoted || l->params[i] != ';')) { 2266 167 2433
while (i < l->plen && (quoted || l->params[i] != ';')) { 24 2242 2266
while (i < l->plen && (quoted || l->params[i] != ';')) { 2242 0 2242
84 2266 ▶ if (l->params[i] == '"') quoted = !quoted;
Branch: 1 branch
BranchTrueFalseTotal
if (l->params[i] == '"') quoted = !quoted; 4 2262 2266
85 2266 i++;
86 }
87 167 ve = i;
88 167 ▶ if (i < l->plen) i++;
Branch: 1 branch
BranchTrueFalseTotal
if (i < l->plen) i++; 0 167 167
89 167 ▶ if (ne - ns == nlen && tkr_ieq(l->params + ns, name, nlen)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ne - ns == nlen && tkr_ieq(l->params + ns, name, nlen)) { 137 30 167
if (ne - ns == nlen && tkr_ieq(l->params + ns, name, nlen)) { 137 0 137
90 137 ▶ if (ve > vs && l->params[vs] == '"' && l->params[ve - 1] == '"') { vs++; ve--; }
Branch: 3 branches
BranchTrueFalseTotal
if (ve > vs && l->params[vs] == '"' && l->params[ve - 1] == '"') { vs++; ve--; } 137 0 137
if (ve > vs && l->params[vs] == '"' && l->params[ve - 1] == '"') { vs++; ve--; } 2 135 137
if (ve > vs && l->params[vs] == '"' && l->params[ve - 1] == '"') { vs++; ve--; } 2 0 2
91 137 *v = l->params + vs;
92 137 *vlen = ve - vs;
93 137 return 1;
94 }
95 }
96 38 return 0;
97 }
98  
99 #define TKR_ICAL_DATE 0
100 #define TKR_ICAL_FLOATING 1
101 #define TKR_ICAL_UTC 2
102  
103 /* A DATE or DATE-TIME value as wall seconds (as if UTC) and its kind. */
104 static int
105 1079 tkr_ical_value(const char *v, size_t len, int64_t *wall, int *kind)
106 {
107 1079 long y, mo, d, h = 0, mi = 0, s = 0;
108 int neg;
109 size_t i;
110 1079 ▶ if (len == 8) *kind = TKR_ICAL_DATE;
Branch: 1 branch
BranchTrueFalseTotal
if (len == 8) *kind = TKR_ICAL_DATE; 857 222 1079
111 222 ▶ else if (len == 15 && v[8] == 'T') *kind = TKR_ICAL_FLOATING;
Branch: 2 branches
BranchTrueFalseTotal
else if (len == 15 && v[8] == 'T') *kind = TKR_ICAL_FLOATING; 212 10 222
else if (len == 15 && v[8] == 'T') *kind = TKR_ICAL_FLOATING; 212 0 212
112 10 ▶ else if (len == 16 && v[8] == 'T' && (v[15] == 'Z' || v[15] == 'z')) *kind = TKR_ICAL_UTC;
Branch: 4 branches
BranchTrueFalseTotal
else if (len == 16 && v[8] == 'T' && (v[15] == 'Z' || v[15] == 'z')) *kind = TKR_ICAL_UTC; 8 2 10
else if (len == 16 && v[8] == 'T' && (v[15] == 'Z' || v[15] == 'z')) *kind = TKR_ICAL_UTC; 8 0 8
else if (len == 16 && v[8] == 'T' && (v[15] == 'Z' || v[15] == 'z')) *kind = TKR_ICAL_UTC; 0 8 8
else if (len == 16 && v[8] == 'T' && (v[15] == 'Z' || v[15] == 'z')) *kind = TKR_ICAL_UTC; 0 0 0
113 2 else return 0;
114 11241 ▶ for (i = 0; i < len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) 10164 1077 11241
115 10164 ▶ if (i != 8 && i != 15 && !isDIGIT(v[i]))
Branch: 3 branches
BranchTrueFalseTotal
if (i != 8 && i != 15 && !isDIGIT(v[i])) 9944 220 10164
if (i != 8 && i != 15 && !isDIGIT(v[i])) 9936 8 9944
if (i != 8 && i != 15 && !isDIGIT(v[i])) 0 9936 9936
116 0 return 0;
117 1077 y = tkr_num(v, 4, 0, &neg); mo = tkr_num(v + 4, 2, 0, &neg); d = tkr_num(v + 6, 2, 0, &neg);
118 1077 ▶ if (*kind != TKR_ICAL_DATE) {
Branch: 1 branch
BranchTrueFalseTotal
if (*kind != TKR_ICAL_DATE) { 220 857 1077
119 220 h = tkr_num(v + 9, 2, 0, &neg); mi = tkr_num(v + 11, 2, 0, &neg); s = tkr_num(v + 13, 2, 0, &neg);
120 }
121 1077 ▶ if (mo < 1 || mo > 12 || d < 1 || (unsigned)d > tkr_days_in_month(y, (unsigned)mo) || h > 23 || mi > 59 || s > 60)
Branch: 7 branches
BranchTrueFalseTotal
if (mo < 1 || mo > 12 || d < 1 || (unsigned)d > tkr_days_in_month(y, (unsigned)mo) || h > 23 || mi > 59 || s > 60) 1077 0 1077
if (mo < 1 || mo > 12 || d < 1 || (unsigned)d > tkr_days_in_month(y, (unsigned)mo) || h > 23 || mi > 59 || s > 60) 1077 0 1077
if (mo < 1 || mo > 12 || d < 1 || (unsigned)d > tkr_days_in_month(y, (unsigned)mo) || h > 23 || mi > 59 || s > 60) 1077 0 1077
if (mo < 1 || mo > 12 || d < 1 || (unsigned)d > tkr_days_in_month(y, (unsigned)mo) || h > 23 || mi > 59 || s > 60) 1077 0 1077
if (mo < 1 || mo > 12 || d < 1 || (unsigned)d > tkr_days_in_month(y, (unsigned)mo) || h > 23 || mi > 59 || s > 60) 1077 0 1077
if (mo < 1 || mo > 12 || d < 1 || (unsigned)d > tkr_days_in_month(y, (unsigned)mo) || h > 23 || mi > 59 || s > 60) 1077 0 1077
if (mo < 1 || mo > 12 || d < 1 || (unsigned)d > tkr_days_in_month(y, (unsigned)mo) || h > 23 || mi > 59 || s > 60) 0 1077 1077
122 0 return 0;
123 1077 *wall = tkr_days_from_civil(y, (unsigned)mo, (unsigned)d) * 86400 + h * 3600 + mi * 60 + s;
124 1077 return 1;
125 }
126  
127 /* A TZID: an IANA name as Time::Keeper reads one, with or without a leading
128 * slash, or a Windows name through Time::Keeper::Zone's table. */
129 static void
130 126 tkr_ical_zone(pTHX_ const char *tz, size_t len, time_keeper_zone *out, const char *what)
131 {
132 126 const time_keeper_abi *T = tkr_tk(aTHX);
133 126 SV *got = NULL;
134 126 ▶ if (len && tz[0] == '/') { tz++; len--; }
Branch: 2 branches
BranchTrueFalseTotal
if (len && tz[0] == '/') { tz++; len--; } 126 0 126
if (len && tz[0] == '/') { tz++; len--; } 2 124 126
135 126 ▶ if ((T->zone_parse)(aTHX_ tz, len, out) == TIME_KEEPER_OK)
Branch: 1 branch
BranchTrueFalseTotal
if ((T->zone_parse)(aTHX_ tz, len, out) == TIME_KEEPER_OK) 122 4 126
136 122 return;
137 4 tkr_tkz(aTHX);
138 {
139 4 dSP;
140 int n;
141 4 ENTER;
142 4 SAVETMPS;
143 4 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 4 4
144 4 ▶ XPUSHs(sv_2mortal(newSVpvs("Time::Keeper::Zone")));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvs("Time::Keeper::Zone"))); 0 4 4
145 4 ▶ XPUSHs(sv_2mortal(newSVpvn(tz, len)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn(tz, len))); 0 4 4
146 4 PUTBACK;
147 4 n = call_method("windows_zone", G_SCALAR);
148 4 SPAGAIN;
149 4 ▶ if (n == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == 1) { 4 0 4
150 4 SV *r = POPs;
151 4 ▶ if (SvOK(r))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(r)) 3 1 4
152 3 got = newSVsv(r);
153 }
154 4 PUTBACK;
155 4 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 4 0 4
156 4 LEAVE;
157 }
158 4 ▶ if (got) {
Branch: 1 branch
BranchTrueFalseTotal
if (got) { 3 1 4
159 STRLEN glen;
160 3 const char *g = SvPV(got, glen);
161 3 int rc = (T->zone_parse)(aTHX_ g, glen, out);
162 3 SvREFCNT_dec(got);
163 3 ▶ if (rc == TIME_KEEPER_OK)
Branch: 1 branch
BranchTrueFalseTotal
if (rc == TIME_KEEPER_OK) 3 0 3
164 3 return;
165 }
166 1 croak("%s: TZID=%" SVf " is not a time zone", what, SVfARG(sv_2mortal(newSVpvn(tz, len))));
167 }
168  
169 /* The lines, into the parts of a set. DTSTART is read first wherever it
170 * stands, since every other property is read in its zone. */
171 static void
172 150 tkr_ical_parse(pTHX_ const char *text, size_t len, tkr_set_parts *p, const char *what)
173 {
174 size_t ulen, pos;
175 150 char *buf = tkr_ical_unfold(aTHX_ text, len, &ulen);
176 tkr_ical_line l;
177 150 int seen_start = 0, rule_lines = 0;
178 const char *tz;
179 size_t tzlen;
180 time_keeper_zone z;
181 int64_t wall;
182 int kind;
183 char err[TKR_ERR];
184 150 SAVEFREEPV(buf);
185 150 tkr_parts_init(p);
186 150 p->zone.kind = TIME_KEEPER_ZONE_FLOATING;
187 482 ▶ for (pos = 0; tkr_ical_next_line(buf, ulen, &pos, &l); ) {
Branch: 1 branch
BranchTrueFalseTotal
for (pos = 0; tkr_ical_next_line(buf, ulen, &pos, &l); ) { 335 147 482
188 335 ▶ if (l.nlen != 7 || !tkr_ieq(l.name, "DTSTART", 7))
Branch: 2 branches
BranchTrueFalseTotal
if (l.nlen != 7 || !tkr_ieq(l.name, "DTSTART", 7)) 146 189 335
if (l.nlen != 7 || !tkr_ieq(l.name, "DTSTART", 7)) 2 144 146
189 191 continue;
190 144 ▶ if (seen_start++) {
Branch: 1 branch
BranchTrueFalseTotal
if (seen_start++) { 1 143 144
191
192 1 croak("%s: two DTSTART lines", what);
193 }
194 143 ▶ if (!tkr_ical_value(l.value, l.vlen, &wall, &kind)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!tkr_ical_value(l.value, l.vlen, &wall, &kind)) { 1 142 143
195
196 1 croak("%s: DTSTART:%" SVf " is not a date or a date and time", what, SVfARG(sv_2mortal(newSVpvn(l.value, l.vlen))));
197 }
198 142 ▶ if (kind == TKR_ICAL_UTC)
Branch: 1 branch
BranchTrueFalseTotal
if (kind == TKR_ICAL_UTC) 8 134 142
199 8 p->zone.kind = TIME_KEEPER_ZONE_UTC;
200 134 ▶ else if (tkr_ical_param(&l, "TZID", &tz, &tzlen)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (tkr_ical_param(&l, "TZID", &tz, &tzlen)) { 119 15 134
201 119 tkr_ical_zone(aTHX_ tz, tzlen, &z, what);
202 118 p->zone = z;
203 }
204 141 p->has_start = 1;
205 141 p->start_is_day = kind == TKR_ICAL_DATE;
206 141 p->start.sec = tkr_zone_instant(aTHX_ &p->zone, wall, what);
207 141 p->start.nsec = 0;
208 }
209 468 ▶ for (pos = 0; tkr_ical_next_line(buf, ulen, &pos, &l); ) {
Branch: 1 branch
BranchTrueFalseTotal
for (pos = 0; tkr_ical_next_line(buf, ulen, &pos, &l); ) { 331 137 468
210 331 int ex = 0, is_rule = 0, is_cron = 0;
211 331 ▶ if ((l.nlen == 5 && tkr_ieq(l.name, "RRULE", 5)) || (l.nlen == 6 && tkr_ieq(l.name, "EXRULE", 6))) {
Branch: 4 branches
BranchTrueFalseTotal
if ((l.nlen == 5 && tkr_ieq(l.name, "RRULE", 5)) || (l.nlen == 6 && tkr_ieq(l.name, "EXRULE", 6))) { 164 167 331
if ((l.nlen == 5 && tkr_ieq(l.name, "RRULE", 5)) || (l.nlen == 6 && tkr_ieq(l.name, "EXRULE", 6))) { 8 156 164
if ((l.nlen == 5 && tkr_ieq(l.name, "RRULE", 5)) || (l.nlen == 6 && tkr_ieq(l.name, "EXRULE", 6))) { 20 155 175
if ((l.nlen == 5 && tkr_ieq(l.name, "RRULE", 5)) || (l.nlen == 6 && tkr_ieq(l.name, "EXRULE", 6))) { 5 15 20
212 161 is_rule = 1;
213 161 ex = l.nlen == 6;
214 }
215 170 ▶ else if (l.nlen == 10 && tkr_ieq(l.name, "X-TKR-CRON", 10))
Branch: 2 branches
BranchTrueFalseTotal
else if (l.nlen == 10 && tkr_ieq(l.name, "X-TKR-CRON", 10)) 3 167 170
else if (l.nlen == 10 && tkr_ieq(l.name, "X-TKR-CRON", 10)) 3 0 3
216 3 is_cron = 1;
217 167 ▶ else if ((l.nlen == 5 && tkr_ieq(l.name, "RDATE", 5)) || (l.nlen == 6 && tkr_ieq(l.name, "EXDATE", 6)))
Branch: 4 branches
BranchTrueFalseTotal
else if ((l.nlen == 5 && tkr_ieq(l.name, "RDATE", 5)) || (l.nlen == 6 && tkr_ieq(l.name, "EXDATE", 6))) 8 159 167
else if ((l.nlen == 5 && tkr_ieq(l.name, "RDATE", 5)) || (l.nlen == 6 && tkr_ieq(l.name, "EXDATE", 6))) 2 6 8
else if ((l.nlen == 5 && tkr_ieq(l.name, "RDATE", 5)) || (l.nlen == 6 && tkr_ieq(l.name, "EXDATE", 6))) 15 146 161
else if ((l.nlen == 5 && tkr_ieq(l.name, "RDATE", 5)) || (l.nlen == 6 && tkr_ieq(l.name, "EXDATE", 6))) 15 0 15
218 21 ex = l.nlen == 6;
219 else
220 146 continue;
221 342 ▶ if (is_rule || is_cron) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_rule || is_cron) { 24 161 185
if (is_rule || is_cron) { 3 21 24
222 tkr_rule r;
223 164 rule_lines++;
224 164 tkr_rule_init(&r);
225 164 ▶ if (ex)
Branch: 1 branch
BranchTrueFalseTotal
if (ex) 5 159 164
226 5 r.flags |= TKR_F_EXRULE;
227 164 r.zone = p->zone;
228 164 r.gap = (uint8_t)p->gap;
229 164 r.overlap = (uint8_t)p->overlap;
230 164 ▶ if (is_cron) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_cron) { 3 161 164
231 3 ▶ if (!tkr_cron_parse(l.value, l.vlen, &r, err)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!tkr_cron_parse(l.value, l.vlen, &r, err)) { 0 3 3
232 0 tkr_parts_free(p);
233 0 croak("%s: X-TKR-CRON: %s", what, err);
234 }
235 3 ▶ if (p->has_start) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->has_start) { 0 3 3
236 0 r.start = p->start;
237 0 r.flags |= TKR_F_HAS_START;
238 }
239 }
240 else {
241 161 ▶ if (!p->has_start) {
Branch: 1 branch
BranchTrueFalseTotal
if (!p->has_start) { 2 159 161
242 2 tkr_parts_free(p);
243 2 ▶ croak("%s: an %s needs a DTSTART", what, ex ? "EXRULE" : "RRULE");
Branch: 1 branch
BranchTrueFalseTotal
croak("%s: an %s needs a DTSTART", what, ex ? "EXRULE" : "RRULE"); 1 1 2
244 }
245 159 r.start = p->start;
246 159 r.flags |= TKR_F_HAS_START;
247 159 ▶ if (!tkr_rrule_parse(l.value, l.vlen, &r, err)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!tkr_rrule_parse(l.value, l.vlen, &r, err)) { 1 158 159
248 1 tkr_parts_free(p);
249 1 ▶ croak("%s: %s: %s", what, ex ? "EXRULE" : "RRULE", err);
Branch: 1 branch
BranchTrueFalseTotal
croak("%s: %s: %s", what, ex ? "EXRULE" : "RRULE", err); 0 1 1
250 }
251 158 ▶ if (r.until_kind == TKR_UNTIL_FLOATING && r.zone.kind != TIME_KEEPER_ZONE_FLOATING) {
Branch: 2 branches
BranchTrueFalseTotal
if (r.until_kind == TKR_UNTIL_FLOATING && r.zone.kind != TIME_KEEPER_ZONE_FLOATING) { 1 157 158
if (r.until_kind == TKR_UNTIL_FLOATING && r.zone.kind != TIME_KEEPER_ZONE_FLOATING) { 1 0 1
252 1 tkr_parts_free(p);
253 1 ▶ croak("%s: %s: UNTIL must be in UTC (end in Z) when DTSTART has a zone", what, ex ? "EXRULE" : "RRULE");
Branch: 1 branch
BranchTrueFalseTotal
croak("%s: %s: UNTIL must be in UTC (end in Z) when DTSTART has a zone", what, ex ? "EXRULE" : "RRULE"); 0 1 1
254 }
255 157 ▶ if (r.until_kind == TKR_UNTIL_UTC && r.zone.kind == TIME_KEEPER_ZONE_FLOATING) {
Branch: 2 branches
BranchTrueFalseTotal
if (r.until_kind == TKR_UNTIL_UTC && r.zone.kind == TIME_KEEPER_ZONE_FLOATING) { 21 136 157
if (r.until_kind == TKR_UNTIL_UTC && r.zone.kind == TIME_KEEPER_ZONE_FLOATING) { 1 20 21
256 1 tkr_parts_free(p);
257 1 ▶ croak("%s: %s: UNTIL must be floating (no Z) when DTSTART is", what, ex ? "EXRULE" : "RRULE");
Branch: 1 branch
BranchTrueFalseTotal
croak("%s: %s: UNTIL must be floating (no Z) when DTSTART is", what, ex ? "EXRULE" : "RRULE"); 0 1 1
258 }
259 }
260 159 ▶ if (!tkr_can_fire(aTHX_ &r, is_cron ? tkr_now_sec(aTHX) : 0, err)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!tkr_can_fire(aTHX_ &r, is_cron ? tkr_now_sec(aTHX) : 0, err)) { 3 156 159
if (!tkr_can_fire(aTHX_ &r, is_cron ? tkr_now_sec(aTHX) : 0, err)) { 1 158 159
261 1 tkr_parts_free(p);
262 1 ▶ croak("%s: %s:%" SVf " %s", what, is_cron ? "X-TKR-CRON" : ex ? "EXRULE" : "RRULE", SVfARG(sv_2mortal(newSVpvn(l.value, l.vlen))), err);
Branch: 2 branches
BranchTrueFalseTotal
croak("%s: %s:%" SVf " %s", what, is_cron ? "X-TKR-CRON" : ex ? "EXRULE" : "RRULE", SVfARG(sv_2mortal(newSVpvn(l.value, l.vlen))), err); 1 0 1
croak("%s: %s:%" SVf " %s", what, is_cron ? "X-TKR-CRON" : ex ? "EXRULE" : "RRULE", SVfARG(sv_2mortal(newSVpvn(l.value, l.vlen))), err); 0 1 1
263 }
264 158 tkr_parts_add_rule(aTHX_ p, &r, ex, what);
265 }
266 else {
267 const char *v, *vend;
268 size_t vl;
269 21 int want_date = 0;
270 21 time_keeper_zone lz = p->zone;
271 21 ▶ if (tkr_ical_param(&l, "VALUE", &v, &vl)) {
Branch: 1 branch
BranchTrueFalseTotal
if (tkr_ical_param(&l, "VALUE", &v, &vl)) { 11 10 21
272 11 ▶ if (vl == 6 && tkr_ieq(v, "PERIOD", 6)) {
Branch: 2 branches
BranchTrueFalseTotal
if (vl == 6 && tkr_ieq(v, "PERIOD", 6)) { 1 10 11
if (vl == 6 && tkr_ieq(v, "PERIOD", 6)) { 1 0 1
273 1 tkr_parts_free(p);
274 1 ▶ croak("%s: %s;VALUE=PERIOD is not supported: a period is not an occurrence", what, ex ? "EXDATE" : "RDATE");
Branch: 1 branch
BranchTrueFalseTotal
croak("%s: %s;VALUE=PERIOD is not supported: a period is not an occurrence", what, ex ? "EXDATE" : "RDATE"); 0 1 1
275 }
276 10 ▶ if (vl == 4 && tkr_ieq(v, "DATE", 4))
Branch: 2 branches
BranchTrueFalseTotal
if (vl == 4 && tkr_ieq(v, "DATE", 4)) 10 0 10
if (vl == 4 && tkr_ieq(v, "DATE", 4)) 10 0 10
277 10 want_date = 1;
278 }
279 20 ▶ if (tkr_ical_param(&l, "TZID", &tz, &tzlen))
Branch: 1 branch
BranchTrueFalseTotal
if (tkr_ical_param(&l, "TZID", &tz, &tzlen)) 7 13 20
280 7 tkr_ical_zone(aTHX_ tz, tzlen, &lz, what);
281 20 v = l.value;
282 20 vend = l.value + l.vlen;
283 121 ▶ while (v < vend) {
Branch: 1 branch
BranchTrueFalseTotal
while (v < vend) { 103 18 121
284 103 const char *e = v;
285 1569 ▶ while (e < vend && *e != ',') e++;
Branch: 2 branches
BranchTrueFalseTotal
while (e < vend && *e != ',') e++; 1549 20 1569
while (e < vend && *e != ',') e++; 1466 83 1549
286 103 ▶ if (!tkr_ical_value(v, (size_t)(e - v), &wall, &kind) || (want_date && kind != TKR_ICAL_DATE)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!tkr_ical_value(v, (size_t)(e - v), &wall, &kind) || (want_date && kind != TKR_ICAL_DATE)) { 102 1 103
if (!tkr_ical_value(v, (size_t)(e - v), &wall, &kind) || (want_date && kind != TKR_ICAL_DATE)) { 11 91 102
if (!tkr_ical_value(v, (size_t)(e - v), &wall, &kind) || (want_date && kind != TKR_ICAL_DATE)) { 1 10 11
287 2 tkr_parts_free(p);
288 2 ▶ croak("%s: %s:%" SVf " is not a %s", what, ex ? "EXDATE" : "RDATE", SVfARG(sv_2mortal(newSVpvn(v, (STRLEN)(e - v)))),
Branch: 2 branches
BranchTrueFalseTotal
croak("%s: %s:%" SVf " is not a %s", what, ex ? "EXDATE" : "RDATE", SVfARG(sv_2mortal(newSVpvn(v, (STRLEN)(e - v)))), 1 1 2
croak("%s: %s:%" SVf " is not a %s", what, ex ? "EXDATE" : "RDATE", SVfARG(sv_2mortal(newSVpvn(v, (STRLEN)(e - v)))), 2 0 2
289 want_date ? "date (YYYYMMDD)" : "date or a date and time");
290 }
291 101 ▶ if (kind == TKR_ICAL_DATE)
Branch: 1 branch
BranchTrueFalseTotal
if (kind == TKR_ICAL_DATE) 11 90 101
292 11 tkr_parts_add_date(aTHX_ p, ex, wall, 1, what);
293 90 ▶ else if (kind == TKR_ICAL_UTC)
Branch: 1 branch
BranchTrueFalseTotal
else if (kind == TKR_ICAL_UTC) 0 90 90
294 0 tkr_parts_add_raw(aTHX_ p, ex, wall, 0, what);
295 else
296 90 tkr_parts_add_raw(aTHX_ p, ex, tkr_zone_instant(aTHX_ &lz, wall, what), 0, what);
297 101 v = e + 1;
298 }
299 }
300 }
301
302 137 ▶ if (!rule_lines && !p->n_rdates) {
Branch: 2 branches
BranchTrueFalseTotal
if (!rule_lines && !p->n_rdates) { 3 134 137
if (!rule_lines && !p->n_rdates) { 3 0 3
303 3 ▶ if (p->has_start)
Branch: 1 branch
BranchTrueFalseTotal
if (p->has_start) 1 2 3
304 1 tkr_parts_add_raw(aTHX_ p, 0, p->start.sec, 0, what);
305 else {
306 2 tkr_parts_free(p);
307 2 croak("%s: nothing recurs: no DTSTART, RRULE or RDATE", what);
308 }
309 }
310 135 }
311  
312 /* ---- the writer ----------------------------------------------------------- */
313  
314 static void
315 374 tkr_ical_wall_text(SV *out, int64_t wall, int day_only, int utc)
316 {
317 374 int64_t days = tkr_floor_div(wall, 86400), rem = wall - days * 86400, y;
318 unsigned m, d;
319 374 tkr_civil_from_days(days, &y, &m, &d);
320 374 ▶ if (day_only)
Branch: 1 branch
BranchTrueFalseTotal
if (day_only) 37 337 374
321 37 sv_catpvf(out, "%04" IVdf "%02u%02u", (IV)y, m, d);
322 else
323 337 ▶ sv_catpvf(out, "%04" IVdf "%02u%02uT%02d%02d%02d%s", (IV)y, m, d, (int)(rem / 3600), (int)(rem % 3600 / 60), (int)(rem % 60), utc ? "Z" : "");
Branch: 1 branch
BranchTrueFalseTotal
sv_catpvf(out, "%04" IVdf "%02u%02uT%02d%02d%02d%s", (IV)y, m, d, (int)(rem / 3600), (int)(rem % 3600 / 60), (int)(rem % 60), utc ? "Z" : ""); 4 333 337
324 374 }
325  
326 /* One content line, folded at 75 octets, CRLF ended. */
327 static void
328 563 tkr_ical_fold(pTHX_ SV *out, SV *line)
329 {
330 563 STRLEN len, i = 0;
331 563 const char *s = SvPV(line, len);
332 586 ▶ while (len - i > TKR_ICAL_FOLD) {
Branch: 1 branch
BranchTrueFalseTotal
while (len - i > TKR_ICAL_FOLD) { 23 563 586
333 23 ▶ sv_catpvn(out, s + i, i ? TKR_ICAL_FOLD - 1 : TKR_ICAL_FOLD);
Branch: 1 branch
BranchTrueFalseTotal
sv_catpvn(out, s + i, i ? TKR_ICAL_FOLD - 1 : TKR_ICAL_FOLD); 16 7 23
334 23 ▶ i += i ? TKR_ICAL_FOLD - 1 : TKR_ICAL_FOLD;
Branch: 1 branch
BranchTrueFalseTotal
i += i ? TKR_ICAL_FOLD - 1 : TKR_ICAL_FOLD; 16 7 23
335 23 sv_catpvs(out, "\r\n ");
336 }
337 563 sv_catpvn(out, s + i, len - i);
338 563 sv_catpvs(out, "\r\n");
339 563 }
340  
341 /* The zone's TZID parameter, or none for UTC and floating. */
342 static void
343 255 tkr_ical_tzid(pTHX_ SV *line, const time_keeper_zone *z)
344 {
345 tkr_rule probe;
346 SV *name;
347 255 ▶ if (z->kind == TIME_KEEPER_ZONE_UTC || z->kind == TIME_KEEPER_ZONE_FLOATING)
Branch: 2 branches
BranchTrueFalseTotal
if (z->kind == TIME_KEEPER_ZONE_UTC || z->kind == TIME_KEEPER_ZONE_FLOATING) 251 4 255
if (z->kind == TIME_KEEPER_ZONE_UTC || z->kind == TIME_KEEPER_ZONE_FLOATING) 1 250 251
348 5 return;
349 250 tkr_rule_init(&probe);
350 250 probe.zone = *z;
351 250 name = sv_2mortal(tkr_zone_name_sv(aTHX_ &probe));
352 250 ▶ if (strchr(SvPV_nolen(name), ':'))
Branch: 1 branch
BranchTrueFalseTotal
if (strchr(SvPV_nolen(name), ':')) 2 248 250
353 2 sv_catpvf(line, ";TZID=\"%" SVf "\"", SVfARG(name));
354 else
355 248 sv_catpvf(line, ";TZID=%" SVf, SVfARG(name));
356 }
357  
358 static void
359 542 tkr_ical_dates(pTHX_ SV *out, const tkr_set *s, const tkr_date *d, unsigned n, const char *prop)
360 {
361 542 unsigned i, days = 0, times = 0;
362 SV *line;
363 650 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 108 542 650
364 108 ▶ if (d[i].is_day) days++; else times++;
Branch: 1 branch
BranchTrueFalseTotal
if (d[i].is_day) days++; else times++; 18 90 108
365 542 ▶ if (times) {
Branch: 1 branch
BranchTrueFalseTotal
if (times) { 8 534 542
366 8 int utc = s->zone.kind == TIME_KEEPER_ZONE_UTC;
367 8 unsigned k = 0;
368 8 line = sv_2mortal(newSVpv(prop, 0));
369 8 tkr_ical_tzid(aTHX_ line, &s->zone);
370 8 sv_catpvs(line, ":");
371 101 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 93 8 101
372 93 ▶ if (d[i].is_day) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (d[i].is_day) continue; 3 90 93
373 90 ▶ if (k++) sv_catpvs(line, ",");
Branch: 1 branch
BranchTrueFalseTotal
if (k++) sv_catpvs(line, ","); 82 8 90
374 90 ▶ tkr_ical_wall_text(line, utc ? d[i].at : tkr_zone_wall(aTHX_ &s->zone, d[i].at), 0, utc);
Branch: 1 branch
BranchTrueFalseTotal
tkr_ical_wall_text(line, utc ? d[i].at : tkr_zone_wall(aTHX_ &s->zone, d[i].at), 0, utc); 0 90 90
375 }
376 8 tkr_ical_fold(aTHX_ out, line);
377 }
378 542 ▶ if (days) {
Branch: 1 branch
BranchTrueFalseTotal
if (days) { 17 525 542
379 17 unsigned k = 0;
380 17 line = sv_2mortal(newSVpv(prop, 0));
381 17 sv_catpvs(line, ";VALUE=DATE:");
382 38 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 21 17 38
383 21 ▶ if (!d[i].is_day) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!d[i].is_day) continue; 3 18 21
384 18 ▶ if (k++) sv_catpvs(line, ",");
Branch: 1 branch
BranchTrueFalseTotal
if (k++) sv_catpvs(line, ","); 1 17 18
385 /* an added day was resolved to its midnight; a removed one is kept as the day */
386 18 ▶ tkr_ical_wall_text(line, prop[0] == 'R' ? tkr_zone_wall(aTHX_ &s->zone, d[i].at) : d[i].at, 1, 0);
Branch: 1 branch
BranchTrueFalseTotal
tkr_ical_wall_text(line, prop[0] == 'R' ? tkr_zone_wall(aTHX_ &s->zone, d[i].at) : d[i].at, 1, 0); 4 14 18
387 }
388 17 tkr_ical_fold(aTHX_ out, line);
389 }
390 542 }
391  
392 /* The set as iCalendar lines. For a frozen form the EXRULEs are kept and
393 * every cron is an X-TKR-CRON line; otherwise a cron is written as the RRULE
394 * it is, and one with no RRULE spelling dies. */
395 static void
396 272 tkr_ical_write(pTHX_ const tkr_set *s, SV *out, int frozen, const char *what)
397 {
398 272 ▶ unsigned i, nr = s->n_rules + (frozen ? s->n_exrules : 0);
Branch: 1 branch
BranchTrueFalseTotal
unsigned i, nr = s->n_rules + (frozen ? s->n_exrules : 0); 8 264 272
399 SV *line;
400 char err[TKR_ERR];
401 272 ▶ if (s->has_start) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->has_start) { 266 6 272
402 266 line = sv_2mortal(newSVpvs("DTSTART"));
403 266 ▶ if (s->start_is_day && s->zone.kind == TIME_KEEPER_ZONE_FLOATING) {
Branch: 2 branches
BranchTrueFalseTotal
if (s->start_is_day && s->zone.kind == TIME_KEEPER_ZONE_FLOATING) { 19 247 266
if (s->start_is_day && s->zone.kind == TIME_KEEPER_ZONE_FLOATING) { 19 0 19
404 19 sv_catpvs(line, ";VALUE=DATE:");
405 19 tkr_ical_wall_text(line, s->start.sec, 1, 0);
406 }
407 else {
408 247 int utc = s->zone.kind == TIME_KEEPER_ZONE_UTC;
409 247 tkr_ical_tzid(aTHX_ line, &s->zone);
410 247 sv_catpvs(line, ":");
411 247 ▶ tkr_ical_wall_text(line, utc ? s->start.sec : tkr_zone_wall(aTHX_ &s->zone, s->start.sec), 0, utc);
Branch: 1 branch
BranchTrueFalseTotal
tkr_ical_wall_text(line, utc ? s->start.sec : tkr_zone_wall(aTHX_ &s->zone, s->start.sec), 0, utc); 4 243 247
412 }
413 266 tkr_ical_fold(aTHX_ out, line);
414 }
415 544 ▶ for (i = 0; i < nr; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nr; i++) { 273 271 544
416 273 const tkr_rule *r = &tkr_set_rules(s)[i];
417 273 ▶ if ((r->flags & TKR_F_CRON) && frozen) {
Branch: 2 branches
BranchTrueFalseTotal
if ((r->flags & TKR_F_CRON) && frozen) { 6 267 273
if ((r->flags & TKR_F_CRON) && frozen) { 3 3 6
418 3 line = sv_2mortal(newSVpvs(""));
419 3 ▶ if (!tkr_cron_write(aTHX_ r, line, err))
Branch: 1 branch
BranchTrueFalseTotal
if (!tkr_cron_write(aTHX_ r, line, err)) 0 3 3
420 0 croak("%s: %s", what, err);
421 {
422 3 SV *whole = sv_2mortal(newSVpvs("X-TKR-CRON:"));
423 3 sv_catsv(whole, line);
424 3 tkr_ical_fold(aTHX_ out, whole);
425 }
426 3 continue;
427 }
428 270 ▶ if (r->flags & TKR_F_EVERY)
Branch: 1 branch
BranchTrueFalseTotal
if (r->flags & TKR_F_EVERY) 0 270 270
429 0 croak("%s: an @every cron is an interval from the last occurrence, which no RRULE says", what);
430 270 ▶ if (r->flags & TKR_F_VIXIE)
Branch: 1 branch
BranchTrueFalseTotal
if (r->flags & TKR_F_VIXIE) 1 269 270
431 1 croak("%s: a cron with both day fields set fires on either, which no RRULE says", what);
432 269 line = sv_2mortal(newSVpvs(""));
433 269 tkr_rrule_write(aTHX_ r, line);
434 {
435 269 ▶ SV *whole = sv_2mortal(newSVpv(i < s->n_rules ? "RRULE:" : "EXRULE:", 0));
Branch: 1 branch
BranchTrueFalseTotal
SV *whole = sv_2mortal(newSVpv(i < s->n_rules ? "RRULE:" : "EXRULE:", 0)); 267 2 269
436 269 sv_catsv(whole, line);
437 269 tkr_ical_fold(aTHX_ out, whole);
438 }
439 }
440 271 tkr_ical_dates(aTHX_ out, s, tkr_set_rdates(s), s->n_rdates, "RDATE");
441 271 tkr_ical_dates(aTHX_ out, s, tkr_set_exdates(s), s->n_exdates, "EXDATE");
442 271 }
443  
444 /* ---- the frozen forms ------------------------------------------------------ */
445  
446 /* The locale's code: a value in it knows. */
447 static SV *
448 28 tkr_locale_code_sv(pTHX_ const time_keeper_zone *z, int locale)
449 {
450 28 const time_keeper_abi *T = tkr_tk(aTHX);
451 time_keeper_instant t;
452 28 SV *val, *code = NULL;
453 28 dSP;
454 int n;
455 28 t.sec = 0; t.nsec = 0;
456 28 val = (T->new_sv)(aTHX_ &t, z, locale);
457 28 ▶ if (!val)
Branch: 1 branch
BranchTrueFalseTotal
if (!val) 0 28 28
458 0 return newSVpvs("en");
459 28 ENTER;
460 28 SAVETMPS;
461 28 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 28 28
462 28 ▶ XPUSHs(sv_2mortal(val));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(val)); 0 28 28
463 28 PUTBACK;
464 28 n = call_method("locale", G_SCALAR);
465 28 SPAGAIN;
466 28 ▶ if (n == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == 1) { 28 0 28
467 28 SV *r = POPs;
468 28 ▶ if (SvOK(r))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(r)) 28 0 28
469 28 code = newSVsv(r);
470 }
471 28 PUTBACK;
472 28 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 28 0 28
473 28 LEAVE;
474 28 ▶ return code ? code : newSVpvs("en");
Branch: 1 branch
BranchTrueFalseTotal
return code ? code : newSVpvs("en"); 0 28 28
475 }
476  
477 static int
478 29 tkr_locale_of_code(pTHX_ const char *code, size_t len)
479 {
480 29 int id = (tkr_tk(aTHX)->locale_find)(code, len);
481 29 return id < 0 ? 0 : id;
482 }
483  
484 /* A rule: "tkr1 <zone> <locale> <start seconds or -> <nanoseconds> <gap> <overlap> rrule|cron <text>". */
485 static SV *
486 20 tkr_rule_freeze_sv(pTHX_ const tkr_rule *r, const char *what)
487 {
488 20 SV *out = newSVpvs("tkr1 "), *zone = sv_2mortal(tkr_zone_name_sv(aTHX_ r)), *code = sv_2mortal(tkr_locale_code_sv(aTHX_ &r->zone, r->locale));
489 20 SV *text = sv_2mortal(newSVpvs(""));
490 char err[TKR_ERR];
491 20 sv_catsv(out, zone);
492 20 sv_catpvs(out, " ");
493 20 sv_catsv(out, code);
494 20 ▶ if (r->flags & TKR_F_HAS_START)
Branch: 1 branch
BranchTrueFalseTotal
if (r->flags & TKR_F_HAS_START) 14 6 20
495 14 sv_catpvf(out, " %" IVdf " %d", (IV)r->start.sec, (int)r->start.nsec);
496 else
497 6 sv_catpvs(out, " - 0");
498 20 sv_catpvf(out, " %s %s ", tkr_gap_names[r->gap], tkr_overlap_names[r->overlap]);
499 20 ▶ if (r->flags & TKR_F_CRON) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->flags & TKR_F_CRON) { 7 13 20
500 7 ▶ if (!tkr_cron_write(aTHX_ r, text, err))
Branch: 1 branch
BranchTrueFalseTotal
if (!tkr_cron_write(aTHX_ r, text, err)) 0 7 7
501 0 croak("%s: %s", what, err);
502 7 sv_catpvs(out, "cron ");
503 }
504 else {
505 13 tkr_rrule_write(aTHX_ r, text);
506 13 sv_catpvs(out, "rrule ");
507 }
508 20 sv_catsv(out, text);
509 20 return out;
510 }
511  
512 /* A signed whole number of any length an epoch needs. */
513 static int
514 16 tkr_i64(const char *s, size_t len, int64_t *out)
515 {
516 16 int64_t v = 0;
517 16 int neg = 0;
518 16 size_t i = 0;
519 16 ▶ if (len && (s[0] == '-' || s[0] == '+')) { neg = s[0] == '-'; i = 1; }
Branch: 3 branches
BranchTrueFalseTotal
if (len && (s[0] == '-' || s[0] == '+')) { neg = s[0] == '-'; i = 1; } 16 0 16
if (len && (s[0] == '-' || s[0] == '+')) { neg = s[0] == '-'; i = 1; } 15 1 16
if (len && (s[0] == '-' || s[0] == '+')) { neg = s[0] == '-'; i = 1; } 0 15 15
520 16 ▶ if (i >= len || len - i > 18) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (i >= len || len - i > 18) return 0; 16 0 16
if (i >= len || len - i > 18) return 0; 0 16 16
521 147 ▶ for (; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; i < len; i++) { 132 15 147
522 132 ▶ if (!isDIGIT(s[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!isDIGIT(s[i])) return 0; 1 131 132
523 131 v = v * 10 + (s[i] - '0');
524 }
525 15 ▶ *out = neg ? -v : v;
Branch: 1 branch
BranchTrueFalseTotal
*out = neg ? -v : v; 1 14 15
526 15 return 1;
527 }
528  
529 static const char *
530 212 tkr_word(const char *p, const char *end, size_t *len)
531 {
532 const char *s;
533 387 ▶ while (p < end && *p == ' ') p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && *p == ' ') p++; 386 1 387
while (p < end && *p == ' ') p++; 175 211 386
534 212 s = p;
535 1225 ▶ while (p < end && *p != ' ' && *p != '\r' && *p != '\n') p++;
Branch: 4 branches
BranchTrueFalseTotal
while (p < end && *p != ' ' && *p != '\r' && *p != '\n') p++; 1220 5 1225
while (p < end && *p != ' ' && *p != '\r' && *p != '\n') p++; 1021 199 1220
while (p < end && *p != ' ' && *p != '\r' && *p != '\n') p++; 1013 8 1021
while (p < end && *p != ' ' && *p != '\r' && *p != '\n') p++; 1013 0 1013
536 212 *len = (size_t)(p - s);
537 212 return s;
538 }
539  
540 static void
541 27 tkr_rule_thaw(pTHX_ SV *frozen, tkr_rule *r, const char *what)
542 {
543 STRLEN len;
544 27 const char *s = SvPV(frozen, len), *end = s + len, *w, *p;
545 size_t wl;
546 char err[TKR_ERR];
547 int neg, i;
548 27 tkr_rule_init(r);
549 27 w = tkr_word(s, end, &wl);
550 27 ▶ if (wl != 4 || memcmp(w, "tkr1", 4) != 0)
Branch: 2 branches
BranchTrueFalseTotal
if (wl != 4 || memcmp(w, "tkr1", 4) != 0) 26 1 27
if (wl != 4 || memcmp(w, "tkr1", 4) != 0) 2 24 26
551 3 croak("%s: not a frozen Time::Keeper::Recurrence", what);
552 24 w = tkr_word(w + wl, end, &wl);
553 24 ▶ if ((tkr_tk(aTHX)->zone_parse)(aTHX_ w, wl, &r->zone) != TIME_KEEPER_OK)
Branch: 1 branch
BranchTrueFalseTotal
if ((tkr_tk(aTHX)->zone_parse)(aTHX_ w, wl, &r->zone) != TIME_KEEPER_OK) 3 21 24
554 3 croak("%s: '%" SVf "' is not a time zone this build knows", what, SVfARG(sv_2mortal(newSVpvn(w, wl))));
555 21 w = tkr_word(w + wl, end, &wl);
556 21 r->locale = tkr_locale_of_code(aTHX_ w, wl);
557 21 w = tkr_word(w + wl, end, &wl);
558 21 ▶ if (!(wl == 1 && w[0] == '-')) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(wl == 1 && w[0] == '-')) { 8 13 21
if (!(wl == 1 && w[0] == '-')) { 3 5 8
559 16 ▶ if (!tkr_i64(w, wl, &r->start.sec))
Branch: 1 branch
BranchTrueFalseTotal
if (!tkr_i64(w, wl, &r->start.sec)) 1 15 16
560 1 croak("%s: a damaged frozen rule", what);
561 15 r->flags |= TKR_F_HAS_START;
562 }
563 20 w = tkr_word(w + wl, end, &wl);
564 20 r->start.nsec = (int32_t)tkr_num(w, wl, 0, &neg);
565 20 w = tkr_word(w + wl, end, &wl);
566 39 ▶ for (i = 0; i < 4; i++) if (strlen(tkr_gap_names[i]) == wl && memcmp(w, tkr_gap_names[i], wl) == 0) break;
Branch: 3 branches
BranchTrueFalseTotal
for (i = 0; i < 4; i++) if (strlen(tkr_gap_names[i]) == wl && memcmp(w, tkr_gap_names[i], wl) == 0) break; 20 18 38
for (i = 0; i < 4; i++) if (strlen(tkr_gap_names[i]) == wl && memcmp(w, tkr_gap_names[i], wl) == 0) break; 19 1 20
for (i = 0; i < 4; i++) if (strlen(tkr_gap_names[i]) == wl && memcmp(w, tkr_gap_names[i], wl) == 0) break; 38 1 39
567 20 ▶ if (i == 4) croak("%s: a damaged frozen rule", what);
Branch: 1 branch
BranchTrueFalseTotal
if (i == 4) croak("%s: a damaged frozen rule", what); 1 19 20
568 19 r->gap = (uint8_t)i;
569 19 w = tkr_word(w + wl, end, &wl);
570 34 ▶ for (i = 0; i < 3; i++) if (strlen(tkr_overlap_names[i]) == wl && memcmp(w, tkr_overlap_names[i], wl) == 0) break;
Branch: 3 branches
BranchTrueFalseTotal
for (i = 0; i < 3; i++) if (strlen(tkr_overlap_names[i]) == wl && memcmp(w, tkr_overlap_names[i], wl) == 0) break; 19 15 34
for (i = 0; i < 3; i++) if (strlen(tkr_overlap_names[i]) == wl && memcmp(w, tkr_overlap_names[i], wl) == 0) break; 19 0 19
for (i = 0; i < 3; i++) if (strlen(tkr_overlap_names[i]) == wl && memcmp(w, tkr_overlap_names[i], wl) == 0) break; 34 0 34
571 19 ▶ if (i == 3) croak("%s: a damaged frozen rule", what);
Branch: 1 branch
BranchTrueFalseTotal
if (i == 3) croak("%s: a damaged frozen rule", what); 0 19 19
572 19 r->overlap = (uint8_t)i;
573 19 w = tkr_word(w + wl, end, &wl);
574 19 p = w + wl;
575 38 ▶ while (p < end && *p == ' ') p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && *p == ' ') p++; 38 0 38
while (p < end && *p == ' ') p++; 19 19 38
576 19 ▶ if (wl == 4 && memcmp(w, "cron", 4) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (wl == 4 && memcmp(w, "cron", 4) == 0) { 6 13 19
if (wl == 4 && memcmp(w, "cron", 4) == 0) { 6 0 6
577 6 ▶ if (!tkr_cron_parse(p, (size_t)(end - p), r, err))
Branch: 1 branch
BranchTrueFalseTotal
if (!tkr_cron_parse(p, (size_t)(end - p), r, err)) 0 6 6
578 0 croak("%s: %s", what, err);
579 }
580 13 ▶ else if (wl == 5 && memcmp(w, "rrule", 5) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (wl == 5 && memcmp(w, "rrule", 5) == 0) { 13 0 13
else if (wl == 5 && memcmp(w, "rrule", 5) == 0) { 13 0 13
581 13 ▶ if (!tkr_rrule_parse(p, (size_t)(end - p), r, err))
Branch: 1 branch
BranchTrueFalseTotal
if (!tkr_rrule_parse(p, (size_t)(end - p), r, err)) 1 12 13
582 1 croak("%s: %s", what, err);
583 }
584 else
585 0 croak("%s: a damaged frozen rule", what);
586 18 }
587  
588 /* A set: "tkrs1 <gap> <overlap> <locale> <start_is_day>" then the lines. */
589 static SV *
590 8 tkr_set_freeze_sv(pTHX_ const tkr_set *s, const char *what)
591 {
592 8 SV *code = sv_2mortal(tkr_locale_code_sv(aTHX_ &s->zone, s->locale));
593 8 SV *out = newSVpvf("tkrs1 %s %s %" SVf " %d\r\n", tkr_gap_names[s->gap], tkr_overlap_names[s->overlap], SVfARG(code), (int)s->start_is_day);
594 8 tkr_ical_write(aTHX_ s, out, 1, what);
595 8 return out;
596 }
597  
598 static void
599 9 tkr_set_thaw(pTHX_ SV *frozen, tkr_set_parts *p, const char *what)
600 {
601 STRLEN len;
602 9 const char *s = SvPV(frozen, len), *end = s + len, *w, *nl;
603 size_t wl;
604 int gap, overlap, i, locale, is_day, neg;
605 9 w = tkr_word(s, end, &wl);
606 9 ▶ if (wl != 5 || memcmp(w, "tkrs1", 5) != 0)
Branch: 2 branches
BranchTrueFalseTotal
if (wl != 5 || memcmp(w, "tkrs1", 5) != 0) 8 1 9
if (wl != 5 || memcmp(w, "tkrs1", 5) != 0) 0 8 8
607 1 croak("%s: not a frozen Time::Keeper::Recurrence::Set", what);
608 8 w = tkr_word(w + wl, end, &wl);
609 17 ▶ for (i = 0; i < 4; i++) if (strlen(tkr_gap_names[i]) == wl && memcmp(w, tkr_gap_names[i], wl) == 0) break;
Branch: 3 branches
BranchTrueFalseTotal
for (i = 0; i < 4; i++) if (strlen(tkr_gap_names[i]) == wl && memcmp(w, tkr_gap_names[i], wl) == 0) break; 8 9 17
for (i = 0; i < 4; i++) if (strlen(tkr_gap_names[i]) == wl && memcmp(w, tkr_gap_names[i], wl) == 0) break; 8 0 8
for (i = 0; i < 4; i++) if (strlen(tkr_gap_names[i]) == wl && memcmp(w, tkr_gap_names[i], wl) == 0) break; 17 0 17
610 8 ▶ if (i == 4) croak("%s: a damaged frozen set", what);
Branch: 1 branch
BranchTrueFalseTotal
if (i == 4) croak("%s: a damaged frozen set", what); 0 8 8
611 8 gap = i;
612 8 w = tkr_word(w + wl, end, &wl);
613 17 ▶ for (i = 0; i < 3; i++) if (strlen(tkr_overlap_names[i]) == wl && memcmp(w, tkr_overlap_names[i], wl) == 0) break;
Branch: 3 branches
BranchTrueFalseTotal
for (i = 0; i < 3; i++) if (strlen(tkr_overlap_names[i]) == wl && memcmp(w, tkr_overlap_names[i], wl) == 0) break; 8 9 17
for (i = 0; i < 3; i++) if (strlen(tkr_overlap_names[i]) == wl && memcmp(w, tkr_overlap_names[i], wl) == 0) break; 8 0 8
for (i = 0; i < 3; i++) if (strlen(tkr_overlap_names[i]) == wl && memcmp(w, tkr_overlap_names[i], wl) == 0) break; 17 0 17
614 8 ▶ if (i == 3) croak("%s: a damaged frozen set", what);
Branch: 1 branch
BranchTrueFalseTotal
if (i == 3) croak("%s: a damaged frozen set", what); 0 8 8
615 8 overlap = i;
616 8 w = tkr_word(w + wl, end, &wl);
617 8 locale = tkr_locale_of_code(aTHX_ w, wl);
618 8 w = tkr_word(w + wl, end, &wl);
619 8 is_day = tkr_num(w, wl, 0, &neg) == 1;
620 8 nl = w + wl;
621 16 ▶ while (nl < end && *nl != '\n') nl++;
Branch: 2 branches
BranchTrueFalseTotal
while (nl < end && *nl != '\n') nl++; 16 0 16
while (nl < end && *nl != '\n') nl++; 8 8 16
622 8 tkr_ical_parse(aTHX_ nl, (size_t)(end - nl), p, what);
623 8 p->gap = gap;
624 8 p->overlap = overlap;
625 8 p->locale = locale;
626 8 p->start_is_day = is_day;
627 17 ▶ for (i = 0; i < (int)(p->n_rules + p->n_exrules); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < (int)(p->n_rules + p->n_exrules); i++) { 9 8 17
628 9 p->rules[i].gap = (uint8_t)gap;
629 9 p->rules[i].overlap = (uint8_t)overlap;
630 }
631 8 }
632  
633 #endif
« include/tkr/tkr_set.h ↑ Summary include/tkr/tkr_obj.h »