]> err.no Git - sope/blob - sope-ical/samples/vcfparsetest.m
54af86b42b672a6c23f4d0a449927cc51e2614e9
[sope] / sope-ical / samples / vcfparsetest.m
1 /*
2   Copyright (C) 2005 Helge Hess
3
4   This file is part of SOPE.
5
6   SOPE is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with SOPE; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #import <Foundation/Foundation.h>
23
24 @interface vcsparsetest : NSObject
25 {
26 }
27
28 - (int)runWithArguments:(NSArray *)_args;
29
30 @end
31
32 #include <NGiCal/NGVCard.h>
33
34 @implementation vcsparsetest
35
36 - (id)init {
37   if ((self = [super init])) {
38   }
39   return self;
40 }
41 - (void)dealloc {
42   [super dealloc];
43 }
44
45 /* parsing */
46
47 - (id)parseFile:(NSString *)_path {
48   if ([_path length] == 0) return nil;
49   return [NGVCard parseVCardsFromSource:[NSURL fileURLWithPath:_path]];
50 }
51
52 - (void)printParsedObject:(NSArray *)_cards {
53   NSLog(@"cards: %@", _cards);
54   
55 #if 0
56   NSLog(@"  subcomponents: %@", [_object subComponents]);
57   
58   printf("%s", [[_object icalString] cString]);
59 #endif
60 }
61
62 /* run */
63
64 - (int)runWithArguments:(NSArray *)_args {
65   NSEnumerator      *args;
66   NSString          *arg;
67   
68   args = [_args objectEnumerator];
69   [args nextObject]; // process name ...
70
71   while ((arg = [args nextObject])) {
72     NSAutoreleasePool *pool2;
73
74     if ([arg hasPrefix:@"-"]) { /* consume defaults */
75       [args nextObject];
76       continue;
77     }
78     
79     pool2 = [[NSAutoreleasePool alloc] init];
80     {    
81       NSArray *cards;
82       
83       NS_DURING
84         cards = [self parseFile:arg];
85       NS_HANDLER
86         abort();
87       NS_ENDHANDLER;
88       
89       if (cards == nil)
90         NSLog(@"could not parse file: '%@'", arg);
91       else
92         [self printParsedObject:cards];
93     }
94     [pool2 release];
95   }
96   return 0;
97 }
98
99 @end /* vcsparsetest */
100
101 int main(int argc, char **argv, char **env)  {
102   NSAutoreleasePool *pool;
103   vcsparsetest *tool;
104   int rc;
105
106   pool = [[NSAutoreleasePool alloc] init];
107 #if LIB_FOUNDATION_LIBRARY  
108   [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
109 #endif
110   
111   if ((tool = [[vcsparsetest alloc] init]) != nil) {
112     NS_DURING
113       rc = [tool runWithArguments:[[NSProcessInfo processInfo] arguments]];
114     NS_HANDLER
115       abort();
116     NS_ENDHANDLER;
117     
118     [tool release];
119   }
120   else
121     rc = 1;
122   
123   [pool release];
124   return rc;
125 }