Back to index

lightning-sunbird  0.9+nobinonly
recur.c
Go to the documentation of this file.
00001 /* -*- Mode: C -*-
00002   ======================================================================
00003   FILE: recur.c
00004   CREATOR: ebusboom 8jun00
00005   
00006   DESCRIPTION:
00007 
00008   Test program for expanding recurrences. Run as:
00009 
00010      ./recur ../../test-data/recur.txt
00011 
00012   
00013   (C) COPYRIGHT 1999 Eric Busboom 
00014   http://www.softwarestudio.org
00015 
00016   The contents of this file are subject to the Mozilla Public License
00017   Version 1.0 (the "License"); you may not use this file except in
00018   compliance with the License. You may obtain a copy of the License at
00019   http://www.mozilla.org/MPL/
00020  
00021   Software distributed under the License is distributed on an "AS IS"
00022   basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
00023   the License for the specific language governing rights and
00024   limitations under the License.
00025 
00026   ======================================================================*/
00027 
00028 #include <assert.h>
00029 #include <string.h> /* for strdup */
00030 #include <stdlib.h> /* for malloc */
00031 #include <stdio.h> /* for printf */
00032 #include <time.h> /* for time() */
00033 #include <signal.h> /* for signal */
00034 #ifndef WIN32
00035 #include <unistd.h> /* for alarm */
00036 #endif
00037 
00038 #include "ical.h"
00039 #include "icalss.h"
00040 
00041 #ifdef WIN32
00042 #define snprintf     _snprintf
00043 #define strcasecmp   stricmp
00044 #endif
00045 
00046 static void sig_alrm(int i){
00047     fprintf(stderr,"Could not get lock on file\n");
00048     exit(1);
00049 }
00050 
00051 static void recur_callback(icalcomponent *comp,
00052                         struct icaltime_span *span,
00053                         void *data)
00054 {
00055   printf("cb: %s", ctime(&span->start));
00056   printf("    %s\n", ctime(&span->end));
00057 
00058 }
00059 
00060 int main(int argc, char *argv[])
00061 {
00062     icalset *cin;
00063     struct icaltimetype next;
00064     icalcomponent *itr;
00065     icalproperty *desc, *dtstart, *rrule;
00066     struct icalrecurrencetype recur;
00067     icalrecur_iterator* ritr;
00068     time_t tt;
00069     char* file; 
00070        
00071     icalerror_set_error_state(ICAL_PARSE_ERROR, ICAL_ERROR_NONFATAL);
00072        
00073 #ifndef WIN32
00074     signal(SIGALRM,sig_alrm);
00075 #endif
00076        
00077     if (argc <= 1){
00078               file = "../../test-data/recur.txt";
00079     } else if (argc == 2){
00080               file = argv[1];
00081     } else {
00082               fprintf(stderr,"usage: recur [input file]\n");
00083               exit(1);
00084     }
00085        
00086 #ifndef WIN32
00087     alarm(300); /* to get file lock */
00088 #endif
00089     cin = icalfileset_new(file);
00090 #ifndef WIN32
00091     alarm(0);
00092 #endif
00093        
00094     if(cin == 0){
00095               fprintf(stderr,"recur: can't open file %s\n",file);
00096               exit(1);
00097     }
00098        
00099        
00100     for (itr = icalfileset_get_first_component(cin);
00101        itr != 0;
00102        itr = icalfileset_get_next_component(cin)){
00103               
00104       struct icaltimetype start = icaltime_from_timet(1,0);
00105       struct icaltimetype end = icaltime_today();
00106 
00107 
00108 
00109               desc = icalcomponent_get_first_property(itr,ICAL_DESCRIPTION_PROPERTY);
00110               dtstart = icalcomponent_get_first_property(itr,ICAL_DTSTART_PROPERTY);
00111               rrule = icalcomponent_get_first_property(itr,ICAL_RRULE_PROPERTY);
00112               
00113               if (desc == 0 || dtstart == 0 || rrule == 0){
00114                      printf("\n******** Error in input component ********\n");
00115                      printf("The following component is malformed:\n %s\n",
00116                             icalcomponent_as_ical_string(itr));
00117                      continue;
00118               }
00119               
00120               printf("\n\n#### %s\n",icalproperty_get_description(desc));
00121               printf("#### %s\n",icalvalue_as_ical_string(icalproperty_get_value(rrule)));
00122               recur = icalproperty_get_rrule(rrule);
00123               start = icalproperty_get_dtstart(dtstart);
00124               
00125               ritr = icalrecur_iterator_new(recur,start);
00126               
00127               tt = icaltime_as_timet(start);
00128               
00129               printf("#### %s\n",ctime(&tt ));
00130 
00131               icalrecur_iterator_free(ritr);
00132               
00133               for(ritr = icalrecur_iterator_new(recur,start),
00134                      next = icalrecur_iterator_next(ritr); 
00135               !icaltime_is_null_time(next);
00136               next = icalrecur_iterator_next(ritr)){
00137                      
00138                      tt = icaltime_as_timet(next);
00139                      
00140                      printf("  %s",ctime(&tt ));        
00141                      
00142               }
00143               icalrecur_iterator_free(ritr);
00144 
00145       icalcomponent_foreach_recurrence(itr, start, end, 
00146                                    recur_callback, NULL);
00147       
00148 
00149               
00150     }
00151 
00152        icalset_free(cin);
00153 
00154        icaltimezone_free_builtin_timezones();
00155        
00156        icalmemory_free_ring();
00157        
00158        free_zone_directory();
00159        
00160     return 0;
00161 }