Index: src/OFDataArray.h ================================================================== --- src/OFDataArray.h +++ src/OFDataArray.h @@ -9,10 +9,12 @@ * the packaging of this file. */ #import "OFObject.h" +@class OFString; + /** * \brief A class for storing arbitrary data in an array. * * If you plan to store large hunks of data, you should consider using * OFBigDataArray, which allocates the memory in pages rather than in bytes. @@ -35,19 +37,37 @@ * \param is The size of each element in the OFDataArray * \return A new autoreleased OFDataArray */ + dataArrayWithItemSize: (size_t)is; +/** + * Creates a new OFDataArary with an item size of 1, containing the data of the + * specified file. + * + * \param path The path of the file + * \return A new autoreleased OFDataArray + */ ++ dataArrayWithContentsOfFile: (OFString*)path; + /** * Initializes an already allocated OFDataArray whose items all have the same * size. * * \param is The size of each element in the OFDataArray * \return An initialized OFDataArray */ - initWithItemSize: (size_t)is; +/** + * Initializes an already allocated OFDataArray with an item size of 1, + * containing the data of the specified file. + * + * \param path The path of the file + * \return An initialized OFDataArray + */ +- initWithContentsOfFile: (OFString*)path; + /** * \return The number of items in the OFDataArray */ - (size_t)count; Index: src/OFDataArray.m ================================================================== --- src/OFDataArray.m +++ src/OFDataArray.m @@ -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; }