2013-02-13 [colin] 3.9.0cvs65
[claws.git] / src / plugins / vcalendar / libical / libical / icalparameter.c
1 /* -*- Mode: C -*-
2   ======================================================================
3   FILE: icalderivedparameters.{c,h}
4   CREATOR: eric 09 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 icalderivedparameters.{c,h}
24
25   Contributions from:
26      Graham Davison (g.m.davison@computer.org)
27
28  ======================================================================*/
29 /*#line 29 "icalparameter.c.in"*/
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33
34
35 #include "icalparameter.h"
36 #include "icalproperty.h"
37 #include "icalerror.h"
38 #include "icalmemory.h"
39 #include "icalparameterimpl.h"
40
41 #include <stdlib.h> /* for malloc() */
42 #include <errno.h>
43 #include <string.h> /* for memset() */
44
45 /* In icalderivedparameter */
46 icalparameter* icalparameter_new_from_value_string(icalparameter_kind kind,const  char* val);
47
48
49 struct icalparameter_impl* icalparameter_new_impl(icalparameter_kind kind)
50 {
51     struct icalparameter_impl* v;
52
53     if ( ( v = (struct icalparameter_impl*)
54            malloc(sizeof(struct icalparameter_impl))) == 0) {
55         icalerror_set_errno(ICAL_NEWFAILED_ERROR);
56         return 0;
57     }
58     
59     strcpy(v->id,"para");
60
61     v->kind = kind;
62     v->size = 0;
63     v->string = 0;
64     v->x_name = 0;
65     v->parent = 0;
66     v->data = 0;
67
68     return v;
69 }
70
71 icalparameter*
72 icalparameter_new (icalparameter_kind kind)
73 {
74     struct icalparameter_impl* v = icalparameter_new_impl(kind);
75
76     return (icalparameter*) v;
77
78 }
79
80 void
81 icalparameter_free (icalparameter* parameter)
82 {
83     struct icalparameter_impl * impl;
84
85     impl = (struct icalparameter_impl*)parameter;
86
87 /*  HACK. This always triggers, even when parameter is non-zero
88     icalerror_check_arg_rv((parameter==0),"parameter");*/
89
90
91 #ifdef ICAL_FREE_ON_LIST_IS_ERROR
92     icalerror_assert( (impl->parent ==0),"Tried to free a parameter that is still attached to a component. ");
93     
94 #else
95     if(impl->parent !=0){
96         return;
97     }
98 #endif
99
100     
101     if (impl->string != 0){
102         free ((void*)impl->string);
103     }
104     
105     if (impl->x_name != 0){
106         free ((void*)impl->x_name);
107     }
108     
109     memset(impl,0,sizeof(impl));
110
111     impl->parent = 0;
112     impl->id[0] = 'X';
113     free(impl);
114 }
115
116
117
118 icalparameter* 
119 icalparameter_new_clone(icalparameter* param)
120 {
121     struct icalparameter_impl *old;
122     struct icalparameter_impl *new;
123
124     old = (struct icalparameter_impl *)param;
125     new = icalparameter_new_impl(old->kind);
126
127     icalerror_check_arg_rz((param!=0),"param");
128
129     if (new == 0){
130         return 0;
131     }
132
133     memcpy(new,old,sizeof(struct icalparameter_impl));
134
135     if (old->string != 0){
136         new->string = icalmemory_strdup(old->string);
137         if (new->string == 0){
138             icalparameter_free(new);
139             return 0;
140         }
141     }
142
143     if (old->x_name != 0){
144         new->x_name = icalmemory_strdup(old->x_name);
145         if (new->x_name == 0){
146             icalparameter_free(new);
147             return 0;
148         }
149     }
150
151     return new;
152 }
153
154 icalparameter* icalparameter_new_from_string(const char *str)
155 {
156     char* eq;
157     char* cpy;
158     icalparameter_kind kind;
159     icalparameter *param;
160
161     icalerror_check_arg_rz(str != 0,"str");
162
163     cpy = icalmemory_strdup(str);
164
165     if (cpy == 0){
166         icalerror_set_errno(ICAL_NEWFAILED_ERROR);
167         return 0;
168     }
169
170     eq = strchr(cpy,'=');
171
172     if(eq == 0){
173         icalerror_set_errno(ICAL_MALFORMEDDATA_ERROR);
174         return 0;
175     }
176
177     *eq = '\0';
178
179     eq++;
180
181     kind = icalparameter_string_to_kind(cpy);
182
183     if(kind == ICAL_NO_PARAMETER){
184         icalerror_set_errno(ICAL_MALFORMEDDATA_ERROR);
185         return 0;
186     }
187
188     param = icalparameter_new_from_value_string(kind,eq);
189
190     if(kind == ICAL_X_PARAMETER){
191         icalparameter_set_xname(param,cpy);
192     }
193
194     free(cpy);
195
196     return param;
197     
198 }
199
200 char*
201 icalparameter_as_ical_string (icalparameter* parameter)
202 {
203     struct icalparameter_impl* impl;
204     size_t buf_size = 1024;
205     char* buf; 
206     char* buf_ptr;
207     char *out_buf;
208     const char *kind_string;
209
210     icalerror_check_arg_rz( (parameter!=0), "parameter");
211
212     /* Create new buffer that we can append names, parameters and a
213        value to, and reallocate as needed. Later, this buffer will be
214        copied to a icalmemory_tmp_buffer, which is managed internally
215        by libical, so it can be given to the caller without fear of
216        the caller forgetting to free it */
217
218     buf = icalmemory_new_buffer(buf_size);
219     buf_ptr = buf;
220     impl = (struct icalparameter_impl*)parameter;
221
222     if(impl->kind == ICAL_X_PARAMETER) {
223
224         icalmemory_append_string(&buf, &buf_ptr, &buf_size, 
225                                  icalparameter_get_xname(impl));
226
227     } else {
228
229         kind_string = icalparameter_kind_to_string(impl->kind);
230         
231         if (impl->kind == ICAL_NO_PARAMETER || 
232             impl->kind == ICAL_ANY_PARAMETER || 
233             kind_string == 0)
234         {
235             icalerror_set_errno(ICAL_BADARG_ERROR);
236             return 0;
237         }
238         
239         
240         /* Put the parameter name into the string */
241         icalmemory_append_string(&buf, &buf_ptr, &buf_size, kind_string);
242
243     }
244
245     icalmemory_append_string(&buf, &buf_ptr, &buf_size, "=");
246
247     if(impl->string !=0){
248         icalmemory_append_string(&buf, &buf_ptr, &buf_size, impl->string); 
249     } else if (impl->data != 0){
250         const char* str = icalparameter_enum_to_string(impl->data);
251         icalmemory_append_string(&buf, &buf_ptr, &buf_size, str); 
252     } else {
253         icalerror_set_errno(ICAL_MALFORMEDDATA_ERROR);
254         return 0;
255     }
256
257     /* Now, copy the buffer to a tmp_buffer, which is safe to give to
258        the caller without worring about de-allocating it. */
259     
260     out_buf = icalmemory_tmp_buffer(strlen(buf));
261     strcpy(out_buf, buf);
262
263     icalmemory_free_buffer(buf);
264
265     return out_buf;
266
267 }
268
269
270 int
271 icalparameter_is_valid (icalparameter* parameter);
272
273
274 icalparameter_kind
275 icalparameter_isa (icalparameter* parameter)
276 {
277     if(parameter == 0){
278         return ICAL_NO_PARAMETER;
279     }
280
281     return ((struct icalparameter_impl *)parameter)->kind;
282 }
283
284
285 int
286 icalparameter_isa_parameter (void* parameter)
287 {
288     struct icalparameter_impl *impl = (struct icalparameter_impl *)parameter;
289
290     if (parameter == 0){
291         return 0;
292     }
293
294     if (strcmp(impl->id,"para") == 0) {
295         return 1;
296     } else {
297         return 0;
298     }
299 }
300
301
302 void
303 icalparameter_set_xname (icalparameter* param, const char* v)
304 {
305     struct icalparameter_impl *impl = (struct icalparameter_impl*)param;
306     icalerror_check_arg_rv( (param!=0),"param");
307     icalerror_check_arg_rv( (v!=0),"v");
308
309     if (impl->x_name != 0){
310         free((void*)impl->x_name);
311     }
312
313     impl->x_name = icalmemory_strdup(v);
314
315     if (impl->x_name == 0){
316         errno = ENOMEM;
317     }
318
319 }
320
321 const char*
322 icalparameter_get_xname (icalparameter* param)
323 {
324     struct icalparameter_impl *impl = (struct icalparameter_impl*)param;
325     icalerror_check_arg_rz( (param!=0),"param");
326
327     return impl->x_name;
328 }
329
330 void
331 icalparameter_set_xvalue (icalparameter* param, const char* v)
332 {
333     struct icalparameter_impl *impl = (struct icalparameter_impl*)param;
334
335     icalerror_check_arg_rv( (param!=0),"param");
336     icalerror_check_arg_rv( (v!=0),"v");
337
338     if (impl->string != 0){
339         free((void*)impl->string);
340     }
341
342     impl->string = icalmemory_strdup(v);
343
344     if (impl->string == 0){
345         errno = ENOMEM;
346     }
347
348 }
349
350 const char*
351 icalparameter_get_xvalue (icalparameter* param)
352 {
353     struct icalparameter_impl *impl = (struct icalparameter_impl*)param;
354
355     icalerror_check_arg_rz( (param!=0),"param");
356
357     return impl->string;
358
359 }
360
361 void icalparameter_set_parent(icalparameter* param,
362                              icalproperty* property)
363 {
364     struct icalparameter_impl *impl = (struct icalparameter_impl*)param;
365
366     icalerror_check_arg_rv( (param!=0),"param");
367
368     impl->parent = property;
369 }
370
371 icalproperty* icalparameter_get_parent(icalparameter* param)
372 {
373     struct icalparameter_impl *impl = (struct icalparameter_impl*)param;
374
375     icalerror_check_arg_rz( (param!=0),"param");
376
377     return impl->parent;
378 }
379
380
381 /* Everything below this line is machine generated. Do not edit. */
382 /* ALTREP */