/*
- Copyright (C) 2000-2005 SKYRIX Software AG
+ Copyright (C) 2000-2007 SKYRIX Software AG
+ Copyright (C) 2007 Helge Hess
This file is part of SOPE.
*/
#include "NSString+DN.h"
+#include <NGExtensions/NSString+Ext.h>
#include "common.h"
static NSString *dnSeparator = @",";
-static NSString *_stripSpaces(NSString *s) {
- NSString *result = nil;
- char *cstr, *tmp;
- unsigned len, clen;
-
- if ([s rangeOfString:@" "].length == 0)
- return s;
-
- len = [s cStringLength];
- cstr = malloc(len + 1);
- [s getCString:cstr];
-
- tmp = cstr;
- clen = len;
-
- /* strip leading spaces */
- while ((*tmp == ' ') && (*tmp != '\0')) {
- tmp++;
- clen--;
- }
-
- /* strip trailing spaces */
- while (clen > 0) {
- if (tmp[clen - 1] != ' ')
- break;
- clen--;
- }
- tmp[clen] = '\0';
-
- result = [NSString stringWithCString:tmp length:clen];
- if (cstr)
- free(cstr);
- return result;
-}
-
static NSArray *cleanDNComponents(NSArray *_components) {
unsigned i, count;
id *cs;
- count = [_components count];
-
- if (count == 0)
+ if ((count = [_components count]) == 0)
return nil;
cs = calloc(count, sizeof(id));
- for (i = 0; i < count; i++) {
- NSString *rdn;
-
- rdn = [_components objectAtIndex:i];
- cs[i] = _stripSpaces(rdn);
- }
+
+ for (i = 0; i < count; i++)
+ cs[i] = [[_components objectAtIndex:i] stringByTrimmingWhiteSpaces];
+
_components = [NSArray arrayWithObjects:cs count:count];
- if (cs) free(cs);
-
+ if (cs != NULL) { free(cs); cs = NULL; }
+
return _components;
}
- (NSString *)stringByAppendingDNComponent:(NSString *)_component {
NSString *s;
- s = _stripSpaces(self);
- if (![s isNotEmpty])
+ if (![(s = [self stringByTrimmingWhiteSpaces]) isNotEmpty])
return _component;
-
+
s = [dnSeparator stringByAppendingString:self];
return [_component stringByAppendingString:s];
}
r = [self rangeOfString:dnSeparator];
if (r.length == 0) return nil;
- return _stripSpaces([self substringFromIndex:(r.location + r.length)]);
+ return [[self substringFromIndex:(r.location + r.length)]
+ stringByTrimmingWhiteSpaces];
}
- (NSString *)lastDNComponent {
r = [self rangeOfString:dnSeparator];
if (r.length == 0) return nil;
- return _stripSpaces([self substringToIndex:r.location]);
+ return [[self substringToIndex:r.location] stringByTrimmingWhiteSpaces];
}
- (const char *)ldapRepresentation {