@@ -8,10 +8,12 @@ * Q Public License 1.0, which can be found in the file LICENSE included in * the packaging of this file. */ #import +#import + #import "OFExceptions.h" @implementation OFException + newWithObject: (id)obj { @@ -18,13 +20,32 @@ return [[OFException alloc] initWithObject: obj]; } - initWithObject: (id)obj { - self = [super init]; + if ((self = [super init])) + errstr = NULL; + + return self; +} + +- free +{ + if (errstr != NULL) + free(errstr); + + return [super free]; +} + +- (void)raise +{ @throw self; - return self; +} + +- (char*)string +{ + return errstr; } @end @implementation OFNoMemException + newWithObject: (id)obj @@ -35,35 +56,34 @@ } - initWithObject: (id)obj andSize: (size_t)size { - fprintf(stderr, "ERROR: Could not allocate %zu bytes for object %s!\n", - size, [obj name]); + if ((self = [super init])) + asprintf(&errstr, "ERROR: Could not allocate %zu bytes for " + "object of class %s!\n", size, [obj name]); - self = [super init]; - @throw self; return self; } @end @implementation OFNotImplementedException + newWithObject: (id)obj - andMethod: (const char*)method + andSelector: (SEL)sel { return [[OFNotImplementedException alloc] initWithObject: obj - andMethod: method]; + andSelector: sel]; } - initWithObject: (id)obj - andMethod: (const char*)method + andSelector: (SEL)sel { - fprintf(stderr, "ERROR: Requested method %s not implemented in %s!\n", - method, [obj name]); + if ((self = [super init])) + /* FIXME: Is casting SEL to char* portable? */ + asprintf(&errstr, "ERROR: Requested selector %s not " + "implemented in %s!\n", (char*)sel, [obj name]); - self = [super init]; - @throw self; return self; } @end @implementation OFMemNotPartOfObjException @@ -75,18 +95,18 @@ } - 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]); - - self = [super init]; - @throw self; + 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]); + return self; } @end @implementation OFOverflowException @@ -95,14 +115,14 @@ return [[OFOverflowException alloc] initWithObject: obj]; } - initWithObject: (id)obj { - fprintf(stderr, "ERROR: Overflow in object %s!\n", [obj name]); + if ((self = [super init])) + asprintf(&errstr, "ERROR: Overflow in object of class %s!\n", + [obj name]); - self = [super init]; - @throw self; return self; } @end @implementation OFOpenFileFailedException @@ -117,15 +137,14 @@ - initWithObject: (id)obj andPath: (const char*)path andMode: (const char*)mode { - fprintf(stderr, "ERROR: Failed to open file %s with mode %s in " - "object %s!\n", path, mode, [self name]); + 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]); - self = [super init]; - @throw self; return self; } @end @implementation OFReadOrWriteFailedException @@ -142,27 +161,25 @@ @implementation OFReadFailedException - 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]); + 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]); - self = [super init]; - @throw self; return self; } @end @implementation OFWriteFailedException - initWithObject: (id)obj andSize: (size_t)size andNItems: (size_t)nitems { - fprintf(stderr, "ERROR: Failed to write %zu items of size %zu in " - "object %s!\n", nitems, size, [obj name]); + 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]); - self = [super init]; - @throw self; return self; } @end