]> err.no Git - sope/blob - sope-ldap/NGLdap/NSString+DN.m
fixed copyrights for 2005
[sope] / sope-ldap / NGLdap / NSString+DN.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21
22 #include "NSString+DN.h"
23 #include "common.h"
24
25 static NSString *dnSeparator = @",";
26
27 static NSString *_stripSpaces(NSString *s) {
28   NSString *result = nil;
29   char     *cstr, *tmp;
30   unsigned len, clen;
31   
32   if ([s rangeOfString:@" "].length == 0)
33     return s;
34   
35   len = [s cStringLength];
36   cstr = malloc(len + 1);
37   [s getCString:cstr];
38
39   tmp  = cstr;
40   clen = len;
41
42   /* strip leading spaces */
43   while ((*tmp == ' ') && (*tmp != '\0')) {
44     tmp++;
45     clen--;
46   }
47
48   /* strip trailing spaces */
49   while (clen > 0) {
50     if (tmp[clen - 1] != ' ')
51       break;
52     clen--;
53   }
54   tmp[clen] = '\0';
55   
56   result = [NSString stringWithCString:tmp length:clen];
57   if (cstr)
58     free(cstr);
59   return result;
60 }
61
62 static NSArray *cleanDNComponents(NSArray *_components) {
63   unsigned i, count;
64   id *cs;
65   
66   count = [_components count];
67   
68   if (count == 0)
69     return nil;
70   
71   cs = calloc(count, sizeof(id));
72   for (i = 0; i < count; i++) {
73     NSString *rdn;
74     
75     rdn = [_components objectAtIndex:i];
76     cs[i] = _stripSpaces(rdn);
77   }
78   _components = [NSArray arrayWithObjects:cs count:count];
79   if (cs) free(cs);
80
81   return _components;
82 }
83
84 @implementation NSString(DNSupport)
85
86 + (NSString *)dnWithComponents:(NSArray *)_components {
87   return [cleanDNComponents(_components) componentsJoinedByString:dnSeparator];
88 }
89
90 - (NSArray *)dnComponents {
91   return cleanDNComponents([self componentsSeparatedByString:dnSeparator]);
92 }
93
94 - (NSString *)stringByAppendingDNComponent:(NSString *)_component {
95   NSString *s;
96
97   s = _stripSpaces(self);
98   if ([s length] == 0)
99     return _component;
100
101   s = [dnSeparator stringByAppendingString:self];
102   return [_component stringByAppendingString:s];
103 }
104
105 - (NSString *)stringByDeletingLastDNComponent {
106   NSRange r;
107   
108   r = [self rangeOfString:dnSeparator];
109   if (r.length == 0) return nil;
110   
111   return _stripSpaces([self substringFromIndex:(r.location + r.length)]);
112 }
113
114 - (NSString *)lastDNComponent {
115   NSRange r;
116   
117   r = [self rangeOfString:dnSeparator];
118   if (r.length == 0) return nil;
119   
120   return _stripSpaces([self substringToIndex:r.location]);
121 }
122
123 - (const char *)ldapRepresentation {
124   return [self UTF8String];
125 }
126
127 - (NSDate *)ldapTimestamp {
128   /* eg: '20000403055250Z' */
129   unsigned   len;
130   short      year, month, day, hour, minute, second;
131   NSString   *tzname;
132   NSTimeZone *tz;
133   
134   if ((len = [self length]) == 0)
135     return nil;
136
137   if (len < 14)
138     return nil;
139   
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];
146
147   /* timezone ??? */
148   tzname = @"GMT";
149   tz = [NSTimeZone timeZoneWithAbbreviation:tzname];
150
151   return [NSCalendarDate dateWithYear:year month:month
152                          day:day hour:hour minute:minute second:second
153                          timeZone:tz];
154 }
155
156 @end /* NSString(DNSupport) */