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 #ifdef WIN32
40 #if defined(_MSC_VER) && (_MSC_VER < 1900)
41 #define snprintf _snprintf
42 #endif
43 #define strcasecmp    stricmp
44 #endif
45 
46 #define TEMP_MAX 1024
47 
48 #ifdef HAVE_PTHREAD
49  #include <pthread.h>
50     static pthread_mutex_t unk_token_mutex = PTHREAD_MUTEX_INITIALIZER;
51 #endif
52 
53 static ical_unknown_token_handling unknownTokenHandling = ICAL_TREAT_AS_ERROR;
54 
icaltriggertype_is_null_trigger(struct icaltriggertype tr)55 int icaltriggertype_is_null_trigger(struct icaltriggertype tr)
56 {
57     if(icaltime_is_null_time(tr.time) &&
58        icaldurationtype_is_null_duration(tr.duration)){
59         return 1;
60     }
61 
62     return 0;
63 }
64 
icaltriggertype_is_bad_trigger(struct icaltriggertype tr)65 int icaltriggertype_is_bad_trigger(struct icaltriggertype tr)
66 {
67     if(icaldurationtype_is_bad_duration(tr.duration)){
68         return 1;
69     }
70 
71     return 0;
72 }
73 
icaltriggertype_from_int(const int reltime)74 struct icaltriggertype icaltriggertype_from_int(const int reltime)
75 {
76     struct icaltriggertype tr;
77 
78     tr.time	= icaltime_null_time();
79     tr.duration = icaldurationtype_from_int(reltime);
80 
81     return tr;
82 }
83 
icaltriggertype_from_string(const char * str)84 struct icaltriggertype icaltriggertype_from_string(const char* str)
85 {
86 
87 
88     struct icaltriggertype tr, null_tr;
89     icalerrorstate es = ICAL_ERROR_DEFAULT;
90     icalerrorenum e;
91 
92     tr.time= icaltime_null_time();
93     tr.duration = icaldurationtype_from_int(0);
94 
95     null_tr = tr;
96 
97 
98     /* Suppress errors so a failure in icaltime_from_string() does not cause an abort */
99     es = icalerror_get_error_state(ICAL_MALFORMEDDATA_ERROR);
100     if(str == 0) goto error;
101     icalerror_set_error_state(ICAL_MALFORMEDDATA_ERROR,ICAL_ERROR_NONFATAL);
102     e = icalerrno;
103     icalerror_set_errno(ICAL_NO_ERROR);
104 
105     tr.time = icaltime_from_string(str);
106 
107     if (icaltime_is_null_time(tr.time)){
108 
109 	tr.duration = icaldurationtype_from_string(str);
110 
111         if (icaldurationtype_is_bad_duration(tr.duration)) goto error;
112     }
113 
114     icalerror_set_error_state(ICAL_MALFORMEDDATA_ERROR,es);
115     icalerror_set_errno(e);
116     return tr;
117 
118  error:
119     icalerror_set_error_state(ICAL_MALFORMEDDATA_ERROR,es);
120     icalerror_set_errno(ICAL_MALFORMEDDATA_ERROR);
121     return tr;
122 
123 }
124 
125 
icalreqstattype_from_string(const char * str)126 struct icalreqstattype icalreqstattype_from_string(const char* str)
127 {
128   const char *p1,*p2;
129   struct icalreqstattype stat;
130   short major=0, minor=0;
131 
132   icalerror_check_arg((str != 0),"str");
133 
134   stat.code = ICAL_UNKNOWN_STATUS;
135   stat.debug = 0;
136    stat.desc = 0;
137 
138   /* Get the status numbers */
139 
140   sscanf(str, "%hd.%hd",&major, &minor);
141 
142   if (major <= 0 || minor < 0){
143     icalerror_set_errno(ICAL_MALFORMEDDATA_ERROR);
144     return stat;
145   }
146 
147   stat.code = icalenum_num_to_reqstat(major, minor);
148 
149   if (stat.code == ICAL_UNKNOWN_STATUS){
150     icalerror_set_errno(ICAL_MALFORMEDDATA_ERROR);
151     return stat;
152   }
153 
154 
155   p1 = strchr(str,';');
156 
157   if (p1 == 0){
158 /*    icalerror_set_errno(ICAL_BADARG_ERROR);*/
159     return stat;
160   }
161 
162   /* Just ignore the second clause; it will be taken from inside the library
163    */
164 
165 
166 
167   p2 = strchr(p1+1,';');
168   if (p2 != 0 && *p2 != 0){
169     stat.debug = icalmemory_tmp_copy(p2 + 1);
170   }
171 
172   return stat;
173 
174 }
175 
icalreqstattype_as_string(struct icalreqstattype stat)176 const char* icalreqstattype_as_string(struct icalreqstattype stat)
177 {
178 	char *buf;
179 	buf = icalreqstattype_as_string_r(stat);
180 	icalmemory_add_tmp_buffer(buf);
181 	return buf;
182 }
183 
184 
icalreqstattype_as_string_r(struct icalreqstattype stat)185 char* icalreqstattype_as_string_r(struct icalreqstattype stat)
186 {
187   char *temp;
188 
189   icalerror_check_arg_rz((stat.code != ICAL_UNKNOWN_STATUS),"Status");
190 
191   temp = (char*)icalmemory_new_buffer(TEMP_MAX);
192 
193   if (stat.desc == 0){
194     stat.desc = icalenum_reqstat_desc(stat.code);
195   }
196 
197   if(stat.debug != 0){
198     snprintf(temp,TEMP_MAX,"%d.%d;%s;%s", icalenum_reqstat_major(stat.code),
199              icalenum_reqstat_minor(stat.code),
200              stat.desc, stat.debug);
201 
202   } else {
203     snprintf(temp,TEMP_MAX,"%d.%d;%s", icalenum_reqstat_major(stat.code),
204              icalenum_reqstat_minor(stat.code),
205              stat.desc);
206   }
207 
208   return temp;
209 }
210 
ical_get_unknown_token_handling_setting(void)211 ical_unknown_token_handling ical_get_unknown_token_handling_setting(void)
212 {
213     ical_unknown_token_handling myHandling;
214 
215 #ifdef HAVE_PTHREAD
216     pthread_mutex_lock (&unk_token_mutex);
217 #endif
218 
219     myHandling = unknownTokenHandling;
220 
221 #ifdef HAVE_PTHREAD
222     pthread_mutex_unlock (&unk_token_mutex);
223 #endif
224 
225     return myHandling;
226 }
227 
ical_set_unknown_token_handling_setting(ical_unknown_token_handling newSetting)228 void ical_set_unknown_token_handling_setting(ical_unknown_token_handling newSetting)
229 {
230 
231 #ifdef HAVE_PTHREAD
232     pthread_mutex_lock (&unk_token_mutex);
233 #endif
234 
235     unknownTokenHandling = newSetting;
236 
237 #ifdef HAVE_PTHREAD
238     pthread_mutex_unlock (&unk_token_mutex);
239 #endif
240 
241 }
242