Back to index

lightning-sunbird  0.9+nobinonly
icaltypes.c
Go to the documentation of this file.
00001 /* -*- Mode: C -*-
00002   ======================================================================
00003   FILE: icaltypes.c
00004   CREATOR: eric 16 May 1999
00005   
00006   $Id: icaltypes.c,v 1.15 2002/10/30 23:59:57 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 icaltypes.c
00024 
00025  ======================================================================*/
00026 #ifdef HAVE_CONFIG_H
00027 #include "config.h"
00028 #endif
00029 
00030 #include "icaltypes.h"
00031 #include "icalerror.h"
00032 #include "icalmemory.h"
00033 #include <stdlib.h> /* for malloc and abs() */
00034 #include <errno.h> /* for errno */
00035 #include <string.h> /* for icalmemory_strdup */
00036 #include <assert.h>
00037 
00038 #ifdef WIN32
00039 #define snprintf      _snprintf
00040 #define strcasecmp    stricmp
00041 #endif
00042 
00043 #define TEMP_MAX 1024
00044 
00045 
00046 int icaltriggertype_is_null_trigger(struct icaltriggertype tr)
00047 {
00048     if(icaltime_is_null_time(tr.time) && 
00049        icaldurationtype_is_null_duration(tr.duration)){
00050         return 1;
00051     }
00052 
00053     return 0;
00054 }
00055     
00056 int icaltriggertype_is_bad_trigger(struct icaltriggertype tr)
00057 {
00058     if(icaldurationtype_is_bad_duration(tr.duration)){
00059         return 1;
00060     }
00061 
00062     return 0;
00063 }
00064 
00065 struct icaltriggertype icaltriggertype_from_int(const int reltime)
00066 {
00067     struct icaltriggertype tr;
00068 
00069     tr.time   = icaltime_null_time();
00070     tr.duration = icaldurationtype_from_int(reltime);
00071 
00072     return tr;
00073 }
00074 
00075 struct icaltriggertype icaltriggertype_from_string(const char* str)
00076 {
00077 
00078     
00079     struct icaltriggertype tr, null_tr;
00080     icalerrorstate es;
00081     icalerrorenum e;
00082 
00083     tr.time= icaltime_null_time();
00084     tr.duration = icaldurationtype_from_int(0);
00085 
00086     null_tr = tr;
00087 
00088     if(str == 0) goto error;
00089 
00090     /* Suppress errors so a failure in icaltime_from_string() does not cause an abort */
00091     es = icalerror_get_error_state(ICAL_MALFORMEDDATA_ERROR);
00092     icalerror_set_error_state(ICAL_MALFORMEDDATA_ERROR,ICAL_ERROR_NONFATAL);
00093     e = icalerrno;
00094     icalerror_set_errno(ICAL_NO_ERROR);
00095 
00096     tr.time = icaltime_from_string(str);
00097 
00098     if (icaltime_is_null_time(tr.time)){
00099 
00100        tr.duration = icaldurationtype_from_string(str);
00101 
00102         if (icaldurationtype_is_bad_duration(tr.duration)) goto error;
00103     } 
00104 
00105     icalerror_set_error_state(ICAL_MALFORMEDDATA_ERROR,es);
00106     icalerror_set_errno(e);
00107     return tr;
00108 
00109  error:
00110     icalerror_set_error_state(ICAL_MALFORMEDDATA_ERROR,es);
00111     icalerror_set_errno(ICAL_MALFORMEDDATA_ERROR);
00112     return tr;
00113 
00114 }
00115 
00116 
00117 struct icalreqstattype icalreqstattype_from_string(const char* str)
00118 {
00119   const char *p1,*p2;
00120   struct icalreqstattype stat;
00121   short major=0, minor=0;
00122 
00123   icalerror_check_arg((str != 0),"str");
00124 
00125   stat.code = ICAL_UNKNOWN_STATUS;
00126   stat.debug = 0; 
00127    stat.desc = 0;
00128 
00129   /* Get the status numbers */
00130 
00131   sscanf(str, "%hd.%hd",&major, &minor);
00132 
00133   if (major <= 0 || minor < 0){
00134     icalerror_set_errno(ICAL_MALFORMEDDATA_ERROR);
00135     return stat;
00136   }
00137 
00138   stat.code = icalenum_num_to_reqstat(major, minor);
00139 
00140   if (stat.code == ICAL_UNKNOWN_STATUS){
00141     icalerror_set_errno(ICAL_MALFORMEDDATA_ERROR);
00142     return stat;
00143   }
00144   
00145 
00146   p1 = strchr(str,';');
00147 
00148   if (p1 == 0){
00149 /*    icalerror_set_errno(ICAL_BADARG_ERROR);*/
00150     return stat;
00151   }
00152 
00153   /* Just ignore the second clause; it will be taken from inside the library 
00154    */
00155 
00156 
00157 
00158   p2 = strchr(p1+1,';');
00159   if (p2 != 0 && *p2 != 0){
00160     stat.debug = p2+1;
00161   } 
00162 
00163   return stat;
00164   
00165 }
00166 
00167 const char* icalreqstattype_as_string(struct icalreqstattype stat)
00168 {
00169   char *temp;
00170 
00171   temp = (char*)icalmemory_tmp_buffer(TEMP_MAX);
00172 
00173   icalerror_check_arg_rz((stat.code != ICAL_UNKNOWN_STATUS),"Status");
00174   
00175   if (stat.desc == 0){
00176     stat.desc = icalenum_reqstat_desc(stat.code);
00177   }
00178   
00179   if(stat.debug != 0){
00180     snprintf(temp,TEMP_MAX,"%d.%d;%s;%s", icalenum_reqstat_major(stat.code),
00181              icalenum_reqstat_minor(stat.code),
00182              stat.desc, stat.debug);
00183     
00184   } else {
00185     snprintf(temp,TEMP_MAX,"%d.%d;%s", icalenum_reqstat_major(stat.code),
00186              icalenum_reqstat_minor(stat.code),
00187              stat.desc);
00188   }
00189 
00190   return temp;
00191 }