@@ -14,18 +14,25 @@ #include #include #include #import "OFDataArray.h" +#import "OFString.h" +#import "OFFile.h" #import "OFExceptions.h" #import "macros.h" @implementation OFDataArray + dataArrayWithItemSize: (size_t)is { return [[[self alloc] initWithItemSize: is] autorelease]; } + ++ dataArrayWithContentsOfFile: (OFString*)path +{ + return [[[self alloc] initWithContentsOfFile: path] autorelease]; +} - init { Class c = isa; [self release]; @@ -42,10 +49,43 @@ @throw [OFInvalidArgumentException newWithClass: isa selector: _cmd]; data = NULL; itemSize = is; + } @catch (id e) { + [self release]; + @throw e; + } + + return self; +} + +- initWithContentsOfFile: (OFString*)path +{ + self = [super init]; + + @try { + OFFile *file = [[OFFile alloc] initWithPath: path + mode: @"rb"]; + itemSize = 1; + + @try { + char *buf = [self allocMemoryWithSize: of_pagesize]; + + while (![file isAtEndOfStream]) { + size_t size; + + size = [file readNBytes: of_pagesize + intoBuffer: buf]; + [self addNItems: size + fromCArray: buf]; + } + + [self freeMemory: buf]; + } @finally { + [file release]; + } } @catch (id e) { [self release]; @throw e; }