@@ -29,10 +29,16 @@ #import "OFExceptions.h" #import "macros.h" #import "asprintf.h" #import "unicode.h" + +#ifndef _WIN32 +# define PATH_DELIM '/' +#else +# define PATH_DELIM '\\' +#endif extern const uint16_t of_iso_8859_15[256]; extern const uint16_t of_windows_1252[256]; /* References for static linking */ @@ -251,10 +257,23 @@ arguments: args] autorelease]; va_end(args); return ret; } + ++ stringWithPath: (OFString*)first, ... +{ + id ret; + va_list args; + + va_start(args, first); + ret = [[[self alloc] initWithPath: first + arguments: args] autorelease]; + va_end(args); + + return ret; +} + stringWithString: (OFString*)str { return [[[self alloc] initWithString: str] autorelease]; } @@ -285,11 +304,10 @@ - initWithCString: (const char*)str encoding: (enum of_string_encoding)encoding length: (size_t)len { - Class c; size_t i, j; self = [super init]; length = len; @@ -310,17 +328,17 @@ case OF_STRING_ENCODING_UTF_8: switch (of_string_check_utf8(str, length)) { case 1: is_utf8 = YES; break; - case -1: + case -1:; /* * We can't use [super dealloc] on OS X here. * Compiler bug? Anyway, [self dealloc] will do here as * we don't reimplement dealloc. */ - c = isa; + Class c = isa; [self dealloc]; @throw [OFInvalidEncodingException newWithClass: c]; } memcpy(string, str, length); @@ -346,18 +364,18 @@ chr = of_iso_8859_15[(uint8_t)str[i]]; break; case OF_STRING_ENCODING_WINDOWS_1252: chr = of_windows_1252[(uint8_t)str[i]]; break; - default: + default:; /* * We can't use [super dealloc] on OS X * here. Compiler bug? Anyway, * [self dealloc] will do here as we * don't reimplement dealloc. */ - c = isa; + Class c = isa; [self dealloc]; @throw [OFInvalidEncodingException newWithClass: c]; } @@ -366,11 +384,11 @@ * We can't use [super dealloc] on OS X * here. Compiler bug? Anyway, * [self dealloc] will do here as we * don't reimplement dealloc. */ - c = isa; + Class c = isa; [self dealloc]; @throw [OFInvalidEncodingException newWithClass: c]; } @@ -382,11 +400,11 @@ * We can't use [super dealloc] on OS X * here. Compiler bug? Anyway, * [self dealloc] will do here as we * don't reimplement dealloc. */ - c = isa; + Class c = isa; [self dealloc]; @throw [OFInvalidEncodingException newWithClass: c]; } @@ -412,17 +430,17 @@ } string[length] = 0; break; - default: + default:; /* * We can't use [super dealloc] on OS X here. * Compiler bug? Anyway, [self dealloc] will do here as we * don't reimplement dealloc. */ - c = isa; + Class c = isa; [self dealloc]; @throw [OFInvalidEncodingException newWithClass: c]; } return self; @@ -451,23 +469,22 @@ - initWithFormat: (OFString*)fmt arguments: (va_list)args { int t; - Class c; self = [super init]; if (fmt == nil) { - c = isa; + Class c = isa; [super dealloc]; @throw [OFInvalidArgumentException newWithClass: c selector: _cmd]; } if ((t = vasprintf(&string, [fmt cString], args)) == -1) { - c = isa; + Class c = isa; [super dealloc]; /* * This is only the most likely error to happen. Unfortunately, * there is no good way to check what really happened. @@ -475,18 +492,18 @@ @throw [OFOutOfMemoryException newWithClass: c]; } length = t; switch (of_string_check_utf8(string, length)) { - case 1: - is_utf8 = YES; - break; - case -1: - free(string); - c = isa; - [super dealloc]; - @throw [OFInvalidEncodingException newWithClass: c]; + case 1: + is_utf8 = YES; + break; + case -1:; + Class c = isa; + free(string); + [super dealloc]; + @throw [OFInvalidEncodingException newWithClass: c]; } @try { [self addMemoryToPool: string]; } @catch (OFException *e) { @@ -495,31 +512,103 @@ } return self; } -- initWithString: (OFString*)str +- initWithPath: (OFString*)first, ... +{ + id ret; + va_list args; + + va_start(args, first); + ret = [self initWithPath: first + arguments: args]; + va_end(args); + + return ret; +} + +- initWithPath: (OFString*)first + arguments: (va_list)args { + OFString *component; + size_t len, i; + va_list args2; Class c; self = [super init]; - string = (char*)[str cString]; - length = [str cStringLength]; + len = [first cStringLength]; + switch (of_string_check_utf8([first cString], len)) { + case 1: + is_utf8 = YES; + break; + case -1: + c = isa; + [self dealloc]; + @throw [OFInvalidEncodingException newWithClass: c]; + } + length += len; - switch (of_string_check_utf8(string, length)) { + va_copy(args2, args); + while ((component = va_arg(args2, OFString*)) != nil) { + len = [component cStringLength]; + length += 1 + len; + + switch (of_string_check_utf8([component cString], len)) { case 1: is_utf8 = YES; break; case -1: c = isa; [self dealloc]; @throw [OFInvalidEncodingException newWithClass: c]; + } + } + + @try { + string = [self allocMemoryWithSize: length + 1]; + } @catch (OFException *e) { + [self dealloc]; + @throw e; + } + + len = [first cStringLength]; + memcpy(string, [first cString], len); + i = len; + + while ((component = va_arg(args, OFString*)) != nil) { + len = [component length]; + string[i] = PATH_DELIM; + memcpy(string + i + 1, [component cString], len); + i += len + 1; + } + + string[i] = '\0'; + + return self; +} + +- initWithString: (OFString*)str +{ + self = [super init]; + + string = (char*)[str cString]; + length = [str cStringLength]; + + switch (of_string_check_utf8(string, length)) { + case 1: + is_utf8 = YES; + break; + case -1:; + Class c = isa; + [self dealloc]; + @throw [OFInvalidEncodingException newWithClass: c]; } if ((string = strdup(string)) == NULL) { - c = isa; + Class c = isa; [self dealloc]; @throw [OFOutOfMemoryException newWithClass: c size: length + 1]; }