2 ======================================================================
4 CREATOR: eric 02 June 2000
6 $Id: icalperiod.c,v 1.13 2008-01-15 23:17:41 dothebart Exp $
9 (C) COPYRIGHT 2000, Eric Busboom <eric@softwarestudio.org>
10 http://www.softwarestudio.org
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of either:
15 The LGPL as published by the Free Software Foundation, version
16 2.1, available at: http://www.fsf.org/copyleft/lesser.html
20 The Mozilla Public License Version 1.0. You may obtain a copy of
21 the License at http://www.mozilla.org/MPL/
23 The Original Code is eric. The Initial Developer of the Original
27 ======================================================================*/
33 #include "icalperiod.h"
40 #include "icalerror.h"
41 #include "icalmemory.h"
46 struct icalperiodtype icalperiodtype_from_string (const char* str)
49 struct icalperiodtype p, null_p;
50 char *s = icalmemory_strdup(str);
54 /* Errors are normally generated in the following code, so save
55 the error state for resoration later */
57 icalerrorenum e = icalerrno;
59 p.start = p.end = icaltime_null_time();
60 p.duration = icaldurationtype_from_int(0);
64 if(s == 0) goto error;
69 if(end == 0) goto error;
74 p.start = icaltime_from_string(start);
76 if (icaltime_is_null_time(p.start)) goto error;
78 es = icalerror_get_error_state(ICAL_MALFORMEDDATA_ERROR);
79 icalerror_set_error_state(ICAL_MALFORMEDDATA_ERROR,ICAL_ERROR_NONFATAL);
81 p.end = icaltime_from_string(end);
83 icalerror_set_error_state(ICAL_MALFORMEDDATA_ERROR,es);
86 if (icaltime_is_null_time(p.end)){
88 p.duration = icaldurationtype_from_string(end);
90 if(icaldurationtype_as_int(p.duration) == 0) goto error;
95 icalmemory_free_buffer(s);
100 icalerror_set_errno(ICAL_MALFORMEDDATA_ERROR);
103 icalmemory_free_buffer (s);
108 const char* icalperiodtype_as_ical_string(struct icalperiodtype p)
111 buf = icalperiodtype_as_ical_string_r(p);
112 icalmemory_add_tmp_buffer(buf);
117 char* icalperiodtype_as_ical_string_r(struct icalperiodtype p)
124 size_t buf_size = 40;
127 buf = (char*)icalmemory_new_buffer(buf_size);
131 start = icaltime_as_ical_string_r(p.start);
132 icalmemory_append_string(&buf, &buf_ptr, &buf_size, start);
133 icalmemory_free_buffer((void*)start);
135 if(!icaltime_is_null_time(p.end)){
136 end = icaltime_as_ical_string_r(p.end);
138 end = icaldurationtype_as_ical_string_r(p.duration);
141 icalmemory_append_char(&buf, &buf_ptr, &buf_size, '/');
143 icalmemory_append_string(&buf, &buf_ptr, &buf_size, end);
144 icalmemory_free_buffer((void*)end);
151 struct icalperiodtype icalperiodtype_null_period(void) {
152 struct icalperiodtype p;
153 p.start = icaltime_null_time();
154 p.end = icaltime_null_time();
155 p.duration = icaldurationtype_null_duration();
159 int icalperiodtype_is_null_period(struct icalperiodtype p){
161 if(icaltime_is_null_time(p.start) &&
162 icaltime_is_null_time(p.end) &&
163 icaldurationtype_is_null_duration(p.duration)){
170 int icalperiodtype_is_valid_period(struct icalperiodtype p){
171 if(icaltime_is_valid_time(p.start) &&
172 (icaltime_is_valid_time(p.end) || icaltime_is_null_time(p.end)) )