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
46 NSMutableString *foldedString;
48 NSRange subStringRange;
50 length = [self length];
55 foldedString = [NSMutableString new];
56 [foldedString autorelease];
58 subStringRange = NSMakeRange (0, 75);
59 [foldedString appendFormat: @"%@\r\n",
60 [self substringWithRange: subStringRange]];
61 subStringRange = NSMakeRange (75, 74);
62 while ((length - subStringRange.location) > 74)
64 [foldedString appendFormat: @" %@\r\n",
65 [self substringWithRange: subStringRange]];
66 subStringRange.location += 74;
68 subStringRange.length = length - subStringRange.location;
69 [foldedString appendFormat: @" %@",
70 [self substringWithRange: subStringRange]];
72 newString = foldedString;
78 - (NSArray *) asCardAttributeValues
80 NSMutableArray *values;
81 NSEnumerator *rawValues;
82 NSString *tmpString, *rawValue, *newString;
84 values = [NSMutableArray new];
88 [self _initCommaSeparator];
90 tmpString = [self stringByReplacingString: @"\\,"
91 withString: commaSeparator];
92 rawValues = [[tmpString componentsSeparatedByString: @","]
94 rawValue = [rawValues nextObject];
97 newString = [rawValue stringByReplacingString: commaSeparator
99 [values addObject: [newString stringByTrimmingSpaces]];
100 rawValue = [rawValues nextObject];
106 - (NSString *) escapedForCards
110 string = [self stringByReplacingString: @"\\"
111 withString: @"\\\\"];
112 string = [string stringByReplacingString: @","
114 // string = [string stringByReplacingString: @":"
115 // withString: @"\\:"];
116 string = [string stringByReplacingString: @";"
118 string = [string stringByReplacingString: @"\n"
120 string = [string stringByReplacingString: @"\r"
126 - (NSString *) unescapedFromCard
130 string = [self stringByReplacingString: @"\\,"
132 string = [string stringByReplacingString: @"\\:"
134 string = [string stringByReplacingString: @"\\;"
136 string = [string stringByReplacingString: @"\\n"
138 string = [string stringByReplacingString: @"\\r"
140 string = [string stringByReplacingString: @"\\\\"
146 - (NSTimeInterval) durationAsTimeInterval
151 P2H30M - "2 hours 30 minutes"
153 dur-value = (["+"] / "-") "P" (dur-date / dur-time / dur-week)
155 dur-date = dur-day [dur-time]
156 dur-time = "T" (dur-hour / dur-minute / dur-second)
157 dur-week = 1*DIGIT "W"
158 dur-hour = 1*DIGIT "H" [dur-minute]
159 dur-minute = 1*DIGIT "M" [dur-second]
160 dur-second = 1*DIGIT "S"
161 dur-day = 1*DIGIT "D"
168 if (![self hasPrefix:@"P"]) {
169 NSLog(@"Cannot parse iCal duration value: '%@'", self);
175 for (i = 1, len = [self length], isTime = NO; i < len; i++) {
178 c = [self characterAtIndex:i];
179 if (c == 't' || c == 'T') {
186 val = (val * 10) + (c - 48);
192 ti += (val * 7 * 24 * 60 * 60);
195 ti += (val * 24 * 60 * 60);
198 ti += (val * 60 * 60);
207 [self logWithFormat: @"cannot process duration unit: '%c'", c];
215 - (NSCalendarDate *) asCalendarDate
218 NSCalendarDate *date;
220 int year, month, day, hour, minute, second;
222 if ([self length] > 14)
224 cursor = NSMakeRange(0, 4);
225 year = [[self substringWithRange: cursor] intValue];
226 cursor.location += cursor.length;
228 month = [[self substringWithRange: cursor] intValue];
229 cursor.location += cursor.length;
230 day = [[self substringWithRange: cursor] intValue];
232 cursor.location += cursor.length + 1;
233 hour = [[self substringWithRange: cursor] intValue];
234 cursor.location += cursor.length;
235 minute = [[self substringWithRange: cursor] intValue];
236 cursor.location += cursor.length;
237 second = [[self substringWithRange: cursor] intValue];
239 utc = [NSTimeZone timeZoneWithAbbreviation: @"GMT"];
240 date = [NSCalendarDate dateWithYear: year month: month
241 day: day hour: hour minute: minute
251 - (NSArray *) commaSeparatedValues
253 NSEnumerator *rawValues;
254 NSMutableArray *values;
255 NSString *currentValue, *newValue;
257 values = [NSMutableArray new];
258 [values autorelease];
260 rawValues = [[self componentsSeparatedByString: @","] objectEnumerator];
261 currentValue = [rawValues nextObject];
264 newValue = [currentValue stringByTrimmingSpaces];
265 if ([newValue length])
266 [values addObject: newValue];
267 currentValue = [rawValues nextObject];