bump to 1.0.0 and clean up spec file
[platform/upstream/libical.git] / src / libical / icalderivedvalue.c.in
1 /* -*- Mode: C -*- */
2 /*======================================================================
3   FILE: icalvalue.c
4   CREATOR: eric 02 May 1999
5   
6   $Id: icalderivedvalue.c.in,v 1.15 2007-04-30 13:57:48 artcancro Exp $
7
8
9  (C) COPYRIGHT 2000, 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 icalvalue.c
23
24   Contributions from:
25      Graham Davison (g.m.davison@computer.org)
26
27
28 ======================================================================*/
29
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33
34 #include "icalerror.h"
35 #include "icalmemory.h"
36 #include "icalparser.h"
37 #include "icalenums.h"
38
39 #include "icalvalueimpl.h"
40
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 */
45 #include <errno.h>
46 #include <time.h> /* for mktime */
47 #include <stdlib.h> /* for atoi and atof */
48 #include <limits.h> /* for SHRT_MAX */ 
49         
50 #ifdef _MSC_VER
51 #define strcasecmp stricmp
52 #endif
53
54 struct icalvalue_impl*  icalvalue_new_impl(icalvalue_kind kind);
55
56 /* This map associates each of the value types with its string
57    representation */
58 struct icalvalue_kind_map {
59         icalvalue_kind kind;
60         char name[20];
61 };
62
63 <insert_code_here>
64
65
66 int icalvalue_kind_is_valid(const icalvalue_kind kind)
67 {
68     int i = 0;
69     do {
70       if (value_map[i].kind == kind)
71         return 1;
72     } while (value_map[i++].kind != ICAL_NO_VALUE);
73
74     return 0;
75 }  
76
77 const char* icalvalue_kind_to_string(const icalvalue_kind kind)
78 {
79     int i;
80
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;
84         }
85     }
86
87     return 0;
88 }
89
90 icalvalue_kind icalvalue_string_to_kind(const char* str)
91 {
92     int i;
93
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;
97         }
98     }
99
100     return  value_map[i].kind;
101
102 }
103
104 icalvalue* icalvalue_new_x (const char* v){
105    struct icalvalue_impl* impl;
106    icalerror_check_arg_rz( (v!=0),"v");
107
108    impl = icalvalue_new_impl(ICAL_X_VALUE);
109
110    icalvalue_set_x((icalvalue*)impl,v);
111    return (icalvalue*)impl;
112 }
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");
116
117     if(impl->x_value!=0) {free((void*)impl->x_value);}
118
119     impl->x_value = icalmemory_strdup(v);
120
121     if (impl->x_value == 0){
122       errno = ENOMEM;
123     }
124  
125  }
126 const char* icalvalue_get_x(const icalvalue* value) {
127
128     icalerror_check_arg_rz( (value!=0),"value");
129     icalerror_check_value_type(value, ICAL_X_VALUE);
130     return value->x_value;
131 }
132
133 /* Recur is a special case, so it is not auto generated. */
134 icalvalue*
135 icalvalue_new_recur (struct icalrecurrencetype v)
136 {
137    struct icalvalue_impl* impl = icalvalue_new_impl(ICAL_RECUR_VALUE);
138     
139    icalvalue_set_recur((icalvalue*)impl,v);
140
141    return (icalvalue*)impl;
142 }
143
144 void
145 icalvalue_set_recur(icalvalue* impl, struct icalrecurrencetype v)
146 {
147     icalerror_check_arg_rv( (impl!=0),"value");
148     icalerror_check_value_type(value, ICAL_RECUR_VALUE);
149
150     if (impl->data.v_recur != 0){
151         free(impl->data.v_recur);
152         impl->data.v_recur = 0;
153     }
154
155     impl->data.v_recur = malloc(sizeof(struct icalrecurrencetype));
156
157     if (impl->data.v_recur == 0){
158         icalerror_set_errno(ICAL_NEWFAILED_ERROR);
159         return;
160     } else {
161         memcpy(impl->data.v_recur, &v, sizeof(struct icalrecurrencetype));
162     }
163                
164 }
165
166 struct icalrecurrencetype
167 icalvalue_get_recur(const icalvalue* value)
168 {
169     struct icalrecurrencetype rt;
170     icalrecurrencetype_clear(&rt);
171
172     icalerror_check_arg_rx( (value!=0),"value", rt);
173     icalerror_check_value_type(value, ICAL_RECUR_VALUE);
174   
175     return *(value->data.v_recur);
176 }
177
178
179
180
181 icalvalue*
182 icalvalue_new_trigger (struct icaltriggertype v)
183 {
184    struct icalvalue_impl* impl = icalvalue_new_impl(ICAL_TRIGGER_VALUE);
185  
186    icalvalue_set_trigger((icalvalue*)impl,v);
187
188    return (icalvalue*)impl;
189 }
190
191 void
192 icalvalue_set_trigger(icalvalue* value, struct icaltriggertype v)
193 {
194     icalerror_check_arg_rv( (value!=0),"value");
195     
196    if(!icaltime_is_null_time(v.time)){
197        icalvalue_set_datetime(value,v.time);
198        value->kind = ICAL_DATETIME_VALUE;
199    } else {
200        icalvalue_set_duration(value,v.duration);
201        value->kind = ICAL_DURATION_VALUE;
202    }
203 }
204
205 struct icaltriggertype
206 icalvalue_get_trigger(const icalvalue* impl)
207 {
208     struct icaltriggertype tr;
209
210     tr.duration = icaldurationtype_from_int(0);
211     tr.time = icaltime_null_time();
212
213     icalerror_check_arg_rx( (impl!=0),"value", tr);
214
215     if(impl) {
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;
222       } else {
223         tr.duration = icaldurationtype_from_int(0);
224         tr.time = icaltime_null_time();
225         icalerror_set_errno(ICAL_BADARG_ERROR);
226       }
227     } else {
228       tr.duration = icaldurationtype_from_int(0);
229       tr.time = icaltime_null_time();
230       icalerror_set_errno(ICAL_BADARG_ERROR);
231     }
232     
233     return tr;
234 }
235
236 icalvalue*
237 icalvalue_new_datetime (struct icaltimetype v){
238
239    struct icalvalue_impl* impl = icalvalue_new_impl(ICAL_DATETIME_VALUE);
240    icalvalue_set_datetime((icalvalue*)impl,v);
241    return (icalvalue*)impl;
242 }
243
244 void
245 icalvalue_set_datetime(icalvalue* value, struct icaltimetype v) {
246     struct icalvalue_impl* impl;
247     icalerror_check_arg_rv( (value!=0),"value");
248
249     icalerror_check_value_type(value, ICAL_DATETIME_VALUE);
250     impl = (struct icalvalue_impl*)value;
251
252
253     impl->data.v_time = v;
254
255     icalvalue_reset_kind(impl);
256 }
257
258 struct icaltimetype
259 icalvalue_get_datetime (const icalvalue* value) {
260   struct icaltimetype dt;
261   dt = icaltime_null_time();
262
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;
266 }
267
268 /* DATE-TIME-PERIOD is a special case, and is not auto generated */
269
270 icalvalue*
271 icalvalue_new_datetimeperiod (struct icaldatetimeperiodtype v)
272 {
273    struct icalvalue_impl* impl = icalvalue_new_impl(ICAL_DATETIMEPERIOD_VALUE);
274
275    icalvalue_set_datetimeperiod(impl,v);
276
277    return (icalvalue*)impl;
278 }
279
280 void
281 icalvalue_set_datetimeperiod(icalvalue* impl, struct icaldatetimeperiodtype v)
282 {
283     icalerror_check_arg_rv( (impl!=0),"value");
284     
285     icalerror_check_value_type(value, ICAL_DATETIMEPERIOD_VALUE);
286
287     if(!icaltime_is_null_time(v.time)){
288         if(!icaltime_is_valid_time(v.time)){
289             icalerror_set_errno(ICAL_BADARG_ERROR);
290             return;
291         }
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);
297             return;
298         }
299         impl->kind = ICAL_PERIOD_VALUE;
300         icalvalue_set_period(impl,v.period);
301     } else {
302         icalerror_set_errno(ICAL_BADARG_ERROR);
303     }
304 }
305
306 struct icaldatetimeperiodtype
307 icalvalue_get_datetimeperiod(const icalvalue* impl)
308 {
309   struct icaldatetimeperiodtype dtp;
310
311   dtp.period = icalperiodtype_null_period();
312   dtp.time = icaltime_null_time();
313
314   icalerror_check_arg_rx( (impl!=0),"value", dtp);
315   icalerror_check_value_type(value, ICAL_DATETIMEPERIOD_VALUE);
316
317   if(impl) {
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();
324     } else {
325       dtp.period = icalperiodtype_null_period();
326       dtp.time = icaltime_null_time();
327       icalerror_set_errno(ICAL_BADARG_ERROR);
328     }   
329   } else {
330     dtp.period = icalperiodtype_null_period();
331     dtp.time = icaltime_null_time();
332     icalerror_set_errno(ICAL_BADARG_ERROR);
333   }     
334
335   return dtp;
336 }
337
338 icalvalue*
339 icalvalue_new_class (enum icalproperty_class v){
340
341     struct icalvalue_impl* impl = icalvalue_new_impl(ICAL_CLASS_VALUE);
342     icalvalue_set_class((icalvalue*)impl,v);
343     return (icalvalue*)impl;
344 }
345
346 void
347 icalvalue_set_class(icalvalue* value, enum icalproperty_class v) {
348     struct icalvalue_impl* impl;
349     icalerror_check_arg_rv( (value!=0),"value");
350
351     icalerror_check_value_type(value, ICAL_CLASS_VALUE);
352     impl = (struct icalvalue_impl*)value;
353
354     impl->data.v_enum = v;
355
356     icalvalue_reset_kind(impl);
357 }
358
359 enum icalproperty_class
360 icalvalue_get_class (const icalvalue* value) {
361
362     icalproperty_class pr;
363     pr = ICAL_CLASS_NONE;
364
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;
369 }
370
371 icalvalue*
372 icalvalue_new_geo (struct icalgeotype v){
373
374     struct icalvalue_impl* impl = icalvalue_new_impl(ICAL_GEO_VALUE);
375     icalvalue_set_geo((icalvalue*)impl,v);
376     return (icalvalue*)impl;
377 }
378
379 void
380 icalvalue_set_geo(icalvalue* value, struct icalgeotype v) {
381     struct icalvalue_impl* impl;
382     icalerror_check_arg_rv( (value!=0),"value");
383
384     icalerror_check_value_type(value, ICAL_GEO_VALUE);
385     impl = (struct icalvalue_impl*)value;
386
387     impl->data.v_geo = v;
388
389     icalvalue_reset_kind(impl);
390 }
391
392 struct icalgeotype
393 icalvalue_get_geo (const icalvalue* value) {
394     struct icalgeotype gt;
395     gt.lat = 255.0;
396     gt.lon = 255.0;
397
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;
401 }
402
403
404 icalvalue *
405 icalvalue_new_attach (icalattach *attach)
406 {
407     struct icalvalue_impl *impl;
408
409     icalerror_check_arg_rz ((attach != NULL), "attach");
410
411     impl = icalvalue_new_impl (ICAL_ATTACH_VALUE);
412     if (!impl) {
413         errno = ENOMEM;
414         return NULL;
415     }
416
417     icalvalue_set_attach ((icalvalue *) impl, attach);
418     return (icalvalue *) impl;
419 }
420
421 void
422 icalvalue_set_attach (icalvalue *value, icalattach *attach)
423 {
424     struct icalvalue_impl *impl;
425  
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");
429   
430     impl = (struct icalvalue_impl *) value;
431  
432     icalattach_ref (attach);
433
434     if (impl->data.v_attach)
435         icalattach_unref (impl->data.v_attach);
436   
437     impl->data.v_attach = attach;
438 }
439
440 icalattach *
441 icalvalue_get_attach (const icalvalue *value)
442 {
443     icalerror_check_arg_rz ((value != NULL), "value");
444     icalerror_check_value_type (value, ICAL_ATTACH_VALUE);
445     
446     return value->data.v_attach;
447 }
448
449
450
451
452
453
454
455 /* The remaining interfaces are 'new', 'set' and 'get' for each of the value
456    types */
457
458
459 /* Everything below this line is machine generated. Do not edit. */