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