]> err.no Git - sope/blob - Recycler/iCalSaxDriver/NSString+ICal.m
5ef22ed77a346b17e547c692a31f2dcda41e9223
[sope] / Recycler / iCalSaxDriver / NSString+ICal.m
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
5
6   OGo 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   OGo 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 OGo; 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 // $Id$
22
23 #include "NSString+ICal.h"
24 #include "common.h"
25 #ifdef XCODE_BUILD
26 #import <libical/ical.h>
27 #else
28 #include <ical.h>
29 #endif
30
31 @implementation NSString(ICalCString)
32
33 - (const char *)icalCString {
34   NSStringEncoding enc;
35   
36   enc = [[self class] libicalStringEncoding];
37   
38   if (enc == NSUTF8StringEncoding)
39     return [self UTF8String];
40   else if (enc == [NSString defaultCStringEncoding])
41     return [self cString];
42   else
43     return [[self dataUsingEncoding:enc] bytes];
44 }
45
46 - (NSString *)icalString {
47   return self;
48 }
49
50 @end /* NSString(ICalCString) */
51
52 @implementation NSObject(TemporaryStringInit)
53
54 + (NSStringEncoding)libicalStringEncoding {
55   return NSUTF8StringEncoding;
56 }
57
58 - (id)initWithICalCString:(const char *)_cstr {
59   NSStringEncoding enc;
60
61   if (_cstr == NULL) {
62     RELEASE(self);
63     return nil;
64   }
65   
66   enc = [[self class] libicalStringEncoding];
67   
68   if (enc == NSUTF8StringEncoding)
69     return [(NSString *)self initWithUTF8String:_cstr];
70   else if (enc == [[self class] defaultCStringEncoding])
71     return [(NSString *)self initWithCString:_cstr];
72   else {
73     NSData *d;
74     
75     d = [[NSData alloc] initWithBytes:_cstr length:strlen(_cstr)];
76     self = [(NSString *)self initWithData:d encoding:enc];
77     RELEASE(d);
78     
79     return self;
80   }
81   
82   return nil;
83 }
84
85 - (id)initWithICalValueHandle:(icalvalue *)_handle {
86   const char *s;
87   
88   if (_handle == NULL) {
89     RELEASE(self);
90     return nil;
91   }
92   if ((s = icalvalue_as_ical_string(_handle)) == NULL) {
93     RELEASE(self);
94     return nil;
95   }
96   return [self initWithICalCString:s];
97 }
98
99 - (id)initWithICalValueOfProperty:(icalproperty *)_prop {
100   icalvalue *val;
101
102   if (_prop == nil) {
103     RELEASE(self);
104     return nil;
105   }
106   
107   if ((val = icalproperty_get_value(_prop)) == NULL) {
108     NSLog(@"%s: ical property has no value ??", __PRETTY_FUNCTION__);
109     RELEASE(self);
110     return nil;
111   }
112   
113   return [self initWithICalValueHandle:val];
114 }
115
116 @end /* NSObject(TemporaryStringInit) */
117