Diffstat (limited to 'libical/src/libical/icaltypes.c') (more/less context) (ignore whitespace changes)
-rw-r--r-- | libical/src/libical/icaltypes.c | 285 |
1 files changed, 285 insertions, 0 deletions
diff --git a/libical/src/libical/icaltypes.c b/libical/src/libical/icaltypes.c new file mode 100644 index 0000000..8c67deb --- a/dev/null +++ b/libical/src/libical/icaltypes.c | |||
@@ -0,0 +1,285 @@ | |||
1 | /* -*- Mode: C -*- | ||
2 | ====================================================================== | ||
3 | FILE: icaltypes.c | ||
4 | CREATOR: eric 16 May 1999 | ||
5 | |||
6 | $Id$ | ||
7 | $Locker$ | ||
8 | |||
9 | |||
10 | (C) COPYRIGHT 2000, Eric Busboom, 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 icaltypes.c | ||
24 | |||
25 | ======================================================================*/ | ||
26 | #ifdef HAVE_CONFIG_H | ||
27 | #include "config.h" | ||
28 | #endif | ||
29 | |||
30 | #include "icaltypes.h" | ||
31 | #include "icalerror.h" | ||
32 | #include "icalmemory.h" | ||
33 | #include <stdlib.h> /* for malloc and abs() */ | ||
34 | #include <errno.h> /* for errno */ | ||
35 | #include <string.h> /* for icalmemory_strdup */ | ||
36 | #include <assert.h> | ||
37 | |||
38 | int snprintf(char *str, size_t n, char const *fmt, ...); | ||
39 | |||
40 | #define TEMP_MAX 1024 | ||
41 | |||
42 | void* | ||
43 | icalattachtype_get_data (struct icalattachtype* type); | ||
44 | |||
45 | struct icalattachtype* | ||
46 | icalattachtype_new() | ||
47 | { | ||
48 | struct icalattachtype* v; | ||
49 | |||
50 | if ( ( v = (struct icalattachtype*) | ||
51 | malloc(sizeof(struct icalattachtype))) == 0) { | ||
52 | errno = ENOMEM; | ||
53 | return 0; | ||
54 | } | ||
55 | |||
56 | v->refcount = 1; | ||
57 | |||
58 | v->binary = 0; | ||
59 | v->owns_binary = 0; | ||
60 | v->base64 = 0; | ||
61 | //fprintf(stderr,"setting base 64 to 0 \n"); | ||
62 | |||
63 | v->owns_base64 = 0; | ||
64 | |||
65 | v->url = 0; | ||
66 | |||
67 | return v; | ||
68 | } | ||
69 | |||
70 | |||
71 | void | ||
72 | icalattachtype_free(struct icalattachtype* v) | ||
73 | { | ||
74 | icalerror_check_arg( (v!=0),"v"); | ||
75 | |||
76 | v->refcount--; | ||
77 | |||
78 | if (v->refcount <= 0){ | ||
79 | |||
80 | if (v->base64 != 0 && v->owns_base64 != 0){ | ||
81 | int val = v->base64 ; | ||
82 | if ( val < 255 ) { | ||
83 | fprintf(stderr,"Possible error in attachment processing (%d)\nPocssible solution: Remove attachment from file.\n",val ); | ||
84 | } | ||
85 | free(v->base64); | ||
86 | } | ||
87 | |||
88 | if (v->binary != 0 && v->owns_binary != 0){ | ||
89 | free(v->binary); | ||
90 | } | ||
91 | |||
92 | if (v->url != 0){ | ||
93 | free(v->url); | ||
94 | } | ||
95 | |||
96 | free(v); | ||
97 | } | ||
98 | } | ||
99 | |||
100 | void icalattachtype_add_reference(struct icalattachtype* v) | ||
101 | { | ||
102 | icalerror_check_arg( (v!=0),"v"); | ||
103 | v->refcount++; | ||
104 | } | ||
105 | |||
106 | void icalattachtype_set_url(struct icalattachtype* v, char* url) | ||
107 | { | ||
108 | icalerror_check_arg( (v!=0),"v"); | ||
109 | |||
110 | if (v->url != 0){ | ||
111 | free (v->url); | ||
112 | } | ||
113 | |||
114 | v->url = icalmemory_strdup(url); | ||
115 | |||
116 | /* HACK This routine should do something if icalmemory_strdup returns NULL */ | ||
117 | |||
118 | } | ||
119 | |||
120 | char* icalattachtype_get_url(struct icalattachtype* v) | ||
121 | { | ||
122 | icalerror_check_arg( (v!=0),"v"); | ||
123 | return v->url; | ||
124 | } | ||
125 | |||
126 | void icalattachtype_set_base64(struct icalattachtype* v, char* base64, | ||
127 | int owns) | ||
128 | { | ||
129 | //fprintf(stderr,"1setbase64 %d \n", base64 ); | ||
130 | icalerror_check_arg( (v!=0),"v"); | ||
131 | //fprintf(stderr,"setbase64 %d \n", base64 ); | ||
132 | v->base64 = base64; | ||
133 | v->owns_base64 = !(owns != 0 ); | ||
134 | |||
135 | } | ||
136 | |||
137 | char* icalattachtype_get_base64(struct icalattachtype* v) | ||
138 | { | ||
139 | icalerror_check_arg( (v!=0),"v"); | ||
140 | return v->base64; | ||
141 | } | ||
142 | |||
143 | void icalattachtype_set_binary(struct icalattachtype* v, char* binary, | ||
144 | int owns) | ||
145 | { | ||
146 | icalerror_check_arg( (v!=0),"v"); | ||
147 | |||
148 | v->binary = binary; | ||
149 | v->owns_binary = !(owns != 0 ); | ||
150 | |||
151 | } | ||
152 | |||
153 | void* icalattachtype_get_binary(struct icalattachtype* v) | ||
154 | { | ||
155 | icalerror_check_arg( (v!=0),"v"); | ||
156 | return v->binary; | ||
157 | } | ||
158 | |||
159 | int icaltriggertype_is_null_trigger(struct icaltriggertype tr) | ||
160 | { | ||
161 | if(icaltime_is_null_time(tr.time) && | ||
162 | icaldurationtype_is_null_duration(tr.duration)){ | ||
163 | return 1; | ||
164 | } | ||
165 | |||
166 | return 0; | ||
167 | } | ||
168 | |||
169 | struct icaltriggertype icaltriggertype_from_string(const char* str) | ||
170 | { | ||
171 | |||
172 | |||
173 | struct icaltriggertype tr, null_tr; | ||
174 | icalerrorstate es; | ||
175 | icalerrorenum e; | ||
176 | |||
177 | tr.time= icaltime_null_time(); | ||
178 | tr.duration = icaldurationtype_from_int(0); | ||
179 | |||
180 | null_tr = tr; | ||
181 | |||
182 | if(str == 0) goto error; | ||
183 | |||
184 | /* Surpress errors so a failure in icaltime_from_string() does not cause an abort */ | ||
185 | es = icalerror_get_error_state(ICAL_MALFORMEDDATA_ERROR); | ||
186 | icalerror_set_error_state(ICAL_MALFORMEDDATA_ERROR,ICAL_ERROR_NONFATAL); | ||
187 | e = icalerrno; | ||
188 | icalerror_set_errno(ICAL_NO_ERROR); | ||
189 | |||
190 | tr.time = icaltime_from_string(str); | ||
191 | |||
192 | if (icaltime_is_null_time(tr.time)){ | ||
193 | |||
194 | tr.duration = icaldurationtype_from_string(str); | ||
195 | |||
196 | if(icaldurationtype_as_int(tr.duration) == 0) goto error; | ||
197 | } | ||
198 | |||
199 | icalerror_set_error_state(ICAL_MALFORMEDDATA_ERROR,es); | ||
200 | icalerror_set_errno(e); | ||
201 | return tr; | ||
202 | |||
203 | error: | ||
204 | icalerror_set_error_state(ICAL_MALFORMEDDATA_ERROR,es); | ||
205 | icalerror_set_errno(ICAL_MALFORMEDDATA_ERROR); | ||
206 | return null_tr; | ||
207 | |||
208 | } | ||
209 | |||
210 | |||
211 | struct icalreqstattype icalreqstattype_from_string(const char* str) | ||
212 | { | ||
213 | const char *p1,*p2; | ||
214 | struct icalreqstattype stat; | ||
215 | int major, minor; | ||
216 | |||
217 | icalerror_check_arg((str != 0),"str"); | ||
218 | |||
219 | stat.code = ICAL_UNKNOWN_STATUS; | ||
220 | stat.debug = 0; | ||
221 | stat.desc = 0; | ||
222 | |||
223 | /* Get the status numbers */ | ||
224 | |||
225 | sscanf(str, "%d.%d",&major, &minor); | ||
226 | |||
227 | if (major <= 0 || minor < 0){ | ||
228 | icalerror_set_errno(ICAL_MALFORMEDDATA_ERROR); | ||
229 | return stat; | ||
230 | } | ||
231 | |||
232 | stat.code = icalenum_num_to_reqstat(major, minor); | ||
233 | |||
234 | if (stat.code == ICAL_UNKNOWN_STATUS){ | ||
235 | icalerror_set_errno(ICAL_MALFORMEDDATA_ERROR); | ||
236 | return stat; | ||
237 | } | ||
238 | |||
239 | |||
240 | p1 = strchr(str,';'); | ||
241 | |||
242 | if (p1 == 0){ | ||
243 | /* icalerror_set_errno(ICAL_BADARG_ERROR);*/ | ||
244 | return stat; | ||
245 | } | ||
246 | |||
247 | /* Just ignore the second clause; it will be taken from inside the library | ||
248 | */ | ||
249 | |||
250 | |||
251 | |||
252 | p2 = strchr(p1+1,';'); | ||
253 | if (p2 != 0 && *p2 != 0){ | ||
254 | stat.debug = p2+1; | ||
255 | } | ||
256 | |||
257 | return stat; | ||
258 | |||
259 | } | ||
260 | |||
261 | const char* icalreqstattype_as_string(struct icalreqstattype stat) | ||
262 | { | ||
263 | char *temp; | ||
264 | |||
265 | temp = (char*)icalmemory_tmp_buffer(TEMP_MAX); | ||
266 | |||
267 | icalerror_check_arg_rz((stat.code != ICAL_UNKNOWN_STATUS),"Status"); | ||
268 | |||
269 | if (stat.desc == 0){ | ||
270 | stat.desc = icalenum_reqstat_desc(stat.code); | ||
271 | } | ||
272 | |||
273 | if(stat.debug != 0){ | ||
274 | snprintf(temp,TEMP_MAX,"%d.%d;%s;%s", icalenum_reqstat_major(stat.code), | ||
275 | icalenum_reqstat_minor(stat.code), | ||
276 | stat.desc, stat.debug); | ||
277 | |||
278 | } else { | ||
279 | snprintf(temp,TEMP_MAX,"%d.%d;%s", icalenum_reqstat_major(stat.code), | ||
280 | icalenum_reqstat_minor(stat.code), | ||
281 | stat.desc); | ||
282 | } | ||
283 | |||
284 | return temp; | ||
285 | } | ||