2 ======================================================================
3 FILE: icalderivedparameters.{c,h}
4 CREATOR: eric 09 May 1999
10 (C) COPYRIGHT 2000, Eric Busboom, http://www.softwarestudio.org
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of either:
15 The LGPL as published by the Free Software Foundation, version
16 2.1, available at: http://www.fsf.org/copyleft/lesser.html
20 The Mozilla Public License Version 1.0. You may obtain a copy of
21 the License at http://www.mozilla.org/MPL/
23 The original code is icalderivedparameters.{c,h}
26 Graham Davison (g.m.davison@computer.org)
28 ======================================================================*/
29 /*#line 29 "icalparameter.c.in"*/
35 #include "icalparameter.h"
36 #include "icalproperty.h"
37 #include "icalerror.h"
38 #include "icalmemory.h"
39 #include "icalparameterimpl.h"
41 #include <stdlib.h> /* for malloc() */
43 #include <string.h> /* for memset() */
45 /* In icalderivedparameter */
46 icalparameter* icalparameter_new_from_value_string(icalparameter_kind kind,const char* val);
49 struct icalparameter_impl* icalparameter_new_impl(icalparameter_kind kind)
51 struct icalparameter_impl* v;
53 if ( ( v = (struct icalparameter_impl*)
54 malloc(sizeof(struct icalparameter_impl))) == 0) {
55 icalerror_set_errno(ICAL_NEWFAILED_ERROR);
72 icalparameter_new (icalparameter_kind kind)
74 struct icalparameter_impl* v = icalparameter_new_impl(kind);
76 return (icalparameter*) v;
81 icalparameter_free (icalparameter* parameter)
83 struct icalparameter_impl * impl;
85 impl = (struct icalparameter_impl*)parameter;
87 /* HACK. This always triggers, even when parameter is non-zero
88 icalerror_check_arg_rv((parameter==0),"parameter");*/
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. ");
101 if (impl->string != 0){
102 free ((void*)impl->string);
105 if (impl->x_name != 0){
106 free ((void*)impl->x_name);
109 memset(impl,0,sizeof(impl));
119 icalparameter_new_clone(icalparameter* param)
121 struct icalparameter_impl *old;
122 struct icalparameter_impl *new;
124 old = (struct icalparameter_impl *)param;
125 new = icalparameter_new_impl(old->kind);
127 icalerror_check_arg_rz((param!=0),"param");
133 memcpy(new,old,sizeof(struct icalparameter_impl));
135 if (old->string != 0){
136 new->string = icalmemory_strdup(old->string);
137 if (new->string == 0){
138 icalparameter_free(new);
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);
154 icalparameter* icalparameter_new_from_string(const char *str)
158 icalparameter_kind kind;
159 icalparameter *param;
161 icalerror_check_arg_rz(str != 0,"str");
163 cpy = icalmemory_strdup(str);
166 icalerror_set_errno(ICAL_NEWFAILED_ERROR);
170 eq = strchr(cpy,'=');
173 icalerror_set_errno(ICAL_MALFORMEDDATA_ERROR);
181 kind = icalparameter_string_to_kind(cpy);
183 if(kind == ICAL_NO_PARAMETER){
184 icalerror_set_errno(ICAL_MALFORMEDDATA_ERROR);
188 param = icalparameter_new_from_value_string(kind,eq);
190 if(kind == ICAL_X_PARAMETER){
191 icalparameter_set_xname(param,cpy);
201 icalparameter_as_ical_string (icalparameter* parameter)
203 struct icalparameter_impl* impl;
204 size_t buf_size = 1024;
208 const char *kind_string;
210 icalerror_check_arg_rz( (parameter!=0), "parameter");
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 */
218 buf = icalmemory_new_buffer(buf_size);
220 impl = (struct icalparameter_impl*)parameter;
222 if(impl->kind == ICAL_X_PARAMETER) {
224 icalmemory_append_string(&buf, &buf_ptr, &buf_size,
225 icalparameter_get_xname(impl));
229 kind_string = icalparameter_kind_to_string(impl->kind);
231 if (impl->kind == ICAL_NO_PARAMETER ||
232 impl->kind == ICAL_ANY_PARAMETER ||
235 icalerror_set_errno(ICAL_BADARG_ERROR);
240 /* Put the parameter name into the string */
241 icalmemory_append_string(&buf, &buf_ptr, &buf_size, kind_string);
245 icalmemory_append_string(&buf, &buf_ptr, &buf_size, "=");
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);
253 icalerror_set_errno(ICAL_MALFORMEDDATA_ERROR);
257 /* Now, copy the buffer to a tmp_buffer, which is safe to give to
258 the caller without worring about de-allocating it. */
260 out_buf = icalmemory_tmp_buffer(strlen(buf));
261 strcpy(out_buf, buf);
263 icalmemory_free_buffer(buf);
271 icalparameter_is_valid (icalparameter* parameter);
275 icalparameter_isa (icalparameter* parameter)
278 return ICAL_NO_PARAMETER;
281 return ((struct icalparameter_impl *)parameter)->kind;
286 icalparameter_isa_parameter (void* parameter)
288 struct icalparameter_impl *impl = (struct icalparameter_impl *)parameter;
294 if (strcmp(impl->id,"para") == 0) {
303 icalparameter_set_xname (icalparameter* param, const char* v)
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");
309 if (impl->x_name != 0){
310 free((void*)impl->x_name);
313 impl->x_name = icalmemory_strdup(v);
315 if (impl->x_name == 0){
322 icalparameter_get_xname (icalparameter* param)
324 struct icalparameter_impl *impl = (struct icalparameter_impl*)param;
325 icalerror_check_arg_rz( (param!=0),"param");
331 icalparameter_set_xvalue (icalparameter* param, const char* v)
333 struct icalparameter_impl *impl = (struct icalparameter_impl*)param;
335 icalerror_check_arg_rv( (param!=0),"param");
336 icalerror_check_arg_rv( (v!=0),"v");
338 if (impl->string != 0){
339 free((void*)impl->string);
342 impl->string = icalmemory_strdup(v);
344 if (impl->string == 0){
351 icalparameter_get_xvalue (icalparameter* param)
353 struct icalparameter_impl *impl = (struct icalparameter_impl*)param;
355 icalerror_check_arg_rz( (param!=0),"param");
361 void icalparameter_set_parent(icalparameter* param,
362 icalproperty* property)
364 struct icalparameter_impl *impl = (struct icalparameter_impl*)param;
366 icalerror_check_arg_rv( (param!=0),"param");
368 impl->parent = property;
371 icalproperty* icalparameter_get_parent(icalparameter* param)
373 struct icalparameter_impl *impl = (struct icalparameter_impl*)param;
375 icalerror_check_arg_rz( (param!=0),"param");
381 /* Everything below this line is machine generated. Do not edit. */