Index: src/OFFile.h ================================================================== --- src/OFFile.h +++ src/OFFile.h @@ -34,10 +34,14 @@ typedef struct stat64 of_stat_t; #else typedef struct stat of_stat_t; #endif +#if defined(OF_MORPHOS) && !defined(OF_IXEMUL) +typedef long BPTR; +#endif + /*! * @class OFFile OFFile.h ObjFW/OFFile.h * * @brief A class which provides methods to read and write files. */ @@ -44,11 +48,11 @@ @interface OFFile: OFSeekableStream { #if !defined(OF_MORPHOS) || defined(OF_IXEMUL) int _fd; #else - long _fd; + BPTR _handle; bool _append; #endif bool _atEndOfStream; } @@ -75,18 +79,29 @@ * @return A new autoreleased OFFile */ + (instancetype)fileWithPath: (OFString *)path mode: (OFString *)mode; +#if !defined(OF_MORPHOS) || defined(OF_IXEMUL) /*! * @brief Creates a new OFFile with the specified file descriptor. * * @param fd A file descriptor, returned from for example open(). - * It is not closed when the OFFile object is deallocated! + * It is closed when the OFFile object is deallocated! * @return A new autoreleased OFFile */ + (instancetype)fileWithFileDescriptor: (int)fd; +#else +/*! + * @brief Creates a new OFFile with the specified handle. + * + * @param handle A handle, returned from for example Open(). + * It is closed when the OFFile object is deallocated! + * @return A new autoreleased OFFile + */ ++ (instancetype)fileWithHandle: (BPTR)handle; +#endif - init OF_UNAVAILABLE; /*! * @brief Initializes an already allocated OFFile. @@ -111,17 +126,29 @@ * @return An initialized OFFile */ - initWithPath: (OFString *)path mode: (OFString *)mode; +#if !defined(OF_MORPHOS) || defined(OF_IXEMUL) /*! * @brief Initializes an already allocated OFFile. * * @param fd A file descriptor, returned from for example open(). - * It is not closed when the OFFile object is deallocated! + * It is closed when the OFFile object is deallocated! + * @return An initialized OFFile */ - initWithFileDescriptor: (int)fd; +#else +/*! + * @brief Initializes an already allocated OFFile. + * + * @param handle A handle, returned from for example Open(). + * It is closed when the OFFile object is deallocated! + * @return An initialized OFFile + */ +- initWithHandle: (BPTR)handle; +#endif @end #ifdef __cplusplus extern "C" { #endif Index: src/OFFile.m ================================================================== --- src/OFFile.m +++ src/OFFile.m @@ -41,12 +41,10 @@ #if defined(OF_MORPHOS) && !defined(OF_IXEMUL) # define BOOL EXEC_BOOL # include # undef BOOL -# define INVALID_FD 0 -# define close(fd) Close(fd) #endif #ifdef OF_WII # define BOOL OGC_BOOL # include @@ -56,14 +54,10 @@ #ifdef OF_NINTENDO_DS # include # include #endif -#ifndef INVALID_FD -# define INVALID_FD -1 -#endif - #ifndef O_BINARY # define O_BINARY 0 #endif #ifndef O_CLOEXEC # define O_CLOEXEC 0 @@ -198,15 +192,21 @@ { return [[[self alloc] initWithPath: path mode: mode] autorelease]; } -+ (instancetype)fileWithFileDescriptor: (int)filedescriptor +#if !defined(OF_MORPHOS) || defined(OF_IXEMUL) ++ (instancetype)fileWithFileDescriptor: (int)fd +{ + return [[[self alloc] initWithFileDescriptor: fd] autorelease]; +} +#else ++ (instancetype)fileWithHandle: (BPTR)handle { - return [[[self alloc] - initWithFileDescriptor: filedescriptor] autorelease]; + return [[[self alloc] initWithHandle: handle] autorelease]; } +#endif - init { OF_INVALID_INIT_METHOD } @@ -241,18 +241,18 @@ errNo: errno]; #else if ((flags = parseMode([mode UTF8String], &_append)) == -1) @throw [OFInvalidArgumentException exception]; - if ((_fd = Open([path cStringWithEncoding: - [OFLocalization encoding]], flags)) == INVALID_FD) + if ((_handle = Open([path cStringWithEncoding: + [OFLocalization encoding]], flags)) == 0) @throw [OFOpenItemFailedException exceptionWithPath: path mode: mode]; if (_append) { - if (Seek64(_fd, 0, OFFSET_END) == -1) + if (Seek64(_handle, 0, OFFSET_END) == -1) @throw [OFOpenItemFailedException exceptionWithPath: path mode: mode]; } #endif @@ -262,56 +262,78 @@ } return self; } +#if !defined(OF_MORPHOS) || defined(OF_IXEMUL) - initWithFileDescriptor: (int)fd { self = [super init]; _fd = fd; return self; } +#else +- initWithHandle: (BPTR)handle +{ + self = [super init]; + + _handle = handle; + + return self; +} +#endif - (bool)lowlevelIsAtEndOfStream { - if (_fd == INVALID_FD) +#if !defined(OF_MORPHOS) || defined(OF_IXEMUL) + if (_fd == -1) + return true; +#else + if (_handle == 0) return true; +#endif return _atEndOfStream; } - (size_t)lowlevelReadIntoBuffer: (void *)buffer length: (size_t)length { ssize_t ret; - if (_fd == INVALID_FD || _atEndOfStream) +#if !defined(OF_MORPHOS) || defined(OF_IXEMUL) + if (_fd == -1 || _atEndOfStream) @throw [OFReadFailedException exceptionWithObject: self requestedLength: length]; -#if defined(OF_WINDOWS) +# ifndef OF_WINDOWS + if ((ret = read(_fd, buffer, length)) < 0) + @throw [OFReadFailedException exceptionWithObject: self + requestedLength: length + errNo: errno]; +# else if (length > UINT_MAX) @throw [OFOutOfRangeException exception]; if ((ret = read(_fd, buffer, (unsigned int)length)) < 0) @throw [OFReadFailedException exceptionWithObject: self requestedLength: length errNo: errno]; -#elif defined(OF_MORPHOS) && !defined(OF_IXEMUL) +# endif +#else + if (_handle == 0 || _atEndOfStream) + @throw [OFReadFailedException exceptionWithObject: self + requestedLength: length]; + if (length > LONG_MAX) @throw [OFOutOfRangeException exception]; - if ((ret = Read(_fd, buffer, length)) < 0) + if ((ret = Read(_handle, buffer, length)) < 0) @throw [OFReadFailedException exceptionWithObject: self requestedLength: length]; -#else - if ((ret = read(_fd, buffer, length)) < 0) - @throw [OFReadFailedException exceptionWithObject: self - requestedLength: length - errNo: errno]; #endif if (ret == 0) _atEndOfStream = true; @@ -319,58 +341,62 @@ } - (void)lowlevelWriteBuffer: (const void *)buffer length: (size_t)length { - if (_fd == INVALID_FD || _atEndOfStream) +#if !defined(OF_MORPHOS) || defined(OF_IXEMUL) + if (_fd == -1 || _atEndOfStream) @throw [OFWriteFailedException exceptionWithObject: self requestedLength: length]; +# ifndef OF_WINDOWS + if (length > SSIZE_MAX) + @throw [OFOutOfRangeException exception]; -#if defined(OF_WINDOWS) + if (write(_fd, buffer, length) != (ssize_t)length) + @throw [OFWriteFailedException exceptionWithObject: self + requestedLength: length + errNo: errno]; +# else if (length > INT_MAX) @throw [OFOutOfRangeException exception]; if (write(_fd, buffer, (int)length) != (int)length) @throw [OFWriteFailedException exceptionWithObject: self requestedLength: length errNo: errno]; -#elif defined(OF_MORPHOS) && !defined(OF_IXEMUL) +# endif +#else + if (_handle == 0 || _atEndOfStream) + @throw [OFWriteFailedException exceptionWithObject: self + requestedLength: length]; if (length > LONG_MAX) @throw [OFOutOfRangeException exception]; if (_append) { - if (Seek64(_fd, 0, OFFSET_END) == -1) + if (Seek64(_handle, 0, OFFSET_END) == -1) @throw [OFWriteFailedException exceptionWithObject: self requestedLength: length]; } - if (Write(_fd, (void *)buffer, length) != (LONG)length) + if (Write(_handle, (void *)buffer, length) != (LONG)length) @throw [OFWriteFailedException exceptionWithObject: self requestedLength: length]; -#else - if (length > SSIZE_MAX) - @throw [OFOutOfRangeException exception]; - - if (write(_fd, buffer, length) != (ssize_t)length) - @throw [OFWriteFailedException exceptionWithObject: self - requestedLength: length - errNo: errno]; #endif } - (of_offset_t)lowlevelSeekToOffset: (of_offset_t)offset whence: (int)whence { of_offset_t ret; - if (_fd == INVALID_FD) +#if !defined(OF_MORPHOS) || defined(OF_IXEMUL) + if (_fd == -1) @throw [OFSeekFailedException exceptionWithStream: self offset: offset whence: whence]; -#if !defined(OF_MORPHOS) || defined(OF_IXEMUL) # if defined(OF_WINDOWS) ret = _lseeki64(_fd, offset, whence); # elif defined(OF_HAVE_OFF64_T) ret = lseek64(_fd, offset, whence); # else @@ -381,19 +407,24 @@ @throw [OFSeekFailedException exceptionWithStream: self offset: offset whence: whence errNo: errno]; #else + if (_handle == 0) + @throw [OFSeekFailedException exceptionWithStream: self + offset: offset + whence: whence]; + switch (whence) { case SEEK_SET: - ret = Seek64(_fd, offset, OFFSET_BEGINNING); + ret = Seek64(_handle, offset, OFFSET_BEGINNING); break; case SEEK_CUR: - ret = Seek64(_fd, offset, OFFSET_CURRENT); + ret = Seek64(_handle, offset, OFFSET_CURRENT); break; case SEEK_END: - ret = Seek64(_fd, offset, OFFSET_END); + ret = Seek64(_handle, offset, OFFSET_END); break; default: ret = -1; break; } @@ -421,21 +452,27 @@ } #endif - (void)close { - if (_fd != INVALID_FD) +#if !defined(OF_MORPHOS) || defined(OF_IXEMUL) + if (_fd != -1) close(_fd); - _fd = INVALID_FD; + _fd = -1; +#else + if (_handle != 0) + Close(_handle); + + _handle = 0; +#endif [super close]; } - (void)dealloc { - if (_fd != INVALID_FD) - close(_fd); + [self close]; [super dealloc]; } @end Index: src/OFStdIOStream+Private.h ================================================================== --- src/OFStdIOStream+Private.h +++ src/OFStdIOStream+Private.h @@ -20,12 +20,11 @@ @interface OFStdIOStream () #if !defined(OF_MORPHOS) || defined(OF_IXEMUL) - (instancetype)of_initWithFileDescriptor: (int)fd OF_METHOD_FAMILY(init); #else -- (instancetype)of_initWithFileDescriptor: (long)handle - closable: (bool)closable - OF_METHOD_FAMILY(init); +- (instancetype)of_initWithHandle: (BPTR)handle + closable: (bool)closable OF_METHOD_FAMILY(init); #endif @end OF_ASSUME_NONNULL_END Index: src/OFStdIOStream.h ================================================================== --- src/OFStdIOStream.h +++ src/OFStdIOStream.h @@ -16,10 +16,14 @@ #import "OFStream.h" OF_ASSUME_NONNULL_BEGIN +#if defined(OF_MORPHOS) && !defined(OF_IXEMUL) +typedef long BPTR; +#endif + /*! * @class OFStdIOStream OFStdIOStream.h ObjFW/OFStdIOStream.h * * @brief A class for providing standard input, output and error as OFStream. * @@ -32,11 +36,11 @@ @interface OFStdIOStream: OFStream { #if !defined(OF_MORPHOS) || defined(OF_IXEMUL) int _fd; #else - long _fd; + BPTR _handle; bool _closable; #endif bool _atEndOfStream; } Index: src/OFStdIOStream.m ================================================================== --- src/OFStdIOStream.m +++ src/OFStdIOStream.m @@ -42,23 +42,14 @@ #if defined(OF_MORPHOS) && !defined(OF_IXEMUL) # define BOOL EXEC_BOOL # include # include # undef BOOL - -# define INVALID_FD 0 # define getpid() ((int)SysBase->ThisTask) -# define read(fd, buf, len) Read(fd, buf, len) -# define write(fd, buf, len) Write(fd, buf, len) - extern struct ExecBase *SysBase; #endif -#ifndef INVALID_FD -# define INVALID_FD -1 -#endif - /* References for static linking */ #ifdef OF_WINDOWS void _reference_to_OFStdIOStream_Win32Console(void) { @@ -114,34 +105,31 @@ BPTR input = Input(), output = Output(); BPTR error = ((struct Process *)SysBase->ThisTask)->pr_CES; bool inputClosable = false, outputClosable = false, errorClosable = false; - if (input == INVALID_FD) { + if (input == 0) { input = Open("*", MODE_OLDFILE); inputClosable = true; } - if (output == INVALID_FD) { + if (output == 0) { output = Open("*", MODE_OLDFILE); outputClosable = true; } - if (error == INVALID_FD) { + if (error == 0) { error = Open("*", MODE_OLDFILE); errorClosable = true; } - of_stdin = [[OFStdIOStream alloc] - of_initWithFileDescriptor: input - closable: inputClosable]; - of_stdout = [[OFStdIOStream alloc] - of_initWithFileDescriptor: output - closable: outputClosable]; - of_stderr = [[OFStdIOStream alloc] - of_initWithFileDescriptor: error - closable: errorClosable]; + of_stdin = [[OFStdIOStream alloc] of_initWithHandle: input + closable: inputClosable]; + of_stdout = [[OFStdIOStream alloc] of_initWithHandle: output + closable: outputClosable]; + of_stderr = [[OFStdIOStream alloc] of_initWithHandle: error + closable: errorClosable]; # endif } #endif - init @@ -157,16 +145,16 @@ _fd = fd; return self; } #else -- (instancetype)of_initWithFileDescriptor: (long)fd - closable: (bool)closable +- (instancetype)of_initWithHandle: (BPTR)handle + closable: (bool)closable { self = [super init]; - _fd = fd; + _handle = handle; _closable = closable; return self; } #endif @@ -178,38 +166,56 @@ [super dealloc]; } - (bool)lowlevelIsAtEndOfStream { - if (_fd == INVALID_FD) +#if !defined(OF_MORPHOS) || defined(OF_IXEMUL) + if (_fd == -1) + return true; +#else + if (_handle == 0) return true; +#endif return _atEndOfStream; } - (size_t)lowlevelReadIntoBuffer: (void *)buffer length: (size_t)length { ssize_t ret; - if (_fd == INVALID_FD || _atEndOfStream) +#if !defined(OF_MORPHOS) || defined(OF_IXEMUL) + if (_fd == -1 || _atEndOfStream) @throw [OFReadFailedException exceptionWithObject: self requestedLength: length]; -#ifndef OF_WINDOWS +# ifndef OF_WINDOWS if ((ret = read(_fd, buffer, length)) < 0) @throw [OFReadFailedException exceptionWithObject: self requestedLength: length errNo: errno]; -#else +# else if (length > UINT_MAX) @throw [OFOutOfRangeException exception]; if ((ret = read(_fd, buffer, (unsigned int)length)) < 0) @throw [OFReadFailedException exceptionWithObject: self requestedLength: length errNo: errno]; +# endif +#else + if (_handle == 0 || _atEndOfStream) + @throw [OFReadFailedException exceptionWithObject: self + requestedLength: length]; + + if (length > LONG_MAX) + @throw [OFOutOfRangeException exception]; + + if ((ret = Read(_handle, buffer, length)) < 0) + @throw [OFReadFailedException exceptionWithObject: self + requestedLength: length]; #endif if (ret == 0) _atEndOfStream = true; @@ -217,30 +223,43 @@ } - (void)lowlevelWriteBuffer: (const void *)buffer length: (size_t)length { - if (_fd == INVALID_FD || _atEndOfStream) +#if !defined(OF_MORPHOS) || defined(OF_IXEMUL) + if (_fd == -1 || _atEndOfStream) @throw [OFWriteFailedException exceptionWithObject: self requestedLength: length]; -#ifndef OF_WINDOWS +# ifndef OF_WINDOWS if (length > SSIZE_MAX) @throw [OFOutOfRangeException exception]; - if (write(_fd, (void *)buffer, length) != (ssize_t)length) + if (write(_fd, buffer, length) != (ssize_t)length) @throw [OFWriteFailedException exceptionWithObject: self requestedLength: length errNo: errno]; -#else +# else if (length > INT_MAX) @throw [OFOutOfRangeException exception]; if (write(_fd, buffer, (int)length) != (int)length) @throw [OFWriteFailedException exceptionWithObject: self requestedLength: length errNo: errno]; +# endif +#else + if (_handle == 0 || _atEndOfStream) + @throw [OFWriteFailedException exceptionWithObject: self + requestedLength: length]; + + if (length > SSIZE_MAX) + @throw [OFOutOfRangeException exception]; + + if (Write(_handle, (void *)buffer, length) != (LONG)length) + @throw [OFWriteFailedException exceptionWithObject: self + requestedLength: length]; #endif } #if !defined(OF_WINDOWS) && (!defined(OF_MORPHOS) || defined(OF_IXEMUL)) - (int)fileDescriptorForReading @@ -255,18 +274,20 @@ #endif - (void)close { #if !defined(OF_MORPHOS) || defined(OF_IXEMUL) - if (_fd != INVALID_FD) + if (_fd != -1) close(_fd); + + _fd = -1; #else - if (_closable && _fd != INVALID_FD) - Close(_fd); -#endif + if (_closable && _handle != 0) + Close(_handle); - _fd = INVALID_FD; + _handle = 0; +#endif [super close]; } - autorelease @@ -288,11 +309,12 @@ return OF_RETAIN_COUNT_MAX; } - (int)columns { -#if defined(HAVE_SYS_IOCTL_H) && defined(TIOCGWINSZ) +#if defined(HAVE_SYS_IOCTL_H) && defined(TIOCGWINSZ) && \ + (!defined(OF_MORPHOS) || defined(OF_IXEMUL)) struct winsize ws; if (ioctl(_fd, TIOCGWINSZ, &ws) != 0) return -1; @@ -302,11 +324,12 @@ #endif } - (int)rows { -#if defined(HAVE_SYS_IOCTL_H) && defined(TIOCGWINSZ) +#if defined(HAVE_SYS_IOCTL_H) && defined(TIOCGWINSZ) && \ + (!defined(OF_MORPHOS) || defined(OF_IXEMUL)) struct winsize ws; if (ioctl(_fd, TIOCGWINSZ, &ws) != 0) return -1; Index: src/OFString.m ================================================================== --- src/OFString.m +++ src/OFString.m @@ -26,11 +26,11 @@ #endif #ifdef HAVE_XLOCALE_H # include #endif -#ifdef OF_HAVE_FILES +#ifdef HAVE_SYS_STAT_H # include #endif #import "OFString.h" #import "OFString_UTF8.h"