Index: src/OFFile.h ================================================================== --- src/OFFile.h +++ src/OFFile.h @@ -11,15 +11,16 @@ #import #import #import "OFObject.h" +#import "OFStream.h" /** * The OFFile class provides functions to read, write and manipulate files. */ -@interface OFFile: OFObject +@interface OFFile: OFObject { FILE *fp; } /** @@ -106,32 +107,61 @@ * The buffer MUST be at least size * nitems big! * \param nitems nitem The number of items to read * The buffer MUST be at least size * nitems big! * \return The number of bytes read */ -- (size_t)readIntoBuffer: (uint8_t*)buf - withSize: (size_t)size - andNItems: (size_t)nitems; +- (size_t)readNItems: (size_t)nitems + ofSize: (size_t)size + intoBuffer: (uint8_t*)buf; + +/** + * Reads from the file into a buffer. + * + * \param buf The buffer into which the data is read + * \param size The size of the data that should be read. + * The buffer MUST be at least size big! + * \return The number of bytes read + */ +- (size_t)readNBytes: (size_t)size + intoBuffer: (uint8_t*)buf; /** * Reads from the file into a new buffer. * * \param size The size of the data that should be read * \param nitem The number of items to read * \return A new buffer with the data read. * It is part of the memory pool of the OFFile. */ -- (uint8_t*)readWithSize: (size_t)size - andNItems: (size_t)nitems; +- (uint8_t*)readNItems: (size_t)nitems + ofSize: (size_t)size; +/** + * Reads from the file into a new buffer. + * + * \param size The size of the data that should be read + * \return A new buffer with the data read. + * It is part of the memory pool of the OFFile. + */ +- (uint8_t*)readNBytes: (size_t)size; /** * Writes from a buffer into the file. * * \param buf The buffer from which the data is written to the file * \param size The size of the data that should be written * \param nitem The number of items to write * \return The number of bytes written */ -- (size_t)writeBuffer: (uint8_t*)buf - withSize: (size_t)size - andNItems: (size_t)nitems; +- (size_t)writeNItems: (size_t)nitems + ofSize: (size_t)size + fromBuffer: (uint8_t*)buf; + +/** + * Writes from a buffer into the file. + * + * \param buf The buffer from which the data is written to the file + * \param size The size of the data that should be written + * \return The number of bytes written + */ +- (size_t)writeNBytes: (size_t)size + fromBuffer: (uint8_t*)buf; @end Index: src/OFFile.m ================================================================== --- src/OFFile.m +++ src/OFFile.m @@ -84,13 +84,13 @@ - (BOOL)atEndOfFile { return (feof(fp) == 0 ? NO : YES); } -- (size_t)readIntoBuffer: (uint8_t*)buf - withSize: (size_t)size - andNItems: (size_t)nitems +- (size_t)readNItems: (size_t)nitems + ofSize: (size_t)size + intoBuffer: (uint8_t*)buf { size_t ret; if ((ret = fread(buf, size, nitems, fp)) == 0 && !feof(fp)) [[OFReadFailedException newWithObject: self @@ -98,34 +98,48 @@ andNItems: nitems] raise]; return ret; } -- (uint8_t*)readWithSize: (size_t)size - andNItems: (size_t)nitems +- (size_t)readNBytes: (size_t)size + intoBuffer: (uint8_t*)buf +{ + return [self readNItems: size + ofSize: 1 + intoBuffer: buf]; +} + +- (uint8_t*)readNItems: (size_t)nitems + ofSize: (size_t)size { uint8_t *ret; ret = [self getMemForNItems: nitems ofSize: size]; @try { - [self readIntoBuffer: ret - withSize: size - andNItems: nitems]; + [self readNItems: nitems + ofSize: size + intoBuffer: ret]; } @catch (OFReadFailedException *e) { [self freeMem: ret]; @throw e; return NULL; } return ret; } -- (size_t)writeBuffer: (uint8_t*)buf - withSize: (size_t)size - andNItems: (size_t)nitems +- (uint8_t*)readNBytes: (size_t)size +{ + return [self readNItems: size + ofSize: 1]; +} + +- (size_t)writeNItems: (size_t)nitems + ofSize: (size_t)size + fromBuffer: (uint8_t*)buf { size_t ret; if ((ret = fwrite(buf, size, nitems, fp)) == 0 && size != 0 && nitems != 0) @@ -133,6 +147,14 @@ andSize: size andNItems: nitems] raise]; return ret; } + +- (size_t)writeNBytes: (size_t)size + fromBuffer: (uint8_t*)buf +{ + return [self writeNItems: size + ofSize: 1 + fromBuffer: buf]; +} @end ADDED src/OFStream.h Index: src/OFStream.h ================================================================== --- src/OFStream.h +++ src/OFStream.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2008 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of libobjfw. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE included in + * the packaging of this file. + */ + +/** + * The OFStream protocol provides functions to read and write streams. + */ +@protocol OFStream +/** + * Reads from the stream into a buffer. + * + * \param buf The buffer into which the data is read + * \param size The size of the data that should be read. + * The buffer MUST be at least size big! + * \return The number of bytes read + */ +- (size_t)readNBytes: (size_t)size + intoBuffer: (uint8_t*)buf; + +/** + * Reads from the stream into a new buffer. + * + * \param size The size of the data that should be read + * \return A new buffer with the data read. + * It is part of the memory pool of the OFFile. + */ +- (uint8_t*)readNBytes: (size_t)size; + +/** + * Writes from a buffer into the file. + * + * \param buf The buffer from which the data is written to the file + * \param size The size of the data that should be written + * \return The number of bytes written + */ +- (size_t)writeNBytes: (size_t)size + fromBuffer: (uint8_t*)buf; +@end Index: tests/OFHashes/OFHashes.m ================================================================== --- tests/OFHashes/OFHashes.m +++ tests/OFHashes/OFHashes.m @@ -33,13 +33,12 @@ OFSHA1Hash *sha1 = [OFSHA1Hash new]; OFFile *f = [OFFile newWithPath: "testfile" andMode: "rb"]; while (![f atEndOfFile]) { - len = [f readIntoBuffer: buf - withSize: 1 - andNItems: 64]; + len = [f readNBytes: 64 + intoBuffer: buf]; [md5 updateWithBuffer: buf ofSize: len]; [sha1 updateWithBuffer: buf ofSize: len]; }