1 /* NSString+NGCards.m - this file is part of SOPE
3 * Copyright (C) 2006 Inverse groupe conseil
5 * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
7 * This file is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * This file is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 #import <Foundation/NSArray.h>
24 #import <Foundation/NSCalendarDate.h>
25 #import <Foundation/NSRange.h>
26 #import <Foundation/NSTimeZone.h>
27 #import <NGExtensions/NSObject+Logs.h>
31 #import "NSString+NGCards.h"
33 static NSString *commaSeparator = nil;
35 @implementation NSString (NGCardsExtensions)
37 - (void) _initCommaSeparator
39 commaSeparator = [NSMutableString stringWithFormat: @"%c", 255];
40 [commaSeparator retain];
43 - (NSString *) foldedForVersitCards
45 NSMutableString *foldedString;
47 NSRange subStringRange;
49 foldedString = [NSMutableString new];
50 [foldedString autorelease];
52 length = [self length];
54 [foldedString appendString: self];
57 subStringRange = NSMakeRange (0, 75);
58 [foldedString appendFormat: @"%@\r\n",
59 [self substringWithRange: subStringRange]];
60 subStringRange = NSMakeRange (75, 74);
61 while ((length - subStringRange.location) > 74)
63 [foldedString appendFormat: @" %@\r\n",
64 [self substringWithRange: subStringRange]];
65 subStringRange.location += 74;
67 subStringRange.length = length - subStringRange.location;
68 [foldedString appendFormat: @" %@",
69 [self substringWithRange: subStringRange]];
75 - (NSArray *) asCardAttributeValues
77 NSMutableArray *values;
78 NSEnumerator *rawValues;
79 NSString *tmpString, *rawValue, *newString;
81 values = [NSMutableArray new];
85 [self _initCommaSeparator];
87 tmpString = [self stringByReplacingString: @"\\,"
88 withString: commaSeparator];
89 rawValues = [[tmpString componentsSeparatedByString: @","]
91 rawValue = [rawValues nextObject];
94 newString = [rawValue stringByReplacingString: commaSeparator
96 [values addObject: [newString stringByTrimmingSpaces]];
97 rawValue = [rawValues nextObject];
103 - (NSString *) escapedForCards
107 string = [self stringByReplacingString: @"\\"
108 withString: @"\\\\"];
109 string = [string stringByReplacingString: @","
111 // string = [string stringByReplacingString: @":"
112 // withString: @"\\:"];
113 string = [string stringByReplacingString: @";"
115 string = [string stringByReplacingString: @"\n"
117 string = [string stringByReplacingString: @"\r"
123 - (NSString *) unescapedFromCard
127 string = [self stringByReplacingString: @"\\,"
129 string = [string stringByReplacingString: @"\\:"
131 string = [string stringByReplacingString: @"\\;"
133 string = [string stringByReplacingString: @"\\n"
135 string = [string stringByReplacingString: @"\\r"
137 string = [string stringByReplacingString: @"\\\\"
143 - (NSTimeInterval) durationAsTimeInterval
148 P2H30M - "2 hours 30 minutes"
150 dur-value = (["+"] / "-") "P" (dur-date / dur-time / dur-week)
152 dur-date = dur-day [dur-time]
153 dur-time = "T" (dur-hour / dur-minute / dur-second)
154 dur-week = 1*DIGIT "W"
155 dur-hour = 1*DIGIT "H" [dur-minute]
156 dur-minute = 1*DIGIT "M" [dur-second]
157 dur-second = 1*DIGIT "S"
158 dur-day = 1*DIGIT "D"
165 if (![self hasPrefix:@"P"]) {
166 NSLog(@"Cannot parse iCal duration value: '%@'", self);
172 for (i = 1, len = [self length], isTime = NO; i < len; i++) {
175 c = [self characterAtIndex:i];
176 if (c == 't' || c == 'T') {
183 val = (val * 10) + (c - 48);
189 ti += (val * 7 * 24 * 60 * 60);
192 ti += (val * 24 * 60 * 60);
195 ti += (val * 60 * 60);
204 [self logWithFormat: @"cannot process duration unit: '%c'", c];
212 - (NSCalendarDate *) asCalendarDate
215 NSCalendarDate *date;
218 int year, month, day, hour, minute, second;
220 length = [self length];
223 cursor = NSMakeRange(0, 4);
224 year = [[self substringWithRange: cursor] intValue];
225 cursor.location += cursor.length;
227 month = [[self substringWithRange: cursor] intValue];
228 cursor.location += cursor.length;
229 day = [[self substringWithRange: cursor] intValue];
233 cursor.location += cursor.length + 1;
234 hour = [[self substringWithRange: cursor] intValue];
235 cursor.location += cursor.length;
236 minute = [[self substringWithRange: cursor] intValue];
237 cursor.location += cursor.length;
238 second = [[self substringWithRange: cursor] intValue];
247 utc = [NSTimeZone timeZoneWithAbbreviation: @"GMT"];
248 date = [NSCalendarDate dateWithYear: year month: month
249 day: day hour: hour minute: minute
259 - (BOOL) isAllDayDate
261 return ([self length] == 8);
264 - (NSArray *) commaSeparatedValues
266 NSEnumerator *rawValues;
267 NSMutableArray *values;
268 NSString *currentValue, *newValue;
270 values = [NSMutableArray new];
271 [values autorelease];
273 rawValues = [[self componentsSeparatedByString: @","] objectEnumerator];
274 currentValue = [rawValues nextObject];
277 newValue = [currentValue stringByTrimmingSpaces];
278 if ([newValue length])
279 [values addObject: newValue];
280 currentValue = [rawValues nextObject];