Diffstat (limited to 'libical/src/libical/icalduration.c') (more/less context) (ignore whitespace changes)
-rw-r--r-- | libical/src/libical/icalduration.c | 89 |
1 files changed, 49 insertions, 40 deletions
diff --git a/libical/src/libical/icalduration.c b/libical/src/libical/icalduration.c index 4468e0f..821a3b7 100644 --- a/libical/src/libical/icalduration.c +++ b/libical/src/libical/icalduration.c | |||
@@ -36,14 +36,9 @@ | |||
36 | #include <stdlib.h> | 36 | #include <stdlib.h> |
37 | #include <stdio.h> | 37 | #include <stdio.h> |
38 | 38 | ||
39 | #ifdef ICAL_NO_LIBICAL | ||
40 | #define icalerror_set_errno(x) | ||
41 | #define icalerror_check_arg_rv(x,y) | ||
42 | #define icalerror_check_arg_re(x,y,z) | ||
43 | #else | ||
44 | #include "icalerror.h" | 39 | #include "icalerror.h" |
45 | #include "icalmemory.h" | 40 | #include "icalmemory.h" |
46 | #endif | 41 | #include "icalvalue.h" |
47 | 42 | ||
48 | 43 | ||
49 | 44 | ||
@@ -51,34 +46,32 @@ | |||
51 | /* From Seth Alves, <alves@hungry.com> */ | 46 | /* From Seth Alves, <alves@hungry.com> */ |
52 | struct icaldurationtype icaldurationtype_from_int(int t) | 47 | struct icaldurationtype icaldurationtype_from_int(int t) |
53 | { | 48 | { |
54 | struct icaldurationtype dur; | 49 | struct icaldurationtype dur; |
55 | int used = 0; | 50 | int used = 0; |
56 | 51 | ||
57 | dur = icaldurationtype_null_duration(); | 52 | dur = icaldurationtype_null_duration(); |
58 | 53 | ||
59 | if(t < 0){ | 54 | if(t < 0){ |
60 | dur.is_neg = 1; | 55 | dur.is_neg = 1; |
61 | t = -t; | 56 | t = -t; |
62 | } | 57 | } |
63 | 58 | ||
64 | if (t % (60 * 60 * 24 * 7) == 0) { | 59 | if (t % (60 * 60 * 24 * 7) == 0) { |
65 | dur.weeks = t / (60 * 60 * 24 * 7); | 60 | dur.weeks = t / (60 * 60 * 24 * 7); |
66 | } else { | 61 | } else { |
67 | used += dur.weeks * (60 * 60 * 24 * 7); | 62 | used += dur.weeks * (60 * 60 * 24 * 7); |
68 | dur.days = (t - used) / (60 * 60 * 24); | 63 | dur.days = (t - used) / (60 * 60 * 24); |
69 | used += dur.days * (60 * 60 * 24); | 64 | used += dur.days * (60 * 60 * 24); |
70 | dur.hours = (t - used) / (60 * 60); | 65 | dur.hours = (t - used) / (60 * 60); |
71 | used += dur.hours * (60 * 60); | 66 | used += dur.hours * (60 * 60); |
72 | dur.minutes = (t - used) / (60); | 67 | dur.minutes = (t - used) / (60); |
73 | used += dur.minutes * (60); | 68 | used += dur.minutes * (60); |
74 | dur.seconds = (t - used); | 69 | dur.seconds = (t - used); |
75 | } | 70 | } |
76 | 71 | ||
77 | return dur; | 72 | return dur; |
78 | } | 73 | } |
79 | 74 | ||
80 | #ifndef ICAL_NO_LIBICAL | ||
81 | #include "icalvalue.h" | ||
82 | struct icaldurationtype icaldurationtype_from_string(const char* str) | 75 | struct icaldurationtype icaldurationtype_from_string(const char* str) |
83 | { | 76 | { |
84 | 77 | ||
@@ -138,7 +131,7 @@ struct icaldurationtype icaldurationtype_from_string(const char* str) | |||
138 | 131 | ||
139 | if (begin_flag == 0) goto error; | 132 | if (begin_flag == 0) goto error; |
140 | /* Get all of the digits, not one at a time */ | 133 | /* Get all of the digits, not one at a time */ |
141 | scan_size = sscanf((char*)(str+i),"%d",&digits); | 134 | scan_size = sscanf(&str[i],"%d",&digits); |
142 | if(scan_size == 0) goto error; | 135 | if(scan_size == 0) goto error; |
143 | break; | 136 | break; |
144 | } | 137 | } |
@@ -187,12 +180,11 @@ struct icaldurationtype icaldurationtype_from_string(const char* str) | |||
187 | 180 | ||
188 | error: | 181 | error: |
189 | icalerror_set_errno(ICAL_MALFORMEDDATA_ERROR); | 182 | icalerror_set_errno(ICAL_MALFORMEDDATA_ERROR); |
190 | memset(&d, 0, sizeof(struct icaldurationtype)); | 183 | return icaldurationtype_bad_duration(); |
191 | return d; | ||
192 | |||
193 | } | 184 | } |
194 | 185 | ||
195 | #define TMP_BUF_SIZE 1024 | 186 | #define TMP_BUF_SIZE 1024 |
187 | static | ||
196 | void append_duration_segment(char** buf, char** buf_ptr, size_t* buf_size, | 188 | void append_duration_segment(char** buf, char** buf_ptr, size_t* buf_size, |
197 | char* sep, unsigned int value) { | 189 | char* sep, unsigned int value) { |
198 | 190 | ||
@@ -253,7 +245,7 @@ char* icaldurationtype_as_ical_string(struct icaldurationtype d) | |||
253 | 245 | ||
254 | } | 246 | } |
255 | } else { | 247 | } else { |
256 | icalmemory_append_string(&buf, &buf_ptr, &buf_size, "PTS0"); | 248 | icalmemory_append_string(&buf, &buf_ptr, &buf_size, "PT0S"); |
257 | } | 249 | } |
258 | 250 | ||
259 | output_line = icalmemory_tmp_copy(buf); | 251 | output_line = icalmemory_tmp_copy(buf); |
@@ -263,8 +255,6 @@ char* icaldurationtype_as_ical_string(struct icaldurationtype d) | |||
263 | 255 | ||
264 | } | 256 | } |
265 | 257 | ||
266 | #endif | ||
267 | |||
268 | 258 | ||
269 | /* From Russel Steinthal */ | 259 | /* From Russel Steinthal */ |
270 | int icaldurationtype_as_int(struct icaldurationtype dur) | 260 | int icaldurationtype_as_int(struct icaldurationtype dur) |
@@ -277,7 +267,7 @@ int icaldurationtype_as_int(struct icaldurationtype dur) | |||
277 | * (dur.is_neg==1? -1 : 1) ) ; | 267 | * (dur.is_neg==1? -1 : 1) ) ; |
278 | } | 268 | } |
279 | 269 | ||
280 | struct icaldurationtype icaldurationtype_null_duration() | 270 | struct icaldurationtype icaldurationtype_null_duration(void) |
281 | { | 271 | { |
282 | struct icaldurationtype d; | 272 | struct icaldurationtype d; |
283 | 273 | ||
@@ -295,6 +285,25 @@ int icaldurationtype_is_null_duration(struct icaldurationtype d) | |||
295 | } | 285 | } |
296 | } | 286 | } |
297 | 287 | ||
288 | /* in icalvalue_new_from_string_with_error, we should not call | ||
289 | icaldurationtype_is_null_duration() to see if there is an error | ||
290 | condition. Null duration is perfectly valid for an alarm. | ||
291 | We cannot depend on the caller to check icalerrno either, | ||
292 | following the philosophy of unix errno. we set the is_neg | ||
293 | to -1 to indicate that this is a bad duration. | ||
294 | */ | ||
295 | struct icaldurationtype icaldurationtype_bad_duration() | ||
296 | { | ||
297 | struct icaldurationtype d; | ||
298 | memset(&d,0,sizeof(struct icaldurationtype)); | ||
299 | d.is_neg = -1; | ||
300 | return d; | ||
301 | } | ||
302 | |||
303 | int icaldurationtype_is_bad_duration(struct icaldurationtype d) | ||
304 | { | ||
305 | return (d.is_neg == -1); | ||
306 | } | ||
298 | 307 | ||
299 | 308 | ||
300 | struct icaltimetype icaltime_add(struct icaltimetype t, | 309 | struct icaltimetype icaltime_add(struct icaltimetype t, |
@@ -316,7 +325,7 @@ struct icaldurationtype icaltime_subtract(struct icaltimetype t1, | |||
316 | time_t t1t = icaltime_as_timet(t1); | 325 | time_t t1t = icaltime_as_timet(t1); |
317 | time_t t2t = icaltime_as_timet(t2); | 326 | time_t t2t = icaltime_as_timet(t2); |
318 | 327 | ||
319 | return icaldurationtype_from_int(t1t-t2t); | 328 | return icaldurationtype_from_int((int)(t1t-t2t)); |
320 | 329 | ||
321 | 330 | ||
322 | } | 331 | } |