summaryrefslogtreecommitdiffabout
path: root/libical/src/libicalss/icalcluster.c
Unidiff
Diffstat (limited to 'libical/src/libicalss/icalcluster.c') (more/less context) (ignore whitespace changes)
-rw-r--r--libical/src/libicalss/icalcluster.c245
1 files changed, 245 insertions, 0 deletions
diff --git a/libical/src/libicalss/icalcluster.c b/libical/src/libicalss/icalcluster.c
new file mode 100644
index 0000000..6d11078
--- a/dev/null
+++ b/libical/src/libicalss/icalcluster.c
@@ -0,0 +1,245 @@
1/* -*- Mode: C -*-
2 ======================================================================
3 FILE: icalcluster.c
4 CREATOR: acampi 13 March 2002
5
6 $Id$
7 $Locker$
8
9 (C) COPYRIGHT 2002, Eric Busboom, http://www.softwarestudio.org
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of either:
13
14 The LGPL as published by the Free Software Foundation, version
15 2.1, available at: http://www.fsf.org/copyleft/lesser.html
16
17 Or:
18
19 The Mozilla Public License Version 1.0. You may obtain a copy of
20 the License at http://www.mozilla.org/MPL/
21
22 The Original Code is eric. The Initial Developer of the Original
23 Code is Eric Busboom
24
25
26 ======================================================================*/
27
28
29/**
30 *
31 * icalcluster is an utility class design to manage clusters of
32 * icalcomponents on behalf of an implementation of icalset. This is
33 * done in order to split out common behavior different classes might
34 * need.
35 * The definition of what exactly a cluster will contain depends on the
36 * icalset subclass. At the basic level, an icluster is just a tuple,
37 * with anything as key and an icalcomponent as value.
38 */
39
40
41#ifdef HAVE_CONFIG_H
42#include "config.h"
43#endif
44
45#include <stdlib.h>
46#include <string.h>
47
48#if 0
49#include <errno.h>
50#include <sys/stat.h> /* for stat */
51#ifndef WIN32
52#include <unistd.h> /* for stat, getpid */
53#else
54#include <io.h>
55#include <share.h>
56#endif
57#include <fcntl.h> /* for fcntl */
58#endif
59
60#include "icalcluster.h"
61#include "icalclusterimpl.h"
62#include "icalgauge.h"
63
64#ifdef WIN32
65 #define snprintf_snprintf
66 #define strcasecmpstricmp
67#endif
68
69
70icalcluster * icalcluster_new_impl(void) {
71
72 struct icalcluster_impl* impl;
73
74 if ((impl = (struct icalcluster_impl*)malloc(
75 sizeof(struct icalcluster_impl))) == 0) {
76 icalerror_set_errno(ICAL_NEWFAILED_ERROR);
77 return 0;
78 }
79
80 memset(impl, 0, sizeof(struct icalcluster_impl));
81 strcpy(impl->id,ICALCLUSTER_ID);
82
83 return impl;
84}
85
86/**
87 * Create a cluster with a key/value pair.
88 *
89 * @todo Always do a deep copy.
90 */
91
92icalcluster * icalcluster_new(const char* key, icalcomponent *data) {
93 struct icalcluster_impl *impl = icalcluster_new_impl();
94 assert(impl->data == 0);
95
96 impl->key= strdup(key);
97 impl->changed= 0;
98 impl->data= 0;
99
100 if (data != NULL) {
101 if (icalcomponent_isa(data) != ICAL_XROOT_COMPONENT) {
102 impl->data = icalcomponent_new(ICAL_XROOT_COMPONENT);
103 icalcomponent_add_component(impl->data, data);
104 } else {
105 impl->data = icalcomponent_new_clone(data);
106 }
107 } else {
108 impl->data = icalcomponent_new(ICAL_XROOT_COMPONENT);
109 }
110
111 return impl;
112}
113
114/**
115 * Deep clone an icalcluster to a new one
116 */
117
118icalcluster *icalcluster_new_clone(const icalcluster *data) {
119 struct icalcluster_impl *old = (struct icalcluster_impl *)data;
120 struct icalcluster_impl *impl = icalcluster_new_impl();
121
122 impl->key= strdup(old->key);
123 impl->data= icalcomponent_new_clone(old->data);
124 impl->changed= 0;
125
126 return impl;
127}
128
129
130void icalcluster_free(icalcluster *impl) {
131 icalerror_check_arg_rv((impl!=0),"cluster");
132
133 if (impl->key != 0){
134 free(impl->key);
135 impl->key = 0;
136 }
137
138 if (impl->data != 0){
139 icalcomponent_free(impl->data);
140 impl->data = 0;
141 }
142
143 free(impl);
144}
145
146
147const char *icalcluster_key(icalcluster *impl) {
148 icalerror_check_arg_rz((impl!=0),"cluster");
149
150 return impl->key;
151}
152
153
154int icalcluster_is_changed(icalcluster *impl) {
155 icalerror_check_arg_rz((impl!=0),"cluster");
156
157 return impl->changed;
158}
159
160
161void icalcluster_mark(icalcluster *impl) {
162 icalerror_check_arg_rv((impl!=0),"cluster");
163
164 impl->changed = 1;
165}
166
167
168void icalcluster_commit(icalcluster *impl) {
169 icalerror_check_arg_rv((impl!=0),"cluster");
170
171 impl->changed = 0;
172}
173
174
175icalcomponent *icalcluster_get_component(icalcluster *impl) {
176
177 icalerror_check_arg_rz((impl!=0),"cluster");
178
179 if (icalcomponent_isa(impl->data) != ICAL_XROOT_COMPONENT) {
180 icalerror_warn("The top component is not an XROOT");
181 fprintf(stderr, "%s\n", icalcomponent_as_ical_string(impl->data));
182 abort();
183 }
184
185 return impl->data;
186}
187
188
189icalerrorenum icalcluster_add_component(icalcluster *impl, icalcomponent* child) {
190
191 icalerror_check_arg_re((impl!=0),"cluster", ICAL_BADARG_ERROR);
192 icalerror_check_arg_re((child!=0),"child",ICAL_BADARG_ERROR);
193
194 icalcomponent_add_component(impl->data, child);
195 icalcluster_mark(impl);
196
197 return ICAL_NO_ERROR;
198}
199
200
201icalerrorenum icalcluster_remove_component(icalcluster *impl, icalcomponent* child) {
202
203 icalerror_check_arg_re((impl!=0),"cluster",ICAL_BADARG_ERROR);
204 icalerror_check_arg_re((child!=0),"child",ICAL_BADARG_ERROR);
205
206 icalcomponent_remove_component(impl->data,child);
207 icalcluster_mark(impl);
208
209 return ICAL_NO_ERROR;
210}
211
212
213int icalcluster_count_components(icalcluster *impl, icalcomponent_kind kind) {
214
215 icalerror_check_arg_re((impl!=0),"cluster",ICAL_BADARG_ERROR);
216
217 return icalcomponent_count_components(impl->data, kind);
218}
219
220
221/** Iterate through components **/
222icalcomponent *icalcluster_get_current_component(icalcluster* impl) {
223
224 icalerror_check_arg_rz((impl!=0),"cluster");
225
226 return icalcomponent_get_current_component(impl->data);
227}
228
229
230icalcomponent *icalcluster_get_first_component(icalcluster* impl) {
231
232 icalerror_check_arg_rz((impl!=0),"cluster");
233
234 return icalcomponent_get_first_component(impl->data,
235 ICAL_ANY_COMPONENT);
236}
237
238
239icalcomponent *icalcluster_get_next_component(icalcluster* impl) {
240
241 icalerror_check_arg_rz((impl!=0),"cluster");
242
243 return icalcomponent_get_next_component(impl->data,
244 ICAL_ANY_COMPONENT);
245}