@@ -26,11 +26,12 @@ #import "autorelease.h" #import "macros.h" @interface OFINIFile (OF_PRIVATE_CATEGORY) -- (void)OF_parseFile: (OFString*)path; +- (void)OF_parseFile: (OFString*)path + encoding: (of_string_encoding_t)encoding; @end static bool isWhitespaceLine(OFString *line) { @@ -55,24 +56,39 @@ @implementation OFINIFile + (instancetype)fileWithPath: (OFString*)path { return [[[self alloc] initWithPath: path] autorelease]; } + ++ (instancetype)fileWithPath: (OFString*)path + encoding: (of_string_encoding_t)encoding +{ + return [[[self alloc] initWithPath: path + encoding: encoding] autorelease]; +} - init { OF_INVALID_INIT_METHOD } - initWithPath: (OFString*)path { + return [self initWithPath: path + encoding: OF_STRING_ENCODING_UTF_8]; +} + +- initWithPath: (OFString*)path + encoding: (of_string_encoding_t)encoding +{ self = [super init]; @try { _categories = [[OFMutableArray alloc] init]; - [self OF_parseFile: path]; + [self OF_parseFile: path + encoding: encoding]; } @catch (id e) { [self release]; @throw e; } @@ -112,10 +128,11 @@ return [category autorelease]; } - (void)OF_parseFile: (OFString*)path + encoding: (of_string_encoding_t)encoding { void *pool = objc_autoreleasePoolPush(); OFFile *file; OFINICategory *category = nil; OFString *line; @@ -129,11 +146,11 @@ return; @throw e; } - while ((line = [file readLine]) != nil) { + while ((line = [file readLineWithEncoding: encoding]) != nil) { if (isWhitespaceLine(line)) continue; if ([line hasPrefix: @"["]) { OFString *categoryName; @@ -159,20 +176,28 @@ objc_autoreleasePoolPop(pool); } - (void)writeToFile: (OFString*)path { + [self writeToFile: path + encoding: OF_STRING_ENCODING_UTF_8]; +} + +- (void)writeToFile: (OFString*)path + encoding: (of_string_encoding_t)encoding +{ void *pool = objc_autoreleasePoolPush(); OFFile *file = [OFFile fileWithPath: path mode: @"w"]; OFEnumerator *enumerator = [_categories objectEnumerator]; OFINICategory *category; bool first = true; while ((category = [enumerator nextObject]) != nil) if ([category OF_writeToStream: file + encoding: encoding first: first]) first = false; objc_autoreleasePoolPop(pool); } @end