Index: src/OFFile.m ================================================================== --- src/OFFile.m +++ src/OFFile.m @@ -449,24 +449,42 @@ return (size_t)bytesWritten; } - (OFStreamOffset)lowlevelSeekToOffset: (OFStreamOffset)offset - whence: (int)whence + whence: (OFSeekWhence)whence { OFStreamOffset ret; if (_handle == OFInvalidFileHandle) @throw [OFNotOpenException exceptionWithObject: self]; #ifndef OF_AMIGAOS + int translatedWhence; + + switch (whence) { + case OFSeekSet: + translatedWhence = SEEK_SET; + break; + case OFSeekCurrent: + translatedWhence = SEEK_CUR; + break; + case OFSeekEnd: + translatedWhence = SEEK_END; + break; + default: + @throw [OFSeekFailedException exceptionWithStream: self + offset: offset + whence: whence + errNo: EINVAL]; + } # if defined(OF_WINDOWS) - ret = _lseeki64(_handle, offset, whence); + ret = _lseeki64(_handle, offset, translatedWhence); # elif defined(HAVE_LSEEK64) - ret = lseek64(_handle, offset, whence); + ret = lseek64(_handle, offset, translatedWhence); # else - ret = lseek(_handle, offset, whence); + ret = lseek(_handle, offset, translatedWhence); # endif if (ret == -1) @throw [OFSeekFailedException exceptionWithStream: self offset: offset @@ -474,17 +492,17 @@ errNo: errno]; #else LONG translatedWhence; switch (whence) { - case SEEK_SET: + case OFSeekSet: translatedWhence = OFFSET_BEGINNING; break; - case SEEK_CUR: + case OFSeekCurrent: translatedWhence = OFFSET_CURRENT; break; - case SEEK_END: + case OFSeekEnd: translatedWhence = OFFSET_END; break; default: @throw [OFSeekFailedException exceptionWithStream: self offset: offset Index: src/OFLHAArchive.m ================================================================== --- src/OFLHAArchive.m +++ src/OFLHAArchive.m @@ -110,11 +110,11 @@ ![_stream isKindOfClass: [OFSeekableStream class]]) @throw [OFInvalidArgumentException exception]; if (_mode == modeAppend) [(OFSeekableStream *)_stream seekToOffset: 0 - whence: SEEK_END]; + whence: OFSeekEnd]; _encoding = OFStringEncodingISO8859_1; } @catch (id e) { [self release]; @throw e; @@ -398,11 +398,11 @@ } if ([stream isKindOfClass: [OFSeekableStream class]] && (sizeof(OFStreamOffset) > 4 || toRead != (OFStreamOffset)toRead)) [(OFSeekableStream *)stream seekToOffset: (OFStreamOffset)toRead - whence: SEEK_CUR]; + whence: OFSeekCurrent]; else { while (toRead > 0) { char buffer[512]; unsigned long long min = toRead; @@ -444,11 +444,11 @@ @try { _entry = [entry mutableCopy]; _encoding = encoding; - _headerOffset = [stream seekToOffset: 0 whence: SEEK_CUR]; + _headerOffset = [stream seekToOffset: 0 whence: OFSeekCurrent]; [_entry of_writeToStream: stream encoding: _encoding]; /* * Retain stream last, so that -[close] called by -[dealloc] * doesn't write in case of an error. @@ -523,16 +523,16 @@ _entry.uncompressedSize = _bytesWritten; _entry.compressedSize = _bytesWritten; _entry.CRC16 = _CRC16; - offset = [_stream seekToOffset: 0 whence: SEEK_CUR]; - [_stream seekToOffset: _headerOffset whence: SEEK_SET]; + offset = [_stream seekToOffset: 0 whence: OFSeekCurrent]; + [_stream seekToOffset: _headerOffset whence: OFSeekSet]; [_entry of_writeToStream: _stream encoding: _encoding]; - [_stream seekToOffset: offset whence: SEEK_SET]; + [_stream seekToOffset: offset whence: OFSeekSet]; [_stream release]; _stream = nil; [super close]; } @end Index: src/OFMemoryStream.m ================================================================== --- src/OFMemoryStream.m +++ src/OFMemoryStream.m @@ -100,22 +100,22 @@ { return (_position == _size); } - (OFStreamOffset)lowlevelSeekToOffset: (OFStreamOffset)offset - whence: (int)whence + whence: (OFSeekWhence)whence { OFStreamOffset new; switch (whence) { - case SEEK_SET: + case OFSeekSet: new = offset; break; - case SEEK_CUR: + case OFSeekCurrent: new = (OFStreamOffset)_position + offset; break; - case SEEK_END: + case OFSeekEnd: new = (OFStreamOffset)_size + offset; break; default: @throw [OFInvalidArgumentException exception]; } Index: src/OFSeekableStream.h ================================================================== --- src/OFSeekableStream.h +++ src/OFSeekableStream.h @@ -27,10 +27,12 @@ #endif #import "OFStream.h" OF_ASSUME_NONNULL_BEGIN + +/** @file */ #if defined(OF_WINDOWS) typedef __int64 OFStreamOffset; #elif defined(OF_ANDROID) typedef long long OFStreamOffset; @@ -40,10 +42,22 @@ typedef off64_t OFStreamOffset; #else typedef off_t OFStreamOffset; #endif +/** + * @brief From where to seek. + */ +typedef enum { + /** Seek to the end of the stream + offset. */ + OFSeekSet, + /** Seek to the current location + offset. */ + OFSeekCurrent, + /** Seek to the specified byte. */ + OFSeekEnd +} OFSeekWhence; + /** * @class OFSeekableStream OFSeekableStream.h ObjFW/OFSeekableStream.h * * @brief A stream that supports seeking. * @@ -60,20 +74,15 @@ /** * @brief Seeks to the specified offset. * * @param offset The offset in bytes - * @param whence From where to seek.@n - * Possible values are: - * Value | Description - * -----------|--------------------------------------- - * `SEEK_SET` | Seek to the specified byte - * `SEEK_CUR` | Seek to the current location + offset - * `SEEK_END` | Seek to the end of the stream + offset + * @param whence From where to seek. * @return The new offset form the start of the file */ -- (OFStreamOffset)seekToOffset: (OFStreamOffset)offset whence: (int)whence; +- (OFStreamOffset)seekToOffset: (OFStreamOffset)offset + whence: (OFSeekWhence)whence; /** * @brief Seek the stream on the lowlevel. * * @warning Do not call this directly! @@ -80,19 +89,13 @@ * * @note Override this method with your actual seek implementation when * subclassing! * * @param offset The offset to seek to - * @param whence From where to seek.@n - * Possible values are: - * Value | Description - * -----------|--------------------------------------- - * `SEEK_SET` | Seek to the specified byte - * `SEEK_CUR` | Seek to the current location + offset - * `SEEK_END` | Seek to the end of the stream + offset + * @param whence From where to seek. * @return The new offset from the start of the file */ - (OFStreamOffset)lowlevelSeekToOffset: (OFStreamOffset)offset - whence: (int)whence; + whence: (OFSeekWhence)whence; @end OF_ASSUME_NONNULL_END Index: src/OFSeekableStream.m ================================================================== --- src/OFSeekableStream.m +++ src/OFSeekableStream.m @@ -37,18 +37,19 @@ return [super init]; } - (OFStreamOffset)lowlevelSeekToOffset: (OFStreamOffset)offset - whence: (int)whence + whence: (OFSeekWhence)whence { OF_UNRECOGNIZED_SELECTOR } -- (OFStreamOffset)seekToOffset: (OFStreamOffset)offset whence: (int)whence +- (OFStreamOffset)seekToOffset: (OFStreamOffset)offset + whence: (OFSeekWhence)whence { - if (whence == SEEK_CUR) + if (whence == OFSeekCurrent) offset -= _readBufferLength; offset = [self lowlevelSeekToOffset: offset whence: whence]; OFFreeMemory(_readBufferMemory); Index: src/OFTarArchive.m ================================================================== --- src/OFTarArchive.m +++ src/OFTarArchive.m @@ -98,11 +98,11 @@ if (![_stream isKindOfClass: [OFSeekableStream class]]) @throw [OFInvalidArgumentException exception]; [(OFSeekableStream *)_stream seekToOffset: -1024 - whence: SEEK_END]; + whence: OFSeekEnd]; [_stream readIntoBuffer: buffer exactLength: 1024]; for (size_t i = 0; i < 1024 / sizeof(uint32_t); i++) if (buffer[i] != 0) empty = false; @@ -109,11 +109,11 @@ if (!empty) @throw [OFInvalidFormatException exception]; [(OFSeekableStream *)stream seekToOffset: -1024 - whence: SEEK_END]; + whence: OFSeekEnd]; } _encoding = OFStringEncodingUTF8; } @catch (id e) { [self release]; @@ -363,20 +363,20 @@ (OFStreamOffset)_toRead == (long long)_toRead) { unsigned long long size; [(OFSeekableStream *)_stream seekToOffset: (OFStreamOffset)_toRead - whence: SEEK_CUR]; + whence: OFSeekCurrent]; _toRead = 0; size = _entry.size; if (size % 512 != 0) [(OFSeekableStream *)_stream seekToOffset: 512 - (size % 512) - whence: SEEK_CUR]; + whence: OFSeekCurrent]; } else { char buffer[512]; unsigned long long size; while (_toRead >= 512) { Index: src/OFZIPArchive.m ================================================================== --- src/OFZIPArchive.m +++ src/OFZIPArchive.m @@ -140,11 +140,11 @@ return field; } static void seekOrThrowInvalidFormat(OFSeekableStream *stream, - OFStreamOffset offset, int whence) + OFStreamOffset offset, OFSeekWhence whence) { @try { [stream seekToOffset: offset whence: whence]; } @catch (OFSeekFailedException *e) { if (e.errNo == EINVAL) @@ -199,11 +199,11 @@ } if (_mode == modeAppend) { _offset = _centralDirectoryOffset; seekOrThrowInvalidFormat((OFSeekableStream *)_stream, - (OFStreamOffset)_offset, SEEK_SET); + (OFStreamOffset)_offset, OFSeekSet); } } @catch (id e) { /* * If we are in write or append mode, we do not want -[close] * to write anything to it on error - after all, it might not @@ -262,11 +262,11 @@ OFStreamOffset offset = -22; bool valid = false; do { seekOrThrowInvalidFormat((OFSeekableStream *)_stream, - offset, SEEK_END); + offset, OFSeekEnd); if ([_stream readLittleEndianInt32] == 0x06054B50) { valid = true; break; } @@ -295,11 +295,11 @@ _centralDirectoryOffset == 0xFFFFFFFF) { int64_t offset64; uint64_t size; seekOrThrowInvalidFormat((OFSeekableStream *)_stream, - offset - 20, SEEK_END); + offset - 20, OFSeekEnd); if ([_stream readLittleEndianInt32] != 0x07064B50) { objc_autoreleasePoolPop(pool); return; } @@ -313,11 +313,11 @@ if (offset64 < 0 || (OFStreamOffset)offset64 != offset64) @throw [OFOutOfRangeException exception]; seekOrThrowInvalidFormat((OFSeekableStream *)_stream, - (OFStreamOffset)offset64, SEEK_SET); + (OFStreamOffset)offset64, OFSeekSet); if ([_stream readLittleEndianInt32] != 0x06064B50) @throw [OFInvalidFormatException exception]; size = [_stream readLittleEndianInt64]; @@ -353,11 +353,11 @@ if (_centralDirectoryOffset < 0 || (OFStreamOffset)_centralDirectoryOffset != _centralDirectoryOffset) @throw [OFOutOfRangeException exception]; seekOrThrowInvalidFormat((OFSeekableStream *)_stream, - (OFStreamOffset)_centralDirectoryOffset, SEEK_SET); + (OFStreamOffset)_centralDirectoryOffset, OFSeekSet); for (size_t i = 0; i < _centralDirectoryEntries; i++) { OFZIPArchiveEntry *entry = [[[OFZIPArchiveEntry alloc] of_initWithStream: _stream] autorelease]; @@ -449,11 +449,11 @@ offset64 = entry.of_localFileHeaderOffset; if (offset64 < 0 || (OFStreamOffset)offset64 != offset64) @throw [OFOutOfRangeException exception]; seekOrThrowInvalidFormat((OFSeekableStream *)_stream, - (OFStreamOffset)offset64, SEEK_SET); + (OFStreamOffset)offset64, OFSeekSet); localFileHeader = [[[OFZIPArchiveLocalFileHeader alloc] initWithStream: _stream] autorelease]; if (![localFileHeader matchesEntry: entry]) @throw [OFInvalidFormatException exception]; Index: src/exceptions/OFSeekFailedException.h ================================================================== --- src/exceptions/OFSeekFailedException.h +++ src/exceptions/OFSeekFailedException.h @@ -26,11 +26,12 @@ */ @interface OFSeekFailedException: OFException { OFSeekableStream *_stream; OFStreamOffset _offset; - int _whence, _errNo; + OFSeekWhence _whence; + int _errNo; } /** * @brief The stream for which seeking failed. */ @@ -42,11 +43,11 @@ @property (readonly, nonatomic) OFStreamOffset offset; /** * @brief To what the offset is relative. */ -@property (readonly, nonatomic) int whence; +@property (readonly, nonatomic) OFSeekWhence whence; /** * @brief The errno of the error that occurred. */ @property (readonly, nonatomic) int errNo; @@ -60,11 +61,11 @@ * @param errNo The errno of the error that occurred * @return A new, autoreleased seek failed exception */ + (instancetype)exceptionWithStream: (OFSeekableStream *)stream offset: (OFStreamOffset)offset - whence: (int)whence + whence: (OFSeekWhence)whence errNo: (int)errNo; + (instancetype)exception OF_UNAVAILABLE; /** @@ -76,12 +77,12 @@ * @param errNo The errno of the error that occurred * @return An initialized seek failed exception */ - (instancetype)initWithStream: (OFSeekableStream *)stream offset: (OFStreamOffset)offset - whence: (int)whence + whence: (OFSeekWhence)whence errNo: (int)errNo OF_DESIGNATED_INITIALIZER; - (instancetype)init OF_UNAVAILABLE; @end OF_ASSUME_NONNULL_END Index: src/exceptions/OFSeekFailedException.m ================================================================== --- src/exceptions/OFSeekFailedException.m +++ src/exceptions/OFSeekFailedException.m @@ -28,11 +28,11 @@ OF_UNRECOGNIZED_SELECTOR } + (instancetype)exceptionWithStream: (OFSeekableStream *)stream offset: (OFStreamOffset)offset - whence: (int)whence + whence: (OFSeekWhence)whence errNo: (int)errNo { return [[[self alloc] initWithStream: stream offset: offset whence: whence @@ -44,11 +44,11 @@ OF_INVALID_INIT_METHOD } - (instancetype)initWithStream: (OFSeekableStream *)stream offset: (OFStreamOffset)offset - whence: (int)whence + whence: (OFSeekWhence)whence errNo: (int)errNo { self = [super init]; _stream = [stream retain]; Index: tests/OFMemoryStreamTests.m ================================================================== --- tests/OFMemoryStreamTests.m +++ tests/OFMemoryStreamTests.m @@ -47,17 +47,17 @@ memcmp(buffer, "ijkl", 5) == 0) TEST(@"-[lowlevelIsAtEndOfStream]", [stream lowlevelIsAtEndOfStream]) TEST(@"-[lowlevelSeekToOffset:whence:]", - [stream lowlevelSeekToOffset: 0 whence: SEEK_CUR] == + [stream lowlevelSeekToOffset: 0 whence: OFSeekCurrent] == sizeof(string) && [stream lowlevelIsAtEndOfStream] && - [stream lowlevelSeekToOffset: 4 whence: SEEK_SET] == 4 && + [stream lowlevelSeekToOffset: 4 whence: OFSeekSet] == 4 && ![stream lowlevelIsAtEndOfStream] && [stream lowlevelReadIntoBuffer: buffer length: 10] == 9 && memcmp(buffer, "efghijkl", 9) == 0 && - [stream lowlevelSeekToOffset: -2 whence: SEEK_END] == 11 && + [stream lowlevelSeekToOffset: -2 whence: OFSeekEnd] == 11 && [stream lowlevelReadIntoBuffer: buffer length: 10] == 2 && memcmp(buffer, "l", 2) == 0 && [stream lowlevelReadIntoBuffer: buffer length: 10] == 0) EXPECT_EXCEPTION(@"Writes rejected on read-only stream", @@ -71,11 +71,11 @@ TEST(@"-[lowlevelWriteBuffer:length:]", [stream lowlevelWriteBuffer: "abcde" length: 5] == 5 && [stream lowlevelWriteBuffer: "fgh" length: 3] == 3 && [stream lowlevelWriteBuffer: "ijkl" length: 5] == 5 && memcmp(data.items, string, data.count) == 0 && - [stream lowlevelSeekToOffset: -3 whence: SEEK_END] == 10) + [stream lowlevelSeekToOffset: -3 whence: OFSeekEnd] == 10) EXPECT_EXCEPTION(@"Out of bound writes rejected", OFWriteFailedException, [stream lowlevelWriteBuffer: "xyz" length: 4])