Index: src/OFFile.h ================================================================== --- src/OFFile.h +++ src/OFFile.h @@ -28,10 +28,12 @@ typedef struct of_file_handle *of_file_handle_t; #endif OF_ASSUME_NONNULL_BEGIN +@class OFURL; + /*! * @class OFFile OFFile.h ObjFW/OFFile.h * * @brief A class which provides methods to read and write files. */ @@ -60,10 +62,31 @@ * @return A new autoreleased OFFile */ + (instancetype)fileWithPath: (OFString *)path mode: (OFString *)mode; +/*! + * @brief Creates a new OFFile with the specified URL and mode. + * + * @param URL The URL to the file to open + * @param mode The mode in which the file should be opened.@n + * Possible modes are: + * Mode | Description + * ---------------|------------------------------------- + * `r` | Read-only + * `r+` | Read-write + * `w` | Write-only, create or truncate + * `wx` | Write-only, create or fail, exclusive + * `w+` | Read-write, create or truncate + * `w+x` | Read-write, create or fail, exclusive + * `a` | Write-only, create or append + * `a+` | Read-write, create or append + * @return A new autoreleased OFFile + */ ++ (instancetype)fileWithURL: (OFURL *)URL + mode: (OFString *)mode; + /*! * @brief Creates a new OFFile with the specified native file handle. * * @param handle A native file handle. If OF_FILE_HANDLE_IS_FD is defined, this * is a file descriptor. The handle is closed when the OFFile @@ -97,10 +120,35 @@ * @return An initialized OFFile */ - (instancetype)initWithPath: (OFString *)path mode: (OFString *)mode; +/*! + * @brief Initializes an already allocated OFFile. + * + * @param URL The URL to the file to open + * @param mode The mode in which the file should be opened.@n + * Possible modes are: + * Mode | Description + * ---------------|------------------------------------- + * `r` | read-only + * `rb` | read-only, binary + * `r+` | read-write + * `rb+` or `r+b` | read-write, binary + * `w` | write-only, create, truncate + * `wb` | write-only, create, truncate, binary + * `w` | read-write, create, truncate + * `wb+` or `w+b` | read-write, create, truncate, binary + * `a` | write-only, create, append + * `ab` | write-only, create, append, binary + * `a+` | read-write, create, append + * `ab+` or `a+b` | read-write, create, append, binary + * @return An initialized OFFile + */ +- (instancetype)initWithURL: (OFURL *)URL + mode: (OFString *)mode; + /*! * @brief Initializes an already allocated OFFile. * * @param handle A native file handle. If OF_FILE_HANDLE_IS_FD is defined, this * is a file descriptor. The handle is closed when the OFFile Index: src/OFFile.m ================================================================== --- src/OFFile.m +++ src/OFFile.m @@ -27,12 +27,13 @@ #ifdef HAVE_SYS_STAT_H # include #endif #import "OFFile.h" -#import "OFString.h" #import "OFLocalization.h" +#import "OFString.h" +#import "OFURL.h" #import "OFInitializationFailedException.h" #import "OFInvalidArgumentException.h" #import "OFNotOpenException.h" #import "OFOpenItemFailedException.h" @@ -180,10 +181,17 @@ 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 { return [[[self alloc] initWithHandle: handle] autorelease]; } @@ -296,10 +304,28 @@ self = [self initWithHandle: handle]; } @catch (id e) { closeHandle(handle); @throw e; } + + return self; +} + +- (instancetype)initWithURL: (OFURL *)URL + mode: (OFString *)mode +{ + @try { + void *pool = objc_autoreleasePoolPush(); + + self = [self initWithPath: [URL fileSystemRepresentation] + mode: mode]; + + objc_autoreleasePoolPop(pool); + } @catch (id e) { + [self release]; + @throw e; + } return self; } - (instancetype)initWithHandle: (of_file_handle_t)handle