2 /*======================================================================
4 CREATOR: eric 02 May 1999
6 $Id: icalderivedvalue.c.in,v 1.15 2007-04-30 13:57:48 artcancro Exp $
9 (C) COPYRIGHT 2000, Eric Busboom, http://www.softwarestudio.org
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of either:
14 The LGPL as published by the Free Software Foundation, version
15 2.1, available at: http://www.fsf.org/copyleft/lesser.html
19 The Mozilla Public License Version 1.0. You may obtain a copy of
20 the License at http://www.mozilla.org/MPL/
22 The original code is icalvalue.c
25 Graham Davison (g.m.davison@computer.org)
28 ======================================================================*/
34 #include "icalerror.h"
35 #include "icalmemory.h"
36 #include "icalparser.h"
37 #include "icalenums.h"
39 #include "icalvalueimpl.h"
41 #include <stdlib.h> /* for malloc */
42 #include <stdio.h> /* for snprintf */
43 #include <string.h> /* For memset, others */
44 #include <stddef.h> /* For offsetof() macro */
46 #include <time.h> /* for mktime */
47 #include <stdlib.h> /* for atoi and atof */
48 #include <limits.h> /* for SHRT_MAX */
51 #define strcasecmp stricmp
54 struct icalvalue_impl* icalvalue_new_impl(icalvalue_kind kind);
56 /* This map associates each of the value types with its string
58 struct icalvalue_kind_map {
66 int icalvalue_kind_is_valid(const icalvalue_kind kind)
70 if (value_map[i].kind == kind)
72 } while (value_map[i++].kind != ICAL_NO_VALUE);
77 const char* icalvalue_kind_to_string(const icalvalue_kind kind)
81 for (i=0; value_map[i].kind != ICAL_NO_VALUE; i++) {
82 if (value_map[i].kind == kind) {
83 return value_map[i].name;
90 icalvalue_kind icalvalue_string_to_kind(const char* str)
94 for (i=0; value_map[i].kind != ICAL_NO_VALUE; i++) {
95 if (strcasecmp(value_map[i].name,str) == 0) {
96 return value_map[i].kind;
100 return value_map[i].kind;
104 icalvalue* icalvalue_new_x (const char* v){
105 struct icalvalue_impl* impl;
106 icalerror_check_arg_rz( (v!=0),"v");
108 impl = icalvalue_new_impl(ICAL_X_VALUE);
110 icalvalue_set_x((icalvalue*)impl,v);
111 return (icalvalue*)impl;
113 void icalvalue_set_x(icalvalue* impl, const char* v) {
114 icalerror_check_arg_rv( (impl!=0),"value");
115 icalerror_check_arg_rv( (v!=0),"v");
117 if(impl->x_value!=0) {free((void*)impl->x_value);}
119 impl->x_value = icalmemory_strdup(v);
121 if (impl->x_value == 0){
126 const char* icalvalue_get_x(const icalvalue* value) {
128 icalerror_check_arg_rz( (value!=0),"value");
129 icalerror_check_value_type(value, ICAL_X_VALUE);
130 return value->x_value;
133 /* Recur is a special case, so it is not auto generated. */
135 icalvalue_new_recur (struct icalrecurrencetype v)
137 struct icalvalue_impl* impl = icalvalue_new_impl(ICAL_RECUR_VALUE);
139 icalvalue_set_recur((icalvalue*)impl,v);
141 return (icalvalue*)impl;
145 icalvalue_set_recur(icalvalue* impl, struct icalrecurrencetype v)
147 icalerror_check_arg_rv( (impl!=0),"value");
148 icalerror_check_value_type(value, ICAL_RECUR_VALUE);
150 if (impl->data.v_recur != 0){
151 free(impl->data.v_recur);
152 impl->data.v_recur = 0;
155 impl->data.v_recur = malloc(sizeof(struct icalrecurrencetype));
157 if (impl->data.v_recur == 0){
158 icalerror_set_errno(ICAL_NEWFAILED_ERROR);
161 memcpy(impl->data.v_recur, &v, sizeof(struct icalrecurrencetype));
166 struct icalrecurrencetype
167 icalvalue_get_recur(const icalvalue* value)
169 struct icalrecurrencetype rt;
170 icalrecurrencetype_clear(&rt);
172 icalerror_check_arg_rx( (value!=0),"value", rt);
173 icalerror_check_value_type(value, ICAL_RECUR_VALUE);
175 return *(value->data.v_recur);
182 icalvalue_new_trigger (struct icaltriggertype v)
184 struct icalvalue_impl* impl = icalvalue_new_impl(ICAL_TRIGGER_VALUE);
186 icalvalue_set_trigger((icalvalue*)impl,v);
188 return (icalvalue*)impl;
192 icalvalue_set_trigger(icalvalue* value, struct icaltriggertype v)
194 icalerror_check_arg_rv( (value!=0),"value");
196 if(!icaltime_is_null_time(v.time)){
197 icalvalue_set_datetime(value,v.time);
198 value->kind = ICAL_DATETIME_VALUE;
200 icalvalue_set_duration(value,v.duration);
201 value->kind = ICAL_DURATION_VALUE;
205 struct icaltriggertype
206 icalvalue_get_trigger(const icalvalue* impl)
208 struct icaltriggertype tr;
210 tr.duration = icaldurationtype_from_int(0);
211 tr.time = icaltime_null_time();
213 icalerror_check_arg_rx( (impl!=0),"value", tr);
216 if(impl->kind == ICAL_DATETIME_VALUE){
217 tr.duration = icaldurationtype_from_int(0);
218 tr.time = impl->data.v_time;
219 } else if(impl->kind == ICAL_DURATION_VALUE){
220 tr.time = icaltime_null_time();
221 tr.duration = impl->data.v_duration;
223 tr.duration = icaldurationtype_from_int(0);
224 tr.time = icaltime_null_time();
225 icalerror_set_errno(ICAL_BADARG_ERROR);
228 tr.duration = icaldurationtype_from_int(0);
229 tr.time = icaltime_null_time();
230 icalerror_set_errno(ICAL_BADARG_ERROR);
237 icalvalue_new_datetime (struct icaltimetype v){
239 struct icalvalue_impl* impl = icalvalue_new_impl(ICAL_DATETIME_VALUE);
240 icalvalue_set_datetime((icalvalue*)impl,v);
241 return (icalvalue*)impl;
245 icalvalue_set_datetime(icalvalue* value, struct icaltimetype v) {
246 struct icalvalue_impl* impl;
247 icalerror_check_arg_rv( (value!=0),"value");
249 icalerror_check_value_type(value, ICAL_DATETIME_VALUE);
250 impl = (struct icalvalue_impl*)value;
253 impl->data.v_time = v;
255 icalvalue_reset_kind(impl);
259 icalvalue_get_datetime (const icalvalue* value) {
260 struct icaltimetype dt;
261 dt = icaltime_null_time();
263 icalerror_check_arg_rx((value!=0),"value", dt);
264 icalerror_check_value_type (value, ICAL_DATETIME_VALUE);
265 return ((struct icalvalue_impl*)value)->data.v_time;
268 /* DATE-TIME-PERIOD is a special case, and is not auto generated */
271 icalvalue_new_datetimeperiod (struct icaldatetimeperiodtype v)
273 struct icalvalue_impl* impl = icalvalue_new_impl(ICAL_DATETIMEPERIOD_VALUE);
275 icalvalue_set_datetimeperiod(impl,v);
277 return (icalvalue*)impl;
281 icalvalue_set_datetimeperiod(icalvalue* impl, struct icaldatetimeperiodtype v)
283 icalerror_check_arg_rv( (impl!=0),"value");
285 icalerror_check_value_type(value, ICAL_DATETIMEPERIOD_VALUE);
287 if(!icaltime_is_null_time(v.time)){
288 if(!icaltime_is_valid_time(v.time)){
289 icalerror_set_errno(ICAL_BADARG_ERROR);
292 impl->kind = ICAL_DATETIME_VALUE;
293 icalvalue_set_datetime(impl,v.time);
294 } else if (!icalperiodtype_is_null_period(v.period)) {
295 if(!icalperiodtype_is_valid_period(v.period)){
296 icalerror_set_errno(ICAL_BADARG_ERROR);
299 impl->kind = ICAL_PERIOD_VALUE;
300 icalvalue_set_period(impl,v.period);
302 icalerror_set_errno(ICAL_BADARG_ERROR);
306 struct icaldatetimeperiodtype
307 icalvalue_get_datetimeperiod(const icalvalue* impl)
309 struct icaldatetimeperiodtype dtp;
311 dtp.period = icalperiodtype_null_period();
312 dtp.time = icaltime_null_time();
314 icalerror_check_arg_rx( (impl!=0),"value", dtp);
315 icalerror_check_value_type(value, ICAL_DATETIMEPERIOD_VALUE);
318 if( impl->kind == ICAL_DATETIME_VALUE || impl->kind == ICAL_DATE_VALUE ){
319 dtp.period = icalperiodtype_null_period();
320 dtp.time = impl->data.v_time;
321 } else if(impl->kind == ICAL_PERIOD_VALUE) {
322 dtp.period = impl->data.v_period;
323 dtp.time = icaltime_null_time();
325 dtp.period = icalperiodtype_null_period();
326 dtp.time = icaltime_null_time();
327 icalerror_set_errno(ICAL_BADARG_ERROR);
330 dtp.period = icalperiodtype_null_period();
331 dtp.time = icaltime_null_time();
332 icalerror_set_errno(ICAL_BADARG_ERROR);
339 icalvalue_new_class (enum icalproperty_class v){
341 struct icalvalue_impl* impl = icalvalue_new_impl(ICAL_CLASS_VALUE);
342 icalvalue_set_class((icalvalue*)impl,v);
343 return (icalvalue*)impl;
347 icalvalue_set_class(icalvalue* value, enum icalproperty_class v) {
348 struct icalvalue_impl* impl;
349 icalerror_check_arg_rv( (value!=0),"value");
351 icalerror_check_value_type(value, ICAL_CLASS_VALUE);
352 impl = (struct icalvalue_impl*)value;
354 impl->data.v_enum = v;
356 icalvalue_reset_kind(impl);
359 enum icalproperty_class
360 icalvalue_get_class (const icalvalue* value) {
362 icalproperty_class pr;
363 pr = ICAL_CLASS_NONE;
365 icalerror_check_arg_rx ((value!=NULL),"value", pr);
366 icalerror_check_arg ((value!=0),"value");
367 icalerror_check_value_type (value, ICAL_CLASS_VALUE);
368 return ((struct icalvalue_impl*)value)->data.v_enum;
372 icalvalue_new_geo (struct icalgeotype v){
374 struct icalvalue_impl* impl = icalvalue_new_impl(ICAL_GEO_VALUE);
375 icalvalue_set_geo((icalvalue*)impl,v);
376 return (icalvalue*)impl;
380 icalvalue_set_geo(icalvalue* value, struct icalgeotype v) {
381 struct icalvalue_impl* impl;
382 icalerror_check_arg_rv( (value!=0),"value");
384 icalerror_check_value_type(value, ICAL_GEO_VALUE);
385 impl = (struct icalvalue_impl*)value;
387 impl->data.v_geo = v;
389 icalvalue_reset_kind(impl);
393 icalvalue_get_geo (const icalvalue* value) {
394 struct icalgeotype gt;
398 icalerror_check_arg_rx((value!=0),"value", gt);
399 icalerror_check_value_type (value, ICAL_GEO_VALUE);
400 return ((struct icalvalue_impl*)value)->data.v_geo;
405 icalvalue_new_attach (icalattach *attach)
407 struct icalvalue_impl *impl;
409 icalerror_check_arg_rz ((attach != NULL), "attach");
411 impl = icalvalue_new_impl (ICAL_ATTACH_VALUE);
417 icalvalue_set_attach ((icalvalue *) impl, attach);
418 return (icalvalue *) impl;
422 icalvalue_set_attach (icalvalue *value, icalattach *attach)
424 struct icalvalue_impl *impl;
426 icalerror_check_arg_rv ((value != NULL), "value");
427 icalerror_check_value_type (value, ICAL_ATTACH_VALUE);
428 icalerror_check_arg_rv ((attach != NULL), "attach");
430 impl = (struct icalvalue_impl *) value;
432 icalattach_ref (attach);
434 if (impl->data.v_attach)
435 icalattach_unref (impl->data.v_attach);
437 impl->data.v_attach = attach;
441 icalvalue_get_attach (const icalvalue *value)
443 icalerror_check_arg_rz ((value != NULL), "value");
444 icalerror_check_value_type (value, ICAL_ATTACH_VALUE);
446 return value->data.v_attach;
455 /* The remaining interfaces are 'new', 'set' and 'get' for each of the value
459 /* Everything below this line is machine generated. Do not edit. */