Back to index

lightning-sunbird  0.9+nobinonly
icalparameter.c
Go to the documentation of this file.
00001 /* -*- Mode: C -*-
00002   ======================================================================
00003   FILE: icalderivedparameters.{c,h}
00004   CREATOR: eric 09 May 1999
00005   
00006   $Id: icalparameter.c,v 1.12 2004/03/17 19:04:53 acampi Exp $
00007   $Locker:  $
00008     
00009 
00010  (C) COPYRIGHT 2000, Eric Busboom, http://www.softwarestudio.org
00011 
00012  This program is free software; you can redistribute it and/or modify
00013  it under the terms of either: 
00014 
00015     The LGPL as published by the Free Software Foundation, version
00016     2.1, available at: http://www.fsf.org/copyleft/lesser.html
00017 
00018   Or:
00019 
00020     The Mozilla Public License Version 1.0. You may obtain a copy of
00021     the License at http://www.mozilla.org/MPL/
00022 
00023   The original code is icalderivedparameters.{c,h}
00024 
00025   Contributions from:
00026      Graham Davison (g.m.davison@computer.org)
00027 
00028  ======================================================================*/
00029 /*#line 29 "icalparameter.c.in"*/
00030 #ifdef HAVE_CONFIG_H
00031 #include <config.h>
00032 #endif
00033 
00034 
00035 #include "icalparameter.h"
00036 #include "icalproperty.h"
00037 #include "icalerror.h"
00038 #include "icalmemory.h"
00039 #include "icalparameterimpl.h"
00040 
00041 #include <stdlib.h> /* for malloc() */
00042 #include <errno.h>
00043 #include <string.h> /* for memset() */
00044 
00045 /* In icalderivedparameter */
00046 icalparameter* icalparameter_new_from_value_string(icalparameter_kind kind,const  char* val);
00047 
00048 
00049 struct icalparameter_impl* icalparameter_new_impl(icalparameter_kind kind)
00050 {
00051     struct icalparameter_impl* v;
00052 
00053     if ( ( v = (struct icalparameter_impl*)
00054           malloc(sizeof(struct icalparameter_impl))) == 0) {
00055        icalerror_set_errno(ICAL_NEWFAILED_ERROR);
00056        return 0;
00057     }
00058     
00059     strcpy(v->id,"para");
00060 
00061     v->kind = kind;
00062     v->size = 0;
00063     v->string = 0;
00064     v->x_name = 0;
00065     v->parent = 0;
00066     v->data = 0;
00067 
00068     return v;
00069 }
00070 
00071 icalparameter*
00072 icalparameter_new (icalparameter_kind kind)
00073 {
00074     struct icalparameter_impl* v = icalparameter_new_impl(kind);
00075 
00076     return (icalparameter*) v;
00077 
00078 }
00079 
00080 icalparameter *
00081 icalparameter_new_x_name(const char *name, const char *value) {
00082 
00083        icalparameter  *ret;
00084 
00085        if (name == NULL || value == NULL)
00086               return NULL;
00087 
00088        ret = icalparameter_new_x(value);
00089        if (ret == NULL)
00090               return NULL;
00091 
00092        icalparameter_set_xname(ret, name);
00093 
00094        return ret;   
00095 }
00096 
00097 void
00098 icalparameter_free (icalparameter* param)
00099 {
00100 
00101 /*  HACK. This always triggers, even when parameter is non-zero
00102     icalerror_check_arg_rv((parameter==0),"parameter");*/
00103 
00104 
00105 #ifdef ICAL_FREE_ON_LIST_IS_ERROR
00106     icalerror_assert( (param->parent ==0),"Tried to free a parameter that is still attached to a component. ");
00107     
00108 #else
00109     if(param->parent !=0){
00110        return;
00111     }
00112 #endif
00113 
00114     
00115     if (param->string != 0){
00116        free ((void*)param->string);
00117     }
00118     
00119     if (param->x_name != 0){
00120        free ((void*)param->x_name);
00121     }
00122     
00123     memset(param,0,sizeof(param));
00124 
00125     param->parent = 0;
00126     param->id[0] = 'X';
00127     free(param);
00128 }
00129 
00130 
00131 
00132 icalparameter* 
00133 icalparameter_new_clone(icalparameter* old)
00134 {
00135     struct icalparameter_impl *new;
00136 
00137     new = icalparameter_new_impl(old->kind);
00138 
00139     icalerror_check_arg_rz((old!=0),"param");
00140 
00141     if (new == 0){
00142        return 0;
00143     }
00144 
00145     memcpy(new,old,sizeof(struct icalparameter_impl));
00146 
00147     if (old->string != 0){
00148        new->string = icalmemory_strdup(old->string);
00149        if (new->string == 0){
00150            icalparameter_free(new);
00151            return 0;
00152        }
00153     }
00154 
00155     if (old->x_name != 0){
00156        new->x_name = icalmemory_strdup(old->x_name);
00157        if (new->x_name == 0){
00158            icalparameter_free(new);
00159            return 0;
00160        }
00161     }
00162 
00163     return new;
00164 }
00165 
00166 icalparameter* icalparameter_new_from_string(const char *str)
00167 {
00168     char* eq;
00169     char* cpy;
00170     icalparameter_kind kind;
00171     icalparameter *param;
00172 
00173     icalerror_check_arg_rz(str != 0,"str");
00174 
00175     cpy = icalmemory_strdup(str);
00176 
00177     if (cpy == 0){
00178         icalerror_set_errno(ICAL_NEWFAILED_ERROR);
00179         return 0;
00180     }
00181 
00182     eq = strchr(cpy,'=');
00183 
00184     if(eq == 0){
00185         icalerror_set_errno(ICAL_MALFORMEDDATA_ERROR);
00186         return 0;
00187     }
00188 
00189     *eq = '\0';
00190 
00191     eq++;
00192 
00193     kind = icalparameter_string_to_kind(cpy);
00194 
00195     if(kind == ICAL_NO_PARAMETER){
00196         icalerror_set_errno(ICAL_MALFORMEDDATA_ERROR);
00197         return 0;
00198     }
00199 
00200     param = icalparameter_new_from_value_string(kind,eq);
00201 
00202     if(kind == ICAL_X_PARAMETER){
00203         icalparameter_set_xname(param,cpy);
00204     }
00205 
00206     free(cpy);
00207 
00208     return param;
00209     
00210 }
00211 
00223 char*
00224 icalparameter_as_ical_string (icalparameter* param)
00225 {
00226     size_t buf_size = 1024;
00227     char* buf; 
00228     char* buf_ptr;
00229     char *out_buf;
00230     const char *kind_string;
00231 
00232     icalerror_check_arg_rz( (param!=0), "parameter");
00233 
00234     /* Create new buffer that we can append names, parameters and a
00235        value to, and reallocate as needed. Later, this buffer will be
00236        copied to a icalmemory_tmp_buffer, which is managed internally
00237        by libical, so it can be given to the caller without fear of
00238        the caller forgetting to free it */
00239 
00240     buf = icalmemory_new_buffer(buf_size);
00241     buf_ptr = buf;
00242 
00243     if(param->kind == ICAL_X_PARAMETER) {
00244 
00245        icalmemory_append_string(&buf, &buf_ptr, &buf_size, 
00246                              icalparameter_get_xname(param));
00247 
00248     } else {
00249 
00250        kind_string = icalparameter_kind_to_string(param->kind);
00251        
00252        if (param->kind == ICAL_NO_PARAMETER || 
00253            param->kind == ICAL_ANY_PARAMETER || 
00254            kind_string == 0)
00255        {
00256            icalerror_set_errno(ICAL_BADARG_ERROR);
00257            return 0;
00258        }
00259        
00260        
00261        /* Put the parameter name into the string */
00262        icalmemory_append_string(&buf, &buf_ptr, &buf_size, kind_string);
00263 
00264     }
00265 
00266     icalmemory_append_string(&buf, &buf_ptr, &buf_size, "=");
00267 
00268     if(param->string !=0){
00269         int qm = 0;
00270 
00271        /* Encapsulate the property in quotes if necessary */
00272        if (strpbrk(param->string, ";:,") != 0) {
00273               icalmemory_append_char (&buf, &buf_ptr, &buf_size, '"');
00274               qm = 1;
00275        }
00276         icalmemory_append_string(&buf, &buf_ptr, &buf_size, param->string); 
00277        if (qm == 1) {
00278               icalmemory_append_char (&buf, &buf_ptr, &buf_size, '"');
00279        }
00280     } else if (param->data != 0){
00281         const char* str = icalparameter_enum_to_string(param->data);
00282         icalmemory_append_string(&buf, &buf_ptr, &buf_size, str); 
00283     } else {
00284         icalerror_set_errno(ICAL_MALFORMEDDATA_ERROR);
00285         return 0;
00286     }
00287 
00288     /* Now, copy the buffer to a tmp_buffer, which is safe to give to
00289        the caller without worring about de-allocating it. */
00290     
00291     out_buf = icalmemory_tmp_copy(buf);
00292     icalmemory_free_buffer(buf);
00293 
00294     return out_buf;
00295 
00296 }
00297 
00298 
00299 int
00300 icalparameter_is_valid (icalparameter* parameter);
00301 
00302 
00303 icalparameter_kind
00304 icalparameter_isa (icalparameter* parameter)
00305 {
00306     if(parameter == 0){
00307        return ICAL_NO_PARAMETER;
00308     }
00309 
00310     return parameter->kind;
00311 }
00312 
00313 
00314 int
00315 icalparameter_isa_parameter (void* parameter)
00316 {
00317     struct icalparameter_impl *impl = (struct icalparameter_impl *)parameter;
00318 
00319     if (parameter == 0){
00320        return 0;
00321     }
00322 
00323     if (strcmp(impl->id,"para") == 0) {
00324        return 1;
00325     } else {
00326        return 0;
00327     }
00328 }
00329 
00330 
00331 void
00332 icalparameter_set_xname (icalparameter* param, const char* v)
00333 {
00334     icalerror_check_arg_rv( (param!=0),"param");
00335     icalerror_check_arg_rv( (v!=0),"v");
00336 
00337     if (param->x_name != 0){
00338        free((void*)param->x_name);
00339     }
00340 
00341     param->x_name = icalmemory_strdup(v);
00342 
00343     if (param->x_name == 0){
00344        errno = ENOMEM;
00345     }
00346 
00347 }
00348 
00349 const char*
00350 icalparameter_get_xname (icalparameter* param)
00351 {
00352     icalerror_check_arg_rz( (param!=0),"param");
00353 
00354     return param->x_name;
00355 }
00356 
00357 void
00358 icalparameter_set_xvalue (icalparameter* param, const char* v)
00359 {
00360     icalerror_check_arg_rv( (param!=0),"param");
00361     icalerror_check_arg_rv( (v!=0),"v");
00362 
00363     if (param->string != 0){
00364        free((void*)param->string);
00365     }
00366 
00367     param->string = icalmemory_strdup(v);
00368 
00369     if (param->string == 0){
00370        errno = ENOMEM;
00371     }
00372 
00373 }
00374 
00375 const char*
00376 icalparameter_get_xvalue (icalparameter* param)
00377 {
00378     icalerror_check_arg_rz( (param!=0),"param");
00379 
00380     return param->string;
00381 }
00382 
00383 void icalparameter_set_parent(icalparameter* param,
00384                           icalproperty* property)
00385 {
00386     icalerror_check_arg_rv( (param!=0),"param");
00387 
00388     param->parent = property;
00389 }
00390 
00391 icalproperty* icalparameter_get_parent(icalparameter* param)
00392 {
00393     icalerror_check_arg_rz( (param!=0),"param");
00394 
00395     return param->parent;
00396 }
00397 
00398 
00399 /* Everything below this line is machine generated. Do not edit. */
00400 /* ALTREP */