Imported Upstream version 0.48
[platform/upstream/libical.git] / src / test / storage.c
1 /* -*- Mode: C -*-
2   ======================================================================
3   FILE: storage.c
4   CREATOR: eric 03 April 1999
5   
6   DESCRIPTION:
7   
8   $Id: storage.c,v 1.6 2008-01-02 20:07:46 dothebart Exp $
9   $Locker:  $
10
11   (C) COPYRIGHT 1999 Eric Busboom 
12   http://www.softwarestudio.org
13
14   The contents of this file are subject to the Mozilla Public License
15   Version 1.0 (the "License"); you may not use this file except in
16   compliance with the License. You may obtain a copy of the License at
17   http://www.mozilla.org/MPL/
18  
19   Software distributed under the License is distributed on an "AS IS"
20   basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
21   the License for the specific language governing rights and
22   limitations under the License.
23
24   The original author is Eric Busboom
25   The original code is usecases.c
26
27     
28   ======================================================================*/
29
30 #include <libical/ical.h>
31 #include <assert.h>
32 #include <string.h> /* for strdup */
33 #include <stdlib.h> /* for malloc */
34 #include <stdio.h> /* for printf */
35 #include <time.h> /* for time() */
36 #include "icalmemory.h"
37 #include "icaldirset.h"
38 #include "icalfileset.h"
39 #ifdef WITH_BDB4
40 #include "icalbdbset.h"
41 #endif
42 #include "icalerror.h"
43 #include "icalrestriction.h"
44 #include "icalcalendar.h"
45
46 #define OUTPUT_FILE "filesetout.ics"
47 #define DATABASE "calendar.db"
48
49 /* define sample calendar struct */
50 struct calendar {
51   int ID;
52   int total_size;
53
54   /* offsets */
55   int total_size_offset;
56   int vcalendar_size_offset;
57   int vcalendar_offset;
58   int title_size_offset;
59   int title_offset;
60
61   /* data */
62   int vcalendar_size;
63   char *vcalendar;
64
65   int title_size;
66   char *title;
67
68 };
69
70 int vcalendar_init(struct calendar **cal, char *vcalendar, char *title);
71
72 #ifdef WITH_BDB4
73 int get_title(DB *dbp, const DBT *pkey, const DBT *pdata, DBT *skey);
74 char * parse_vcalendar(const DBT *dbt) ;
75 #endif
76
77 char * pack_calendar(struct calendar *cal, int size);
78 struct calendar * unpack_calendar(char *str, int size);
79
80 char str[] = "BEGIN:VCALENDAR\n\
81 PRODID:\"-//RDU Software//NONSGML HandCal//EN\"\n\
82 VERSION:2.0\n\
83 BEGIN:VTIMEZONE\n\
84 TZID:US-Eastern\n\
85 BEGIN:STANDARD\n\
86 DTSTART:19981025T020000\n\
87 RDATE:19981025T020000\n\
88 TZOFFSETFROM:-0400\n\
89 TZOFFSETTO:-0500\n\
90 TZNAME:EST\n\
91 END:STANDARD\n\
92 BEGIN:DAYLIGHT\n\
93 DTSTART:19990404T020000\n\
94 RDATE:19990404T020000\n\
95 TZOFFSETFROM:-0500\n\
96 TZOFFSETTO:-0400\n\
97 TZNAME:EDT\n\
98 END:DAYLIGHT\n\
99 END:VTIMEZONE\n\
100 BEGIN:VEVENT\n\
101 DTSTAMP:19980309T231000Z\n\
102 UID:guid-1.host1.com\n\
103 ORGANIZER;ROLE=CHAIR:MAILTO:mrbig@host.com\n\
104 ATTENDEE;RSVP=TRUE;ROLE=REQ-PARTICIPANT;CUTYPE=GROUP:MAILTO:employee-A@host.com\n\
105 DESCRIPTION:Project XYZ Review Meeting\n\
106 CATEGORIES:MEETING\n\
107 CLASS:PUBLIC\n\
108 CREATED:19980309T130000Z\n\
109 SUMMARY:XYZ Project Review\n\
110 DTSTART;TZID=US-Eastern:19980312T083000\n\
111 DTEND;TZID=US-Eastern:19980312T093000\n\
112 LOCATION:1CP Conference Room 4350\n\
113 END:VEVENT\n\
114 BEGIN:BOOGA\n\
115 DTSTAMP:19980309T231000Z\n\
116 X-LIC-FOO:Booga\n\
117 DTSTOMP:19980309T231000Z\n\
118 UID:guid-1.host1.com\n\
119 END:BOOGA\n\
120 END:VCALENDAR";
121
122 char str2[] = "BEGIN:VCALENDAR\n\
123 PRODID:\"-//RDU Software//NONSGML HandCal//EN\"\n\
124 VERSION:2.0\n\
125 BEGIN:VEVENT\n\
126 DTSTAMP:19980309T231000Z\n\
127 UID:guid-1.host1.com\n\
128 ORGANIZER;ROLE=CHAIR:MAILTO:mrbig@host.com\n\
129 ATTENDEE;RSVP=TRUE;ROLE=REQ-PARTICIPANT;CUTYPE=GROUP:MAILTO:employee-A@host.com\n\
130 DESCRIPTION:Project XYZ Review Meeting\n\
131 CATEGORIES:MEETING\n\
132 CLASS:PUBLIC\n\
133 CREATED:19980309T130000Z\n\
134 SUMMARY:XYZ Project Review\n\
135 DTSTART;TZID=US-Eastern:19980312T083000\n\
136 DTEND;TZID=US-Eastern:19980312T093000\n\
137 LOCATION:1CP Conference Room 4350\n\
138 END:VEVENT\n\
139 END:VCALENDAR\n\
140 ";
141
142 void test_fileset()
143 {
144     icalfileset *cout;
145     int month = 0;
146     int count=0;
147     struct icaltimetype start, end;
148     icalcomponent *c,*clone, *itr;
149
150     start = icaltime_from_timet( time(0),0);
151     end = start;
152     end.hour++;
153
154     cout = icalfileset_new(OUTPUT_FILE);
155     assert(cout != 0);
156
157     c = icalparser_parse_string(str2);
158     assert(c != 0);
159
160     /* Add data to the file */
161
162     for(month = 1; month < 10; month++){
163         icalcomponent *event;
164         icalproperty *dtstart, *dtend;
165
166         cout = icalfileset_new(OUTPUT_FILE);
167         assert(cout != 0);
168
169         start.month = month; 
170         end.month = month;
171         
172         clone = icalcomponent_new_clone(c);
173         assert(clone !=0);
174         event = icalcomponent_get_first_component(clone,ICAL_VEVENT_COMPONENT);
175         assert(event != 0);
176
177         dtstart = icalcomponent_get_first_property(event,ICAL_DTSTART_PROPERTY);
178         assert(dtstart!=0);
179         icalproperty_set_dtstart(dtstart,start);
180         
181         dtend = icalcomponent_get_first_property(event,ICAL_DTEND_PROPERTY);
182         assert(dtend!=0);
183         icalproperty_set_dtend(dtend,end);
184         
185         icalfileset_add_component(cout,clone);
186         icalfileset_commit(cout);
187
188         icalset_free(cout);
189
190     }
191
192     /* Print them out */
193
194     cout = icalfileset_new(OUTPUT_FILE);
195     assert(cout != 0);
196     
197     for (itr = icalfileset_get_first_component(cout);
198          itr != 0;
199          itr = icalfileset_get_next_component(cout)){
200
201       icalcomponent *event;
202       icalproperty *dtstart, *dtend;
203
204       count++;
205
206       event = icalcomponent_get_first_component(itr,ICAL_VEVENT_COMPONENT);
207
208       dtstart = icalcomponent_get_first_property(event,ICAL_DTSTART_PROPERTY);
209       dtend = icalcomponent_get_first_property(event,ICAL_DTEND_PROPERTY);
210       
211       printf("%d %s %s\n",count, icalproperty_as_ical_string(dtstart),
212              icalproperty_as_ical_string(dtend));
213
214     }
215
216     /* Remove all of them */
217
218     icalset_free(cout);
219
220     cout = icalfileset_new(OUTPUT_FILE);
221     assert(cout != 0);
222     
223     for (itr = icalfileset_get_first_component(cout);
224          itr != 0;
225          itr = icalfileset_get_next_component(cout)){
226
227
228       icalfileset_remove_component(cout, itr);
229     }
230
231     icalset_free(cout);
232
233
234     /* Print them out again */
235
236     cout = icalfileset_new(OUTPUT_FILE);
237     assert(cout != 0);
238     count =0;
239     
240     for (itr = icalfileset_get_first_component(cout);
241          itr != 0;
242          itr = icalfileset_get_next_component(cout)){
243
244       icalcomponent *event;
245       icalproperty *dtstart, *dtend;
246
247       count++;
248
249       event = icalcomponent_get_first_component(itr,ICAL_VEVENT_COMPONENT);
250
251       dtstart = icalcomponent_get_first_property(event,ICAL_DTSTART_PROPERTY);
252       dtend = icalcomponent_get_first_property(event,ICAL_DTEND_PROPERTY);
253       
254       printf("%d %s %s\n",count, icalproperty_as_ical_string(dtstart),
255              icalproperty_as_ical_string(dtend));
256
257     }
258
259     icalset_free(cout);
260
261
262 }
263
264 /*
265    In this example, we're storing a calendar with several components
266    under the reference id "calendar_7286" and retrieving records based
267    on title, "month_1" through "month_10".  We use a number of the 
268    "optional" arguments to specify secondary indices, sub-databases
269    (i.e. having multiple databases residing within a single Berkeley
270    DB file), and keys for storage and retrieval.
271 */
272
273 #ifdef WITH_BDB4
274 void test_bdbset()
275 {
276     icalbdbset *cout;
277     int month = 0;
278     int count=0;
279     int num_components=0;
280     int szdata_len=0;
281     int ret=0;
282     char *subdb, *szdata, *szpacked_data;
283     char uid[255];
284     struct icaltimetype start, end;
285     icalcomponent *c,*clone, *itr;
286     DB *dbp, *sdbp;
287     DBT key, data;
288     DBC *dbcp;
289
290     struct calendar *cal;
291     int cal_size;
292
293     start = icaltime_from_timet( time(0),0);
294     end = start;
295     end.hour++;
296
297     /* Note: as per the Berkeley DB ref pages: 
298      *
299      * The database argument is optional, and allows applications to
300      * have multiple databases in a single file. Although no database
301      * argument needs to be specified, it is an error to attempt to
302      * open a second database in a file that was not initially created
303      * using a database name. 
304      *
305      */
306
307     subdb = "calendar_id";
308     sdbp = 0;
309
310     /* open database, using subdb */
311     dbp = icalbdbset_database_open(DATABASE, subdb, DB_HASH);
312     sdbp = icalbdbset_secondary_open(dbp, 
313                                      DATABASE, 
314                                      "title", 
315                                      get_title, 
316                                      DB_HASH); 
317
318     c = icalparser_parse_string(str2);
319     assert(c != 0);
320
321     /* Add data to the file */
322
323     for(month = 1; month < 10; month++){
324       icalcomponent *event;
325       icalproperty *dtstart, *dtend, *location;
326
327       /* retrieve data */
328       cout = icalbdbset_new(dbp, sdbp, NULL);  
329       assert(cout != 0);
330
331       start.month = month; 
332       end.month = month;
333                 
334       clone = icalcomponent_new_clone(c);
335       assert(clone !=0);
336       event = icalcomponent_get_first_component(clone,
337                                                 ICAL_VEVENT_COMPONENT);
338       assert(event != 0);
339
340       dtstart = icalcomponent_get_first_property(event,
341                                                  ICAL_DTSTART_PROPERTY);
342       assert(dtstart!=0);
343       icalproperty_set_dtstart(dtstart,start);
344         
345       dtend = icalcomponent_get_first_property(event,ICAL_DTEND_PROPERTY);
346       assert(dtend!=0);
347       icalproperty_set_dtend(dtend,end);
348
349       location = icalcomponent_get_first_property(event, ICAL_LOCATION_PROPERTY);
350       assert(location!=0);
351
352 #if 0
353       /* change the uid to include the month */
354       sprintf(uid, "%s_%d", icalcomponent_get_uid(clone), month);
355       icalcomponent_set_uid(clone, uid);
356 #endif
357
358       icalbdbset_add_component(cout,clone);
359
360       /* commit changes */
361       icalbdbset_commit(cout);
362
363       num_components = icalcomponent_count_components(clone, ICAL_ANY_COMPONENT);
364
365       icalset_free(cout); 
366
367     }
368
369     /* try out the cursor operations */
370     memset(&key, 0, sizeof(DBT));
371     memset(&data, 0, sizeof(DBT));
372     
373     ret = icalbdbset_acquire_cursor(dbp, &dbcp);
374     ret = icalbdbset_get_first(dbcp, &key, &data);
375     ret = icalbdbset_get_next(dbcp, &key, &data);
376     ret = icalbdbset_get_last(dbcp, &key, &data);
377
378     /* Print them out */
379
380     for(month = 1, count=0; month < 10; month++){
381       char *title;
382
383       icalcomponent *event;
384       icalproperty *dtstart, *dtend;
385
386       cout = icalbdbset_new(dbp, sdbp, NULL);
387       assert(cout != 0);
388     
389       for (itr = icalbdbset_get_first_component(cout);
390            itr != 0;
391            itr = icalbdbset_get_next_component(cout)){
392
393         icalcomponent *event;
394         icalproperty *dtstart, *dtend;
395
396         count++;
397
398       event = icalcomponent_get_first_component(itr,ICAL_VEVENT_COMPONENT);
399
400       dtstart = icalcomponent_get_first_property(event,ICAL_DTSTART_PROPERTY);
401       dtend = icalcomponent_get_first_property(event,ICAL_DTEND_PROPERTY);
402       
403       printf("%d %s %s\n",count, icalproperty_as_ical_string(dtstart),
404              icalproperty_as_ical_string(dtend));
405
406       }
407       icalset_free(cout);
408     }
409
410     /* close database */
411     icalbdbset_database_close(dbp);
412     icalbdbset_database_close(sdbp); 
413
414     /* open database */
415     dbp = icalbdbset_database_open(DATABASE, subdb, DB_HASH);
416     sdbp = icalbdbset_secondary_open(dbp, 
417                                      DATABASE, 
418                                      "title", 
419                                      get_title, 
420                                      DB_HASH); 
421
422     /* Remove all of them */
423     for(month = 1; month < 10; month++){
424
425       cout = icalbdbset_new(dbp, sdbp, NULL);
426       assert(cout != 0);
427     
428       for (itr = icalbdbset_get_first_component(cout);
429            itr != 0;
430            itr = icalbdbset_get_next_component(cout)){
431         
432         icalbdbset_remove_component(cout, itr);
433       }
434
435       icalbdbset_commit(cout);
436       icalset_free(cout);
437
438     }
439
440     /* Print them out again */
441
442     for(month = 1, count=0; month < 10; month++){
443       char *title;
444
445       icalcomponent *event;
446       icalproperty *dtstart, *dtend;
447
448       cout = icalbdbset_new(dbp, sdbp, NULL);
449       assert(cout != 0);
450     
451       for (itr = icalbdbset_get_first_component(cout);
452            itr != 0;
453            itr = icalbdbset_get_next_component(cout)){
454
455         icalcomponent *event;
456         icalproperty *dtstart, *dtend;
457
458         count++;
459
460       event = icalcomponent_get_first_component(itr,ICAL_VEVENT_COMPONENT);
461
462       dtstart = icalcomponent_get_first_property(event,ICAL_DTSTART_PROPERTY);
463       dtend = icalcomponent_get_first_property(event,ICAL_DTEND_PROPERTY);
464       
465       printf("%d %s %s\n",count, icalproperty_as_ical_string(dtstart),
466              icalproperty_as_ical_string(dtend));
467
468       }
469       icalset_free(cout);
470     }
471 }
472
473 /* get_title -- extracts a secondary key (the vcalendar)
474  * from a primary key/data pair */
475
476 /* just create a random title for now */
477
478 int get_title(DB *dbp, const DBT *pkey, const DBT *pdata, DBT *skey)
479 {
480   icalcomponent *cl;
481   char title[255];
482
483   memset(skey, 0, sizeof(DBT)); 
484
485   cl = icalparser_parse_string((char *)pdata->data);
486   sprintf(title, "title_%s", icalcomponent_get_uid(cl));
487
488   skey->data = strdup(title);
489   skey->size = strlen(skey->data);
490   return (0); 
491 }
492
493 char * parse_vcalendar(const DBT *dbt) 
494 {
495   char *str;
496   struct calendar *cal;
497
498   str = (char *)dbt->data;
499   cal = unpack_calendar(str, dbt->size);
500
501   return cal->vcalendar;
502 }
503
504 #endif
505
506
507 int vcalendar_init(struct calendar **rcal, char *vcalendar, char *title) 
508 {
509   int vcalendar_size, title_size, total_size;
510   struct calendar *cal;
511
512   if(vcalendar) 
513     vcalendar_size = strlen(vcalendar);
514   else {
515     vcalendar = "";
516     vcalendar_size = strlen(vcalendar);
517   }
518
519   if(title) 
520     title_size = strlen(title);
521   else {
522     title = "";
523     title_size = strlen(title);
524   }
525
526   total_size = sizeof(struct calendar) + vcalendar_size + title_size;
527
528   if((cal = (struct calendar *)malloc(total_size))==NULL)
529     return 0;
530   memset(cal, 0, total_size);
531
532   /* offsets */
533   cal->total_size_offset     = sizeof(int);
534   cal->vcalendar_size_offset = (sizeof(int) * 7);
535   cal->vcalendar_offset      = cal->vcalendar_size_offset + sizeof(int);
536   cal->title_size_offset     = cal->vcalendar_offset + vcalendar_size;
537   cal->title_offset          = cal->title_size_offset + sizeof(int);
538
539   /* sizes */
540   cal->total_size            = total_size;
541   cal->vcalendar_size        = vcalendar_size;
542   cal->title_size            = title_size;
543
544   if (vcalendar && *vcalendar) 
545     cal->vcalendar = strdup(vcalendar);
546
547   if (title && *title)
548     cal->title     = strdup(title);
549
550   *rcal = cal;
551
552   return 0;
553 }
554
555 char * pack_calendar(struct calendar *cal, int size) 
556 {
557   char *str;
558
559   if((str = (char *)malloc(sizeof(char) * size))==NULL)
560     return 0;
561
562   /* ID */
563   memcpy(str, &cal->ID, sizeof(cal->ID));
564
565   /* total_size */
566   memcpy(str + cal->total_size_offset,
567          &cal->total_size,
568          sizeof(cal->total_size));
569
570   /* vcalendar_size */
571   memcpy(str + cal->vcalendar_size_offset, 
572          &cal->vcalendar_size, 
573          sizeof(cal->vcalendar_size));
574
575   /* vcalendar */
576   memcpy(str + cal->vcalendar_offset, 
577          cal->vcalendar, 
578          cal->vcalendar_size);
579
580   /* title_size */
581   memcpy(str + cal->title_size_offset,
582          &cal->title_size,
583          sizeof(cal->title_size));
584
585   /* title */
586   memcpy(str + cal->title_offset,
587          cal->title,
588          cal->title_size);
589
590   return str;
591 }
592
593 struct calendar * unpack_calendar(char *str, int size)
594 {
595   struct calendar *cal;
596   if((cal = (struct calendar *) malloc(size))==NULL)
597     return 0;
598   memset(cal, 0, size);
599
600   /* offsets */
601   cal->total_size_offset     = sizeof(int);
602   cal->vcalendar_size_offset = (sizeof(int) * 7);
603   cal->vcalendar_offset      = cal->vcalendar_size_offset + sizeof(int);
604
605   /* ID */
606   memcpy(&cal->ID, str, sizeof(cal->ID));
607
608   /* total_size */
609   memcpy(&cal->total_size,
610          str + cal->total_size_offset,
611          sizeof(cal->total_size));
612
613   /* vcalendar_size */
614   memcpy(&cal->vcalendar_size, 
615          str + cal->vcalendar_size_offset, 
616          sizeof(cal->vcalendar_size));
617
618   if((cal->vcalendar = (char *)malloc(sizeof(char) * 
619                                       cal->vcalendar_size))==NULL)
620     return 0;
621
622   /* vcalendar */
623   memcpy(cal->vcalendar, 
624          (char *)(str + cal->vcalendar_offset), 
625          cal->vcalendar_size);
626
627   cal->title_size_offset     = cal->vcalendar_offset + cal->vcalendar_size;
628   cal->title_offset          = cal->title_size_offset + sizeof(int);
629
630   /* title_size */
631   memcpy(&cal->title_size,
632          str + cal->title_size_offset,
633          sizeof(cal->title_size));
634
635   if((cal->title = (char *)malloc(sizeof(char) *
636                                   cal->title_size))==NULL)
637     return 0;
638
639   /* title*/
640   memcpy(cal->title,
641          (char *)(str + cal->title_offset),
642          cal->title_size);
643
644   return cal;
645 }
646
647 int test_dirset()
648 {
649
650     icalcomponent *c, *gauge;
651     icalerrorenum error;
652     icalcomponent *itr;
653     icalfileset* cluster;
654     struct icalperiodtype rtime;
655     icaldirset *s = icaldirset_new("store");
656     int i;
657
658     assert(s != 0);
659
660     rtime.start = icaltime_from_timet( time(0),0);
661
662     cluster = icalfileset_new(OUTPUT_FILE);
663
664     assert(cluster != 0);
665
666 #define NUMCOMP 4
667
668     /* Duplicate every component in the cluster NUMCOMP times */
669
670     icalerror_clear_errno();
671
672     for (i = 1; i<NUMCOMP+1; i++){
673
674         /*rtime.start.month = i%12;*/
675         rtime.start.month = i;
676         rtime.end = rtime.start;
677         rtime.end.hour++;
678         
679         for (itr = icalfileset_get_first_component(cluster);
680              itr != 0;
681              itr = icalfileset_get_next_component(cluster)){
682             icalcomponent *clone, *inner;
683             icalproperty *p;
684
685             inner = icalcomponent_get_first_component(itr,ICAL_VEVENT_COMPONENT);
686             if (inner == 0){
687               continue;
688             }
689
690             /* Change the dtstart and dtend times in the component
691                pointed to by Itr*/
692
693             clone = icalcomponent_new_clone(itr);
694             inner = icalcomponent_get_first_component(itr,ICAL_VEVENT_COMPONENT);
695
696             assert(icalerrno == ICAL_NO_ERROR);
697             assert(inner !=0);
698
699             /* DTSTART*/
700             p = icalcomponent_get_first_property(inner,ICAL_DTSTART_PROPERTY);
701             assert(icalerrno  == ICAL_NO_ERROR);
702
703             if (p == 0){
704                 p = icalproperty_new_dtstart(rtime.start);
705                 icalcomponent_add_property(inner,p);
706             } else {
707                 icalproperty_set_dtstart(p,rtime.start);
708             }
709             assert(icalerrno  == ICAL_NO_ERROR);
710
711             /* DTEND*/
712             p = icalcomponent_get_first_property(inner,ICAL_DTEND_PROPERTY);
713             assert(icalerrno  == ICAL_NO_ERROR);
714
715             if (p == 0){
716                 p = icalproperty_new_dtstart(rtime.end);
717                 icalcomponent_add_property(inner,p);
718             } else {
719                 icalproperty_set_dtstart(p,rtime.end);
720             }
721             assert(icalerrno  == ICAL_NO_ERROR);
722             
723             printf("\n----------\n%s\n---------\n",icalcomponent_as_ical_string(inner));
724
725             error = icaldirset_add_component(s,
726                                              icalcomponent_new_clone(itr));
727             
728             assert(icalerrno  == ICAL_NO_ERROR);
729
730         }
731
732     }
733     
734     gauge = 
735         icalcomponent_vanew(
736             ICAL_VCALENDAR_COMPONENT,
737             icalcomponent_vanew(
738                 ICAL_VEVENT_COMPONENT,  
739                 icalproperty_vanew_summary(
740                     "Submit Income Taxes",
741                     icalparameter_new_xliccomparetype(ICAL_XLICCOMPARETYPE_EQUAL),
742                     0),
743                 0),
744             icalcomponent_vanew(
745                 ICAL_VEVENT_COMPONENT,  
746                 icalproperty_vanew_summary(
747                     "Bastille Day Party",
748                     icalparameter_new_xliccomparetype(ICAL_XLICCOMPARETYPE_EQUAL),
749                     0),
750                 0),
751             0);
752
753 #if 0
754
755
756     icaldirset_select(s,gauge);
757
758     for(c = icaldirset_first(s); c != 0; c = icaldirset_next(s)){
759         
760         printf("Got one! (%d)\n", count++);
761         
762         if (c != 0){
763             printf("%s", icalcomponent_as_ical_string(c));;
764             if (icaldirset_store(s2,c) == 0){
765                 printf("Failed to write!\n");
766             }
767             icalcomponent_free(c);
768         } else {
769             printf("Failed to get component\n");
770         }
771     }
772
773
774     icalset_free(s2);
775 #endif
776
777
778     for(c = icaldirset_get_first_component(s); 
779         c != 0; 
780         c = icaldirset_get_next_component(s)){
781
782         if (c != 0){
783             printf("%s", icalcomponent_as_ical_string(c));;
784         } else {
785             printf("Failed to get component\n");
786         }
787
788     }
789
790     /* Remove all of the components */
791     i=0;
792     while((c=icaldirset_get_current_component(s)) != 0 ){
793         i++;
794
795         icaldirset_remove_component(s,c);
796     }
797         
798
799     icalset_free(s);
800     return 0;
801 }
802
803 #if 0
804 void test_calendar()
805 {
806     icalcomponent *comp;
807     icalfileset *c;
808     icaldirset *s;
809     icalcalendar* calendar = icalcalendar_new("calendar");
810     icalerrorenum error;
811     struct icaltimetype atime = icaltime_null_time();
812
813     comp = icalcomponent_vanew(
814         ICAL_VEVENT_COMPONENT,
815         icalproperty_new_version("2.0"),
816         icalproperty_new_description("This is an event"),
817         icalproperty_new_dtstart(atime),
818         icalproperty_vanew_comment(
819             "Another Comment",
820             icalparameter_new_cn("A Common Name 1"),
821             icalparameter_new_cn("A Common Name 2"),
822             icalparameter_new_cn("A Common Name 3"),
823                 icalparameter_new_cn("A Common Name 4"),
824             0),
825         icalproperty_vanew_xlicerror(
826             "This is only a test",
827             icalparameter_new_xlicerrortype(ICAL_XLICERRORTYPE_COMPONENTPARSEERROR),
828             0),
829         
830         0);
831         
832         
833     s = icalcalendar_get_booked(calendar);
834
835     error = icaldirset_add_component(s,comp);
836     
837     assert(error == ICAL_NO_ERROR);
838
839     c = icalcalendar_get_properties(calendar);
840
841     error = icalfileset_add_component(c,icalcomponent_new_clone(comp));
842
843     assert(error == ICAL_NO_ERROR);
844
845     icalcalendar_free(calendar);
846
847 }
848 #endif
849
850 int main(int argc, char *argv[])
851 {
852
853     printf("\n------------Test File Set---------------\n");
854     test_fileset(); 
855
856     printf("\n------------Test Dir Set---------------\n");
857     test_dirset();
858
859 #ifdef WITH_BDB4
860     printf("\n------------Test BerkeleyDB Set---------------\n");
861     test_bdbset();
862 #endif
863
864 #if 0
865     printf("\n------------Test Calendar---------------\n");
866     test_calendar();
867 #endif
868
869     return 0;
870 }
871
872
873