Index: src/OFExceptions.h ================================================================== --- src/OFExceptions.h +++ src/OFExceptions.h @@ -14,67 +14,101 @@ // FIXME: Exceptions should include which type of error occoured (fopen etc.) @interface OFException: OFObject { - char *errstr; + id object; + char *string; } + newWithObject: (id)obj; - initWithObject: (id)obj; - free; - raise; -- (char*)string; +- (char*)cString; @end @interface OFNoMemException: OFException +{ + size_t req_size; +} + + newWithObject: (id)obj andSize: (size_t)size; - initWithObject: (id)obj andSize: (size_t)size; +- (char*)cString; +- (size_t)requestedSize; @end @interface OFNotImplementedException: OFException +{ + SEL selector; +} + + newWithObject: (id)obj andSelector: (SEL)sel; - initWithObject: (id)obj andSelector: (SEL)sel; +- (char*)cString; +- (SEL)selector; @end @interface OFMemNotPartOfObjException: OFException +{ + void *pointer; +} + + newWithObject: (id)obj andPointer: (void*)ptr; - initWithObject: (id)obj andPointer: (void*)ptr; +- (char*)cString; +- (void*)pointer; @end @interface OFOverflowException: OFException + newWithObject: (id)obj; - initWithObject: (id)obj; @end @interface OFOpenFileFailedException: OFException +{ + char *path; + char *mode; +} + + newWithObject: (id)obj - andPath: (const char*)path - andMode: (const char*)mode; + andPath: (const char*)p + andMode: (const char*)m; - initWithObject: (id)obj - andPath: (const char*)path - andMode: (const char*)mode; + andPath: (const char*)p + andMode: (const char*)m; +- free; +- (char*)cString; +- (char*)path; +- (char*)mode; @end @interface OFReadOrWriteFailedException: OFException +{ + size_t req_size; + size_t req_items; +} + + newWithObject: (id)obj andSize: (size_t)size andNItems: (size_t)nitems; -@end - -@interface OFReadFailedException: OFReadOrWriteFailedException - initWithObject: (id)obj andSize: (size_t)size andNItems: (size_t)nitems; +- (size_t)requestedSize; +- (size_t)requestedItems; +@end + +@interface OFReadFailedException: OFReadOrWriteFailedException +- (char*)cString; @end @interface OFWriteFailedException: OFReadOrWriteFailedException -- initWithObject: (id)obj - andSize: (size_t)size - andNItems: (size_t)nitems; +- (char*)cString; @end Index: src/OFExceptions.m ================================================================== --- src/OFExceptions.m +++ src/OFExceptions.m @@ -34,20 +34,22 @@ return [[OFException alloc] initWithObject: obj]; } - initWithObject: (id)obj { - if ((self = [super init])) - errstr = NULL; + if ((self = [super init])) { + object = obj; + string = NULL; + } return self; } - free { - if (errstr != NULL) - free(errstr); + if (string != NULL) + free(string); return [super free]; } - raise @@ -54,13 +56,13 @@ { @throw self; return self; } -- (char*)string +- (char*)cString { - return errstr; + return string; } @end @implementation OFNoMemException + newWithObject: (id)obj @@ -71,21 +73,31 @@ } - initWithObject: (id)obj andSize: (size_t)size { - if ((self = [super init])) { - if (obj != nil) - asprintf(&errstr, "ERROR: Could not allocate %zu bytes " - "for object of class %s!\n", size, [obj name]); - else - asprintf(&errstr, "ERROR: Could not allocate %zu bytes " - "for object of class (null)!\n", size); - } + if ((self = [super initWithObject: obj])) + req_size = size; return self; } + +- (char*)cString +{ + if (string != NULL) + return string; + + asprintf(&string, "ERROR: Could not allocate %zu bytes for object of" + "class %s!\n", req_size, object != nil ? [object name] : "(null)"); + + return string; +} + +- (size_t)requestedSize +{ + return req_size; +} @end @implementation OFNotImplementedException + newWithObject: (id)obj andSelector: (SEL)sel @@ -95,16 +107,31 @@ } - initWithObject: (id)obj andSelector: (SEL)sel { - if ((self = [super init])) - asprintf(&errstr, "ERROR: Requested selector %s not " - "implemented in %s!\n", SEL_NAME(sel), [obj name]); + if ((self = [super initWithObject: obj])) + selector = sel; return self; } + +- (char*)cString +{ + if (string != NULL) + return string; + + asprintf(&string, "ERROR: Requested selector %s not implemented in " + "%s!\n", SEL_NAME(selector), [object name]); + + return string; +} + +- (SEL)selector +{ + return selector; +} @end @implementation OFMemNotPartOfObjException + newWithObject: (id)obj andPointer: (void*)ptr @@ -114,20 +141,35 @@ } - initWithObject: (id)obj andPointer: (void*)ptr { - if ((self = [super init])) - asprintf(&errstr, "ERROR: Memory at %p was not allocated as " - "part of object of class\n" - "ERROR: %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]); + if ((self = [super initWithObject: obj])) + pointer = ptr; return self; } + +- (char*)cString +{ + if (string != NULL) + return string; + + asprintf(&string, "ERROR: Memory at %p was not allocated as part of " + "object of class\n" + "ERROR: %s!\n" + "ERROR: -> Not changing memory allocation!\n" + "ERROR: (Hint: It is also possible that you tried to free the same " + "memory twice!)\n", pointer, [object name]); + + return string; +} + +- (void*)pointer +{ + return pointer; +} @end @implementation OFOverflowException + newWithObject: (id)obj { @@ -134,43 +176,77 @@ return [[OFOverflowException alloc] initWithObject: obj]; } - initWithObject: (id)obj { - if ((self = [super init])) { - if (obj != nil) - asprintf(&errstr, "ERROR: Overflow in object of class " - "%s!\n", [obj name]); - else - errstr = strdup("ERROR: Overflow in object of class " - "(null)!\n"); - } - - return self; + return (self = [super initWithObject: obj]); +} + +- (char*)cString +{ + if (string != NULL) + return string; + + asprintf(&string, "ERROR: Overflow in object of class %s!\n", + object != nil ? [object name] : "(null)"); + + return string; } @end @implementation OFOpenFileFailedException + newWithObject: (id)obj - andPath: (const char*)path - andMode: (const char*)mode + andPath: (const char*)p + andMode: (const char*)m { return [[OFOpenFileFailedException alloc] initWithObject: obj - andPath: path - andMode: mode]; + andPath: p + andMode: m]; } - initWithObject: (id)obj - andPath: (const char*)path - andMode: (const char*)mode + andPath: (const char*)p + andMode: (const char*)m { - if ((self = [super init])) - asprintf(&errstr, "ERROR: Failed to open file %s with mode %s " - "in object of class %s!\n", path, mode, [self name]); + if ((self = [super init])) { + path = p != NULL ? strdup(p) : NULL; + mode = m != NULL ? strdup(m) : NULL; + } return self; } + +- free +{ + if (path != NULL) + free(path); + if (mode != NULL) + free(mode); + + return [super free]; +} + +- (char*)cString +{ + if (string != NULL) + return string; + + asprintf(&string, "ERROR: Failed to open file %s with mode %s " + "in object of class %s!\n", path, mode, [self name]); + + return string; +} + +- (char*)path +{ + return path; +} + +- (char*)mode +{ + return mode; +} @end @implementation OFReadOrWriteFailedException + newWithObject: (id)obj andSize: (size_t)size @@ -178,32 +254,54 @@ { return [[OFReadOrWriteFailedException alloc] initWithObject: obj andSize: size andNItems: nitems]; } -@end -@implementation OFReadFailedException - initWithObject: (id)obj andSize: (size_t)size andNItems: (size_t)nitems { - if ((self = [super init])) - asprintf(&errstr, "ERROR: Failed to read %zu items of size " - "%zu in object of class %s!\n", nitems, size, [obj name]); + if ((self = [super init])) { + req_size = size; + req_items = nitems; + } return self; } + +- (size_t)requestedSize +{ + return req_size; +} + +- (size_t)requestedItems +{ + return req_items; +} +@end + +@implementation OFReadFailedException +- (char*)cString +{ + if (string != NULL) + return string;; + + asprintf(&string, "ERROR: Failed to read %zu items of size %zu in " + "object of class %s!\n", req_items, req_size, [object name]); + + return string; +} @end @implementation OFWriteFailedException -- initWithObject: (id)obj - andSize: (size_t)size - andNItems: (size_t)nitems -{ - if ((self = [super init])) - asprintf(&errstr, "ERROR: Failed to write %zu items of size " - "%zu in object of class %s!\n", nitems, size, [obj name]); - - return self; +- (char*)cString +{ + if (string != NULL) + return string; + + asprintf(&string, "ERROR: Failed to write %zu items of size %zu in " + "object of class %s!\n", req_items, req_size, [object name]); + + return string; } @end Index: tests/OFObject/OFObject.m ================================================================== --- tests/OFObject/OFObject.m +++ tests/OFObject/OFObject.m @@ -21,11 +21,11 @@ @try { \ code; \ } @catch (exception *e) { \ caught = true; \ puts("CAUGHT! Error string was:"); \ - fputs([e string], stdout); \ + fputs([e cString], stdout); \ puts("Resuming..."); \ } \ if (!caught) { \ puts("NOT CAUGHT!"); \ return 1; \