ADDED TODO Index: TODO ================================================================== --- TODO +++ TODO @@ -0,0 +1,16 @@ +OFArray +OFDictionary +OFSortedArray +OFSocket +OFAutoreleasePool + +OFStack +OFQueue + +OFXMLParser +OFHash [cryptographic] + +OFSortedQueue + +OFTLSSocket +OFXMPPClient Index: src/OFCString.h ================================================================== --- src/OFCString.h +++ src/OFCString.h @@ -20,8 +20,8 @@ } - initWithCString: (char*)str; - (char*)cString; - (OFString*)clone; -- (int)compare: (OFString*)str; +- (int)compareTo: (OFString*)str; - (OFString*)append: (OFString*)str; @end Index: src/OFCString.m ================================================================== --- src/OFCString.m +++ src/OFCString.m @@ -22,11 +22,11 @@ if (str == NULL) { length = 0; string = NULL; } else { length = strlen(str); - string = [self getMem: length + 1]; + string = [self getMemWithSize: length + 1]; memcpy(string, str, length + 1); } } return self; } @@ -39,11 +39,11 @@ - (OFString*)clone { return [OFString newWithCString: string]; } -- (int)compare: (OFString*)str +- (int)compareTo: (OFString*)str { return strcmp(string, [str cString]); } - (OFString*)append: (OFString*)str Index: src/OFConstCString.h ================================================================== --- src/OFConstCString.h +++ src/OFConstCString.h @@ -17,7 +17,7 @@ } - initWithConstCString: (const char*)str; - (const char*)cString; - (OFString*)clone; -- (int)compare: (OFString*)str; +- (int)compareTo: (OFString*)str; @end Index: src/OFConstCString.m ================================================================== --- src/OFConstCString.m +++ src/OFConstCString.m @@ -35,10 +35,10 @@ - (OFString*)clone { return [OFString newWithConstCString: string]; } -- (int)compare: (OFString*)str +- (int)compareTo: (OFString*)str { return strcmp(string, [str cString]); } @end Index: src/OFConstWideCString.h ================================================================== --- src/OFConstWideCString.h +++ src/OFConstWideCString.h @@ -19,7 +19,7 @@ } - initWithConstWideCString: (const wchar_t*)wstr; - (const wchar_t*)wcString; - (OFString*)clone; -- (int)compare: (OFString*)str; +- (int)compareTo: (OFString*)str; @end Index: src/OFConstWideCString.m ================================================================== --- src/OFConstWideCString.m +++ src/OFConstWideCString.m @@ -35,10 +35,10 @@ - (OFString*)clone { return [OFString newWithConstWideCString: string]; } -- (int)compare: (OFString*)str +- (int)compareTo: (OFString*)str { return wcscmp(string, [str wcString]); } @end Index: src/OFExceptions.h ================================================================== --- src/OFExceptions.h +++ src/OFExceptions.h @@ -10,13 +10,22 @@ */ #import #import "OFObject.h" +// FIXME: Exceptions should include which type of error occoured (fopen etc.) + @interface OFException: OFObject +{ + char *errstr; +} + + newWithObject: (id)obj; - initWithObject: (id)obj; +- free; +- (void)raise; +- (char*)string; @end @interface OFNoMemException: OFException + newWithObject: (id)obj andSize: (size_t)size; @@ -24,13 +33,13 @@ andSize: (size_t)size; @end @interface OFNotImplementedException: OFException + newWithObject: (id)obj - andMethod: (const char*)method; + andSelector: (SEL)sel; - initWithObject: (id)obj - andMethod: (const char*)method; + andSelector: (SEL)sel; @end @interface OFMemNotPartOfObjException: OFException + newWithObject: (id)obj andPointer: (void*)ptr; Index: src/OFExceptions.m ================================================================== --- src/OFExceptions.m +++ src/OFExceptions.m @@ -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 Index: src/OFFile.h ================================================================== --- src/OFFile.h +++ src/OFFile.h @@ -18,18 +18,29 @@ FILE *fp; } + newWithPath: (const char*)path andMode: (const char*)mode; ++ (int)changeModeOfFile: (const char*)path + toMode: (mode_t)mode; ++ (int)changeOwnerOfFile: (const char*)path + toOwner: (uid_t)owner + andGroup: (gid_t)group; ++ (int)delete: (const char*)path; ++ (int)link: (const char*)src + to: (const char*)dest; ++ (int)symlink: (const char*)src + to: (const char*)dest; + - initWithPath: (const char*)path andMode: (const char*)mode; - free; -- (BOOL)isEndOfFile; +- (BOOL)atEndOfFile; - (size_t)readIntoBuffer: (char*)buf withSize: (size_t)size andNItems: (size_t)nItems; - (char*)readWithSize: (size_t)size andNItems: (size_t)nitems; - (size_t)writeBuffer: (char*)buf withSize: (size_t)size andNItems: (size_t)nitems; @end Index: src/OFFile.m ================================================================== --- src/OFFile.m +++ src/OFFile.m @@ -9,10 +9,13 @@ * the packaging of this file. */ #import #import +#import +#import +#import #import "OFFile.h" #import "OFExceptions.h" @implementation OFFile @@ -20,10 +23,45 @@ andMode: (const char*)mode { return [[OFFile alloc] initWithPath: path andMode: mode]; } + ++ (int)changeModeOfFile: (const char*)path + toMode: (mode_t)mode +{ + // FIXME: On error, throw exception + return chmod(path, mode); +} + ++ (int)changeOwnerOfFile: (const char*)path + toOwner: (uid_t)owner + andGroup: (gid_t)group +{ + // FIXME: On error, throw exception + return chown(path, owner, group); +} + ++ (int)delete: (const char*)path +{ + // FIXME: On error, throw exception + return unlink(path); +} + ++ (int)link: (const char*)src + to: (const char*)dest +{ + // FIXME: On error, throw exception + return link(src, dest); +} + ++ (int)symlink: (const char*)src + to: (const char*)dest +{ + // FIXME: On error, throw exception + return symlink(src, dest); +} - initWithPath: (const char*)path andMode: (const char*)mode { if ((self = [super init])) { @@ -39,11 +77,11 @@ { fclose(fp); return [super free]; } -- (BOOL)isEndOfFile +- (BOOL)atEndOfFile { return feof(fp); } - (size_t)readIntoBuffer: (char*)buf @@ -50,14 +88,14 @@ withSize: (size_t)size andNItems: (size_t)nitems { size_t ret; - if ((ret = fread(buf, size, nitems, fp)) == 0 && ![self isEndOfFile]) - [OFReadFailedException newWithObject: self - andSize: size - andNItems: nitems]; + if ((ret = fread(buf, size, nitems, fp)) == 0 && !feof(fp)) + [[OFReadFailedException newWithObject: self + andSize: size + andNItems: nitems] raise]; return ret; } - (char*)readWithSize: (size_t)size @@ -66,15 +104,15 @@ uint64_t memsize; char *ret; if (size >= 0xFFFFFFFF || nitems >= 0xFFFFFFFF || (memsize = (uint64_t)nitems * size) > 0xFFFFFFFF) { - [OFOverflowException newWithObject: self]; + [[OFOverflowException newWithObject: self] raise]; return NULL; } - ret = [self getMem: (size_t)memsize]; + ret = [self getMemWithSize: (size_t)memsize]; @try { [self readIntoBuffer: ret withSize: size andNItems: nitems]; @@ -93,12 +131,12 @@ { size_t ret; if ((ret = fwrite(buf, size, nitems, fp)) == 0 && size != 0 && nitems != 0) - [OFWriteFailedException newWithObject: self - andSize: size - andNItems: nitems]; + [[OFWriteFailedException newWithObject: self + andSize: size + andNItems: nitems] raise]; return ret; } @end Index: src/OFObject.h ================================================================== --- src/OFObject.h +++ src/OFObject.h @@ -21,11 +21,11 @@ { struct __ofobject_allocated_mem *__mem_pool; } - init; -- (void*)getMem: (size_t)size; +- (void*)getMemWithSize: (size_t)size; - (void*)resizeMem: (void*)ptr toSize: (size_t)size; - (void)freeMem: (void*)ptr; - free; @end Index: src/OFObject.m ================================================================== --- src/OFObject.m +++ src/OFObject.m @@ -20,25 +20,26 @@ if ((self = [super init]) != nil) __mem_pool = NULL; return self; } -- (void*)getMem: (size_t)size +- (void*)getMemWithSize: (size_t)size { struct __ofobject_allocated_mem *iter; if ((iter = malloc(sizeof(struct __ofobject_allocated_mem))) == NULL) { - [OFNoMemException newWithObject: self + [[OFNoMemException newWithObject: self andSize: sizeof(struct - __ofobject_allocated_mem)]; + __ofobject_allocated_mem) + ] raise]; return NULL; } if ((iter->ptr = malloc(size)) == NULL) { free(iter); - [OFNoMemException newWithObject: self - andSize: size]; + [[OFNoMemException newWithObject: self + andSize: size] raise]; return NULL; } iter->next = NULL; iter->prev = __mem_pool; @@ -57,22 +58,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) { - [OFNoMemException newWithObject: self - andSize: size]; + [[OFNoMemException newWithObject: self + andSize: size] raise]; return NULL; } iter->ptr = ptr; return ptr; } } - [OFMemNotPartOfObjException newWithObject: self - andPointer: ptr]; + [[OFMemNotPartOfObjException newWithObject: self + andPointer: ptr] raise]; return NULL; } - (void)freeMem: (void*)ptr; { @@ -92,12 +93,12 @@ return; } } - [OFMemNotPartOfObjException newWithObject: self - andPointer: ptr]; + [[OFMemNotPartOfObjException newWithObject: self + andPointer: ptr] raise]; } - free { struct __ofobject_allocated_mem *iter, *iter2; Index: src/OFString.h ================================================================== --- src/OFString.h +++ src/OFString.h @@ -20,13 +20,14 @@ + newWithConstCString: (const char*)str; + newWithConstWideCString: (const wchar_t*)str; + newWithCString: (char*)str; + newWithWideCString: (wchar_t*)str; + - (char*)cString; - (wchar_t*)wcString; - (size_t)length; - (OFString*)setTo: (OFString*)str; - (OFString*)clone; -- (int)compare: (OFString*)str; +- (int)compareTo: (OFString*)str; - (OFString*)append: (OFString*)str; @end Index: src/OFString.m ================================================================== --- src/OFString.m +++ src/OFString.m @@ -40,19 +40,19 @@ return [[OFWideCString alloc] initWithWideCString: str]; } - (char*)cString { - [OFNotImplementedException newWithObject: self - andMethod: "cString"]; + [[OFNotImplementedException newWithObject: self + andSelector: @selector(cString)] raise]; return NULL; } - (wchar_t*)wcString { - [OFNotImplementedException newWithObject: self - andMethod: "wcString"]; + [[OFNotImplementedException newWithObject: self + andSelector: @selector(wcString)] raise]; return NULL; } - (size_t)length { @@ -66,24 +66,25 @@ return self; } - (OFString*)clone { - [OFNotImplementedException newWithObject: self - andMethod: "clone"]; + [[OFNotImplementedException newWithObject: self + andSelector: @selector(clone)] raise]; return nil; } -- (int)compare: (OFString*)str +- (int)compareTo: (OFString*)str { - [OFNotImplementedException newWithObject: self - andMethod: "compare:"]; + [[OFNotImplementedException newWithObject: self + andSelector: @selector(compareTo:)] + raise]; return 0; } - (OFString*)append: (OFString*)str { - [OFNotImplementedException newWithObject: self - andMethod: "append:"]; + [[OFNotImplementedException newWithObject: self + andSelector: @selector(append:)] raise]; return nil; } @end Index: src/OFWideCString.h ================================================================== --- src/OFWideCString.h +++ src/OFWideCString.h @@ -20,8 +20,8 @@ } - initWithWideCString: (wchar_t*)str; - (wchar_t*)wcString; - (OFString*)clone; -- (int)compare: (OFString*)str; +- (int)compareTo: (OFString*)str; - (OFString*)append: (OFString*)str; @end Index: src/OFWideCString.m ================================================================== --- src/OFWideCString.m +++ src/OFWideCString.m @@ -22,11 +22,12 @@ if (str == NULL) { length = 0; string = NULL; } else { length = wcslen(str); - string = [self getMem: (length + 1) * sizeof(wchar_t)]; + string = [self getMemWithSize: (length + 1) * + sizeof(wchar_t)]; wmemcpy(string, str, length + 1); } } return self; } @@ -39,11 +40,11 @@ - (OFString*)clone { return [OFString newWithWideCString: string]; } -- (int)compare: (OFString*)str +- (int)compareTo: (OFString*)str { return wcscmp(string, [str wcString]); } - (OFString*)append: (OFString*)str Index: tests/OFObject/OFObject.m ================================================================== --- tests/OFObject/OFObject.m +++ tests/OFObject/OFObject.m @@ -14,21 +14,23 @@ #import #import "OFObject.h" #import "OFExceptions.h" -#define CATCH_EXCEPTION(code, exception) \ - caught = false; \ - @try { \ - code; \ - } @catch (exception *e) { \ - caught = true; \ - puts("CAUGHT! Resuming..."); \ - } \ - if (!caught) { \ - puts("NOT CAUGHT!"); \ - return 1; \ +#define CATCH_EXCEPTION(code, exception) \ + caught = false; \ + @try { \ + code; \ + } @catch (exception *e) { \ + caught = true; \ + puts("CAUGHT! Error string was:"); \ + fputs([e string], stdout); \ + puts("Resuming..."); \ + } \ + if (!caught) { \ + puts("NOT CAUGHT!"); \ + return 1; \ } int main() { @@ -41,11 +43,11 @@ "exception)..."); CATCH_EXCEPTION([obj freeMem: NULL], OFMemNotPartOfObjException) /* Test allocating memory */ puts("Allocating memory through object..."); - p = [obj getMem: 4096]; + p = [obj getMemWithSize: 4096]; puts("Allocated 4096 bytes."); /* Test freeing the just allocated memory */ puts("Freeing just allocated memory..."); [obj freeMem: p]; @@ -55,13 +57,13 @@ puts("Trying to free it again (should throw an exception)..."); CATCH_EXCEPTION([obj freeMem: p], OFMemNotPartOfObjException) /* Test multiple memory chunks */ puts("Allocating 3 chunks of memory..."); - p = [obj getMem: 4096]; - q = [obj getMem: 4096]; - r = [obj getMem: 4096]; + p = [obj getMemWithSize: 4096]; + q = [obj getMemWithSize: 4096]; + r = [obj getMemWithSize: 4096]; puts("Allocated 3 * 4096 bytes."); /* Free them */ puts("Now freeing them..."); [obj freeMem: p]; @@ -75,18 +77,18 @@ CATCH_EXCEPTION([obj freeMem: q], OFMemNotPartOfObjException) CATCH_EXCEPTION([obj freeMem: r], OFMemNotPartOfObjException) puts("Got all 3!"); puts("Trying to allocate more memory than possible..."); - CATCH_EXCEPTION(p = [obj getMem: 4294967295U], OFNoMemException) + CATCH_EXCEPTION(p = [obj getMemWithSize: 4294967295U], OFNoMemException) puts("Allocating 1 byte..."); - p = [obj getMem: 1]; + p = [obj getMemWithSize: 1]; puts("Trying to resize that 1 byte to more than possible..."); CATCH_EXCEPTION(p = [obj resizeMem: p toSize: 4294967295U], OFNoMemException) /* TODO: Test if freeing object frees all memory */ return 0; } Index: tests/OFString/OFString.m ================================================================== --- tests/OFString/OFString.m +++ tests/OFString/OFString.m @@ -27,18 +27,18 @@ [s2 append: [OFString newWithConstCString: "123"]]; s3 = [s1 clone]; [s4 setTo: s2]; - if (![s1 compare: s3]) + if (![s1 compareTo: s3]) puts("s1 and s3 match! GOOD!"); else { puts("s1 and s3 don't match!"); return 1; } - if (![s2 compare: s4]) + if (![s2 compareTo: s4]) puts("s2 and s4 match! GOOD!"); else { puts("s1 and s3 don't match!"); return 1; } Index: tests/OFWideString/OFWideString.m ================================================================== --- tests/OFWideString/OFWideString.m +++ tests/OFWideString/OFWideString.m @@ -26,18 +26,18 @@ [s2 append: [OFString newWithConstWideCString: L"123"]]; s3 = [s1 clone]; [s4 setTo: s2]; - if (![s1 compare: s3]) + if (![s1 compareTo: s3]) puts("s1 and s3 match! GOOD!"); else { puts("s1 and s3 don't match!"); return 1; } - if (![s2 compare: s4]) + if (![s2 compareTo: s4]) puts("s2 and s4 match! GOOD!"); else { puts("s1 and s3 don't match!"); return 1; }