1 /* -*- Mode: C -*-
2 ======================================================================
3 FILE: icalperiod.c
4 CREATOR: eric 02 June 2000
5
6 $Id: icalperiod.c,v 1.13 2008-01-15 23:17:41 dothebart Exp $
7 $Locker: $
8
9 (C) COPYRIGHT 2000, Eric Busboom <eric@softwarestudio.org>
10 http://www.softwarestudio.org
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of either:
14
15 The LGPL as published by the Free Software Foundation, version
16 2.1, available at: http://www.fsf.org/copyleft/lesser.html
17
18 Or:
19
20 The Mozilla Public License Version 1.0. You may obtain a copy of
21 the License at http://www.mozilla.org/MPL/
22
23 The Original Code is eric. The Initial Developer of the Original
24 Code is Eric Busboom
25
26
27 ======================================================================*/
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include "icalperiod.h"
34
35 #include <assert.h>
36 #include <string.h>
37 #include <stdlib.h>
38 #include <stdio.h>
39
40 #include "icalerror.h"
41 #include "icalmemory.h"
42
43
44
45
icalperiodtype_from_string(const char * str)46 struct icalperiodtype icalperiodtype_from_string (const char* str)
47 {
48
49 struct icalperiodtype p, null_p;
50 char *s = icalmemory_strdup(str);
51 char *start, *end = s;
52 icalerrorstate es;
53
54 /* Errors are normally generated in the following code, so save
55 the error state for resoration later */
56
57 icalerrorenum e = icalerrno;
58
59 p.start = p.end = icaltime_null_time();
60 p.duration = icaldurationtype_from_int(0);
61
62 null_p = p;
63
64 if(s == 0) goto error;
65
66 start = s;
67 end = strchr(s, '/');
68
69 if(end == 0) goto error;
70
71 *end = 0;
72 end++;
73
74 p.start = icaltime_from_string(start);
75
76 if (icaltime_is_null_time(p.start)) goto error;
77
78 es = icalerror_get_error_state(ICAL_MALFORMEDDATA_ERROR);
79 icalerror_set_error_state(ICAL_MALFORMEDDATA_ERROR,ICAL_ERROR_NONFATAL);
80
81 p.end = icaltime_from_string(end);
82
83 icalerror_set_error_state(ICAL_MALFORMEDDATA_ERROR,es);
84
85
86 if (icaltime_is_null_time(p.end)){
87
88 p.duration = icaldurationtype_from_string(end);
89
90 if(icaldurationtype_as_int(p.duration) == 0) goto error;
91 }
92
93 icalerrno = e;
94
95 icalmemory_free_buffer(s);
96
97 return p;
98
99 error:
100 icalerror_set_errno(ICAL_MALFORMEDDATA_ERROR);
101
102 if (s)
103 icalmemory_free_buffer (s);
104 return null_p;
105 }
106
107
icalperiodtype_as_ical_string(struct icalperiodtype p)108 const char* icalperiodtype_as_ical_string(struct icalperiodtype p)
109 {
110 char *buf;
111 buf = icalperiodtype_as_ical_string_r(p);
112 icalmemory_add_tmp_buffer(buf);
113 return buf;
114 }
115
116
icalperiodtype_as_ical_string_r(struct icalperiodtype p)117 char* icalperiodtype_as_ical_string_r(struct icalperiodtype p)
118 {
119
120 char* start;
121 char* end;
122
123 char *buf;
124 size_t buf_size = 40;
125 char* buf_ptr = 0;
126
127 buf = (char*)icalmemory_new_buffer(buf_size);
128 buf_ptr = buf;
129
130
131 start = icaltime_as_ical_string_r(p.start);
132 icalmemory_append_string(&buf, &buf_ptr, &buf_size, start);
133 icalmemory_free_buffer(start);
134
135 if(!icaltime_is_null_time(p.end)){
136 end = icaltime_as_ical_string_r(p.end);
137 } else {
138 end = icaldurationtype_as_ical_string_r(p.duration);
139 }
140
141 icalmemory_append_char(&buf, &buf_ptr, &buf_size, '/');
142
143 icalmemory_append_string(&buf, &buf_ptr, &buf_size, end);
144 icalmemory_free_buffer(end);
145
146 return buf;
147 }
148
149
150
icalperiodtype_null_period(void)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();
156
157 return p;
158 }
icalperiodtype_is_null_period(struct icalperiodtype p)159 int icalperiodtype_is_null_period(struct icalperiodtype p){
160
161 if(icaltime_is_null_time(p.start) &&
162 icaltime_is_null_time(p.end) &&
163 icaldurationtype_is_null_duration(p.duration)){
164 return 1;
165 } else {
166 return 0;
167 }
168 }
169
icalperiodtype_is_valid_period(struct icalperiodtype p)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)) )
173 {
174 return 1;
175 }
176
177 return 0;
178 }
179
180