Index: src/Makefile ================================================================== --- src/Makefile +++ src/Makefile @@ -1,19 +1,21 @@ LIB = ${LIB_PREFIX}objfw${LIB_SUFFIX} LIB_MAJOR = 1 LIB_MINOR = 0 -SRCS = OFConstString.m \ - OFConstWideString.m \ +SRCS = OFConstCString.m \ + OFCString.m \ + OFConstWideCString.m \ OFExceptions.m \ OFList.m \ OFListObject.m \ OFObject.m \ OFString.m \ - OFWideString.m + OFWideCString.m INCLUDES = OFConstString.h \ + OFCString.h \ OFConstWideString.h \ OFExceptions.h \ OFList.h \ OFListObject.h \ OFObject.h \ ADDED src/OFCString.h Index: src/OFCString.h ================================================================== --- src/OFCString.h +++ src/OFCString.h @@ -0,0 +1,27 @@ +/* + * 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 "OFObject.h" +#import "OFString.h" + +@interface OFCString: OFString +{ + char *string; +} + +- initWithCString: (char*)str; +- (char*)cString; +- (OFString*)clone; +- (int)compare: (OFString*)str; +- (OFString*)append: (OFString*)str; +@end ADDED src/OFCString.m Index: src/OFCString.m ================================================================== --- src/OFCString.m +++ src/OFCString.m @@ -0,0 +1,70 @@ +/* + * 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 "OFCString.h" +#import "OFExceptions.h" + +@implementation OFCString +- initWithCString: (const char*)str +{ + if ((self = [super init])) { + if (str == NULL) { + length = 0; + string = NULL; + } else { + length = strlen(str); + string = [self getMem: length + 1]; + memcpy(string, str, length + 1); + } + } + return self; +} + +- (char*)cString +{ + return string; +} + +- (OFString*)clone +{ + return [OFString newWithCString: string]; +} + +- (int)compare: (OFString*)str +{ + return strcmp(string, [str cString]); +} + +- (OFString*)append: (OFString*)str +{ + char *newstr; + size_t newlen, strlength; + + if (str == NULL) + return [self setTo: str]; + + strlength = [str length]; + newlen = length + strlength; + + newstr = [self resizeMem: string + toSize: newlen + 1]; + + memcpy(newstr + length, [str cString], strlength + 1); + + length = newlen; + string = newstr; + + return self; +} +@end ADDED src/OFConstCString.h Index: src/OFConstCString.h ================================================================== --- src/OFConstCString.h +++ src/OFConstCString.h @@ -0,0 +1,23 @@ +/* + * 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 "OFString.h" + +@interface OFConstCString: OFString +{ + const char *string; +} + +- initWithConstCString: (const char*)str; +- (const char*)cString; +- (OFString*)clone; +- (int)compare: (OFString*)str; +@end ADDED src/OFConstCString.m Index: src/OFConstCString.m ================================================================== --- src/OFConstCString.m +++ src/OFConstCString.m @@ -0,0 +1,44 @@ +/* + * 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 "OFConstCString.h" + +@implementation OFConstCString +- initWithConstCString: (const char*)str +{ + if ((self = [super init])) { + if (str == NULL) { + length = 0; + string = NULL; + } else { + length = strlen(str); + string = str; + } + } + return self; +} + +- (const char*)cString +{ + return string; +} + +- (OFString*)clone +{ + return [OFString newWithConstCString: string]; +} + +- (int)compare: (OFString*)str +{ + return strcmp(string, [str cString]); +} +@end DELETED src/OFConstString.h Index: src/OFConstString.h ================================================================== --- src/OFConstString.h +++ src/OFConstString.h @@ -1,27 +0,0 @@ -/* - * 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 "OFObject.h" - -@interface OFConstString: OFObject -{ - const char *string; - size_t length; -} - -+ new: (const char*)str; -- init; -- init: (const char*)str; -- (const char*)cString; -- (size_t)length; -- (int)compare: (OFConstString*)str; -@end DELETED src/OFConstString.m Index: src/OFConstString.m ================================================================== --- src/OFConstString.m +++ src/OFConstString.m @@ -1,54 +0,0 @@ -/* - * 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 "OFConstString.h" - -@implementation OFConstString -+ new: (const char*)str -{ - return [[OFConstString alloc] init: str]; -} - -- init -{ - return [self init: NULL]; -} - -- init: (const char*)str -{ - if ((self = [super init])) { - if (str == NULL) { - length = 0; - string = NULL; - } else { - length = strlen(str); - string = str; - } - } - return self; -} - -- (const char*)cString -{ - return string; -} - -- (size_t)length -{ - return length; -} - -- (int)compare: (OFConstString*)str -{ - return strcmp(string, [str cString]); -} -@end ADDED src/OFConstWideCString.h Index: src/OFConstWideCString.h ================================================================== --- src/OFConstWideCString.h +++ src/OFConstWideCString.h @@ -0,0 +1,25 @@ +/* + * 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 "OFString.h" + +@interface OFConstWideCString: OFString +{ + const wchar_t *string; +} + +- initWithConstWideCString: (const wchar_t*)wstr; +- (const wchar_t*)wcString; +- (OFString*)clone; +- (int)compare: (OFString*)str; +@end ADDED src/OFConstWideCString.m Index: src/OFConstWideCString.m ================================================================== --- src/OFConstWideCString.m +++ src/OFConstWideCString.m @@ -0,0 +1,44 @@ +/* + * 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 "OFConstWideCString.h" + +@implementation OFConstWideCString +- initWithConstWideCString: (const wchar_t*)str +{ + if ((self = [super init])) { + if (str == NULL) { + length = 0; + string = NULL; + } else { + length = wcslen(str); + string = str; + } + } + return self; +} + +- (const wchar_t*)wcString +{ + return string; +} + +- (OFString*)clone +{ + return [OFString newWithConstWideCString: string]; +} + +- (int)compare: (OFString*)str +{ + return wcscmp(string, [str wcString]); +} +@end DELETED src/OFConstWideString.h Index: src/OFConstWideString.h ================================================================== --- src/OFConstWideString.h +++ src/OFConstWideString.h @@ -1,28 +0,0 @@ -/* - * 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; -- (int)compare: (OFConstWideString*)str; -@end DELETED src/OFConstWideString.m Index: src/OFConstWideString.m ================================================================== --- src/OFConstWideString.m +++ src/OFConstWideString.m @@ -1,54 +0,0 @@ -/* - * 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; -} - -- (int)compare: (OFConstWideString*)str -{ - return wcscmp(wstring, [str wcString]); -} -@end Index: src/OFExceptions.h ================================================================== --- src/OFExceptions.h +++ src/OFExceptions.h @@ -21,12 +21,19 @@ + new: (id)obj withSize: (size_t)size; - init: (id)obj withSize: (size_t)size; @end + +@interface OFNotImplementedException: OFException ++ new: (id)obj + withMethod: (const char*)method; +- init: (id)obj + withMethod: (const char*)method; +@end @interface OFMemNotPartOfObjException: OFException + new: (id)obj withPtr: (void*)ptr; - init: (id)obj withPtr: (void*)ptr; @end Index: src/OFExceptions.m ================================================================== --- src/OFExceptions.m +++ src/OFExceptions.m @@ -35,11 +35,27 @@ - init: (id)obj withSize: (size_t)size { fprintf(stderr, "ERROR: Could not allocate %zu bytes for object %s!\n", size, [obj name]); + return [super init]; +} +@end + +@implementation OFNotImplementedException ++ new: (id)obj + withMethod: (const char*)method +{ + return [[OFNotImplementedException alloc] init: obj + withMethod: method]; +} +- init: (id)obj + withMethod: (const char*)method +{ + fprintf(stderr, "ERROR: Requested method %s not implemented in %s!\n", + method, [obj name]); return [super init]; } @end @implementation OFMemNotPartOfObjException Index: src/OFString.h ================================================================== --- src/OFString.h +++ src/OFString.h @@ -7,25 +7,26 @@ * 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" -#import "OFConstString.h" @interface OFString: OFObject { - char *string; size_t length; } -+ new: (const char*)str; -- init; -- init: (const char*)str; ++ newWithConstCString: (const char*)str; ++ newWithConstWideCString: (const wchar_t*)str; ++ newWithCString: (char*)str; ++ newWithWideCString: (wchar_t*)str; - (char*)cString; +- (wchar_t*)wcString; - (size_t)length; -- (OFString*)setTo: (OFConstString*)str; +- (OFString*)setTo: (OFString*)str; - (OFString*)clone; -- (OFString*)append: (OFConstString*)str; -- (int)compare: (OFConstString*)str; +- (int)compare: (OFString*)str; +- (OFString*)append: (OFString*)str; @end Index: src/OFString.m ================================================================== --- src/OFString.m +++ src/OFString.m @@ -9,103 +9,76 @@ * the packaging of this file. */ #import #import + #import "OFString.h" +#import "OFConstCString.h" +#import "OFConstWideCString.h" +#import "OFCString.h" +#import "OFWideCString.h" #import "OFExceptions.h" @implementation OFString -+ new: (const char*)str -{ - return [[OFString alloc] init: str]; -} - -- init -{ - return [self init: NULL]; -} - -- init: (const char*)str -{ - if ((self = [super init])) { - if (str == NULL) { - length = 0; - string = NULL; - } else { - length = strlen(str); - string = [self getMem: length + 1]; - memcpy(string, str, length + 1); - } - } - return self; ++ newWithConstCString: (const char*)str +{ + return [[OFConstCString alloc] initWithConstCString: str]; +} + ++ newWithConstWideCString: (const wchar_t*)str +{ + return [[OFConstWideCString alloc] initWithConstWideCString: str]; +} + ++ newWithCString: (char*)str +{ + return [[OFCString alloc] initWithCString: str]; +} + ++ newWithWideCString: (wchar_t*)str +{ + return [[OFWideCString alloc] initWithWideCString: str]; } - (char*)cString { - return string; + @throw [OFNotImplementedException new: self withMethod: "cString"]; + return NULL; +} + +- (wchar_t*)wcString +{ + @throw [OFNotImplementedException new: self withMethod: "wcString"]; + return NULL; } - (size_t)length { return length; } -- (OFString*)setTo: (OFConstString*)str -{ - char *newstr; - size_t newlen; - - if ([str cString] == NULL) { - [self freeMem: string]; - - length = 0; - string = NULL; - - return self; - } - - newlen = [str length]; - newstr = [self getMem: newlen + 1]; - memcpy(newstr, [str cString], newlen + 1); - - if (string != NULL) - [self freeMem: string]; - - length = newlen; - string = newstr; - +- (OFString*)setTo: (OFString*)str +{ + [self free]; + self = [str clone]; return self; } - (OFString*)clone { - return [OFString new: string]; -} - -- (OFString*)append: (OFConstString*)str -{ - char *newstr; - size_t newlen, strlength; - - if (str == NULL) - return [self setTo: str]; - - strlength = [str length]; - newlen = length + strlength; - - newstr = [self resizeMem: string - toSize: newlen + 1]; - - memcpy(newstr + length, [str cString], strlength + 1); - - length = newlen; - string = newstr; - - return self; -} - -- (int)compare: (OFConstString*)str -{ - return strcmp(string, [str cString]); + @throw [OFNotImplementedException new: self withMethod: "clone"]; + return nil; +} + +- (int)compare: (OFString*)str +{ + @throw [OFNotImplementedException new: self withMethod: "compare:"]; + return 0; +} + +- (OFString*)append: (OFString*)str +{ + @throw [OFNotImplementedException new: self withMethod: "append:"]; + return nil; } @end ADDED src/OFWideCString.h Index: src/OFWideCString.h ================================================================== --- src/OFWideCString.h +++ src/OFWideCString.h @@ -0,0 +1,27 @@ +/* + * 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 "OFString.h" + +@interface OFWideCString: OFString +{ + wchar_t *string; +} + +- initWithWideCString: (wchar_t*)str; +- (wchar_t*)wcString; +- (OFString*)clone; +- (int)compare: (OFString*)str; +- (OFString*)append: (OFString*)str; +@end ADDED src/OFWideCString.m Index: src/OFWideCString.m ================================================================== --- src/OFWideCString.m +++ src/OFWideCString.m @@ -0,0 +1,70 @@ +/* + * 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 "OFWideCString.h" +#import "OFExceptions.h" + +@implementation OFWideCString +- initWithWideCString: (wchar_t*)str +{ + if ((self = [super init])) { + if (str == NULL) { + length = 0; + string = NULL; + } else { + length = wcslen(str); + string = [self getMem: (length + 1) * sizeof(wchar_t)]; + wmemcpy(string, str, length + 1); + } + } + return self; +} + +- (wchar_t*)wcString +{ + return string; +} + +- (OFString*)clone +{ + return [OFString newWithWideCString: string]; +} + +- (int)compare: (OFString*)str +{ + return wcscmp(string, [str wcString]); +} + +- (OFString*)append: (OFString*)str +{ + wchar_t *newstr; + size_t newlen, strlength; + + if ([str wcString] == NULL) + return [self setTo: str]; + + strlength = [str length]; + newlen = length + strlength; + + newstr = [self resizeMem: string + toSize: (newlen + 1) * sizeof(wchar_t)]; + + wmemcpy(newstr + length, [str wcString], strlength + 1); + + length = newlen; + string = newstr; + + return self; +} +@end DELETED src/OFWideString.h Index: src/OFWideString.h ================================================================== --- src/OFWideString.h +++ src/OFWideString.h @@ -1,33 +0,0 @@ -/* - * 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" -#import "OFConstWideString.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: (OFConstWideString*)wstr; -- (OFWideString*)clone; -- (OFWideString*)append: (OFConstWideString*)wstr; -- (int)compare: (OFConstWideString*)str; -@end DELETED src/OFWideString.m Index: src/OFWideString.m ================================================================== --- src/OFWideString.m +++ src/OFWideString.m @@ -1,112 +0,0 @@ -/* - * 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" -#import "OFExceptions.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); - wstring = [self getMem: (length + 1) * sizeof(wchar_t)]; - wmemcpy(wstring, wstr, length + 1); - } - } - return self; -} - -- (wchar_t*)wcString -{ - return wstring; -} - -- (size_t)length -{ - return length; -} - -- (OFWideString*)setTo: (OFConstWideString*)wstr -{ - wchar_t *newstr; - size_t newlen; - - if ([wstr wcString] == NULL) { - [self freeMem:wstring]; - - length = 0; - wstring = NULL; - - return self; - } - - newlen = [wstr length]; - newstr = [self getMem: (newlen + 1) * sizeof(wchar_t)]; - wmemcpy(newstr, [wstr wcString], newlen + 1); - - if (wstring != NULL) - [self freeMem: wstring]; - - length = newlen; - wstring = newstr; - - return self; -} - -- (OFWideString*)clone -{ - return [OFWideString new: wstring]; -} - -- (OFWideString*)append: (OFConstWideString*)wstr -{ - wchar_t *newstr; - size_t newlen, strlength; - - if ([wstr wcString] == NULL) - return [self setTo: wstr]; - - strlength = [wstr length]; - newlen = length + strlength; - - newstr = [self resizeMem: wstring - toSize: (newlen + 1) * sizeof(wchar_t)]; - - wmemcpy(newstr + length, [wstr wcString], strlength + 1); - - length = newlen; - wstring = newstr; - - return self; -} - -- (int)compare: (OFConstWideString*)str -{ - return wcscmp(wstring, [str wcString]); -} -@end Index: tests/OFList/OFList.m ================================================================== --- tests/OFList/OFList.m +++ tests/OFList/OFList.m @@ -30,13 +30,13 @@ OFList *list; OFListObject *iter; list = [OFList new]; - [list addNew: [OFString new: strings[0]]]; - [list addNew: [OFString new: strings[1]]]; - [list addNew: [OFString new: strings[2]]]; + [list addNew: [OFString newWithConstCString: strings[0]]]; + [list addNew: [OFString newWithConstCString: strings[1]]]; + [list addNew: [OFString newWithConstCString: strings[2]]]; for (iter = [list first], i = 0; iter != nil; iter = [iter next], i++) if (!strcmp([(OFString*)[iter data] cString], strings[i])) printf("Element %zu is expected element. GOOD!\n", i); else { Index: tests/OFString/OFString.m ================================================================== --- tests/OFString/OFString.m +++ tests/OFString/OFString.m @@ -17,35 +17,35 @@ /* TODO: Do real checks */ int main() { - OFString *s1 = [OFString new: "test"]; - OFString *s2 = [[OFString alloc] init: ""]; + OFString *s1 = [OFString newWithCString: "test"]; + OFString *s2 = [OFString newWithCString: ""]; OFString *s3; - OFString *s4 = [OFString new]; + OFString *s4 = [OFString newWithConstCString: NULL]; - [s2 append: [OFConstString new: "123"]]; + [s2 append: [OFString newWithConstCString: "123"]]; s3 = [s1 clone]; - [s4 setTo: (OFConstString*)s2]; + [s4 setTo: s2]; - if (![s1 compare: (OFConstString*)s3]) + if (![s1 compare: s3]) puts("s1 and s3 match! GOOD!"); else { puts("s1 and s3 don't match!"); return 1; } - if (![s2 compare: (OFConstString*)s4]) + if (![s2 compare: s4]) puts("s2 and s4 match! GOOD!"); else { puts("s1 and s3 don't match!"); return 1; } - if (!strcmp([[s1 append: (OFConstString*)s2] cString], "test123")) + if (!strcmp([[s1 append: s2] cString], "test123")) puts("s1 appended with s2 is the expected string! GOOD!"); else { puts("s1 appended with s2 is not the expected string!"); return 1; } Index: tests/OFWideString/OFWideString.m ================================================================== --- tests/OFWideString/OFWideString.m +++ tests/OFWideString/OFWideString.m @@ -9,42 +9,42 @@ * the packaging of this file. */ #import -#import "OFWideString.h" +#import "OFString.h" /* TODO: Do real checks */ int main() { - OFWideString *s1 = [OFWideString new: L"test"]; - OFWideString *s2 = [[OFWideString alloc] init: L""]; - OFWideString *s3; - OFWideString *s4 = [OFWideString new]; + OFString *s1 = [OFString newWithWideCString: L"test"]; + OFString *s2 = [OFString newWithWideCString: L""]; + OFString *s3; + OFString *s4 = [OFString newWithConstWideCString: NULL]; - [s2 append: [OFConstWideString new: L"123"]]; + [s2 append: [OFString newWithConstWideCString: L"123"]]; s3 = [s1 clone]; - [s4 setTo: (OFConstWideString*)s2]; + [s4 setTo: s2]; - if (![s1 compare: (OFConstWideString*)s3]) + if (![s1 compare: s3]) puts("s1 and s3 match! GOOD!"); else { puts("s1 and s3 don't match!"); return 1; } - if (![s2 compare: (OFConstWideString*)s4]) + if (![s2 compare: s4]) puts("s2 and s4 match! GOOD!"); else { puts("s1 and s3 don't match!"); return 1; } - if (!wcscmp([[s1 append: (OFConstWideString*)s2] wcString], L"test123")) + if (!wcscmp([[s1 append: s2] wcString], L"test123")) puts("s1 appended with s2 is the expected string! GOOD!"); else { puts("s1 appended with s2 is not the expected string!"); return 1; }