summaryrefslogtreecommitdiffabout
path: root/libical/src/libical/icallangbind.c
blob: c07903492f2b7309b1e8e4f69e7befcdf1256b06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* -*- Mode: C -*-
  ======================================================================
  FILE: icallangbind.c
  CREATOR: eric 15 dec 2000
  
  DESCRIPTION:
  
  $Id$
  $Locker$

  (C) COPYRIGHT 1999 Eric Busboom 
  http://www.softwarestudio.org
  
  This package is free software and is provided "as is" without
  express or implied warranty.  It may be used, redistributed and/or
  modified under the same terms as perl itself. ( Either the Artistic
  License or the GPL. )

  ======================================================================*/

#include "icalcomponent.h"
#include "icalproperty.h"
#include "icalerror.h"
#include "icalmemory.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

int snprintf(char *str, size_t n, char const *fmt, ...);

int* icallangbind_new_array(int size){
    int* p = (int*)malloc(size*sizeof(int));
    return p; /* Caller handles failures */
}

void icallangbind_free_array(int* array){
    free(array);
}

int icallangbind_access_array(int* array, int index) {
    return array[index];
}                    



/* LIke icalcomponent_get_first_component, buut takes a string for the
   kind and can iterate over X properties as if each X name was a
   seperate kind */
icalproperty* icallangbind_get_first_property(icalcomponent *c,
                                              const char* prop)
{
    icalproperty_kind kind = icalproperty_string_to_kind(prop);
    icalproperty *p;

    if (kind == ICAL_NO_PROPERTY){
	return 0;
    }

    if(kind == ICAL_X_PROPERTY){
        for(p = icalcomponent_get_first_property(c,kind);
            p !=0;
            p = icalcomponent_get_next_property(c,kind)){
            
            if(strcmp(icalproperty_get_x_name(p),prop) == 0){
                return p;
            }                
        }
    } else {
        p=icalcomponent_get_first_property(c,kind);

        return p;
    }
	
    return 0;

}

icalproperty* icallangbind_get_next_property(icalcomponent *c,
                                              const char* prop)
{
    icalproperty_kind kind = icalenum_string_to_property_kind(prop);
    icalproperty *p;

    if (kind == ICAL_NO_PROPERTY){
	return 0;
    }

    if(kind == ICAL_X_PROPERTY){
        for(p = icalcomponent_get_next_property(c,kind);
            p !=0;
            p = icalcomponent_get_next_property(c,kind)){
            
            if(strcmp(icalproperty_get_x_name(p),prop) == 0){
                return p;
            }                
        }
    } else {
        p=icalcomponent_get_next_property(c,kind);

        return p;
    }
	
    return 0;

}


icalcomponent* icallangbind_get_first_component(icalcomponent *c,
                                              const char* comp)
{
    icalcomponent_kind kind = icalenum_string_to_component_kind(comp);

    if (kind == ICAL_NO_COMPONENT){
	return 0;
    }
    return icalcomponent_get_first_component(c,kind);
}

icalcomponent* icallangbind_get_next_component(icalcomponent *c,
                                              const char* comp)
{
    icalcomponent_kind kind = icalenum_string_to_component_kind(comp);

    if (kind == ICAL_NO_COMPONENT){
	return 0;
    }
    return icalcomponent_get_next_component(c,kind);
}


#define APPENDS(x) icalmemory_append_string(&buf, &buf_ptr, &buf_size, x);

#define APPENDC(x) icalmemory_append_char(&buf, &buf_ptr, &buf_size, x);

const char* icallangbind_property_eval_string(icalproperty* prop, char* sep)
{
    char tmp[25];
    size_t buf_size = 1024;
    char* buf = icalmemory_new_buffer(buf_size);
    char* buf_ptr = buf;
    icalparameter *param;
    
    icalvalue* value;

    if( prop == 0){
	return 0;
    }

    APPENDS("{ ");

    value = icalproperty_get_value(prop);

    APPENDS(" 'name' ");
    APPENDS(sep);
    APPENDC('\'');
    APPENDS(icalenum_property_kind_to_string(icalproperty_isa(prop)));
    APPENDC('\'');

    if(value){
        APPENDS(", 'value_type' ");
        APPENDS(sep);
        APPENDC('\'');
        APPENDS(icalenum_value_kind_to_string(icalvalue_isa(value)));
        APPENDC('\'');
    }

    APPENDS(", 'pid' ");
    APPENDS(sep);
    APPENDC('\'');
    snprintf(tmp,25,"%p",prop);
    APPENDS(tmp);
    APPENDC('\'');


    if(value){
        switch (icalvalue_isa(value)){
	
        case ICAL_ATTACH_VALUE:
        case ICAL_BINARY_VALUE: 
        case ICAL_NO_VALUE: {
            icalerror_set_errno(ICAL_INTERNAL_ERROR);
            break;
        }

        default: 
        {
            const char* str = icalvalue_as_ical_string(value);
            char* copy = (char*) malloc(strlen(str)+1);
            
            const char *i;
            char *j;

            if(copy ==0){
                icalerror_set_errno(ICAL_NEWFAILED_ERROR);
                break; 
            }
            /* Remove any newlines */
                
            for(j=copy, i = str; *i != 0; j++,i++){
                if(*i=='\n'){
                    i++;
                }   
                *j = *i;
            }
                
            *j = 0;
                
            APPENDS(", 'value'");
            APPENDS(sep);
            APPENDC('\'');
            APPENDS(copy);
            APPENDC('\'');
            
            free(copy);
            break;

        }
        }
    }

    /* Add Parameters */

    for(param = icalproperty_get_first_parameter(prop,ICAL_ANY_PARAMETER);
        param != 0;
        param = icalproperty_get_next_parameter(prop,ICAL_ANY_PARAMETER)){
        
        const char* str = icalparameter_as_ical_string(param);
        char *copy = icalmemory_tmp_copy(str);
        char *v;

        if(copy == 0){
            icalerror_set_errno(ICAL_NEWFAILED_ERROR);
            continue;
        }

        v = strchr(copy,'=');


        if(v == 0){
            continue;
        }

        *v = 0;

        v++;

        APPENDS(", ");
        APPENDC('\'');
        APPENDS(copy);
        APPENDC('\'');
        APPENDS(sep);
        APPENDC('\'');
        APPENDS(v);        
        APPENDC('\'');
        
    }


    APPENDC('}');

    icalmemory_add_tmp_buffer(buf);
    return buf;

}

#include "fcntl.h"
int icallangbind_string_to_open_flag(const char* str)
{
    if (strcmp(str,"r") == 0) {return O_RDONLY;}
    else if (strcmp(str,"r+") == 0) {return O_RDWR;}
    else if (strcmp(str,"w") == 0) {return O_WRONLY;}
    else if (strcmp(str,"a") == 0) {return O_WRONLY|O_APPEND;}
    else return -1;
}