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