]> err.no Git - sope/blob - sope-ical/versitSaxDriver/VSStringFormatter.m
f2035e760fe6e9a27ee284886181585d922df0a8
[sope] / sope-ical / versitSaxDriver / VSStringFormatter.m
1 /*
2  Copyright (C) 2004 OpenGroupware.org
3  
4  This file is part of versitSaxDriver, written for the OpenGroupware.org
5  project (OGo).
6  
7  OGo is free software; you can redistribute it and/or modify it under
8  the terms of the GNU Lesser General Public License as published by the
9  Free Software Foundation; either version 2, or (at your option) any
10  later version.
11  
12  OGo is distributed in the hope that it will be useful, but WITHOUT ANY
13  WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15  License for more details.
16  
17  You should have received a copy of the GNU Lesser General Public
18  License along with OGo; see the file COPYING.  If not, write to the
19  Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20  02111-1307, USA.
21  */
22 // $Id$
23
24
25 #include "VSStringFormatter.h"
26 #include "common.h"
27
28 @implementation VSStringFormatter
29
30 static NSCharacterSet *escSet = nil;
31
32 + (void)initialize {
33   static BOOL didInit = NO;
34   
35   if(didInit)
36     return;
37   
38   didInit = YES;
39   escSet = [[NSCharacterSet characterSetWithCharactersInString:@"\\"] retain];
40 }
41
42 + (id)sharedFormatter {
43   static id sharedInstance = nil;
44   if(!sharedInstance) {
45     sharedInstance = [[self alloc] init];
46   }
47   return sharedInstance;
48 }
49
50 - (NSString *)stringByUnescapingRFC2445Text:(NSString *)_s {
51   NSMutableString *safeString;
52   unsigned        length;
53   NSRange         prevRange, escRange;
54   NSRange         todoRange;
55   BOOL            needsEscaping;
56
57   length    = [_s length];
58   prevRange = NSMakeRange(0, length);
59   escRange  = [_s rangeOfCharacterFromSet:escSet options:0 range:prevRange];
60
61   needsEscaping = escRange.length > 0 ? YES : NO;
62   if (!needsEscaping)
63     return _s; /* cheap */
64   
65   safeString = [NSMutableString stringWithCapacity:length];
66   
67   do {
68     prevRange.length = escRange.location - prevRange.location;
69     if (prevRange.length > 0)
70       [safeString appendString:[_s substringWithRange:prevRange]];
71
72     /* test edge case */
73     if(NSMaxRange(escRange) < length) {
74       unichar c = [_s characterAtIndex:escRange.location + 1];
75       if(c == 'n' || c == 'N') {
76         [safeString appendString:@"\n"];
77         escRange.length += 1; /* skip the 'n' */
78       }
79     }
80     
81     prevRange.location = NSMaxRange(escRange);
82     todoRange.location = prevRange.location;
83     todoRange.length   = length - prevRange.location;
84     escRange           = [_s rangeOfCharacterFromSet:escSet
85                              options:0
86                              range:todoRange];
87   }
88   while(escRange.length > 0);
89   
90   if (todoRange.length > 0)
91     [safeString appendString:[_s substringWithRange:todoRange]];
92   
93   return safeString;
94 }
95
96 @end