Imported Upstream version 1.0.0
[platform/upstream/libical.git] / src / libical / icaltypes.c
1 /* -*- Mode: C -*-
2   ======================================================================
3   FILE: icaltypes.c
4   CREATOR: eric 16 May 1999
5   
6   $Id: icaltypes.c,v 1.18 2008-01-15 23:17:42 dothebart Exp $
7   $Locker:  $
8     
9
10  (C) COPYRIGHT 2000, Eric Busboom <eric@softwarestudio.org>
11      http://www.softwarestudio.org
12
13  This program is free software; you can redistribute it and/or modify
14  it under the terms of either: 
15
16     The LGPL as published by the Free Software Foundation, version
17     2.1, available at: http://www.fsf.org/copyleft/lesser.html
18
19   Or:
20
21     The Mozilla Public License Version 1.0. You may obtain a copy of
22     the License at http://www.mozilla.org/MPL/
23
24   The original code is icaltypes.c
25
26  ======================================================================*/
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include "icaltypes.h"
32 #include "icalerror.h"
33 #include "icalmemory.h"
34 #include <stdlib.h> /* for malloc and abs() */
35 #include <errno.h> /* for errno */
36 #include <string.h> /* for icalmemory_strdup */
37 #include <assert.h>
38
39 #if defined(_MSC_VER)
40 #define snprintf _snprintf
41 #define strcasecmp stricmp
42 #endif
43
44 #define TEMP_MAX 1024
45
46 #ifdef HAVE_PTHREAD
47  #include <pthread.h>    
48     static pthread_mutex_t unk_token_mutex = PTHREAD_MUTEX_INITIALIZER;
49 #endif
50
51 static ical_unknown_token_handling unknownTokenHandling = ICAL_TREAT_AS_ERROR;
52
53 int icaltriggertype_is_null_trigger(struct icaltriggertype tr)
54 {
55     if(icaltime_is_null_time(tr.time) && 
56        icaldurationtype_is_null_duration(tr.duration)){
57         return 1;
58     }
59
60     return 0;
61 }
62     
63 int icaltriggertype_is_bad_trigger(struct icaltriggertype tr)
64 {
65     if(icaldurationtype_is_bad_duration(tr.duration)){
66         return 1;
67     }
68
69     return 0;
70 }
71
72 struct icaltriggertype icaltriggertype_from_int(const int reltime)
73 {
74     struct icaltriggertype tr;
75
76     tr.time     = icaltime_null_time();
77     tr.duration = icaldurationtype_from_int(reltime);
78
79     return tr;
80 }
81
82 struct icaltriggertype icaltriggertype_from_string(const char* str)
83 {
84
85     
86     struct icaltriggertype tr;
87     icalerrorstate es = ICAL_ERROR_DEFAULT;
88     icalerrorenum e;
89
90     tr.time= icaltime_null_time();
91     tr.duration = icaldurationtype_from_int(0);
92
93     /* Suppress errors so a failure in icaltime_from_string() does not cause an abort */
94     es = icalerror_get_error_state(ICAL_MALFORMEDDATA_ERROR);
95     if(str == 0) goto error;
96     icalerror_set_error_state(ICAL_MALFORMEDDATA_ERROR,ICAL_ERROR_NONFATAL);
97     e = icalerrno;
98     icalerror_set_errno(ICAL_NO_ERROR);
99
100     tr.time = icaltime_from_string(str);
101
102     if (icaltime_is_null_time(tr.time)){
103
104         tr.duration = icaldurationtype_from_string(str);
105
106         if (icaldurationtype_is_bad_duration(tr.duration)) goto error;
107     } 
108
109     icalerror_set_error_state(ICAL_MALFORMEDDATA_ERROR,es);
110     icalerror_set_errno(e);
111     return tr;
112
113  error:
114     icalerror_set_error_state(ICAL_MALFORMEDDATA_ERROR,es);
115     icalerror_set_errno(ICAL_MALFORMEDDATA_ERROR);
116     return tr;
117
118 }
119
120
121 struct icalreqstattype icalreqstattype_from_string(const char* str)
122 {
123   const char *p1,*p2;
124   struct icalreqstattype stat;
125   short major=0, minor=0;
126
127   icalerror_check_arg((str != 0),"str");
128
129   stat.code = ICAL_UNKNOWN_STATUS;
130   stat.debug = 0; 
131    stat.desc = 0;
132
133   /* Get the status numbers */
134
135   sscanf(str, "%hd.%hd",&major, &minor);
136
137   if (major <= 0 || minor < 0){
138     icalerror_set_errno(ICAL_MALFORMEDDATA_ERROR);
139     return stat;
140   }
141
142   stat.code = icalenum_num_to_reqstat(major, minor);
143
144   if (stat.code == ICAL_UNKNOWN_STATUS){
145     icalerror_set_errno(ICAL_MALFORMEDDATA_ERROR);
146     return stat;
147   }
148   
149
150   p1 = strchr(str,';');
151
152   if (p1 == 0){
153 /*    icalerror_set_errno(ICAL_BADARG_ERROR);*/
154     return stat;
155   }
156
157   /* Just ignore the second clause; it will be taken from inside the library 
158    */
159
160
161
162   p2 = strchr(p1+1,';');
163   if (p2 != 0 && *p2 != 0){
164     stat.debug = p2+1;
165   } 
166
167   return stat;
168   
169 }
170
171 const char* icalreqstattype_as_string(struct icalreqstattype stat)
172 {
173         char *buf;
174         buf = icalreqstattype_as_string_r(stat);
175         icalmemory_add_tmp_buffer(buf);
176         return buf;
177 }
178
179
180 char* icalreqstattype_as_string_r(struct icalreqstattype stat)
181 {
182   char *temp;
183
184   icalerror_check_arg_rz((stat.code != ICAL_UNKNOWN_STATUS),"Status");
185
186   temp = (char*)icalmemory_new_buffer(TEMP_MAX);
187   
188   if (stat.desc == 0){
189     stat.desc = icalenum_reqstat_desc(stat.code);
190   }
191   
192   if(stat.debug != 0){
193     snprintf(temp,TEMP_MAX,"%d.%d;%s;%s", icalenum_reqstat_major(stat.code),
194              icalenum_reqstat_minor(stat.code),
195              stat.desc, stat.debug);
196     
197   } else {
198     snprintf(temp,TEMP_MAX,"%d.%d;%s", icalenum_reqstat_major(stat.code),
199              icalenum_reqstat_minor(stat.code),
200              stat.desc);
201   }
202
203   return temp;
204 }
205
206 ical_unknown_token_handling ical_get_unknown_token_handling_setting(void)
207 {
208     ical_unknown_token_handling myHandling;
209
210 #ifdef HAVE_PTHREAD
211     pthread_mutex_lock (&unk_token_mutex);
212 #endif
213
214     myHandling = unknownTokenHandling;
215
216 #ifdef HAVE_PTHREAD
217     pthread_mutex_unlock (&unk_token_mutex);
218 #endif
219
220     return myHandling;
221 }
222
223 void ical_set_unknown_token_handling_setting(ical_unknown_token_handling newSetting)
224 {
225
226 #ifdef HAVE_PTHREAD
227     pthread_mutex_lock (&unk_token_mutex);
228 #endif
229
230     unknownTokenHandling = newSetting;
231
232 #ifdef HAVE_PTHREAD
233     pthread_mutex_unlock (&unk_token_mutex);
234 #endif
235
236 }