Index: src/Makefile ================================================================== --- src/Makefile +++ src/Makefile @@ -1,21 +1,25 @@ LIB = ${LIB_PREFIX}objfw${LIB_SUFFIX} LIB_MAJOR = 1 LIB_MINOR = 0 -SRCS = OFConstString.m \ - OFList.m \ - OFListObject.m \ - OFObject.m \ - OFString.m +SRCS = OFConstString.m \ + OFConstWideString.m \ + OFList.m \ + OFListObject.m \ + OFObject.m \ + OFString.m \ + OFWideString.m OBJCFLAGS += -fPIC -DPIC -fno-nil-recivers -fconstant-string-class=OFConstString INCLUDES = OFConstString.h \ + OFConstWideString.h \ OFList.h \ OFListObject.h \ OFObject.h \ - OFString.h + OFString.h \ + OFWideString.h include ../buildsys.mk LD = ${OBJC} LIBS += -lobjc Index: src/OFConstString.m ================================================================== --- src/OFConstString.m +++ src/OFConstString.m @@ -7,11 +7,10 @@ * This file is part of libobjfw. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE included in * the packaging of this file. */ -#import #import #import "OFConstString.h" @implementation OFConstString + new:(const char*)str @@ -29,11 +28,11 @@ if ((self = [super init])) { if (str == NULL) { length = 0; string = NULL; } else { - length = strlen(string); + length = strlen(str); string = str; } } return self; } ADDED src/OFConstWideString.h Index: src/OFConstWideString.h ================================================================== --- src/OFConstWideString.h +++ src/OFConstWideString.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2008 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of libobjfw. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE included in + * the packaging of this file. + */ + +#import +#import +#import "OFObject.h" + +@interface OFConstWideString: OFObject +{ + const wchar_t *wstring; + size_t length; +} + ++ new:(const wchar_t*)wstr; +- init; +- init:(const wchar_t*)wstr; +- (const wchar_t*)wcString; +- (size_t)length; +@end + +/* vim: se syn=objc: */ ADDED src/OFConstWideString.m Index: src/OFConstWideString.m ================================================================== --- src/OFConstWideString.m +++ src/OFConstWideString.m @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2008 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of libobjfw. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE included in + * the packaging of this file. + */ + +#import +#import "OFConstWideString.h" + +@implementation OFConstWideString ++ new:(const wchar_t*)wstr +{ + return [[OFConstWideString alloc] init:wstr]; +} + +- init +{ + return [self init:NULL]; +} + +- init:(const wchar_t*)wstr +{ + if ((self = [super init])) { + if (wstr == NULL) { + length = 0; + wstring = NULL; + } else { + length = wcslen(wstr); + wstring = wstr; + } + } + return self; +} + +- (const wchar_t*)wcString +{ + return wstring; +} + +- (size_t)length +{ + return length; +} +@end Index: src/OFString.m ================================================================== --- src/OFString.m +++ src/OFString.m @@ -29,11 +29,11 @@ if ((self = [super init])) { if (str == NULL) { length = 0; string = NULL; } else { - length = strlen(string); + length = strlen(str); if ((string = [self getMem:length]) == NULL) return NULL; memcpy(string, str, length); } } @@ -78,37 +78,34 @@ return self; } - (OFString*)clone { - if (string != NULL) - return [OFString new:string]; - return [OFString new]; + return [OFString new:string]; } - (OFString*)append: (const char*)str { - char *new_string; - size_t new_length, str_length; + char *newstr; + size_t newlen, strlength; if (str == NULL) return [self setTo:str]; - str_length = strlen(str); - new_length = length + str_length; + strlength = strlen(str); + newlen = length + strlength; - if ((new_string = - [self resizeMem:string toSize:new_length + 1]) == NULL) { + if ((newstr = [self resizeMem:string toSize:newlen + 1]) == NULL) { /* FIXME: Add error handling */ return nil; } - string = new_string; + string = newstr; - memcpy(string + length, str, str_length); - string[new_length] = '\0'; + memcpy(string + length, str, strlength); + string[newlen] = '\0'; - length = new_length; + length = newlen; return self; } @end ADDED src/OFWideString.h Index: src/OFWideString.h ================================================================== --- src/OFWideString.h +++ src/OFWideString.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2008 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of libobjfw. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE included in + * the packaging of this file. + */ + +#import +#import +#import "OFObject.h" + +@interface OFWideString: OFObject +{ + wchar_t *wstring; + size_t length; +} + ++ new:(const wchar_t*)wstr; +- init; +- init:(const wchar_t*)wstr; +- (wchar_t*)wcString; +- (size_t)length; +- (OFWideString*)setTo:(const wchar_t*)wstr; +- (OFWideString*)clone; +- (OFWideString*)append:(const wchar_t*)wstr; +@end + +/* vim: se syn=objc: */ ADDED src/OFWideString.m Index: src/OFWideString.m ================================================================== --- src/OFWideString.m +++ src/OFWideString.m @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2008 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of libobjfw. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE included in + * the packaging of this file. + */ + +#import +#import +#import +#import "OFWideString.h" + +@implementation OFWideString ++ new:(const wchar_t*)wstr +{ + return [[OFWideString alloc] init:wstr]; +} + +- init +{ + return [self init:NULL]; +} + +- init:(const wchar_t*)wstr +{ + if ((self = [super init])) { + if (wstr == NULL) { + length = 0; + wstring = NULL; + } else { + length = wcslen(wstr); + if ((wstring = + [self getMem:length * sizeof(wchar_t)]) == NULL) + return NULL; + memcpy(wstring, wstr, length * sizeof(wchar_t)); + } + } + return self; +} + +- (wchar_t*)wcString +{ + return wstring; +} + +- (size_t)length +{ + return length; +} + +- (OFWideString*)setTo:(const wchar_t*)wstr +{ + wchar_t *newstr; + size_t newlen; + + if (wstr == NULL) { + [self freeMem:wstring]; + + length = 0; + wstring = NULL; + + return self; + } + + newlen = wcslen(wstr); + if ((newstr = [self getMem:newlen * sizeof(wchar_t)]) == NULL) + return nil; + memcpy(newstr, wstr, newlen * sizeof(wchar_t)); + + if (wstring != NULL) + [self freeMem:wstring]; + + length = newlen; + wstring = newstr; + + return self; +} + +- (OFWideString*)clone +{ + return [OFWideString new:wstring]; +} + +- (OFWideString*)append: (const wchar_t*)wstr +{ + wchar_t *newstr; + size_t newlen, strlength; + + if (wstr == NULL) + return [self setTo:wstr]; + + strlength = wcslen(wstr); + newlen = length + strlength; + + if ((newstr = [self resizeMem:wstring toSize:(newlen + 1) * + sizeof(wchar_t)]) == NULL) { + /* FIXME: Add error handling */ + return nil; + } + + wstring = newstr; + + memcpy(wstring + length, wstr, strlength); + wstring[newlen] = '\0'; + + length = newlen; + + return self; +} +@end