Index: src/OFArray.m ================================================================== --- src/OFArray.m +++ src/OFArray.m @@ -56,11 +56,11 @@ } - (void*)item: (size_t)item { if (item >= items) - @throw [OFOutOfRangeException newWithObject: self]; + @throw [OFOutOfRangeException newWithClass: [self class]]; return data + item * itemsize; } - (void*)last @@ -69,11 +69,11 @@ } - add: (void*)item { if (SIZE_MAX - items < 1) - @throw [OFOutOfRangeException newWithObject: self]; + @throw [OFOutOfRangeException newWithClass: [self class]]; data = [self resizeMem: data toNItems: items + 1 ofSize: itemsize]; @@ -84,11 +84,11 @@ - addNItems: (size_t)nitems fromCArray: (void*)carray { if (nitems > SIZE_MAX - items) - @throw [OFOutOfRangeException newWithObject: self]; + @throw [OFOutOfRangeException newWithClass: [self class]]; data = [self resizeMem: data toNItems: items + nitems ofSize: itemsize]; @@ -99,11 +99,11 @@ } - removeNItems: (size_t)nitems { if (nitems > items) - @throw [OFOutOfRangeException newWithObject: self]; + @throw [OFOutOfRangeException newWithClass: [self class]]; data = [self resizeMem: data toNItems: items - nitems ofSize: itemsize]; @@ -128,11 +128,11 @@ - add: (void*)item { size_t nsize; if (SIZE_MAX - items < 1 || items + 1 > SIZE_MAX / itemsize) - @throw [OFOutOfRangeException newWithObject: self]; + @throw [OFOutOfRangeException newWithClass: [self class]]; nsize = ((items + 1) * itemsize + lastpagebyte) & ~lastpagebyte; if (size != nsize) data = [self resizeMem: data @@ -148,11 +148,11 @@ fromCArray: (void*)carray { size_t nsize; if (nitems > SIZE_MAX - items || items + nitems > SIZE_MAX / itemsize) - @throw [OFOutOfRangeException newWithObject: self]; + @throw [OFOutOfRangeException newWithClass: [self class]]; nsize = ((items + nitems) * itemsize + lastpagebyte) & ~lastpagebyte; if (size != nsize) data = [self resizeMem: data @@ -168,11 +168,11 @@ - removeNItems: (size_t)nitems { size_t nsize; if (nitems > items) - @throw [OFOutOfRangeException newWithObject: self]; + @throw [OFOutOfRangeException newWithClass: [self class]]; nsize = ((items - nitems) * itemsize + lastpagebyte) & ~lastpagebyte; if (size != nsize) data = [self resizeMem: data Index: src/OFExceptions.h ================================================================== --- src/OFExceptions.h +++ src/OFExceptions.h @@ -14,36 +14,34 @@ /** * The OFException class is the base class for all exceptions in ObjFW. */ @interface OFException: OFObject { - id object; - char *string; + Class class; + char *string; } /** * Creates a new exception. * - * \param obj The object which caused the exception + * \param class The class of the object which caused the exception * \return A new exception */ -+ newWithObject: (id)obj; ++ newWithClass: (Class)class; /** * Initializes an already allocated OFException. * - * \param obj The object which caused the exception + * \param class The class of the object which caused the exception * \return An initialized OFException */ -- initWithObject: (id)obj; - -- free; +- initWithClass: (Class)class; /** - * \return The object that caused the exception + * \return The class of the object which caused the exception */ -- (id)object; +- (Class)class; /** * \return An error message for the exception as a C string */ - (const char*)cString; @@ -56,31 +54,26 @@ { size_t req_size; } /** - * \param obj The object which caused the exception + * \param class The class of the object which caused the exception * \param size The size of the memory that couldn't be allocated * \return A new no memory exception */ -+ newWithObject: (id)obj - andSize: (size_t)size; ++ newWithClass: (Class)class + andSize: (size_t)size; /** * Initializes an already allocated no memory exception. * - * \param obj The object which caused the exception + * \param class The class of the object which caused the exception * \param size The size of the memory that couldn't be allocated * \return An initialized no memory exception */ -- initWithObject: (id)obj - andSize: (size_t)size; - -/** - * \return An error message for the exception as a C string - */ -- (const char*)cString; +- initWithClass: (Class)class + andSize: (size_t)size; /** * \return The size of the memoory that couldn't be allocated */ - (size_t)requestedSize; @@ -93,31 +86,26 @@ { void *pointer; } /** - * \param obj The object which caused the exception + * \param class The class of the object which caused the exception * \param ptr A pointer to the memory that is not part of the object * \return A new memory not part of object exception */ -+ newWithObject: (id)obj - andPointer: (void*)ptr; ++ newWithClass: (Class)class + andPointer: (void*)ptr; /** * Initializes an already allocated memory not part of object exception. * - * \param obj The object which caused the exception + * \param class The class of the object which caused the exception * \param ptr A pointer to the memory that is not part of the object * \return An initialized memory not part of object exception */ -- initWithObject: (id)obj - andPointer: (void*)ptr; - -/** - * \return An error message for the exception as a C string - */ -- (const char*)cString; +- initWithClass: (Class)class + andPointer: (void*)ptr; /** * \return A pointer to the memory which is not part of the object */ - (void*)pointer; @@ -125,69 +113,28 @@ /** * An OFException indicating the given value is out of range. */ @interface OFOutOfRangeException: OFException {} -/** - * \return An error message for the exception as a C string - */ -- (const char*)cString; @end /** * An OFException indicating that the encoding is invalid for this object. */ @interface OFInvalidEncodingException: OFException {} -/** - * \return An error message for the exception as a C string - */ -- (const char*)cString; @end /** * An OFException indicating that the format is invalid. */ @interface OFInvalidFormatException: OFException {} -/** - * \return An error message for the exception as a C string - */ -- (const char*)cString; @end /** * An OFException indicating that initializing something failed. */ @interface OFInitializationFailedException: OFException -{ - Class class; -} - -/** - * Creates a new exception. - * - * \param class The class which caused the exception - * \return A new exception - */ -+ newWithClass: (Class)class; - -/** - * Initializes an already allocated OFException. - * - * \param obj The object which caused the exception - * \return An initialized OFException - */ -- initWithClass: (Class)class; - -/** - * \return An error message for the exception as a C string - */ -- (const char*)cString; - -/** - * \return The class which caused the exception - */ -- (Class)class; @end /** * An OFException indicating the file couldn't be opened. */ @@ -197,37 +144,30 @@ char *mode; int err; } /** - * \param obj The object which caused the exception + * \param class The class of the object which caused the exception * \param path A C string of the path to the file tried to open * \param mode A C string of the mode in which the file should have been opened * \return A new open file failed exception */ -+ newWithObject: (id)obj - andPath: (const char*)path - andMode: (const char*)mode; ++ newWithClass: (Class)class + andPath: (const char*)path + andMode: (const char*)mode; /** * Initializes an already allocated open file failed exception. * - * \param obj The object which caused the exception + * \param class The class of the object which caused the exception * \param path A C string of the path to the file which couldn't be opened * \param mode A C string of the mode in which the file should have been opened * \return An initialized open file failed exception */ -- initWithObject: (id)obj - andPath: (const char*)path - andMode: (const char*)mode; - -- free; - -/** - * \return An error message for the exception as a C string - */ -- (const char*)cString; +- initWithClass: (Class)class + andPath: (const char*)path + andMode: (const char*)mode; /** * \return The errno from when the exception was created */ - (int)errNo; @@ -253,48 +193,48 @@ BOOL has_items; int err; } /** - * \param obj The object which caused the exception - * \param size The requested size of the data that couldn't be read / written - * \param nitems The requested number of items that couldn't be read / written - * \return A new open file failed exception - */ -+ newWithObject: (id)obj - andSize: (size_t)size - andNItems: (size_t)nitems; - -/** - * \param obj The object which caused the exception - * \param size The requested size of the data that couldn't be read / written - * \return A new open file failed exception - */ -+ newWithObject: (id)obj - andSize: (size_t)size; - -/** - * Initializes an already allocated read or write failed exception. - * - * \param obj The object which caused the exception - * \param size The requested size of the data that couldn't be read / written - * \param nitems The requested number of items that couldn't be read / written - * \return A new open file failed exception - */ -- initWithObject: (id)obj - andSize: (size_t)size - andNItems: (size_t)nitems; - -/** - * Initializes an already allocated read or write failed exception. - * - * \param obj The object which caused the exception - * \param size The requested size of the data that couldn't be read / written - * \return A new open file failed exception - */ -- initWithObject: (id)obj - andSize: (size_t)size; + * \param class The class of the object which caused the exception + * \param size The requested size of the data that couldn't be read / written + * \param nitems The requested number of items that couldn't be read / written + * \return A new open file failed exception + */ ++ newWithClass: (Class)class + andSize: (size_t)size + andNItems: (size_t)nitems; + +/** + * \param class The class of the object which caused the exception + * \param size The requested size of the data that couldn't be read / written + * \return A new open file failed exception + */ ++ newWithClass: (Class)class + andSize: (size_t)size; + +/** + * Initializes an already allocated read or write failed exception. + * + * \param class The class of the object which caused the exception + * \param size The requested size of the data that couldn't be read / written + * \param nitems The requested number of items that couldn't be read / written + * \return A new open file failed exception + */ +- initWithClass: (Class)class + andSize: (size_t)size + andNItems: (size_t)nitems; + +/** + * Initializes an already allocated read or write failed exception. + * + * \param class The class of the object which caused the exception + * \param size The requested size of the data that couldn't be read / written + * \return A new open file failed exception + */ +- initWithClass: (Class)class + andSize: (size_t)size; /** * \return The errno from when the exception was created */ - (int)errNo; @@ -317,65 +257,41 @@ /** * An OFException indicating a read to the file failed. */ @interface OFReadFailedException: OFReadOrWriteFailedException {} -/** - * \return An error message for the exception as a C string - */ -- (const char*)cString; @end /** * An OFException indicating a write to the file failed. */ @interface OFWriteFailedException: OFReadOrWriteFailedException {} -/** - * \return An error message for the exception as a C string - */ -- (const char*)cString; @end /** * An OFException indicating that setting an option failed. */ @interface OFSetOptionFailedException: OFException {} -/*** - * \return An error message for the exception as a C string. - */ -- (const char*)cString; @end /** * An OFException indicating a socket is not connected or bound. */ @interface OFNotConnectedException: OFException {} -/** - * \return An error message for the exception as a C string. - */ -- (const char*)cString; @end /** * An OFException indicating an attempt to connect or bind an already connected * or bound socket. */ @interface OFAlreadyConnectedException: OFException {} -/** - * \return An error message for the exception as a C string. - */ -- (const char*)cString; @end /** * An OFException indicating that the specified port is invalid. */ @interface OFInvalidPortException: OFException {} -/** - * \return An error message for the exception as a C string. - */ -- (const char*)cString; @end /** * An OFException indicating the translation of an address failed. */ @@ -385,37 +301,30 @@ char *service; int err; } /** - * \param obj The object which caused the exception + * \param class The class of the object which caused the exception * \param node The node for which translation was requested * \param service The service of the node for which translation was requested * \return A new address translation failed exception */ -+ newWithObject: (id)obj - andNode: (const char*)node - andService: (const char*)service; ++ newWithClass: (Class)class + andNode: (const char*)node + andService: (const char*)service; /** * Initializes an already allocated address translation failed exception. * - * \param obj The object which caused the exception + * \param class The class of the object which caused the exception * \param node The node for which translation was requested * \param service The service of the node for which translation was requested * \return An initialized address translation failed exception */ -- initWithObject: (id)obj - andNode: (const char*)node - andService: (const char*)service; - -- free; - -/** - * \return An error message for the exception as a C string. - */ -- (const char*)cString; +- initWithClass: (Class)class + andNode: (const char*)node + andService: (const char*)service; /** * \return The errno from when the exception was created */ - (int)errNo; @@ -440,37 +349,30 @@ uint16_t port; int err; } /** - * \param obj The object which caused the exception + * \param class The class of the object which caused the exception * \param host The host to which the connection failed * \param port The port on the host to which the connection failed * \return A new connection failed exception */ -+ newWithObject: (id)obj - andHost: (const char*)host - andPort: (uint16_t)port; ++ newWithClass: (Class)class + andHost: (const char*)host + andPort: (uint16_t)port; /** * Initializes an already allocated connection failed exception. * - * \param obj The object which caused the exception + * \param class The class of the object which caused the exception * \param host The host to which the connection failed * \param port The port on the host to which the connection failed * \return An initialized connection failed exception */ -- initWithObject: (id)obj - andHost: (const char*)host - andPort: (uint16_t)port; - -- free; - -/** - * \return An error message for the exception as a C string. - */ -- (const char*)cString; +- initWithClass: (Class)class + andHost: (const char*)host + andPort: (uint16_t)port; /** * \return The errno from when the exception was created */ - (int)errNo; @@ -496,41 +398,34 @@ int family; int err; } /** - * \param obj The object which caused the exception + * \param class The class of the object which caused the exception * \param host The host on which binding failed * \param port The port on which binding failed * \param family The family for which binnding failed * \return A new bind failed exception */ -+ newWithObject: (id)obj - andHost: (const char*)host - andPort: (uint16_t)port - andFamily: (int)family; ++ newWithClass: (Class)class + andHost: (const char*)host + andPort: (uint16_t)port + andFamily: (int)family; /** * Initializes an already allocated bind failed exception. * - * \param obj The object which caused the exception + * \param class The class of the object which caused the exception * \param host The host on which binding failed * \param port The port on which binding failed * \param family The family for which binnding failed * \return An initialized bind failed exception */ -- initWithObject: (id)obj - andHost: (const char*)host - andPort: (uint16_t)port - andFamily: (int)family; - -- free; - -/** - * \return An error message for the exception as a C string. - */ -- (const char*)cString; +- initWithClass: (Class)class + andHost: (const char*)host + andPort: (uint16_t)port + andFamily: (int)family; /** * \return The errno from when the exception was created */ - (int)errNo; @@ -559,31 +454,26 @@ int backlog; int err; } /** - * \param obj The object which caused the exception + * \param class The class of the object which caused the exception * \param backlog The requested size of the back log * \return A new listen failed exception */ -+ newWithObject: (id)obj - andBackLog: (int)backlog; ++ newWithClass: (Class)class + andBackLog: (int)backlog; /** * Initializes an already allocated listen failed exception * - * \param obj The object which caused the exception + * \param class The class of the object which caused the exception * \param backlog The requested size of the back log * \return An initialized listen failed exception */ -- initWithObject: (id)obj - andBackLog: (int)backlog; - -/** - * \return An error message for the exception as a C string. - */ -- (const char*)cString; +- initWithClass: (Class)class + andBackLog: (int)backlog; /** * \return The errno from when the exception was created */ - (int)errNo; @@ -600,23 +490,10 @@ @interface OFAcceptFailedException: OFException { int err; } -/** - * Initializes an already allocated accept failed exception. - * - * \param obj The object which caused the exception - * \return An initialized accept failed exception - */ -- initWithObject: (id)obj; - -/** - * \return An error message for the exception as a C string. - */ -- (const char*)cString; - /** * \return The errno from when the exception was created */ - (int)errNo; @end Index: src/OFExceptions.m ================================================================== --- src/OFExceptions.m +++ src/OFExceptions.m @@ -35,19 +35,19 @@ #ifndef HAVE_ASPRINTF #import "asprintf.h" #endif @implementation OFException -+ newWithObject: (id)obj ++ newWithClass: (Class)class_ { - return [[self alloc] initWithObject: obj]; + return [[self alloc] initWithClass: class_]; } -- initWithObject: (id)obj +- initWithClass: (Class)class_ { if ((self = [super init])) { - object = obj; + class = class_; string = NULL; } return self; } @@ -58,33 +58,33 @@ free(string); return [super free]; } -- (id)object +- (Class)class { - return object; + return class; } - (const char*)cString { return string; } @end @implementation OFNoMemException -+ newWithObject: (id)obj ++ newWithClass: (Class)class_ + andSize: (size_t)size +{ + return [[self alloc] initWithClass: class_ + andSize: size]; +} + +- initWithClass: (Class)class_ andSize: (size_t)size { - return [[self alloc] initWithObject: obj - andSize: size]; -} - -- initWithObject: (id)obj - andSize: (size_t)size -{ - if ((self = [super initWithObject: obj])) + if ((self = [super initWithClass: class_])) req_size = size; return self; } @@ -91,12 +91,12 @@ - (const char*)cString { if (string != NULL) return string; - asprintf(&string, "Could not allocate %zu bytes for object of class " - "%s!", req_size, object != nil ? [object name] : "(null)"); + asprintf(&string, "Could not allocate %zu bytes in class %s!", + req_size, [class name]); return string; } - (size_t)requestedSize @@ -104,21 +104,21 @@ return req_size; } @end @implementation OFMemNotPartOfObjException -+ newWithObject: (id)obj ++ newWithClass: (Class)class_ + andPointer: (void*)ptr +{ + return [[self alloc] initWithClass: class_ + andPointer: ptr]; +} + +- initWithClass: (Class)class_ andPointer: (void*)ptr { - return [[self alloc] initWithObject: obj - andPointer: ptr]; -} - -- initWithObject: (id)obj - andPointer: (void*)ptr -{ - if ((self = [super initWithObject: obj])) + if ((self = [super initWithClass: class_])) pointer = ptr; return self; } @@ -128,11 +128,11 @@ return string; asprintf(&string, "Memory at %p was not allocated as part of object " "of class %s, thus the memory allocation was not changed! It is " "also possible that there was an attempt to free the same memory " - "twice.", pointer, [object name]); + "twice.", pointer, [class name]); return string; } - (void*)pointer @@ -145,12 +145,11 @@ - (const char*)cString { if (string != NULL) return string; - asprintf(&string, "Value out of range in object of class %s!", - object != nil ? [object name] : "(null)"); + asprintf(&string, "Value out of range in class %s!", [class name]); return string; } @end @@ -158,12 +157,12 @@ - (const char*)cString { if (string != NULL) return string; - asprintf(&string, "The encoding is invalid for object of classs %s!", - [object name]); + asprintf(&string, "The encoding is invalid for classs %s!", + [class name]); return string; } @end @@ -171,65 +170,43 @@ - (const char*)cString { if (string != NULL) return string; - asprintf(&string, "The format is invalid for object of classs %s!", - [object name]); + asprintf(&string, "The format is invalid for classs %s!", [class name]); return string; } @end @implementation OFInitializationFailedException -+ newWithClass: (Class)class_ -{ - return [[self alloc] initWithClass: class_]; -} - -- initWithClass: (Class)class_ -{ - if ((self = [super init])) { - object = nil; - string = NULL; - class = class_; - } - - return self; -} - - (const char*)cString { if (string != NULL) return string; asprintf(&string, "Initialization failed for class %s!", [class name]); return string; } - -- (Class)class -{ - return class; -} -@end - -@implementation OFOpenFileFailedException -+ newWithObject: (id)obj - andPath: (const char*)path_ - andMode: (const char*)mode_ -{ - return [[self alloc] initWithObject: obj - andPath: path_ - andMode: mode_]; -} - -- initWithObject: (id)obj - andPath: (const char*)path_ - andMode: (const char*)mode_ -{ - if ((self = [super initWithObject: obj])) { +@end + +@implementation OFOpenFileFailedException ++ newWithClass: (Class)class_ + andPath: (const char*)path_ + andMode: (const char*)mode_ +{ + return [[self alloc] initWithClass: class_ + andPath: path_ + andMode: mode_]; +} + +- initWithClass: (Class)class_ + andPath: (const char*)path_ + andMode: (const char*)mode_ +{ + if ((self = [super initWithClass: class_])) { path = (path_ != NULL ? strdup(path_) : NULL); mode = (mode_ != NULL ? strdup(mode_) : NULL); err = GET_ERR; } @@ -249,12 +226,12 @@ - (const char*)cString { if (string != NULL) return string; - asprintf(&string, "Failed to open file %s with mode %s in object of " - "class %s! " ERRFMT, path, mode, [self name], ERRPARAM); + asprintf(&string, "Failed to open file %s with mode %s in class %s! " + ERRFMT, path, mode, [self name], ERRPARAM); return string; } - (int)errNo @@ -272,44 +249,44 @@ return mode; } @end @implementation OFReadOrWriteFailedException -+ newWithObject: (id)obj ++ newWithClass: (Class)class_ + andSize: (size_t)size + andNItems: (size_t)nitems +{ + return [[self alloc] initWithClass: class_ + andSize: size + andNItems: nitems]; +} + ++ newWithClass: (Class)class_ + andSize: (size_t)size +{ + return [[self alloc] initWithClass: class_ + andSize: size]; +} + +- initWithClass: (Class)class_ andSize: (size_t)size andNItems: (size_t)nitems { - return [[self alloc] initWithObject: obj - andSize: size - andNItems: nitems]; -} - -+ newWithObject: (id)obj - andSize: (size_t)size -{ - return [[self alloc] initWithObject: obj - andSize: size]; -} - -- initWithObject: (id)obj - andSize: (size_t)size - andNItems: (size_t)nitems -{ - if ((self = [super initWithObject: obj])) { + if ((self = [super initWithClass: class_])) { req_size = size; req_items = nitems; has_items = YES; err = GET_ERR; } return self; } -- initWithObject: (id)obj - andSize: (size_t)size +- initWithClass: (Class)class_ + andSize: (size_t)size { - if ((self = [super initWithObject: obj])) { + if ((self = [super initWithClass: class_])) { req_size = size; req_items = 0; has_items = NO; err = GET_ERR; } @@ -344,15 +321,15 @@ if (string != NULL) return string;; if (has_items) asprintf(&string, "Failed to read %zu items of size %zu in " - "object of class %s! " ERRFMT, req_items, req_size, - [object name], ERRPARAM); + "class %s! " ERRFMT, req_items, req_size, [class name], + ERRPARAM); else - asprintf(&string, "Failed to read %zu bytes in object of class " - "%s! " ERRFMT, req_size, [object name], ERRPARAM); + asprintf(&string, "Failed to read %zu bytes in class %s! " + ERRFMT, req_size, [class name], ERRPARAM); return string; } @end @@ -362,15 +339,15 @@ if (string != NULL) return string; if (has_items) asprintf(&string, "Failed to write %zu items of size %zu in " - "object of class %s! " ERRFMT, req_items, req_size, - [object name], ERRPARAM); + "class %s! " ERRFMT, req_items, req_size, [class name], + ERRPARAM); else - asprintf(&string, "Failed to write %zu bytes in object of " - "class %s! " ERRFMT, req_size, [object name], ERRFMT); + asprintf(&string, "Failed to write %zu bytes in class %s! " + ERRFMT, req_size, [class name], ERRFMT); return string; } @end @@ -378,12 +355,12 @@ - (const char*)cString { if (string != NULL) return string; - asprintf(&string, "Setting an option for an object of type type %s " - "failed!", [object name]); + asprintf(&string, "Setting an option in class %s failed!", + [class name]); return string; } @end @@ -392,11 +369,11 @@ { if (string != NULL) return string; asprintf(&string, "The socket of type %s is not connected or bound!", - [object name]); + [class name]); return string; } @end @@ -405,11 +382,11 @@ { if (string != NULL) return string; asprintf(&string, "The socket of type %s is already connected or bound " - "and thus can't be connected or bound again!", [object name]); + "and thus can't be connected or bound again!", [class name]); return string; } @end @@ -419,31 +396,31 @@ if (string != NULL) return string; asprintf(&string, "The port specified is not valid for a socket of " "type %s! This usually means you tried to use port 0, which is an " - "invalid port.", [object name]); + "invalid port.", [class name]); return string; } @end @implementation OFAddressTranslationFailedException -+ newWithObject: (id)obj ++ newWithClass: (Class)class_ + andNode: (const char*)node_ + andService: (const char*)service_ +{ + return [self newWithClass: class_ + andNode: node_ + andService: service_]; +} + +- initWithClass: (Class)class_ andNode: (const char*)node_ andService: (const char*)service_ { - return [self newWithObject: obj - andNode: node_ - andService: service_]; -} - -- initWithObject: (id)obj - andNode: (const char*)node_ - andService: (const char*)service_ -{ - if ((self = [super initWithObject: obj])) { + if ((self = [super initWithClass: class_])) { node = (node_ != NULL ? strdup(node_) : NULL); service = (service_ != NULL ? strdup(service_) : NULL); err = GET_SOCK_ERR; } @@ -464,15 +441,15 @@ { if (string != NULL) return string; asprintf(&string, "The service %s on %s could not be translated to an " - "address for an object of type %s. This means that either the node " - "was not found, there is no such service on the node, there was a " - "problem with the name server, there was a problem with your " - "network connection or you specified an invalid node or service. " - ERRFMT, service, node, [object name], ERRPARAM); + "address in class %s. This means that either the node was not " + "found, there is no such service on the node, there was a problem " + "with the name server, there was a problem with your network " + "connection or you specified an invalid node or service. " ERRFMT, + service, node, [class name], ERRPARAM); return string; } - (int)errNo @@ -490,24 +467,24 @@ return service; } @end @implementation OFConnectionFailedException -+ newWithObject: (id)obj ++ newWithClass: (Class)class_ + andHost: (const char*)host_ + andPort: (uint16_t)port_ +{ + return [self newWithClass: class_ + andHost: host_ + andPort: port_]; +} + +- initWithClass: (Class)class_ andHost: (const char*)host_ andPort: (uint16_t)port_ { - return [self newWithObject: obj - andHost: host_ - andPort: port_]; -} - -- initWithObject: (id)obj - andHost: (const char*)host_ - andPort: (uint16_t)port_ -{ - if ((self = [super initWithObject: obj])) { + if ((self = [super initWithClass: class_])) { host = (host_ != NULL ? strdup(host_) : NULL); port = port_; err = GET_SOCK_ERR; } @@ -526,11 +503,11 @@ { if (string != NULL) return string; asprintf(&string, "A connection to %s:%d could not be established in " - "object of type %s! " ERRFMT, host, port, [object name], ERRPARAM); + "class %s! " ERRFMT, host, port, [class name], ERRPARAM); return string; } - (int)errNo @@ -548,27 +525,27 @@ return port; } @end @implementation OFBindFailedException -+ newWithObject: (id)obj ++ newWithClass: (Class)class_ + andHost: (const char*)host_ + andPort: (uint16_t)port_ + andFamily: (int)family_ +{ + return [self newWithClass: class_ + andHost: host_ + andPort: port_ + andFamily: family_]; +} + +- initWithClass: (Class)class_ andHost: (const char*)host_ andPort: (uint16_t)port_ andFamily: (int)family_ { - return [self newWithObject: obj - andHost: host_ - andPort: port_ - andFamily: family_]; -} - -- initWithObject: (id)obj - andHost: (const char*)host_ - andPort: (uint16_t)port_ - andFamily: (int)family_ -{ - if ((self = [super initWithObject: obj])) { + if ((self = [super initWithClass: class_])) { host = (host_ != NULL ? strdup(host_) : NULL); port = port_; family = family_; err = GET_SOCK_ERR; } @@ -588,12 +565,11 @@ { if (string != NULL) return string; asprintf(&string, "Binding to port %d on %s using family %d failed in " - "object of type %s! " ERRFMT, port, host, family, [object name], - ERRPARAM); + "class %s! " ERRFMT, port, host, family, [class name], ERRPARAM); return string; } - (int)errNo @@ -616,21 +592,21 @@ return family; } @end @implementation OFListenFailedException -+ newWithObject: (id)obj ++ newWithClass: (Class)class_ + andBackLog: (int)backlog_ +{ + return [[self alloc] initWithClass: class_ + andBackLog: backlog_]; +} + +- initWithClass: (Class)class_ andBackLog: (int)backlog_ { - return [[self alloc] initWithObject: obj - andBackLog: backlog_]; -} - -- initWithObject: (id)obj - andBackLog: (int)backlog_ -{ - if ((self = [super initWithObject: obj])) { + if ((self = [super initWithClass: class_])) { backlog = backlog_; err = GET_SOCK_ERR; } return self; @@ -640,11 +616,11 @@ { if (string != NULL) return string; asprintf(&string, "Failed to listen in socket of type %s with a back " - "log of %d! "ERRFMT, [object name], backlog, ERRPARAM); + "log of %d! "ERRFMT, [class name], backlog, ERRPARAM); return string; } - (int)errNo @@ -657,13 +633,13 @@ return backlog; } @end @implementation OFAcceptFailedException -- initWithObject: (id)obj +- initWithClass: (Class)class_ { - if ((self = [super initWithObject: obj])) + if ((self = [super initWithClass: class_])) err = GET_SOCK_ERR; return self; } @@ -671,15 +647,15 @@ { if (string != NULL) return string; asprintf(&string, "Failed to accept connection in socket of type %s! " - ERRFMT, [object name], ERRPARAM); + ERRFMT, [class name], ERRPARAM); return string; } - (int)errNo { return err; } @end Index: src/OFFile.m ================================================================== --- src/OFFile.m +++ src/OFFile.m @@ -78,15 +78,20 @@ } - initWithPath: (const char*)path andMode: (const char*)mode { + Class c; + if ((self = [super init])) { - if ((fp = fopen(path, mode)) == NULL) - @throw [OFOpenFileFailedException newWithObject: self - andPath: path - andMode: mode]; + if ((fp = fopen(path, mode)) == NULL) { + c = [self class]; + [super free]; + @throw [OFOpenFileFailedException newWithClass: c + andPath: path + andMode: mode]; + } } return self; } - free @@ -106,13 +111,13 @@ intoBuffer: (uint8_t*)buf { size_t ret; if ((ret = fread(buf, size, nitems, fp)) == 0 && !feof(fp)) - @throw [OFReadFailedException newWithObject: self - andSize: size - andNItems: nitems]; + @throw [OFReadFailedException newWithClass: [self class] + andSize: size + andNItems: nitems]; return ret; } - (size_t)readNBytes: (size_t)size @@ -155,13 +160,13 @@ { size_t ret; if ((ret = fwrite(buf, size, nitems, fp)) == 0 && size != 0 && nitems != 0) - @throw [OFWriteFailedException newWithObject: self - andSize: size - andNItems: nitems]; + @throw [OFWriteFailedException newWithClass: [self class] + andSize: size + andNItems: nitems]; return ret; } - (size_t)writeNBytes: (size_t)size Index: src/OFNumber.m ================================================================== --- src/OFNumber.m +++ src/OFNumber.m @@ -10,63 +10,63 @@ */ #import "OFNumber.h" #import "OFExceptions.h" -#define RETURN_AS(t) \ - switch (type) { \ - case OF_NUMBER_CHAR: \ - return (t)value.char_; \ - case OF_NUMBER_SHORT: \ - return (t)value.short_; \ - case OF_NUMBER_INT: \ - return (t)value.int_; \ - case OF_NUMBER_LONG: \ - return (t)value.long_; \ - case OF_NUMBER_UCHAR: \ - return (t)value.uchar; \ - case OF_NUMBER_USHORT: \ - return (t)value.ushort; \ - case OF_NUMBER_UINT: \ - return (t)value.uint; \ - case OF_NUMBER_ULONG: \ - return (t)value.ulong; \ - case OF_NUMBER_INT8: \ - return (t)value.int8; \ - case OF_NUMBER_INT16: \ - return (t)value.int16; \ - case OF_NUMBER_INT32: \ - return (t)value.int32; \ - case OF_NUMBER_INT64: \ - return (t)value.int64; \ - case OF_NUMBER_UINT8: \ - return (t)value.uint8; \ - case OF_NUMBER_UINT16: \ - return (t)value.uint16; \ - case OF_NUMBER_UINT32: \ - return (t)value.uint32; \ - case OF_NUMBER_UINT64: \ - return (t)value.uint64; \ - case OF_NUMBER_SIZE: \ - return (t)value.size; \ - case OF_NUMBER_SSIZE: \ - return (t)value.ssize; \ - case OF_NUMBER_PTRDIFF: \ - return (t)value.ptrdiff; \ - case OF_NUMBER_INTPTR: \ - return (t)value.intptr; \ - case OF_NUMBER_FLOAT: \ - return (t)value.float_; \ - case OF_NUMBER_DOUBLE: \ - return (t)value.double_; \ - case OF_NUMBER_LONG_DOUBLE: \ - return (t)value.longdouble; \ - default: \ - @throw [OFInvalidFormatException newWithObject: self]; \ - \ - /* Make gcc happy */ \ - return 0; \ +#define RETURN_AS(t) \ + switch (type) { \ + case OF_NUMBER_CHAR: \ + return (t)value.char_; \ + case OF_NUMBER_SHORT: \ + return (t)value.short_; \ + case OF_NUMBER_INT: \ + return (t)value.int_; \ + case OF_NUMBER_LONG: \ + return (t)value.long_; \ + case OF_NUMBER_UCHAR: \ + return (t)value.uchar; \ + case OF_NUMBER_USHORT: \ + return (t)value.ushort; \ + case OF_NUMBER_UINT: \ + return (t)value.uint; \ + case OF_NUMBER_ULONG: \ + return (t)value.ulong; \ + case OF_NUMBER_INT8: \ + return (t)value.int8; \ + case OF_NUMBER_INT16: \ + return (t)value.int16; \ + case OF_NUMBER_INT32: \ + return (t)value.int32; \ + case OF_NUMBER_INT64: \ + return (t)value.int64; \ + case OF_NUMBER_UINT8: \ + return (t)value.uint8; \ + case OF_NUMBER_UINT16: \ + return (t)value.uint16; \ + case OF_NUMBER_UINT32: \ + return (t)value.uint32; \ + case OF_NUMBER_UINT64: \ + return (t)value.uint64; \ + case OF_NUMBER_SIZE: \ + return (t)value.size; \ + case OF_NUMBER_SSIZE: \ + return (t)value.ssize; \ + case OF_NUMBER_PTRDIFF: \ + return (t)value.ptrdiff; \ + case OF_NUMBER_INTPTR: \ + return (t)value.intptr; \ + case OF_NUMBER_FLOAT: \ + return (t)value.float_; \ + case OF_NUMBER_DOUBLE: \ + return (t)value.double_; \ + case OF_NUMBER_LONG_DOUBLE: \ + return (t)value.longdouble; \ + default: \ + @throw [OFInvalidFormatException newWithClass: [self class]]; \ + \ + /* Make gcc happy */ \ + return 0; \ } @implementation OFNumber + newWithChar: (char)char_ { Index: src/OFObject.m ================================================================== --- src/OFObject.m +++ src/OFObject.m @@ -49,16 +49,16 @@ memchunks_size = __memchunks_size + 1; if (SIZE_MAX - __memchunks_size == 0 || memchunks_size > SIZE_MAX / sizeof(void*)) - @throw [OFOutOfRangeException newWithObject: self]; + @throw [OFOutOfRangeException newWithClass: [self class]]; if ((memchunks = realloc(__memchunks, memchunks_size * sizeof(void*))) == NULL) - @throw [OFNoMemException newWithObject: self - andSize: memchunks_size]; + @throw [OFNoMemException newWithClass: [self class] + andSize: memchunks_size]; __memchunks = memchunks; __memchunks[__memchunks_size] = ptr; __memchunks_size = memchunks_size; @@ -75,21 +75,21 @@ memchunks_size = __memchunks_size + 1; if (SIZE_MAX - __memchunks_size == 0 || memchunks_size > SIZE_MAX / sizeof(void*)) - @throw [OFOutOfRangeException newWithObject: self]; + @throw [OFOutOfRangeException newWithClass: [self class]]; + + if ((ptr = malloc(size)) == NULL) + @throw [OFNoMemException newWithClass: [self class] + andSize: size]; if ((memchunks = realloc(__memchunks, - memchunks_size * sizeof(void*))) == NULL) - @throw [OFNoMemException newWithObject: self - andSize: memchunks_size]; - - if ((ptr = malloc(size)) == NULL) { - free(memchunks); - @throw [OFNoMemException newWithObject: self - andSize: size]; + memchunks_size * sizeof(void*))) == NULL) { + free(ptr); + @throw [OFNoMemException newWithClass: [self class] + andSize: memchunks_size]; } __memchunks = memchunks; __memchunks[__memchunks_size] = ptr; __memchunks_size = memchunks_size; @@ -102,11 +102,11 @@ { if (nitems == 0 || size == 0) return NULL; if (nitems > SIZE_MAX / size) - @throw [OFOutOfRangeException newWithObject: self]; + @throw [OFOutOfRangeException newWithClass: [self class]]; return [self getMemWithSize: nitems * size]; } - (void*)resizeMem: (void*)ptr @@ -125,20 +125,21 @@ iter = __memchunks + __memchunks_size; while (iter-- > __memchunks) { if (OF_UNLIKELY(*iter == ptr)) { if (OF_UNLIKELY((ptr = realloc(ptr, size)) == NULL)) - @throw [OFNoMemException newWithObject: self - andSize: size]; + @throw [OFNoMemException + newWithClass: [self class] + andSize: size]; *iter = ptr; return ptr; } } - @throw [OFMemNotPartOfObjException newWithObject: self - andPointer: ptr]; + @throw [OFMemNotPartOfObjException newWithClass: [self class] + andPointer: ptr]; return NULL; /* never reached, but makes gcc happy */ } - (void*)resizeMem: (void*)ptr toNItems: (size_t)nitems @@ -154,11 +155,11 @@ [self freeMem: ptr]; return NULL; } if (nitems > SIZE_MAX / size) - @throw [OFOutOfRangeException newWithObject: self]; + @throw [OFOutOfRangeException newWithClass: [self class]]; memsize = nitems * size; return [self resizeMem: ptr toSize: memsize]; } @@ -179,11 +180,11 @@ last = __memchunks[memchunks_size]; if (OF_UNLIKELY(__memchunks_size == 0 || memchunks_size > SIZE_MAX / sizeof(void*))) @throw [OFOutOfRangeException - newWithObject: self]; + newWithClass: [self class]]; if (OF_UNLIKELY(memchunks_size == 0)) { free(ptr); free(__memchunks); @@ -194,12 +195,12 @@ } if (OF_UNLIKELY((memchunks = realloc(__memchunks, memchunks_size * sizeof(void*))) == NULL)) @throw [OFNoMemException - newWithObject: self - andSize: memchunks_size]; + newWithClass: [self class] + andSize: memchunks_size]; free(ptr); __memchunks = memchunks; __memchunks[i] = last; __memchunks_size = memchunks_size; @@ -206,10 +207,10 @@ return self; } } - @throw [OFMemNotPartOfObjException newWithObject: self - andPointer: ptr]; + @throw [OFMemNotPartOfObjException newWithClass: [self class] + andPointer: ptr]; return self; /* never reached, but makes gcc happy */ } @end Index: src/OFString.m ================================================================== --- src/OFString.m +++ src/OFString.m @@ -139,22 +139,25 @@ return self; } - initFromCString: (const char*)str { + Class c; + if ((self = [super init])) { if (str != NULL) { length = strlen(str); switch (check_utf8(str, length)) { case 1: is_utf8 = YES; break; case -1: + c = [self class]; [super free]; @throw [OFInvalidEncodingException - newWithObject: self]; + newWithClass: c]; } string = [self getMemWithSize: length + 1]; memcpy(string, str, length + 1); } @@ -178,33 +181,36 @@ - initFromFormatCString: (const char*)fmt withArguments: (va_list)args { int t; + Class c; if ((self = [super init])) { - if (fmt == NULL) - @throw [OFInvalidFormatException newWithObject: self]; - - if ((t = vasprintf(&string, fmt, args)) == -1) - /* - * This is only the most likely error to happen. - * Unfortunately, as errno isn't always thread-safe, - * there's no good way for us to find out what really - * happened. - */ - @throw [OFNoMemException newWithObject: self]; + if (fmt == NULL) { + c = [self class]; + [super free]; + @throw [OFInvalidFormatException newWithClass: c]; + } + + if ((t = vasprintf(&string, fmt, args)) == -1) { + c = [self class]; + [super free]; + @throw [OFInitializationFailedException + newWithClass: c]; + } length = t; switch (check_utf8(string, length)) { case 1: is_utf8 = YES; break; case -1: free(string); + c = [self class]; [super free]; - @throw [OFInvalidEncodingException newWithObject: self]; + @throw [OFInvalidEncodingException newWithClass: c]; } @try { [self addToMemoryPool: string]; } @catch (OFException *e) { @@ -260,11 +266,11 @@ switch (check_utf8(str, strlength)) { case 1: is_utf8 = YES; break; case -1: - @throw [OFInvalidEncodingException newWithObject: self]; + @throw [OFInvalidEncodingException newWithClass: [self class]]; } newlen = length + strlength; newstr = [self resizeMem: string toSize: newlen + 1]; @@ -294,22 +300,25 @@ andArguments: (va_list)args { char *t; if (fmt == NULL) - @throw [OFInvalidFormatException newWithObject: self]; + @throw [OFInvalidFormatException newWithClass: [self class]]; if ((vasprintf(&t, fmt, args)) == -1) /* * This is only the most likely error to happen. * Unfortunately, as errno isn't always thread-safe, there's * no good way for us to find out what really happened. */ - @throw [OFNoMemException newWithObject: self]; + @throw [OFNoMemException newWithClass: [self class]]; - [self appendCString: t]; - free(t); + @try { + [self appendCString: t]; + } @finally { + free(t); + } return self; } - reverse @@ -336,17 +345,19 @@ continue; /* A start byte can't happen first as we reversed everything */ if (OF_UNLIKELY(string[i] & 0x40)) { madvise(string, len, MADV_NORMAL); - @throw [OFInvalidEncodingException newWithObject: self]; + @throw [OFInvalidEncodingException + newWithClass: [self class]]; } /* Next byte must not be ASCII */ if (OF_UNLIKELY(length < i + 1 || !(string[i + 1] & 0x80))) { madvise(string, len, MADV_NORMAL); - @throw [OFInvalidEncodingException newWithObject: self]; + @throw [OFInvalidEncodingException + newWithClass: [self class]]; } /* Next byte is the start byte */ if (OF_LIKELY(string[i + 1] & 0x40)) { string[i] ^= string[i + 1]; @@ -358,11 +369,12 @@ } /* Second next byte must not be ASCII */ if (OF_UNLIKELY(length < i + 2 || !(string[i + 2] & 0x80))) { madvise(string, len, MADV_NORMAL); - @throw [OFInvalidEncodingException newWithObject: self]; + @throw [OFInvalidEncodingException + newWithClass: [self class]]; } /* Second next byte is the start byte */ if (OF_LIKELY(string[i + 2] & 0x40)) { string[i] ^= string[i + 2]; @@ -374,11 +386,12 @@ } /* Third next byte must not be ASCII */ if (OF_UNLIKELY(length < i + 3 || !(string[i + 3] & 0x80))) { madvise(string, len, MADV_NORMAL); - @throw [OFInvalidEncodingException newWithObject: self]; + @throw [OFInvalidEncodingException + newWithClass: [self class]]; } /* Third next byte is the start byte */ if (OF_LIKELY(string[i + 3] & 0x40)) { string[i] ^= string[i + 3]; @@ -393,11 +406,11 @@ continue; } /* UTF-8 does not allow more than 4 bytes per character */ madvise(string, len, MADV_NORMAL); - @throw [OFInvalidEncodingException newWithObject: self]; + @throw [OFInvalidEncodingException newWithClass: [self class]]; } madvise(string, len, MADV_NORMAL); return self; @@ -406,11 +419,11 @@ - upper { size_t i = length; if (is_utf8) - @throw [OFInvalidEncodingException newWithObject: self]; + @throw [OFInvalidEncodingException newWithClass: [self class]]; while (i--) string[i] = toupper(string[i]); return self; @@ -419,13 +432,13 @@ - lower { size_t i = length; if (is_utf8) - @throw [OFInvalidEncodingException newWithObject: self]; + @throw [OFInvalidEncodingException newWithClass: [self class]]; while (i--) string[i] = tolower(string[i]); return self; } @end Index: src/OFTCPSocket.m ================================================================== --- src/OFTCPSocket.m +++ src/OFTCPSocket.m @@ -75,26 +75,26 @@ { struct addrinfo hints, *res, *res0; char portstr[6]; if (!port) - @throw [OFInvalidPortException newWithObject: self]; + @throw [OFInvalidPortException newWithClass: [self class]]; if (sock != INVALID_SOCKET) - @throw [OFAlreadyConnectedException newWithObject: self]; + @throw [OFAlreadyConnectedException newWithClass: [self class]]; memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; snprintf(portstr, 6, "%d", port); if (getaddrinfo(host, portstr, &hints, &res0)) @throw [OFAddressTranslationFailedException - newWithObject: self - andNode: host - andService: portstr]; + newWithClass: [self class] + andNode: host + andService: portstr]; for (res = res0; res != NULL; res = res->ai_next) { if ((sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == INVALID_SOCKET) continue; @@ -109,13 +109,13 @@ } freeaddrinfo(res0); if (sock == INVALID_SOCKET) - @throw [OFConnectionFailedException newWithObject: self - andHost: host - andPort: port]; + @throw [OFConnectionFailedException newWithClass: [self class] + andHost: host + andPort: port]; return self; } - bindOn: (const char*)host @@ -124,39 +124,39 @@ { struct addrinfo hints, *res; char portstr[6]; if (!port) - @throw [OFInvalidPortException newWithObject: self]; + @throw [OFInvalidPortException newWithClass: [self class]]; if (sock != INVALID_SOCKET) - @throw [OFAlreadyConnectedException newWithObject: self]; + @throw [OFAlreadyConnectedException newWithClass: [self class]]; if ((sock = socket(family, SOCK_STREAM, 0)) == INVALID_SOCKET) - @throw [OFBindFailedException newWithObject: self - andHost: host - andPort: port - andFamily: family]; + @throw [OFBindFailedException newWithClass: [self class] + andHost: host + andPort: port + andFamily: family]; memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = family; hints.ai_socktype = SOCK_STREAM; snprintf(portstr, 6, "%d", port); if (getaddrinfo(host, portstr, &hints, &res)) @throw [OFAddressTranslationFailedException - newWithObject: self - andNode: host - andService: portstr]; + newWithClass: [self class] + andNode: host + andService: portstr]; if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) { freeaddrinfo(res); - @throw [OFBindFailedException newWithObject: self - andHost: host - andPort: port - andFamily: family]; + @throw [OFBindFailedException newWithClass: [self class] + andHost: host + andPort: port + andFamily: family]; } freeaddrinfo(res); return self; @@ -163,27 +163,27 @@ } - listenWithBackLog: (int)backlog { if (sock == INVALID_SOCKET) - @throw [OFNotConnectedException newWithObject: self]; + @throw [OFNotConnectedException newWithClass: [self class]]; if (listen(sock, backlog) == -1) - @throw [OFListenFailedException newWithObject: self - andBackLog: backlog]; + @throw [OFListenFailedException newWithClass: [self class] + andBackLog: backlog]; return self; } - listen { if (sock == INVALID_SOCKET) - @throw [OFNotConnectedException newWithObject: self]; + @throw [OFNotConnectedException newWithClass: [self class]]; if (listen(sock, 5) == -1) - @throw [OFListenFailedException newWithObject: self - andBackLog: 5]; + @throw [OFListenFailedException newWithClass: [self class] + andBackLog: 5]; return self; } - (OFTCPSocket*)accept @@ -203,11 +203,11 @@ @throw e; } if ((s = accept(sock, addr, &addrlen)) == INVALID_SOCKET) { [newsock free]; - @throw [OFAcceptFailedException newWithObject: self]; + @throw [OFAcceptFailedException newWithClass: [self class]]; } [newsock setSocket: s]; [newsock setSocketAddress: addr withLength: addrlen]; @@ -219,15 +219,15 @@ intoBuffer: (uint8_t*)buf { ssize_t ret; if (sock == INVALID_SOCKET) - @throw [OFNotConnectedException newWithObject: self]; + @throw [OFNotConnectedException newWithClass: [self class]]; if ((ret = recv(sock, (char*)buf, size, 0)) < 1) - @throw [OFReadFailedException newWithObject: self - andSize: size]; + @throw [OFReadFailedException newWithClass: [self class] + andSize: size]; /* This is safe, as we already checked < 1 */ return ret; } @@ -234,11 +234,11 @@ - (uint8_t*)readNBytes: (size_t)size { uint8_t *ret; if (sock == INVALID_SOCKET) - @throw [OFNotConnectedException newWithObject: self]; + @throw [OFNotConnectedException newWithClass: [self class]]; ret = [self getMemWithSize: size]; @try { [self readNBytes: size @@ -255,24 +255,24 @@ fromBuffer: (const uint8_t*)buf { ssize_t ret; if (sock == INVALID_SOCKET) - @throw [OFNotConnectedException newWithObject: self]; + @throw [OFNotConnectedException newWithClass: [self class]]; if ((ret = send(sock, (char*)buf, size, 0)) == -1) - @throw [OFWriteFailedException newWithObject: self - andSize: size]; + @throw [OFWriteFailedException newWithClass: [self class] + andSize: size]; /* This is safe, as we already checked for -1 */ return ret; } - (size_t)writeCString: (const char*)str { if (sock == INVALID_SOCKET) - @throw [OFNotConnectedException newWithObject: self]; + @throw [OFNotConnectedException newWithClass: [self class]]; return [self writeNBytes: strlen(str) fromBuffer: (const uint8_t*)str]; } @@ -280,24 +280,24 @@ { #ifndef _WIN32 int flags; if ((flags = fcntl(sock, F_GETFL)) == -1) - @throw [OFSetOptionFailedException newWithObject: self]; + @throw [OFSetOptionFailedException newWithClass: [self class]]; if (enable) flags &= ~O_NONBLOCK; else flags |= O_NONBLOCK; if (fcntl(sock, F_SETFL, flags) == -1) - @throw [OFSetOptionFailedException newWithObject: self]; + @throw [OFSetOptionFailedException newWithClass: [self class]]; #else u_long v = enable; if (ioctlsocket(sock, FIONBIO, &v) == SOCKET_ERROR) - @throw [OFSetOptionFailedException newWithObject: self]; + @throw [OFSetOptionFailedException newWithClass: [self class]]; #endif return self; } @@ -304,19 +304,19 @@ - enableKeepAlives: (BOOL)enable { int v = enable; if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char*)&v, sizeof(v))) - @throw [OFSetOptionFailedException newWithObject: self]; + @throw [OFSetOptionFailedException newWithClass: [self class]]; return self; } - close { if (sock == INVALID_SOCKET) - @throw [OFNotConnectedException newWithObject: self]; + @throw [OFNotConnectedException newWithClass: [self class]]; sock = INVALID_SOCKET; if (saddr != NULL) [self freeMem: saddr]; Index: src/OFXMLFactory.m ================================================================== --- src/OFXMLFactory.m +++ src/OFXMLFactory.m @@ -26,47 +26,43 @@ * We already have a clue about how big the resulting string will get, so we * can prealloc and only resize when really necessary - OFString would always * resize when we append, which would be slow here. */ -static inline BOOL -xf_resize_chars(char **str, size_t *len, size_t add) +static inline void +xf_resize_chars(char **str, size_t *len, size_t add, Class class) { char *str2; size_t len2; if (add > SIZE_MAX - *len) - @throw [OFOutOfRangeException newWithObject: nil]; + @throw [OFOutOfRangeException newWithClass: class]; len2 = *len + add; if ((str2 = realloc(*str, len2)) == NULL) { if (*str) free(*str); *str = NULL; - return NO; + @throw [OFNoMemException newWithClass: class + andSize: len2]; } *str = str2; *len = len2; - - return YES; } -static inline BOOL -xf_add2chars(char **str, size_t *len, size_t *pos, const char *add) +static inline void +xf_add2chars(char **str, size_t *len, size_t *pos, const char *add, Class class) { size_t add_len; add_len = strlen(add); - if (!xf_resize_chars(str, len, add_len)) - return NO; + xf_resize_chars(str, len, add_len, class); memcpy(*str + *pos, add, add_len); *pos += add_len; - - return YES; } @implementation OFXMLFactory + (char*)escapeCString: (const char*)s { @@ -73,51 +69,33 @@ char *ret; size_t i, j, len, nlen; len = nlen = strlen(s); if (SIZE_MAX - len < 1) - @throw [OFOutOfRangeException newWithObject: nil]; + @throw [OFOutOfRangeException newWithClass: self]; nlen++; if ((ret = malloc(nlen)) == NULL) - @throw [OFNoMemException newWithObject: nil - andSize: nlen]; + @throw [OFNoMemException newWithClass: self + andSize: nlen]; for (i = j = 0; i < len; i++) { switch (s[i]) { case '<': - if (OF_UNLIKELY(!xf_add2chars(&ret, &nlen, &j, "<"))) - @throw [OFNoMemException - newWithObject: nil - andSize: nlen + 4]; + xf_add2chars(&ret, &nlen, &j, "<", self); break; case '>': - if (OF_UNLIKELY(!xf_add2chars(&ret, &nlen, &j, ">"))) - @throw [OFNoMemException - newWithObject: nil - andSize: nlen + 4]; + xf_add2chars(&ret, &nlen, &j, ">", self); break; case '"': - if (OF_UNLIKELY(!xf_add2chars(&ret, &nlen, &j, - """))) - @throw [OFNoMemException - newWithObject: nil - andSize: nlen + 6]; + xf_add2chars(&ret, &nlen, &j, """, self); break; case '\'': - if (OF_UNLIKELY(!xf_add2chars(&ret, &nlen, &j, - "'"))) - @throw [OFNoMemException - newWithObject: nil - andSize: nlen + 6]; + xf_add2chars(&ret, &nlen, &j, "'", self); break; case '&': - if (OF_UNLIKELY(!xf_add2chars(&ret, &nlen, &j, - "&"))) - @throw [OFNoMemException - newWithObject: nil - andSize: nlen + 5]; + xf_add2chars(&ret, &nlen, &j, "&", self); break; default: ret[j++] = s[i]; break; } @@ -136,16 +114,16 @@ va_list args; /* Start of tag */ len = strlen(name); if (SIZE_MAX - len < 3) - @throw [OFOutOfRangeException newWithObject: nil]; + @throw [OFOutOfRangeException newWithClass: self]; len += 3; if ((xml = malloc(len)) == NULL) - @throw [OFNoMemException newWithObject: nil - andSize: len]; + @throw [OFNoMemException newWithClass: self + andSize: len]; i = 0; xml[i++] = '<'; memcpy(xml + i, name, strlen(name)); i += strlen(name); @@ -152,61 +130,47 @@ /* Arguments */ va_start(args, data); while ((arg = va_arg(args, char*)) != NULL && (val = va_arg(args, char*)) != NULL) { - char *esc_val; - - if (OF_UNLIKELY((esc_val = - [OFXMLFactory escapeCString: val]) == NULL)) { - /* - * escapeCString already throws an exception, - * no need to throw a second one here. - */ - free(xml); - return NULL; - } - - if (OF_UNLIKELY(!xf_resize_chars(&xml, &len, 1 + strlen(arg) + - 2 + strlen(esc_val) + 1))) { - free(esc_val); - @throw [OFNoMemException - newWithObject: nil - andSize: len + 1 + strlen(arg) + 2 + - strlen(esc_val) + 1]; - } - - xml[i++] = ' '; - memcpy(xml + i, arg, strlen(arg)); - i += strlen(arg); - xml[i++] = '='; - xml[i++] = '\''; - memcpy(xml + i, esc_val, strlen(esc_val)); - i += strlen(esc_val); - xml[i++] = '\''; - - free(esc_val); + char *esc_val = NULL; + + @try { + esc_val = [OFXMLFactory escapeCString: val]; + } @catch (OFException *e) { + free(xml); + @throw e; + } + + @try { + xf_resize_chars(&xml, &len, 1 + strlen(arg) + 2 + + strlen(esc_val) + 1, self); + + xml[i++] = ' '; + memcpy(xml + i, arg, strlen(arg)); + i += strlen(arg); + xml[i++] = '='; + xml[i++] = '\''; + memcpy(xml + i, esc_val, strlen(esc_val)); + i += strlen(esc_val); + xml[i++] = '\''; + } @finally { + free(esc_val); + } } va_end(args); /* End of tag */ if (close) { if (data == NULL) { - if (!xf_resize_chars(&xml, &len, 2 - 1)) - @throw [OFNoMemException - newWithObject: nil - andSize: len + 2 - 1]; + xf_resize_chars(&xml, &len, 2 - 1, self); xml[i++] = '/'; xml[i++] = '>'; } else { - if (!xf_resize_chars(&xml, &len, 1 + strlen(data) + - 2 + strlen(name) + 1 - 1)) - @throw [OFNoMemException - newWithObject: nil - andSize: len + 1 + strlen(data) + 2 + - strlen(name) + 1 - 1]; + xf_resize_chars(&xml, &len, 1 + strlen(data) + 2 + + strlen(name) + 1 - 1, self); xml[i++] = '>'; memcpy(xml + i, data, strlen(data)); i += strlen(data); xml[i++] = '<'; @@ -230,31 +194,25 @@ if (strs[0] == NULL) return NULL; len = strlen(*strs); if (SIZE_MAX - len < 1) - @throw [OFOutOfRangeException newWithObject: nil]; + @throw [OFOutOfRangeException newWithClass: self]; len++; if ((ret = malloc(len)) == NULL) - @throw [OFNoMemException newWithObject: nil - andSize: len]; + @throw [OFNoMemException newWithClass: self + andSize: len]; memcpy(ret, strs[0], len - 1); pos = len - 1; - for (i = 1; strs[i] != NULL; i++) { - if (OF_UNLIKELY(!xf_add2chars(&ret, &len, &pos, strs[i]))) { - free(ret); - @throw [OFNoMemException - newWithObject: nil - andSize: len + strlen(strs[i])]; - } - } + for (i = 1; strs[i] != NULL; i++) + xf_add2chars(&ret, &len, &pos, strs[i], self); for (i = 0; strs[i] != NULL; i++) free(strs[i]); ret[pos] = 0; return ret; } @end