Back to index

lightning-sunbird  0.9+nobinonly
icalperiod.c
Go to the documentation of this file.
00001 /* -*- Mode: C -*-
00002   ======================================================================
00003   FILE: icalperiod.c
00004   CREATOR: eric 02 June 2000
00005   
00006   $Id: icalperiod.c,v 1.11 2002/10/30 21:22:03 acampi Exp $
00007   $Locker:  $
00008     
00009  (C) COPYRIGHT 2000, Eric Busboom, http://www.softwarestudio.org
00010 
00011  This program is free software; you can redistribute it and/or modify
00012  it under the terms of either: 
00013 
00014     The LGPL as published by the Free Software Foundation, version
00015     2.1, available at: http://www.fsf.org/copyleft/lesser.html
00016 
00017   Or:
00018 
00019     The Mozilla Public License Version 1.0. You may obtain a copy of
00020     the License at http://www.mozilla.org/MPL/
00021 
00022  The Original Code is eric. The Initial Developer of the Original
00023  Code is Eric Busboom
00024 
00025 
00026  ======================================================================*/
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 #include "icalperiod.h"
00033 
00034 #include <assert.h>
00035 #include <string.h>
00036 #include <stdlib.h>
00037 #include <stdio.h>
00038 
00039 #include "icalerror.h"
00040 #include "icalmemory.h"
00041 
00042 
00043 
00044 
00045 struct icalperiodtype icalperiodtype_from_string (const char* str)
00046 {
00047     
00048     struct icalperiodtype p, null_p;
00049     char *s = icalmemory_strdup(str);
00050     char *start, *end = s;
00051     icalerrorstate es;
00052 
00053     /* Errors are normally generated in the following code, so save
00054        the error state for resoration later */
00055 
00056     icalerrorenum e = icalerrno;
00057 
00058     p.start = p.end = icaltime_null_time();
00059     p.duration = icaldurationtype_from_int(0);
00060 
00061     null_p = p;
00062 
00063     if(s == 0) goto error;
00064 
00065     start = s;
00066     end = strchr(s, '/');
00067 
00068     if(end == 0) goto error;
00069 
00070     *end = 0;
00071     end++;
00072 
00073     p.start = icaltime_from_string(start);
00074 
00075     if (icaltime_is_null_time(p.start)) goto error;
00076 
00077     es = icalerror_get_error_state(ICAL_MALFORMEDDATA_ERROR);
00078     icalerror_set_error_state(ICAL_MALFORMEDDATA_ERROR,ICAL_ERROR_NONFATAL);
00079 
00080     p.end = icaltime_from_string(end);
00081 
00082     icalerror_set_error_state(ICAL_MALFORMEDDATA_ERROR,es);
00083     
00084 
00085     if (icaltime_is_null_time(p.end)){
00086 
00087        p.duration = icaldurationtype_from_string(end);
00088 
00089        if(icaldurationtype_as_int(p.duration) == 0) goto error;
00090     } 
00091 
00092     icalerrno = e;
00093 
00094     icalmemory_free_buffer(s);
00095 
00096     return p;
00097 
00098  error:
00099     icalerror_set_errno(ICAL_MALFORMEDDATA_ERROR);
00100 
00101     if (s)
00102        icalmemory_free_buffer (s);
00103     return null_p;
00104 }
00105 
00106 
00107 const char* icalperiodtype_as_ical_string(struct icalperiodtype p)
00108 {
00109 
00110     const char* start;
00111     const char* end;
00112 
00113     char *buf;
00114     size_t buf_size = 40;
00115     char* buf_ptr = 0;
00116 
00117     buf = (char*)icalmemory_new_buffer(buf_size);
00118     buf_ptr = buf;
00119     
00120 
00121     start = icaltime_as_ical_string(p.start);
00122 
00123     icalmemory_append_string(&buf, &buf_ptr, &buf_size, start); 
00124 
00125     if(!icaltime_is_null_time(p.end)){
00126        end = icaltime_as_ical_string(p.end);
00127     } else {
00128        end = icaldurationtype_as_ical_string(p.duration);
00129     }
00130 
00131     icalmemory_append_char(&buf, &buf_ptr, &buf_size, '/'); 
00132 
00133     icalmemory_append_string(&buf, &buf_ptr, &buf_size, end); 
00134     
00135        icalmemory_add_tmp_buffer(buf);
00136 
00137     return buf;
00138 }
00139 
00140 
00141 
00142 struct icalperiodtype icalperiodtype_null_period(void) {
00143     struct icalperiodtype p;
00144     p.start = icaltime_null_time();
00145     p.end = icaltime_null_time();
00146     p.duration = icaldurationtype_null_duration();
00147 
00148     return p;
00149 }
00150 int icalperiodtype_is_null_period(struct icalperiodtype p){
00151     
00152     if(icaltime_is_null_time(p.start) && 
00153        icaltime_is_null_time(p.end) && 
00154        icaldurationtype_is_null_duration(p.duration)){
00155        return 1;
00156     } else {
00157        return 0;
00158     }
00159 }
00160 
00161 int icalperiodtype_is_valid_period(struct icalperiodtype p){
00162     if(icaltime_is_valid_time(p.start) && 
00163        (icaltime_is_valid_time(p.end) || icaltime_is_null_time(p.end)) )
00164        {
00165            return 1;
00166        }
00167 
00168     return 0;
00169 }
00170