Diffstat (limited to 'libical/src/libicalss/icalset.c') (more/less context) (ignore whitespace changes)
-rw-r--r-- | libical/src/libicalss/icalset.c | 367 |
1 files changed, 367 insertions, 0 deletions
diff --git a/libical/src/libicalss/icalset.c b/libical/src/libicalss/icalset.c new file mode 100644 index 0000000..2120609 --- a/dev/null +++ b/libical/src/libicalss/icalset.c | |||
@@ -0,0 +1,367 @@ | |||
1 | /* -*- Mode: C -*- */ | ||
2 | /*====================================================================== | ||
3 | FILE: icalset.c | ||
4 | CREATOR: eric 17 Jul 2000 | ||
5 | |||
6 | |||
7 | Icalset is the "base class" for representations of a collection of | ||
8 | iCal components. Derived classes (actually delegates) include: | ||
9 | |||
10 | icalfileset Store components in a single file | ||
11 | icaldirset Store components in multiple files in a directory | ||
12 | icalheapset Store components on the heap | ||
13 | icalmysqlset Store components in a mysql database. | ||
14 | |||
15 | $Id$ | ||
16 | $Locker$ | ||
17 | |||
18 | (C) COPYRIGHT 2000, Eric Busboom, http://www.softwarestudio.org | ||
19 | |||
20 | This program is free software; you can redistribute it and/or modify | ||
21 | it under the terms of either: | ||
22 | |||
23 | The LGPL as published by the Free Software Foundation, version | ||
24 | 2.1, available at: http://www.fsf.org/copyleft/lesser.html | ||
25 | |||
26 | Or: | ||
27 | |||
28 | The Mozilla Public License Version 1.0. You may obtain a copy of | ||
29 | the License at http://www.mozilla.org/MPL/ | ||
30 | |||
31 | The Original Code is eric. The Initial Developer of the Original | ||
32 | Code is Eric Busboom | ||
33 | |||
34 | |||
35 | ======================================================================*/ | ||
36 | |||
37 | #include "ical.h" | ||
38 | #include "icalset.h" | ||
39 | #include "icalfileset.h" | ||
40 | #include "icalfilesetimpl.h" | ||
41 | #include "icaldirset.h" | ||
42 | #include "icaldirsetimpl.h" | ||
43 | #include <stdlib.h> | ||
44 | /*#include "icalheapset.h"*/ | ||
45 | /*#include "icalmysqlset.h"*/ | ||
46 | |||
47 | #define ICALSET_ID "set " | ||
48 | |||
49 | struct icalset_fp { | ||
50 | void (*free)(icalset* set); | ||
51 | const char* (*path)(icalset* set); | ||
52 | void (*mark)(icalset* set); | ||
53 | icalerrorenum (*commit)(icalset* set); | ||
54 | icalerrorenum (*add_component)(icalset* set, icalcomponent* comp); | ||
55 | icalerrorenum (*remove_component)(icalset* set, icalcomponent* comp); | ||
56 | int (*count_components)(icalset* set, | ||
57 | icalcomponent_kind kind); | ||
58 | icalerrorenum (*select)(icalset* set, icalcomponent* gauge); | ||
59 | void (*clear)(icalset* set); | ||
60 | icalcomponent* (*fetch)(icalset* set, const char* uid); | ||
61 | icalcomponent* (*fetch_match)(icalset* set, icalcomponent *comp); | ||
62 | int (*has_uid)(icalset* set, const char* uid); | ||
63 | icalerrorenum (*modify)(icalset* set, icalcomponent *old, | ||
64 | icalcomponent *new); | ||
65 | icalcomponent* (*get_current_component)(icalset* set); | ||
66 | icalcomponent* (*get_first_component)(icalset* set); | ||
67 | icalcomponent* (*get_next_component)(icalset* set); | ||
68 | }; | ||
69 | |||
70 | struct icalset_fp icalset_dirset_fp = { | ||
71 | icaldirset_free, | ||
72 | icaldirset_path, | ||
73 | icaldirset_mark, | ||
74 | icaldirset_commit, | ||
75 | icaldirset_add_component, | ||
76 | icaldirset_remove_component, | ||
77 | icaldirset_count_components, | ||
78 | icaldirset_select, | ||
79 | icaldirset_clear, | ||
80 | icaldirset_fetch, | ||
81 | icaldirset_fetch_match, | ||
82 | icaldirset_has_uid, | ||
83 | icaldirset_modify, | ||
84 | icaldirset_get_current_component, | ||
85 | icaldirset_get_first_component, | ||
86 | icaldirset_get_next_component | ||
87 | }; | ||
88 | |||
89 | |||
90 | struct icalset_fp icalset_fileset_fp = { | ||
91 | icalfileset_free, | ||
92 | icalfileset_path, | ||
93 | icalfileset_mark, | ||
94 | icalfileset_commit, | ||
95 | icalfileset_add_component, | ||
96 | icalfileset_remove_component, | ||
97 | icalfileset_count_components, | ||
98 | icalfileset_select, | ||
99 | icalfileset_clear, | ||
100 | icalfileset_fetch, | ||
101 | icalfileset_fetch_match, | ||
102 | icalfileset_has_uid, | ||
103 | icalfileset_modify, | ||
104 | icalfileset_get_current_component, | ||
105 | icalfileset_get_first_component, | ||
106 | icalfileset_get_next_component | ||
107 | }; | ||
108 | |||
109 | struct icalset_impl { | ||
110 | |||
111 | char id[5]; /* "set " */ | ||
112 | |||
113 | void *derived_impl; | ||
114 | struct icalset_fp *fp; | ||
115 | }; | ||
116 | |||
117 | /* Figure out what was actually passed in as the set. This could be a | ||
118 | set or and of the derived types such as dirset or fileset. Note | ||
119 | this routine returns a value, not a reference, to avoid memory | ||
120 | leaks in the methods */ | ||
121 | struct icalset_impl icalset_get_impl(icalset* set) | ||
122 | { | ||
123 | struct icalset_impl impl; | ||
124 | |||
125 | memset(&impl,0,sizeof(impl)); | ||
126 | icalerror_check_arg_re( (set!=0),"set",impl); | ||
127 | |||
128 | if(strcmp((char*)set,ICALSET_ID)==0) { | ||
129 | /* It is actually a set, so just sent the reference back out. */ | ||
130 | return *(struct icalset_impl*)set; | ||
131 | } else if(strcmp((char*)set,ICALFILESET_ID)==0) { | ||
132 | /* Make a new set from the fileset */ | ||
133 | impl.fp = &icalset_fileset_fp; | ||
134 | impl.derived_impl = set; | ||
135 | strcpy(impl.id,ICALFILESET_ID);/* HACK. Is this necessary? */ | ||
136 | return impl; | ||
137 | } else if(strcmp((char*)set,ICALDIRSET_ID)==0) { | ||
138 | /* Make a new set from the dirset */ | ||
139 | impl.fp = &icalset_dirset_fp; | ||
140 | impl.derived_impl = set; | ||
141 | strcpy(impl.id,ICALDIRSET_ID);/* HACK. Is this necessary? */ | ||
142 | return impl; | ||
143 | } else { | ||
144 | /* The type of set is unknown, so throw an error */ | ||
145 | icalerror_assert((0),"Unknown set type"); | ||
146 | return impl; | ||
147 | } | ||
148 | } | ||
149 | |||
150 | |||
151 | struct icalset_impl* icalset_new_impl() | ||
152 | { | ||
153 | |||
154 | struct icalset_impl* impl; | ||
155 | |||
156 | if ( ( impl = (struct icalset_impl*) | ||
157 | malloc(sizeof(struct icalset_impl))) == 0) { | ||
158 | icalerror_set_errno(ICAL_NEWFAILED_ERROR); | ||
159 | return 0; | ||
160 | } | ||
161 | |||
162 | strcpy(impl->id,ICALSET_ID); | ||
163 | |||
164 | impl->derived_impl = 0; | ||
165 | impl->fp = 0; | ||
166 | |||
167 | return impl; | ||
168 | } | ||
169 | |||
170 | struct icalset_impl* icalset_new_file_from_ref(icalfileset *fset) | ||
171 | { | ||
172 | struct icalset_impl *impl = icalset_new_impl(); | ||
173 | |||
174 | icalerror_check_arg_rz( (fset!=0),"fset"); | ||
175 | |||
176 | if(impl == 0){ | ||
177 | free(impl); | ||
178 | return 0; | ||
179 | } | ||
180 | |||
181 | impl->derived_impl = fset; | ||
182 | |||
183 | if (impl->derived_impl == 0){ | ||
184 | free(impl); | ||
185 | return 0; | ||
186 | } | ||
187 | |||
188 | impl->fp = &icalset_fileset_fp; | ||
189 | |||
190 | return (struct icalset_impl*)impl; | ||
191 | } | ||
192 | |||
193 | icalset* icalset_new_file(const char* path) | ||
194 | { | ||
195 | icalfileset *fset = icalfileset_new(path); | ||
196 | |||
197 | if(fset == 0){ | ||
198 | return 0; | ||
199 | } | ||
200 | |||
201 | return (icalset*)icalset_new_file_from_ref(fset); | ||
202 | } | ||
203 | |||
204 | icalset* icalset_new_dir_from_ref(icaldirset *dset) | ||
205 | { | ||
206 | |||
207 | struct icalset_impl *impl = icalset_new_impl(); | ||
208 | |||
209 | icalerror_check_arg_rz( (dset!=0),"dset"); | ||
210 | |||
211 | if(impl == 0){ | ||
212 | return 0; | ||
213 | } | ||
214 | |||
215 | impl->derived_impl = dset; | ||
216 | |||
217 | if (impl->derived_impl == 0){ | ||
218 | free(impl); | ||
219 | return 0; | ||
220 | } | ||
221 | |||
222 | impl->fp = &icalset_dirset_fp; | ||
223 | |||
224 | return impl; | ||
225 | } | ||
226 | |||
227 | icalset* icalset_new_dir(const char* path) | ||
228 | { | ||
229 | icaldirset *dset = icaldirset_new(path); | ||
230 | |||
231 | if(dset == 0){ | ||
232 | return 0; | ||
233 | } | ||
234 | |||
235 | return icalset_new_dir_from_ref(dset); | ||
236 | } | ||
237 | |||
238 | icalset* icalset_new_heap(void) | ||
239 | { | ||
240 | struct icalset_impl *impl = icalset_new_impl(); | ||
241 | |||
242 | |||
243 | if(impl == 0){ | ||
244 | free(impl); | ||
245 | return 0; | ||
246 | } | ||
247 | |||
248 | return 0; | ||
249 | } | ||
250 | |||
251 | icalset* icalset_new_mysql(const char* path) | ||
252 | { | ||
253 | struct icalset_impl *impl = icalset_new_impl(); | ||
254 | |||
255 | if(impl == 0){ | ||
256 | free(impl); | ||
257 | return 0; | ||
258 | } | ||
259 | |||
260 | return 0; | ||
261 | } | ||
262 | |||
263 | void icalset_free(icalset* set) | ||
264 | { | ||
265 | struct icalset_impl impl = icalset_get_impl(set); | ||
266 | (*(impl.fp->free))(impl.derived_impl); | ||
267 | |||
268 | if(strcmp((char*)set,ICALSET_ID)) { | ||
269 | free(set); | ||
270 | } | ||
271 | } | ||
272 | |||
273 | const char* icalset_path(icalset* set) | ||
274 | { | ||
275 | struct icalset_impl impl = icalset_get_impl(set); | ||
276 | return (*(impl.fp->path))(impl.derived_impl); | ||
277 | } | ||
278 | |||
279 | void icalset_mark(icalset* set) | ||
280 | { | ||
281 | struct icalset_impl impl = icalset_get_impl(set); | ||
282 | (*(impl.fp->mark))(impl.derived_impl); | ||
283 | } | ||
284 | |||
285 | icalerrorenum icalset_commit(icalset* set) | ||
286 | { | ||
287 | struct icalset_impl impl = icalset_get_impl(set); | ||
288 | return (*(impl.fp->commit))(impl.derived_impl); | ||
289 | } | ||
290 | |||
291 | icalerrorenum icalset_add_component(icalset* set, icalcomponent* comp) | ||
292 | { | ||
293 | struct icalset_impl impl = icalset_get_impl(set); | ||
294 | return (*(impl.fp->add_component))(impl.derived_impl,comp); | ||
295 | } | ||
296 | |||
297 | icalerrorenum icalset_remove_component(icalset* set, icalcomponent* comp) | ||
298 | { | ||
299 | struct icalset_impl impl = icalset_get_impl(set); | ||
300 | return (*(impl.fp->remove_component))(impl.derived_impl,comp); | ||
301 | } | ||
302 | |||
303 | int icalset_count_components(icalset* set,icalcomponent_kind kind) | ||
304 | { | ||
305 | struct icalset_impl impl = icalset_get_impl(set); | ||
306 | return (*(impl.fp->count_components))(impl.derived_impl,kind); | ||
307 | } | ||
308 | |||
309 | icalerrorenum icalset_select(icalset* set, icalcomponent* gauge) | ||
310 | { | ||
311 | struct icalset_impl impl = icalset_get_impl(set); | ||
312 | return (*(impl.fp->select))(impl.derived_impl,gauge); | ||
313 | } | ||
314 | |||
315 | void icalset_clear(icalset* set) | ||
316 | { | ||
317 | struct icalset_impl impl = icalset_get_impl(set); | ||
318 | (*(impl.fp->clear))(impl.derived_impl); | ||
319 | } | ||
320 | |||
321 | icalcomponent* icalset_fetch(icalset* set, const char* uid) | ||
322 | { | ||
323 | struct icalset_impl impl = icalset_get_impl(set); | ||
324 | return (*(impl.fp->fetch))(impl.derived_impl,uid); | ||
325 | } | ||
326 | |||
327 | icalcomponent* icalset_fetch_match(icalset* set, icalcomponent *comp) | ||
328 | { | ||
329 | struct icalset_impl impl = icalset_get_impl(set); | ||
330 | return (*(impl.fp->fetch_match))(impl.derived_impl,comp); | ||
331 | } | ||
332 | |||
333 | |||
334 | int icalset_has_uid(icalset* set, const char* uid) | ||
335 | { | ||
336 | struct icalset_impl impl = icalset_get_impl(set); | ||
337 | return (*(impl.fp->has_uid))(impl.derived_impl,uid); | ||
338 | } | ||
339 | |||
340 | icalerrorenum icalset_modify(icalset* set, icalcomponent *old, | ||
341 | icalcomponent *new) | ||
342 | { | ||
343 | struct icalset_impl impl = icalset_get_impl(set); | ||
344 | return (*(impl.fp->modify))(impl.derived_impl,old,new); | ||
345 | } | ||
346 | |||
347 | icalcomponent* icalset_get_current_component(icalset* set) | ||
348 | { | ||
349 | struct icalset_impl impl = icalset_get_impl(set); | ||
350 | return (*(impl.fp->get_current_component))(impl.derived_impl); | ||
351 | } | ||
352 | |||
353 | icalcomponent* icalset_get_first_component(icalset* set) | ||
354 | { | ||
355 | struct icalset_impl impl = icalset_get_impl(set); | ||
356 | return (*(impl.fp->get_first_component))(impl.derived_impl); | ||
357 | } | ||
358 | |||
359 | icalcomponent* icalset_get_next_component(icalset* set) | ||
360 | { | ||
361 | struct icalset_impl impl = icalset_get_impl(set); | ||
362 | return (*(impl.fp->get_next_component))(impl.derived_impl); | ||
363 | } | ||
364 | |||
365 | |||
366 | |||
367 | |||