2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
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
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.
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
22 #include "NSString+DN.h"
25 static NSString *dnSeparator = @",";
27 static NSString *_stripSpaces(NSString *s) {
28 NSString *result = nil;
32 if ([s rangeOfString:@" "].length == 0)
35 len = [s cStringLength];
36 cstr = malloc(len + 1);
42 /* strip leading spaces */
43 while ((*tmp == ' ') && (*tmp != '\0')) {
48 /* strip trailing spaces */
50 if (tmp[clen - 1] != ' ')
56 result = [NSString stringWithCString:tmp length:clen];
62 static NSArray *cleanDNComponents(NSArray *_components) {
66 count = [_components count];
71 cs = calloc(count, sizeof(id));
72 for (i = 0; i < count; i++) {
75 rdn = [_components objectAtIndex:i];
76 cs[i] = _stripSpaces(rdn);
78 _components = [NSArray arrayWithObjects:cs count:count];
84 @implementation NSString(DNSupport)
86 + (NSString *)dnWithComponents:(NSArray *)_components {
87 return [cleanDNComponents(_components) componentsJoinedByString:dnSeparator];
90 - (NSArray *)dnComponents {
91 return cleanDNComponents([self componentsSeparatedByString:dnSeparator]);
94 - (NSString *)stringByAppendingDNComponent:(NSString *)_component {
97 s = _stripSpaces(self);
101 s = [dnSeparator stringByAppendingString:self];
102 return [_component stringByAppendingString:s];
105 - (NSString *)stringByDeletingLastDNComponent {
108 r = [self rangeOfString:dnSeparator];
109 if (r.length == 0) return nil;
111 return _stripSpaces([self substringFromIndex:(r.location + r.length)]);
114 - (NSString *)lastDNComponent {
117 r = [self rangeOfString:dnSeparator];
118 if (r.length == 0) return nil;
120 return _stripSpaces([self substringToIndex:r.location]);
123 - (const char *)ldapRepresentation {
124 return [self UTF8String];
127 - (NSDate *)ldapTimestamp {
128 /* eg: '20000403055250Z' */
130 short year, month, day, hour, minute, second;
134 if ((len = [self length]) == 0)
140 year = [[self substringWithRange:NSMakeRange(0, 4)] intValue];
141 month = [[self substringWithRange:NSMakeRange(4, 2)] intValue];
142 day = [[self substringWithRange:NSMakeRange(6, 2)] intValue];
143 hour = [[self substringWithRange:NSMakeRange(8, 2)] intValue];
144 minute = [[self substringWithRange:NSMakeRange(10, 2)] intValue];
145 second = [[self substringWithRange:NSMakeRange(12, 2)] intValue];
149 tz = [NSTimeZone timeZoneWithAbbreviation:tzname];
151 return [NSCalendarDate dateWithYear:year month:month
152 day:day hour:hour minute:minute second:second
156 @end /* NSString(DNSupport) */