+/*
+ Copyright (C) 2000-2004 SKYRIX Software AG
+
+ This file is part of OGo
+
+ OGo is free software; you can redistribute it and/or modify it under
+ the terms of the GNU Lesser General Public License as published by the
+ Free Software Foundation; either version 2, or (at your option) any
+ later version.
+
+ OGo is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with OGo; see the file COPYING. If not, write to the
+ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
+ 02111-1307, USA.
+*/
+// $Id$
+
+
+#import "NSString+iCal.h"
+
+
+@implementation NSString (SOGoiCal)
+
+#if 0
+- (NSString *)iCalFoldedString {
+ /* RFC2445, 4.1 Content Lines
+
+ The iCalendar object is organized into individual lines of text,
+ called content lines. Content lines are delimited by a line break,
+ which is a CRLF sequence (US-ASCII decimal 13, followed by US-ASCII
+ decimal 10).
+ Lines of text SHOULD NOT be longer than 75 octets, excluding the line
+ break. Long content lines SHOULD be split into a multiple line
+ representations using a line "folding" technique. That is, a long
+ line can be split between any two characters by inserting a CRLF
+ immediately followed by a single linear white space character (i.e.,
+ SPACE, US-ASCII decimal 32 or HTAB, US-ASCII decimal 9).
+ Any sequence of CRLF followed immediately by a single linear white space
+ character is ignored (i.e., removed) when processing the content type.
+ */
+}
+#endif
+
+- (NSString *)iCalDQUOTESafeString {
+ static NSCharacterSet *escapeSet = nil;
+
+ if(escapeSet == nil) {
+ escapeSet = [NSCharacterSet characterSetWithCharactersInString:@"\""];
+ [escapeSet retain];
+ }
+ return [self iCalEscapedStringWithEscapeSet:escapeSet];
+}
+
+- (NSString *)iCalSafeString {
+ static NSCharacterSet *escapeSet = nil;
+
+ if(escapeSet == nil) {
+ escapeSet = [NSCharacterSet characterSetWithCharactersInString:@"\n,;\""];
+ [escapeSet retain];
+ }
+ return [self iCalEscapedStringWithEscapeSet:escapeSet];
+}
+
+- (NSString *)iCalEscapedStringWithEscapeSet:(NSCharacterSet *)_es {
+ /* Escape unsafe characters */
+ NSMutableString *safeString;
+ NSRange r, er;
+ BOOL needsEscaping;
+ unsigned length;
+
+ length = [self length];
+ r = NSMakeRange(0, length);
+ er = [self rangeOfCharacterFromSet:_es options:0 range:r];
+ needsEscaping = er.length > 0 ? YES : NO;
+ if(!needsEscaping) {
+ return self; /* cheap */
+ }
+ /* wild guess */
+ safeString = [NSMutableString stringWithCapacity:length + 10];
+ if(needsEscaping) {
+ NSRange todoRange;
+ /*
+ r == previous range, upto er.location
+ er == escape range
+ todoRange == what we still need to scan
+ */
+ length = r.length;
+ do {
+ NSString *s;
+
+ r.length = (er.location - 1) - r.location;
+ s = [self substringWithRange:r];
+ [safeString appendString:s];
+ [safeString appendString:@"\\"];
+ if([self characterAtIndex:er.location] == '\n') {
+ s = @"n";
+ }
+ else {
+ s = [self substringWithRange:er];
+ }
+ [safeString appendString:s];
+ r.location = NSMaxRange(er);
+ todoRange.location = r.location;
+ todoRange.length = length - r.location;
+ er = [self rangeOfCharacterFromSet:_es
+ options:0
+ range:todoRange];
+ }
+ while(er.length > 0);
+ if(todoRange.length > 0) {
+ [safeString appendString:[self substringWithRange:todoRange]];
+ }
+ }
+ return safeString;
+}
+
+@end