1 /* -*- Mode: C -*-
2   ======================================================================
3   FILE: icallangbind.c
4   CREATOR: eric 15 dec 2000
5 
6   DESCRIPTION:
7 
8   $Id: icallangbind.c,v 1.24 2008-01-02 20:07:31 dothebart Exp $
9   $Locker:  $
10 
11   (C) COPYRIGHT 1999 Eric Busboom
12   http://www.softwarestudio.org
13 
14   This package is free software and is provided "as is" without
15   express or implied warranty.  It may be used, redistributed and/or
16   modified under the same terms as perl itself. ( Either the Artistic
17   License or the GPL. )
18 
19   ======================================================================*/
20 
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 
25 #include "icalcomponent.h"
26 #include "icalproperty.h"
27 #include "icalerror.h"
28 #include "icalmemory.h"
29 #include "icalvalue.h"
30 #include <stdlib.h>
31 #include <string.h>
32 
33 #ifdef WIN32
34 #if defined(_MSC_VER) && (_MSC_VER < 1900)
35 #define snprintf _snprintf
36 #endif
37 #define strcasecmp    stricmp
38 #endif
39 
icallangbind_new_array(int size)40 int* icallangbind_new_array(int size){
41     int* p = (int*)malloc(size*sizeof(int));
42     return p; /* Caller handles failures */
43 }
44 
icallangbind_free_array(int * array)45 void icallangbind_free_array(int* array){
46     free(array);
47 }
48 
icallangbind_access_array(int * array,int index)49 int icallangbind_access_array(int* array, int index) {
50     return array[index];
51 }
52 
53 /** Iterators to fetch parameters given property */
54 
icallangbind_get_first_parameter(icalproperty * prop)55 icalparameter* icallangbind_get_first_parameter(icalproperty *prop)
56 
57 {
58     icalparameter_kind kind = ICAL_ANY_PARAMETER;
59 
60     return icalproperty_get_first_parameter(prop,kind);
61 }
62 
icallangbind_get_next_parameter(icalproperty * prop)63 icalparameter* icallangbind_get_next_parameter(icalproperty *prop)
64 {
65     icalparameter_kind kind = ICAL_ANY_PARAMETER;
66 
67     return icalproperty_get_next_parameter(prop,kind);
68 }
69 
70 
71 /** Like icalcomponent_get_first_component(), but takes a string for the
72    kind and can iterate over X properties as if each X name was a
73    seperate kind */
74 
icallangbind_get_first_property(icalcomponent * c,const char * prop)75 icalproperty* icallangbind_get_first_property(icalcomponent *c,
76                                               const char* prop)
77 {
78     icalproperty_kind kind = icalproperty_string_to_kind(prop);
79     icalproperty *p;
80 
81     if (kind == ICAL_NO_PROPERTY){
82 	return 0;
83     }
84 
85     if(kind == ICAL_X_PROPERTY){
86         for(p = icalcomponent_get_first_property(c,kind);
87             p !=0;
88             p = icalcomponent_get_next_property(c,kind)){
89 
90             if(strcmp(icalproperty_get_x_name(p),prop) == 0){
91                 return p;
92             }
93         }
94     } else {
95         p=icalcomponent_get_first_property(c,kind);
96 
97         return p;
98     }
99 
100     return 0;
101 
102 }
103 
icallangbind_get_next_property(icalcomponent * c,const char * prop)104 icalproperty* icallangbind_get_next_property(icalcomponent *c,
105                                               const char* prop)
106 {
107     icalproperty_kind kind = icalenum_string_to_property_kind(prop);
108     icalproperty *p;
109 
110     if (kind == ICAL_NO_PROPERTY){
111 	return 0;
112     }
113 
114     if(kind == ICAL_X_PROPERTY){
115         for(p = icalcomponent_get_next_property(c,kind);
116             p !=0;
117             p = icalcomponent_get_next_property(c,kind)){
118 
119             if(strcmp(icalproperty_get_x_name(p),prop) == 0){
120                 return p;
121             }
122         }
123     } else {
124         p=icalcomponent_get_next_property(c,kind);
125 
126         return p;
127     }
128 
129     return 0;
130 
131 }
132 
133 
icallangbind_get_first_component(icalcomponent * c,const char * comp)134 icalcomponent* icallangbind_get_first_component(icalcomponent *c,
135                                               const char* comp)
136 {
137     icalcomponent_kind kind = icalenum_string_to_component_kind(comp);
138 
139     if (kind == ICAL_NO_COMPONENT){
140 	return 0;
141     }
142     return icalcomponent_get_first_component(c,kind);
143 }
144 
icallangbind_get_next_component(icalcomponent * c,const char * comp)145 icalcomponent* icallangbind_get_next_component(icalcomponent *c,
146                                               const char* comp)
147 {
148     icalcomponent_kind kind = icalenum_string_to_component_kind(comp);
149 
150     if (kind == ICAL_NO_COMPONENT){
151 	return 0;
152     }
153     return icalcomponent_get_next_component(c,kind);
154 }
155 
156 
157 #define APPENDS(x) icalmemory_append_string(&buf, &buf_ptr, &buf_size, x);
158 
159 #define APPENDC(x) icalmemory_append_char(&buf, &buf_ptr, &buf_size, x);
160 
icallangbind_property_eval_string_r(icalproperty * prop,char * sep)161 char* icallangbind_property_eval_string_r(icalproperty* prop, char* sep)
162 {
163     char tmp[25];
164     size_t buf_size = 1024;
165     char* buf;
166     char* buf_ptr;
167     icalparameter *param;
168 
169     icalvalue* value;
170 
171     if( prop == 0){
172 	return 0;
173     }
174 
175     buf = icalmemory_new_buffer(buf_size);
176     buf_ptr = buf;
177 
178     APPENDS("{ ");
179 
180     value = icalproperty_get_value(prop);
181 
182     APPENDS(" 'name' ");
183     APPENDS(sep);
184     APPENDC('\'');
185     APPENDS(icalproperty_kind_to_string(icalproperty_isa(prop)));
186     APPENDC('\'');
187 
188     if(value){
189         APPENDS(", 'value_type' ");
190         APPENDS(sep);
191         APPENDC('\'');
192         APPENDS(icalvalue_kind_to_string(icalvalue_isa(value)));
193         APPENDC('\'');
194     }
195 
196     APPENDS(", 'pid' ");
197     APPENDS(sep);
198     APPENDC('\'');
199     snprintf(tmp,25,"%p",prop);
200     APPENDS(tmp);
201     APPENDC('\'');
202 
203 
204     if(value){
205         switch (icalvalue_isa(value)){
206 
207         case ICAL_ATTACH_VALUE:
208         case ICAL_BINARY_VALUE:
209         case ICAL_NO_VALUE: {
210             icalerror_set_errno(ICAL_INTERNAL_ERROR);
211             break;
212         }
213 
214         default:
215         {
216             char* str = icalvalue_as_ical_string_r(value);
217             char* copy = (char*) malloc(strlen(str)+1);
218 
219             const char *i;
220             char *j;
221 
222             if(copy ==0){
223                 icalerror_set_errno(ICAL_NEWFAILED_ERROR);
224                 break;
225             }
226             /* Remove any newlines */
227 
228             for(j=copy, i = str; *i != 0; j++,i++){
229                 if(*i=='\n'){
230                     i++;
231                 }
232                 *j = *i;
233             }
234 
235             *j = 0;
236 
237             APPENDS(", 'value'");
238             APPENDS(sep);
239             APPENDC('\'');
240             APPENDS(copy);
241             APPENDC('\'');
242 
243             free(copy);
244 	    free(str);
245             break;
246 
247         }
248         }
249     }
250 
251     /* Add Parameters */
252 
253     for(param = icalproperty_get_first_parameter(prop,ICAL_ANY_PARAMETER);
254         param != 0;
255         param = icalproperty_get_next_parameter(prop,ICAL_ANY_PARAMETER)){
256 
257         char *copy = icalparameter_as_ical_string_r(param);
258         char *v;
259 
260         if(copy == 0){
261             icalerror_set_errno(ICAL_NEWFAILED_ERROR);
262             continue;
263         }
264 
265         v = strchr(copy,'=');
266 
267 
268         if(v == 0){
269             free(copy);
270             continue;
271         }
272 
273         *v = 0;
274 
275         v++;
276 
277         APPENDS(", ");
278         APPENDC('\'');
279         APPENDS(copy);
280         APPENDC('\'');
281         APPENDS(sep);
282         APPENDC('\'');
283         APPENDS(v);
284         APPENDC('\'');
285 	free(copy);
286     }
287 
288 
289     APPENDC('}');
290 
291     return buf;
292 
293 }
294 
icallangbind_property_eval_string(icalproperty * prop,char * sep)295 const char* icallangbind_property_eval_string(icalproperty* prop, char* sep)
296 {
297 	char *buf;
298 	buf = icallangbind_property_eval_string_r(prop, sep);
299 	icalmemory_add_tmp_buffer(buf);
300 	return buf;
301 }
302 
303 
304 #include "fcntl.h"
icallangbind_string_to_open_flag(const char * str)305 int icallangbind_string_to_open_flag(const char* str)
306 {
307     if (strcmp(str,"r") == 0) {return O_RDONLY;}
308     else if (strcmp(str,"r+") == 0) {return O_RDWR;}
309     else if (strcmp(str,"w") == 0) {return O_WRONLY;}
310     else if (strcmp(str,"w+") == 0) {return O_RDWR|O_CREAT;}
311     else if (strcmp(str,"a") == 0) {return O_WRONLY|O_APPEND;}
312     else return -1;
313 }
314 
315 
icallangbind_quote_as_ical_r(const char * str)316 char* icallangbind_quote_as_ical_r(const char* str)
317 {
318     size_t buf_size = 2 * strlen(str);
319 
320     /* assume every char could be quoted */
321     char* buf = icalmemory_new_buffer(buf_size);
322     int result;
323 
324     result = icalvalue_encode_ical_string(str, buf, buf_size);
325 
326     return buf;
327 }
328 
329 
icallangbind_quote_as_ical(const char * str)330 const char* icallangbind_quote_as_ical(const char* str)
331 {
332 	char *buf;
333 	buf = icallangbind_quote_as_ical_r(str);
334 	icalmemory_add_tmp_buffer(buf);
335 	return(buf);
336 }
337 
338