@@ -13,10 +13,12 @@ * file. */ #include "config.h" +#define _LARGEFILE64_SOURCE + #include #include #ifdef HAVE_FCNTL_H # include @@ -75,35 +77,39 @@ #endif #ifndef OF_AMIGAOS # define closeHandle(h) close(h) #else -static struct of_file_handle { - of_file_handle_t previous, next; +static struct _OFFileHandle { + struct _OFFileHandle *previous, *next; BPTR handle; bool append; } *firstHandle = NULL; static void -closeHandle(of_file_handle_t handle) +closeHandle(OFFileHandle handle) { Close(handle->handle); + + Forbid(); if (handle->previous != NULL) handle->previous->next = handle->next; if (handle->next != NULL) handle->next->previous = handle->previous; if (firstHandle == handle) firstHandle = handle->next; - free(handle); + Permit(); + + OFFreeMemory(handle); } OF_DESTRUCTOR() { - for (of_file_handle_t iter = firstHandle; iter != NULL; + for (OFFileHandle iter = firstHandle; iter != NULL; iter = iter->next) Close(iter->handle); } #endif @@ -178,38 +184,33 @@ @throw [OFInitializationFailedException exceptionWithClass: self]; #endif } -+ (instancetype)fileWithPath: (OFString *)path - mode: (OFString *)mode -{ - return [[[self alloc] initWithPath: path - mode: mode] autorelease]; -} - -+ (instancetype)fileWithURL: (OFURL *)URL - mode: (OFString *)mode -{ - return [[[self alloc] initWithURL: URL - mode: mode] autorelease]; -} - -+ (instancetype)fileWithHandle: (of_file_handle_t)handle ++ (instancetype)fileWithPath: (OFString *)path mode: (OFString *)mode +{ + return [[[self alloc] initWithPath: path mode: mode] autorelease]; +} + ++ (instancetype)fileWithURL: (OFURL *)URL mode: (OFString *)mode +{ + return [[[self alloc] initWithURL: URL mode: mode] autorelease]; +} + ++ (instancetype)fileWithHandle: (OFFileHandle)handle { return [[[self alloc] initWithHandle: handle] autorelease]; } - (instancetype)init { OF_INVALID_INIT_METHOD } -- (instancetype)initWithPath: (OFString *)path - mode: (OFString *)mode +- (instancetype)initWithPath: (OFString *)path mode: (OFString *)mode { - of_file_handle_t handle; + OFFileHandle handle; @try { void *pool = objc_autoreleasePoolPush(); int flags; @@ -239,11 +240,11 @@ @throw [OFOpenItemFailedException exceptionWithPath: path mode: mode errNo: errno]; #else - handle = of_alloc(1, sizeof(*handle)); + handle = OFAllocMemory(1, sizeof(*handle)); @try { if ((flags = parseMode(mode.UTF8String, &handle->append)) == -1) @throw [OFInvalidArgumentException exception]; @@ -292,20 +293,24 @@ exceptionWithPath: path mode: mode errNo: EIO]; } } + + Forbid(); handle->previous = NULL; handle->next = firstHandle; if (firstHandle != NULL) firstHandle->previous = handle; firstHandle = handle; + + Permit(); } @catch (id e) { - free(handle); + OFFreeMemory(handle); @throw e; } #endif objc_autoreleasePoolPop(pool); @@ -322,12 +327,11 @@ } return self; } -- (instancetype)initWithURL: (OFURL *)URL - mode: (OFString *)mode +- (instancetype)initWithURL: (OFURL *)URL mode: (OFString *)mode { void *pool = objc_autoreleasePoolPush(); OFString *fileSystemRepresentation; @try { @@ -335,19 +339,18 @@ } @catch (id e) { [self release]; @throw e; } - self = [self initWithPath: fileSystemRepresentation - mode: mode]; + self = [self initWithPath: fileSystemRepresentation mode: mode]; objc_autoreleasePoolPop(pool); return self; } -- (instancetype)initWithHandle: (of_file_handle_t)handle +- (instancetype)initWithHandle: (OFFileHandle)handle { self = [super init]; _handle = handle; @@ -354,22 +357,21 @@ return self; } - (bool)lowlevelIsAtEndOfStream { - if (_handle == OF_INVALID_FILE_HANDLE) + if (_handle == OFInvalidFileHandle) @throw [OFNotOpenException exceptionWithObject: self]; return _atEndOfStream; } -- (size_t)lowlevelReadIntoBuffer: (void *)buffer - length: (size_t)length +- (size_t)lowlevelReadIntoBuffer: (void *)buffer length: (size_t)length { ssize_t ret; - if (_handle == OF_INVALID_FILE_HANDLE) + if (_handle == OFInvalidFileHandle) @throw [OFNotOpenException exceptionWithObject: self]; #if defined(OF_WINDOWS) if (length > UINT_MAX) @throw [OFOutOfRangeException exception]; @@ -397,14 +399,13 @@ _atEndOfStream = true; return ret; } -- (size_t)lowlevelWriteBuffer: (const void *)buffer - length: (size_t)length +- (size_t)lowlevelWriteBuffer: (const void *)buffer length: (size_t)length { - if (_handle == OF_INVALID_FILE_HANDLE) + if (_handle == OFInvalidFileHandle) @throw [OFNotOpenException exceptionWithObject: self]; #if defined(OF_WINDOWS) int bytesWritten; @@ -456,16 +457,15 @@ #endif return (size_t)bytesWritten; } -- (of_offset_t)lowlevelSeekToOffset: (of_offset_t)offset - whence: (int)whence +- (OFFileOffset)lowlevelSeekToOffset: (OFFileOffset)offset whence: (int)whence { - of_offset_t ret; + OFFileOffset ret; - if (_handle == OF_INVALID_FILE_HANDLE) + if (_handle == OFInvalidFileHandle) @throw [OFNotOpenException exceptionWithObject: self]; #ifndef OF_AMIGAOS # if defined(OF_WINDOWS) ret = _lseeki64(_handle, offset, whence); @@ -531,22 +531,22 @@ } #endif - (void)close { - if (_handle == OF_INVALID_FILE_HANDLE) + if (_handle == OFInvalidFileHandle) @throw [OFNotOpenException exceptionWithObject: self]; closeHandle(_handle); - _handle = OF_INVALID_FILE_HANDLE; + _handle = OFInvalidFileHandle; [super close]; } - (void)dealloc { - if (_handle != OF_INVALID_FILE_HANDLE) + if (_handle != OFInvalidFileHandle) [self close]; [super dealloc]; } @end