Back to index

lightning-sunbird  0.9+nobinonly
icallangbind.c
Go to the documentation of this file.
00001 /* -*- Mode: C -*-
00002   ======================================================================
00003   FILE: icallangbind.c
00004   CREATOR: eric 15 dec 2000
00005   
00006   DESCRIPTION:
00007   
00008   $Id: icallangbind.c,v 1.22 2002/10/24 13:44:30 acampi Exp $
00009   $Locker:  $
00010 
00011   (C) COPYRIGHT 1999 Eric Busboom 
00012   http://www.softwarestudio.org
00013   
00014   This package is free software and is provided "as is" without
00015   express or implied warranty.  It may be used, redistributed and/or
00016   modified under the same terms as perl itself. ( Either the Artistic
00017   License or the GPL. )
00018 
00019   ======================================================================*/
00020 
00021 #include "icalcomponent.h"
00022 #include "icalproperty.h"
00023 #include "icalerror.h"
00024 #include "icalmemory.h"
00025 #include "icalvalue.h"
00026 #include <stdlib.h>
00027 #include <string.h>
00028 
00029 #ifdef WIN32
00030 #define snprintf      _snprintf
00031 #define strcasecmp    stricmp
00032 #endif
00033 
00034 int* icallangbind_new_array(int size){
00035     int* p = (int*)malloc(size*sizeof(int));
00036     return p; /* Caller handles failures */
00037 }
00038 
00039 void icallangbind_free_array(int* array){
00040     free(array);
00041 }
00042 
00043 int icallangbind_access_array(int* array, int index) {
00044     return array[index];
00045 }                    
00046 
00049 icalparameter* icallangbind_get_first_parameter(icalproperty *prop)
00050 
00051 {
00052     icalparameter_kind kind = ICAL_ANY_PARAMETER;
00053     
00054     return icalproperty_get_first_parameter(prop,kind);
00055 }
00056 
00057 icalparameter* icallangbind_get_next_parameter(icalproperty *prop)
00058 {
00059     icalparameter_kind kind = ICAL_ANY_PARAMETER;
00060     
00061     return icalproperty_get_next_parameter(prop,kind);
00062 }
00063                                                             
00064 
00069 icalproperty* icallangbind_get_first_property(icalcomponent *c,
00070                                               const char* prop)
00071 {
00072     icalproperty_kind kind = icalproperty_string_to_kind(prop);
00073     icalproperty *p;
00074 
00075     if (kind == ICAL_NO_PROPERTY){
00076        return 0;
00077     }
00078 
00079     if(kind == ICAL_X_PROPERTY){
00080         for(p = icalcomponent_get_first_property(c,kind);
00081             p !=0;
00082             p = icalcomponent_get_next_property(c,kind)){
00083             
00084             if(strcmp(icalproperty_get_x_name(p),prop) == 0){
00085                 return p;
00086             }                
00087         }
00088     } else {
00089         p=icalcomponent_get_first_property(c,kind);
00090 
00091         return p;
00092     }
00093        
00094     return 0;
00095 
00096 }
00097 
00098 icalproperty* icallangbind_get_next_property(icalcomponent *c,
00099                                               const char* prop)
00100 {
00101     icalproperty_kind kind = icalenum_string_to_property_kind(prop);
00102     icalproperty *p;
00103 
00104     if (kind == ICAL_NO_PROPERTY){
00105        return 0;
00106     }
00107 
00108     if(kind == ICAL_X_PROPERTY){
00109         for(p = icalcomponent_get_next_property(c,kind);
00110             p !=0;
00111             p = icalcomponent_get_next_property(c,kind)){
00112             
00113             if(strcmp(icalproperty_get_x_name(p),prop) == 0){
00114                 return p;
00115             }                
00116         }
00117     } else {
00118         p=icalcomponent_get_next_property(c,kind);
00119 
00120         return p;
00121     }
00122        
00123     return 0;
00124 
00125 }
00126 
00127 
00128 icalcomponent* icallangbind_get_first_component(icalcomponent *c,
00129                                               const char* comp)
00130 {
00131     icalcomponent_kind kind = icalenum_string_to_component_kind(comp);
00132 
00133     if (kind == ICAL_NO_COMPONENT){
00134        return 0;
00135     }
00136     return icalcomponent_get_first_component(c,kind);
00137 }
00138 
00139 icalcomponent* icallangbind_get_next_component(icalcomponent *c,
00140                                               const char* comp)
00141 {
00142     icalcomponent_kind kind = icalenum_string_to_component_kind(comp);
00143 
00144     if (kind == ICAL_NO_COMPONENT){
00145        return 0;
00146     }
00147     return icalcomponent_get_next_component(c,kind);
00148 }
00149 
00150 
00151 #define APPENDS(x) icalmemory_append_string(&buf, &buf_ptr, &buf_size, x);
00152 
00153 #define APPENDC(x) icalmemory_append_char(&buf, &buf_ptr, &buf_size, x);
00154 
00155 
00156 const char* icallangbind_property_eval_string(icalproperty* prop, char* sep)
00157 {
00158     char tmp[25];
00159     size_t buf_size = 1024;
00160     char* buf = icalmemory_new_buffer(buf_size);
00161     char* buf_ptr = buf;
00162     icalparameter *param;
00163     
00164     icalvalue* value;
00165 
00166     if( prop == 0){
00167        return 0;
00168     }
00169 
00170     APPENDS("{ ");
00171 
00172     value = icalproperty_get_value(prop);
00173 
00174     APPENDS(" 'name' ");
00175     APPENDS(sep);
00176     APPENDC('\'');
00177     APPENDS(icalproperty_kind_to_string(icalproperty_isa(prop)));
00178     APPENDC('\'');
00179 
00180     if(value){
00181         APPENDS(", 'value_type' ");
00182         APPENDS(sep);
00183         APPENDC('\'');
00184         APPENDS(icalvalue_kind_to_string(icalvalue_isa(value)));
00185         APPENDC('\'');
00186     }
00187 
00188     APPENDS(", 'pid' ");
00189     APPENDS(sep);
00190     APPENDC('\'');
00191     snprintf(tmp,25,"%p",prop);
00192     APPENDS(tmp);
00193     APPENDC('\'');
00194 
00195 
00196     if(value){
00197         switch (icalvalue_isa(value)){
00198        
00199         case ICAL_ATTACH_VALUE:
00200         case ICAL_BINARY_VALUE: 
00201         case ICAL_NO_VALUE: {
00202             icalerror_set_errno(ICAL_INTERNAL_ERROR);
00203             break;
00204         }
00205 
00206         default: 
00207         {
00208             const char* str = icalvalue_as_ical_string(value);
00209             char* copy = (char*) malloc(strlen(str)+1);
00210             
00211             const char *i;
00212             char *j;
00213 
00214             if(copy ==0){
00215                 icalerror_set_errno(ICAL_NEWFAILED_ERROR);
00216                 break; 
00217             }
00218             /* Remove any newlines */
00219                 
00220             for(j=copy, i = str; *i != 0; j++,i++){
00221                 if(*i=='\n'){
00222                     i++;
00223                 }   
00224                 *j = *i;
00225             }
00226                 
00227             *j = 0;
00228                 
00229             APPENDS(", 'value'");
00230             APPENDS(sep);
00231             APPENDC('\'');
00232             APPENDS(copy);
00233             APPENDC('\'');
00234             
00235             free(copy);
00236             break;
00237 
00238         }
00239         }
00240     }
00241 
00242     /* Add Parameters */
00243 
00244     for(param = icalproperty_get_first_parameter(prop,ICAL_ANY_PARAMETER);
00245         param != 0;
00246         param = icalproperty_get_next_parameter(prop,ICAL_ANY_PARAMETER)){
00247         
00248         const char* str = icalparameter_as_ical_string(param);
00249         char *copy = icalmemory_tmp_copy(str);
00250         char *v;
00251 
00252         if(copy == 0){
00253             icalerror_set_errno(ICAL_NEWFAILED_ERROR);
00254             continue;
00255         }
00256 
00257         v = strchr(copy,'=');
00258 
00259 
00260         if(v == 0){
00261             continue;
00262         }
00263 
00264         *v = 0;
00265 
00266         v++;
00267 
00268         APPENDS(", ");
00269         APPENDC('\'');
00270         APPENDS(copy);
00271         APPENDC('\'');
00272         APPENDS(sep);
00273         APPENDC('\'');
00274         APPENDS(v);        
00275         APPENDC('\'');
00276         
00277     }
00278 
00279 
00280     APPENDC('}');
00281 
00282     icalmemory_add_tmp_buffer(buf);
00283     return buf;
00284 
00285 }
00286 
00287 #include "fcntl.h"
00288 int icallangbind_string_to_open_flag(const char* str)
00289 {
00290     if (strcmp(str,"r") == 0) {return O_RDONLY;}
00291     else if (strcmp(str,"r+") == 0) {return O_RDWR;}
00292     else if (strcmp(str,"w") == 0) {return O_WRONLY;}
00293     else if (strcmp(str,"w+") == 0) {return O_RDWR|O_CREAT;}
00294     else if (strcmp(str,"a") == 0) {return O_WRONLY|O_APPEND;}
00295     else return -1;
00296 }
00297 
00298 
00299 const char* icallangbind_quote_as_ical(const char* str)
00300 {
00301     size_t buf_size = 2 * strlen(str);
00302 
00303     /* assume every char could be quoted */
00304     char* buf = icalmemory_new_buffer(buf_size);
00305     int result;
00306 
00307     result = icalvalue_encode_ical_string(str, buf, buf_size);
00308 
00309     icalmemory_add_tmp_buffer(buf);
00310 
00311     return buf;
00312 }