@@ -13,11 +13,24 @@ #define _GNU_SOURCE #import #import #import + +#ifndef _WIN32 #import +#define GET_ERR errno +#define GET_SOCK_ERR errno +#define ERRFMT "Error string was: %s" +#define ERRPARAM strerror(err) +#else +#import +#define GET_ERR GetLastError() +#define GET_SOCK_ERR WSAGetLastError() +#define ERRFMT "Error code was: %d" +#define ERRPARAM err +#endif #import "OFExceptions.h" #ifndef HAVE_ASPRINTF #import "asprintf.h" @@ -202,11 +215,11 @@ andMode: (const char*)mode_ { if ((self = [super initWithObject: obj])) { path = (path_ != NULL ? strdup(path_) : NULL); mode = (mode_ != NULL ? strdup(mode_) : NULL); - err = errno; + err = GET_ERR; } return self; } @@ -224,12 +237,11 @@ { if (string != NULL) return string; asprintf(&string, "Failed to open file %s with mode %s in object of " - "class %s! Error string was: %s", path, mode, [self name], - strerror(err)); + "class %s! " ERRFMT, path, mode, [self name], ERRPARAM); return string; } - (int)errNo @@ -271,11 +283,11 @@ { if ((self = [super initWithObject: obj])) { req_size = size; req_items = nitems; has_items = YES; - err = errno; + err = GET_ERR; } return self; } @@ -284,11 +296,11 @@ { if ((self = [super initWithObject: obj])) { req_size = size; req_items = 0; has_items = NO; - err = errno; + err = GET_ERR; } return self; } @@ -319,16 +331,15 @@ if (string != NULL) return string;; if (has_items) asprintf(&string, "Failed to read %zu items of size %zu in " - "object of class %s! Error string was: %s", req_items, - req_size, [object name], strerror(err)); + "object of class %s! " ERRFMT, req_items, req_size, + [object name], ERRPARAM); else asprintf(&string, "Failed to read %zu bytes in object of class " - "%s! Error string was: %s", req_size, [object name], - strerror(err)); + "%s! " ERRFMT, req_size, [object name], ERRPARAM); return string; } @end @@ -338,16 +349,15 @@ if (string != NULL) return string; if (has_items) asprintf(&string, "Failed to write %zu items of size %zu in " - "object of class %s! Error string was: %s", req_items, - req_size, [object name], strerror(err)); + "object of class %s! " ERRFMT, req_items, req_size, + [object name], ERRPARAM); else asprintf(&string, "Failed to write %zu bytes in object of " - "class %s! Error string was: %s", req_size, [object name], - strerror(err)); + "class %s! " ERRFMT, req_size, [object name], ERRFMT); return string; } @end @@ -419,11 +429,11 @@ andService: (const char*)service_ { if ((self = [super initWithObject: obj])) { node = (node_ != NULL ? strdup(node_) : NULL); service = (service_ != NULL ? strdup(service_) : NULL); - err = errno; + err = GET_SOCK_ERR; } return self; } @@ -445,12 +455,11 @@ 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. " - "Error string was: %s", service, node, [object name], - strerror(err)); + ERRFMT, service, node, [object name], ERRPARAM); return string; } - (int)errNo @@ -484,11 +493,11 @@ andPort: (uint16_t)port_ { if ((self = [super initWithObject: obj])) { host = (host_ != NULL ? strdup(host_) : NULL); port = port_; - err = errno; + err = GET_SOCK_ERR; } return self; } @@ -504,12 +513,11 @@ { if (string != NULL) return string; asprintf(&string, "A connection to %s:%d could not be established in " - "object of type %s! Error string was: %s", host, port, - [object name], strerror(err)); + "object of type %s! " ERRFMT, host, port, [object name], ERRPARAM); return string; } - (int)errNo @@ -547,11 +555,11 @@ { if ((self = [super initWithObject: obj])) { host = (host_ != NULL ? strdup(host_) : NULL); port = port_; family = family_; - err = errno; + err = GET_SOCK_ERR; } return self; } @@ -567,12 +575,12 @@ { if (string != NULL) return string; asprintf(&string, "Binding to port %d on %s using family %d failed in " - "object of type %s! Error string was: %s", port, host, family, - [object name], strerror(err)); + "object of type %s! " ERRFMT, port, host, family, [object name], + ERRPARAM); return string; } - (int)errNo @@ -607,11 +615,11 @@ - initWithObject: (id)obj andBackLog: (int)backlog_ { if ((self = [super initWithObject: obj])) { backlog = backlog_; - err = errno; + err = GET_SOCK_ERR; } return self; } @@ -619,12 +627,11 @@ { if (string != NULL) return string; asprintf(&string, "Failed to listen in socket of type %s with a back " - "log of %d! Error string was: %s", [object name], backlog, - strerror(err)); + "log of %d! "ERRFMT, [object name], backlog, ERRPARAM); return string; } - (int)errNo @@ -640,11 +647,11 @@ @implementation OFAcceptFailedException - initWithObject: (id)obj { if ((self = [super initWithObject: obj])) - err = errno; + err = GET_SOCK_ERR; return self; } - (const char*)cString @@ -651,15 +658,15 @@ { if (string != NULL) return string; asprintf(&string, "Failed to accept connection in socket of type %s! " - "Error string was: %s", [object name], strerror(err)); + ERRFMT, [object name], ERRPARAM); return string; } - (int)errNo { return err; } @end