Index: src/OFINIFile.h ================================================================== --- src/OFINIFile.h +++ src/OFINIFile.h @@ -18,10 +18,11 @@ #import "OFINICategory.h" OF_ASSUME_NONNULL_BEGIN @class OFMutableArray OF_GENERIC(ObjectType); +@class OFURL; /** * @class OFINIFile OFINIFile.h ObjFW/OFINIFile.h * * @brief A class for reading, creating and modifying INI files. @@ -38,51 +39,50 @@ @property (readonly, nonatomic) OFArray OF_GENERIC(OFINICategory *) *categories; /** * @brief Creates a new OFINIFile with the contents of the specified file. * - * @param path The path to the file whose contents the OFINIFile should contain + * @param URL The URL to the file whose contents the OFINIFile should contain * * @return A new, autoreleased OFINIFile with the contents of the specified file */ -+ (instancetype)fileWithPath: (OFString *)path; ++ (instancetype)fileWithURL: (OFURL *)URL; /** * @brief Creates a new OFINIFile with the contents of the specified file in * the specified encoding. * - * @param path The path to the file whose contents the OFINIFile should contain + * @param URL The URL to the file whose contents the OFINIFile should contain * @param encoding The encoding of the specified file * * @return A new, autoreleased OFINIFile with the contents of the specified file */ -+ (instancetype)fileWithPath: (OFString *)path - encoding: (OFStringEncoding)encoding; ++ (instancetype)fileWithURL: (OFURL *)URL encoding: (OFStringEncoding)encoding; - (instancetype)init OF_UNAVAILABLE; /** * @brief Initializes an already allocated OFINIFile with the contents of the * specified file. * - * @param path The path to the file whose contents the OFINIFile should contain + * @param URL The URL to the file whose contents the OFINIFile should contain * * @return An initialized OFINIFile with the contents of the specified file */ -- (instancetype)initWithPath: (OFString *)path; +- (instancetype)initWithURL: (OFURL *)URL; /** * @brief Initializes an already allocated OFINIFile with the contents of the * specified file in the specified encoding. * - * @param path The path to the file whose contents the OFINIFile should contain + * @param URL The URL to the file whose contents the OFINIFile should contain * @param encoding The encoding of the specified file * * @return An initialized OFINIFile with the contents of the specified file */ -- (instancetype)initWithPath: (OFString *)path - encoding: (OFStringEncoding)encoding +- (instancetype)initWithURL: (OFURL *)URL + encoding: (OFStringEncoding)encoding OF_DESIGNATED_INITIALIZER; /** * @brief Returns an @ref OFINICategory for the category with the specified * name. @@ -95,20 +95,20 @@ - (OFINICategory *)categoryForName: (OFString *)name; /** * @brief Writes the contents of the OFINIFile to a file. * - * @param path The path of the file to write to + * @param URL The URL of the file to write to */ -- (void)writeToFile: (OFString *)path; +- (void)writeToURL: (OFURL *)URL; /** * @brief Writes the contents of the OFINIFile to a file in the specified * encoding. * - * @param path The path of the file to write to + * @param URL The URL of the file to write to * @param encoding The encoding to use */ -- (void)writeToFile: (OFString *)path encoding: (OFStringEncoding)encoding; +- (void)writeToURL: (OFURL *)URL encoding: (OFStringEncoding)encoding; @end OF_ASSUME_NONNULL_END Index: src/OFINIFile.m ================================================================== --- src/OFINIFile.m +++ src/OFINIFile.m @@ -17,21 +17,23 @@ #include #import "OFINIFile.h" #import "OFArray.h" -#import "OFString.h" -#import "OFFile.h" +#import "OFINICategory+Private.h" #import "OFINICategory.h" -#import "OFINICategory+Private.h" +#import "OFStream.h" +#import "OFString.h" +#import "OFURL.h" +#import "OFURLHandler.h" #import "OFInvalidFormatException.h" #import "OFOpenItemFailedException.h" OF_DIRECT_MEMBERS @interface OFINIFile () -- (void)of_parseFile: (OFString *)path encoding: (OFStringEncoding)encoding; +- (void)of_parseURL: (OFURL *)URL encoding: (OFStringEncoding)encoding; @end static bool isWhitespaceLine(OFString *line) { @@ -46,41 +48,38 @@ } @implementation OFINIFile @synthesize categories = _categories; -+ (instancetype)fileWithPath: (OFString *)path ++ (instancetype)fileWithURL: (OFURL *)URL { - return [[[self alloc] initWithPath: path] autorelease]; + return [[[self alloc] initWithURL: URL] autorelease]; } -+ (instancetype)fileWithPath: (OFString *)path - encoding: (OFStringEncoding)encoding ++ (instancetype)fileWithURL: (OFURL *)URL encoding: (OFStringEncoding)encoding { - return [[[self alloc] initWithPath: path - encoding: encoding] autorelease]; + return [[[self alloc] initWithURL: URL encoding: encoding] autorelease]; } - (instancetype)init { OF_INVALID_INIT_METHOD } -- (instancetype)initWithPath: (OFString *)path +- (instancetype)initWithURL: (OFURL *)URL { - return [self initWithPath: path encoding: OFStringEncodingUTF8]; + return [self initWithURL: URL encoding: OFStringEncodingAutodetect]; } -- (instancetype)initWithPath: (OFString *)path - encoding: (OFStringEncoding)encoding +- (instancetype)initWithURL: (OFURL *)URL encoding: (OFStringEncoding)encoding { self = [super init]; @try { _categories = [[OFMutableArray alloc] init]; - [self of_parseFile: path encoding: encoding]; + [self of_parseURL: URL encoding: encoding]; } @catch (id e) { [self release]; @throw e; } @@ -109,19 +108,20 @@ objc_autoreleasePoolPop(pool); return category; } -- (void)of_parseFile: (OFString *)path encoding: (OFStringEncoding)encoding +- (void)of_parseURL: (OFURL *)URL encoding: (OFStringEncoding)encoding { void *pool = objc_autoreleasePoolPush(); - OFFile *file; + OFStream *file; OFINICategory *category = nil; OFString *line; @try { - file = [OFFile fileWithPath: path mode: @"r"]; + file = [[OFURLHandler handlerForURL: URL] openItemAtURL: URL + mode: @"r"]; } @catch (OFOpenItemFailedException *e) { /* Handle missing file like an empty file */ if (e.errNo == ENOENT) return; @@ -152,19 +152,21 @@ } objc_autoreleasePoolPop(pool); } -- (void)writeToFile: (OFString *)path +- (void)writeToURL: (OFURL *)URL { - [self writeToFile: path encoding: OFStringEncodingUTF8]; + [self writeToURL: URL encoding: OFStringEncodingUTF8]; } -- (void)writeToFile: (OFString *)path encoding: (OFStringEncoding)encoding +- (void)writeToURL: (OFURL *)URL encoding: (OFStringEncoding)encoding { void *pool = objc_autoreleasePoolPush(); - OFFile *file = [OFFile fileWithPath: path mode: @"w"]; + OFStream *file = [[OFURLHandler handlerForURL: URL] + openItemAtURL: URL + mode: @"w"]; bool first = true; for (OFINICategory *category in _categories) if ([category of_writeToStream: file encoding: encoding Index: src/OFINIFileSettings.h ================================================================== --- src/OFINIFileSettings.h +++ src/OFINIFileSettings.h @@ -15,16 +15,17 @@ #import "OFSettings.h" OF_ASSUME_NONNULL_BEGIN -@class OFString; @class OFINIFile; +@class OFString; +@class OFURL; @interface OFINIFileSettings: OFSettings { - OFString *_filePath; + OFURL *_fileURL; OFINIFile *_INIFile; } @end OF_ASSUME_NONNULL_END Index: src/OFINIFileSettings.m ================================================================== --- src/OFINIFileSettings.m +++ src/OFINIFileSettings.m @@ -30,14 +30,13 @@ @try { void *pool = objc_autoreleasePoolPush(); OFString *fileName; fileName = [applicationName stringByAppendingString: @".ini"]; - _filePath = [[[OFSystemInfo - userConfigURL].fileSystemRepresentation - stringByAppendingPathComponent: fileName] copy]; - _INIFile = [[OFINIFile alloc] initWithPath: _filePath]; + _fileURL = [[[OFSystemInfo userConfigURL] + URLByAppendingPathComponent: fileName] copy]; + _INIFile = [[OFINIFile alloc] initWithURL: _fileURL]; objc_autoreleasePoolPop(pool); } @catch (id e) { [self release]; @throw e; @@ -46,11 +45,11 @@ return self; } - (void)dealloc { - [_filePath release]; + [_fileURL release]; [_INIFile release]; [super dealloc]; } @@ -242,8 +241,8 @@ objc_autoreleasePoolPop(pool); } - (void)save { - [_INIFile writeToFile: _filePath]; + [_INIFile writeToURL: _fileURL]; } @end Index: tests/OFINIFileTests.m ================================================================== --- tests/OFINIFileTests.m +++ tests/OFINIFileTests.m @@ -41,22 +41,24 @@ @"bool=false\r\n" @"float=0.25\r\n" @"array1=foo\r\n" @"array1=bar\r\n" @"double=0.75\r\n"; + OFURL *URL; OFINIFile *file; OFINICategory *tests, *foobar, *types; OFArray *array; #ifndef OF_NINTENDO_DS - OFString *writePath; + OFURL *writeURL; #endif module = @"OFINIFile"; - TEST(@"+[fileWithPath:encoding:]", - (file = [OFINIFile fileWithPath: @"testfile.ini" - encoding: OFStringEncodingCodepage437])) + URL = [OFURL fileURLWithPath: @"testfile.ini"]; + TEST(@"+[fileWithURL:encoding:]", + (file = [OFINIFile fileWithURL: URL + encoding: OFStringEncodingCodepage437])) tests = [file categoryForName: @"tests"]; foobar = [file categoryForName: @"foobar"]; types = [file categoryForName: @"types"]; TEST(@"-[categoryForName:]", @@ -112,23 +114,22 @@ module = @"OFINIFile"; /* FIXME: Find a way to write files on Nintendo DS */ #ifndef OF_NINTENDO_DS - writePath = [[OFSystemInfo temporaryDirectoryURL] + writeURL = [[OFSystemInfo temporaryDirectoryURL] URLByAppendingPathComponent: @"objfw-tests.ini" - isDirectory: false] - .fileSystemRepresentation; + isDirectory: false]; TEST(@"-[writeToFile:encoding:]", - R([file writeToFile: writePath - encoding: OFStringEncodingCodepage437]) && - [[OFString stringWithContentsOfFile: writePath - encoding: OFStringEncodingCodepage437] + R([file writeToURL: writeURL + encoding: OFStringEncodingCodepage437]) && + [[OFString stringWithContentsOfURL: writeURL + encoding: OFStringEncodingCodepage437] isEqual: output]) - [[OFFileManager defaultManager] removeItemAtPath: writePath]; + [[OFFileManager defaultManager] removeItemAtURL: writeURL]; #else (void)output; #endif objc_autoreleasePoolPop(pool); } @end