Index: src/Makefile ================================================================== --- src/Makefile +++ src/Makefile @@ -4,10 +4,11 @@ SRCS = OFConstCString.m \ OFCString.m \ OFConstWideCString.m \ OFExceptions.m \ + OFFile.m \ OFList.m \ OFListObject.m \ OFObject.m \ OFString.m \ OFWideCString.m @@ -14,10 +15,11 @@ INCLUDES = OFConstString.h \ OFCString.h \ OFConstWideString.h \ OFExceptions.h \ + OFFile.h \ OFList.h \ OFListObject.h \ OFObject.h \ OFString.h \ OFWideString.h Index: src/OFExceptions.h ================================================================== --- src/OFExceptions.h +++ src/OFExceptions.h @@ -11,29 +11,43 @@ #import #import "OFObject.h" @interface OFException: OFObject -+ new: (id)obj; -- init: (id)obj; ++ newWithObject: (id)obj; +- initWithObject: (id)obj; @end @interface OFNoMemException: OFException -+ new: (id)obj - withSize: (size_t)size; -- init: (id)obj - withSize: (size_t)size; ++ newWithObject: (id)obj + andSize: (size_t)size; +- initWithObject: (id)obj + andSize: (size_t)size; @end @interface OFNotImplementedException: OFException -+ new: (id)obj - withMethod: (const char*)method; -- init: (id)obj - withMethod: (const char*)method; ++ newWithObject: (id)obj + andMethod: (const char*)method; +- initWithObject: (id)obj + andMethod: (const char*)method; @end @interface OFMemNotPartOfObjException: OFException -+ new: (id)obj - withPtr: (void*)ptr; -- init: (id)obj - withPtr: (void*)ptr; ++ newWithObject: (id)obj + andPointer: (void*)ptr; +- initWithObject: (id)obj + andPointer: (void*)ptr; +@end + +@interface OFOverflowException: OFException ++ newWithObject: (id)obj; +- initWithObject: (id)obj; +@end + +@interface OFReadFailedException: OFException ++ newWithObject: (id)obj + andSize: (size_t)size + andNItems: (size_t)nitems; +- initWithObject: (id)obj + andSize: (size_t)size + andNItems: (size_t)nitems; @end Index: src/OFExceptions.m ================================================================== --- src/OFExceptions.m +++ src/OFExceptions.m @@ -11,70 +11,113 @@ #import #import "OFExceptions.h" @implementation OFException -+ new: (id)obj ++ newWithObject: (id)obj { - return [[OFException alloc] init: obj]; + return [[OFException alloc] initWithObject: obj]; } -- init: (id)obj +- initWithObject: (id)obj { + @throw self; return [super init]; } @end @implementation OFNoMemException -+ new: (id)obj - withSize: (size_t)size ++ newWithObject: (id)obj + andSize: (size_t)size { - return [[OFNoMemException alloc] init: obj - withSize: size]; + return [[OFNoMemException alloc] initWithObject: obj + andSize: size]; } -- init: (id)obj - withSize: (size_t)size +- initWithObject: (id)obj + andSize: (size_t)size { fprintf(stderr, "ERROR: Could not allocate %zu bytes for object %s!\n", size, [obj name]); + + @throw self; return [super init]; } @end @implementation OFNotImplementedException -+ new: (id)obj - withMethod: (const char*)method ++ newWithObject: (id)obj + andMethod: (const char*)method { - return [[OFNotImplementedException alloc] init: obj - withMethod: method]; + return [[OFNotImplementedException alloc] initWithObject: obj + andMethod: method]; } -- init: (id)obj - withMethod: (const char*)method +- initWithObject: (id)obj + andMethod: (const char*)method { fprintf(stderr, "ERROR: Requested method %s not implemented in %s!\n", method, [obj name]); + + @throw self; return [super init]; } @end @implementation OFMemNotPartOfObjException -+ new: (id)obj - withPtr: (void*)ptr ++ newWithObject: (id)obj + andPointer: (void*)ptr { - return [[OFMemNotPartOfObjException alloc] init: obj - withPtr: ptr]; + return [[OFMemNotPartOfObjException alloc] initWithObject: obj + andPointer: ptr]; } -- init: (id)obj - withPtr: (void*)ptr +- initWithObject: (id)obj + andPointer: (void*)ptr { fprintf(stderr, "ERROR: Memory at %p was not allocated as part of " "object %s!\n" "ERROR: -> Not changing memory allocation!\n" "ERROR: (Hint: It is possible that you tried to free the same " "memory twice!)\n", ptr, [obj name]); + @throw self; + return [super init]; +} +@end + +@implementation OFOverflowException ++ newWithObject: (id)obj +{ + return [[OFOverflowException alloc] initWithObject: obj]; +} + +- initWithObject: (id)obj +{ + fprintf(stderr, "ERROR: Overflow in object %s!\n", [obj name]); + + @throw self; + return [super init]; +} +@end + +@implementation OFReadFailedException ++ newWithObject: (id)obj + andSize: (size_t)size + andNItems: (size_t)nitems +{ + return [[OFReadFailedException alloc] initWithObject: obj + andSize: size + andNItems: nitems]; +} + +- initWithObject: (id)obj + andSize: (size_t)size + andNItems: (size_t)nitems +{ + fprintf(stderr, "ERROR: Failed to read %zu items of size %zu in " + "object %s!\n", nitems, size, [obj name]); + + @throw self; return [super init]; } @end ADDED src/OFFile.h Index: src/OFFile.h ================================================================== --- src/OFFile.h +++ src/OFFile.h @@ -0,0 +1,28 @@ +/* + * 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 OFFile: OFObject +{ + FILE *fp; +} + ++ newWithPath: (const char*)path + andMode: (const char*)mode; +- initWithPath: (const char*)path + andMode: (const char*)mode; +- free; +- (char*)readWithSize: (size_t)size + andNItems: (size_t)nitems; +@end ADDED src/OFFile.m Index: src/OFFile.m ================================================================== --- src/OFFile.m +++ src/OFFile.m @@ -0,0 +1,66 @@ +/* + * 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 "OFFile.h" +#import "OFExceptions.h" + +@implementation OFFile ++ newWithPath: (const char*)path + andMode: (const char*)mode +{ + return [[OFFile alloc] initWithPath: path + andMode: mode]; +} + +- initWithPath: (const char*)path + andMode: (const char*)mode +{ + if ((self = [super init])) { + if ((fp = fopen(path, mode)) == NULL) { + [self free]; + return nil; + } + } + return self; +} + +- free +{ + fclose(fp); + return [super free]; +} + +- (char*)readWithSize: (size_t)size + andNItems: (size_t)nitems +{ + uint64_t memsize; + char *ret; + + if ((memsize = (uint64_t)nitems * size) > 0xFFFFFFFF) { + [OFOverflowException newWithObject: self]; + return NULL; + } + + ret = [self getMem: (size_t)memsize]; + + if (fread(ret, size, nitems, fp) <= 0 && !feof(fp)) { + [self freeMem: ret]; + [OFReadFailedException newWithObject: self + andSize: size + andNItems: nitems]; + return NULL; + } + + return ret; +} +@end Index: src/OFObject.m ================================================================== --- src/OFObject.m +++ src/OFObject.m @@ -25,20 +25,20 @@ - (void*)getMem: (size_t)size { struct __ofobject_allocated_mem *iter; if ((iter = malloc(sizeof(struct __ofobject_allocated_mem))) == NULL) { - @throw [OFNoMemException new: self - withSize: sizeof( - struct __ofobject_allocated_mem)]; + [OFNoMemException newWithObject: self + andSize: sizeof(struct + __ofobject_allocated_mem)]; return NULL; } if ((iter->ptr = malloc(size)) == NULL) { free(iter); - @throw [OFNoMemException new: self - withSize: size]; + [OFNoMemException newWithObject: self + andSize: size]; return NULL; } iter->next = NULL; iter->prev = __mem_pool; @@ -57,22 +57,22 @@ struct __ofobject_allocated_mem *iter; for (iter = __mem_pool; iter != NULL; iter = iter->prev) { if (iter->ptr == ptr) { if ((ptr = realloc(iter->ptr, size)) == NULL) { - @throw [OFNoMemException new: self - withSize: size]; + [OFNoMemException newWithObject: self + andSize: size]; return NULL; } iter->ptr = ptr; return ptr; } } - @throw [OFMemNotPartOfObjException new: self - withPtr: ptr]; + [OFMemNotPartOfObjException newWithObject: self + andPointer: ptr]; return NULL; } - (void)freeMem: (void*)ptr; { @@ -92,12 +92,12 @@ return; } } - @throw [OFMemNotPartOfObjException new: self - withPtr: ptr]; + [OFMemNotPartOfObjException newWithObject: self + andPointer: ptr]; } - free { struct __ofobject_allocated_mem *iter, *iter2; Index: src/OFString.m ================================================================== --- src/OFString.m +++ src/OFString.m @@ -40,17 +40,19 @@ return [[OFWideCString alloc] initWithWideCString: str]; } - (char*)cString { - @throw [OFNotImplementedException new: self withMethod: "cString"]; + [OFNotImplementedException newWithObject: self + andMethod: "cString"]; return NULL; } - (wchar_t*)wcString { - @throw [OFNotImplementedException new: self withMethod: "wcString"]; + [OFNotImplementedException newWithObject: self + andMethod: "wcString"]; return NULL; } - (size_t)length { @@ -64,21 +66,24 @@ return self; } - (OFString*)clone { - @throw [OFNotImplementedException new: self withMethod: "clone"]; + [OFNotImplementedException newWithObject: self + andMethod: "clone"]; return nil; } - (int)compare: (OFString*)str { - @throw [OFNotImplementedException new: self withMethod: "compare:"]; + [OFNotImplementedException newWithObject: self + andMethod: "compare:"]; return 0; } - (OFString*)append: (OFString*)str { - @throw [OFNotImplementedException new: self withMethod: "append:"]; + [OFNotImplementedException newWithObject: self + andMethod: "append:"]; return nil; } @end