Index: src/OFString.h ================================================================== --- src/OFString.h +++ src/OFString.h @@ -43,10 +43,11 @@ #ifdef __cplusplus } #endif @class OFArray; +@class OFURL; /** * \brief A class for handling strings. */ @interface OFString: OFObject @@ -148,10 +149,30 @@ * \return A new autoreleased OFString */ + stringWithContentsOfFile: (OFString*)path encoding: (of_string_encoding_t)encoding; +/** + * Creates a new OFString with the contents of the specified URL, assuming + * UTF-8 encoding. + * + * \param url The URL to the contents for the string + * \return A new autoreleased OFString + */ ++ stringWithContentsOfURL: (OFURL*)url; + +/** + * Creates a new OFString with the contents of the specified URL in the + * specified encoding. + * + * \param url The URL to the contents for the string + * \param encoding The encoding to assume + * \return A new autoreleased OFString + */ ++ stringWithContentsOfURL: (OFURL*)url + encoding: (of_string_encoding_t)encoding; + /** * Initializes an already allocated OFString from a UTF-8 encoded C string. * * \param str A UTF-8 encoded C string to initialize the OFString with * \return An initialized OFString @@ -259,10 +280,30 @@ * \return An initialized OFString */ - initWithContentsOfFile: (OFString*)path encoding: (of_string_encoding_t)encoding; +/** + * Initializes an already allocated OFString with the contents of the specified + * URL, assuming UTF-8 encoding. + * + * \param url The URL to the contents for the string + * \return An initialized OFString + */ +- initWithContentsOfURL: (OFURL*)url; + +/** + * Initializes an already allocated OFString with the contents of the specified + * URL in the specified encoding. + * + * \param url The URL to the contents for the string + * \param encoding The encoding to assume + * \return An initialized OFString + */ +- initWithContentsOfURL: (OFURL*)url + encoding: (of_string_encoding_t)encoding; + /** * \return The OFString as a UTF-8 encoded C string */ - (const char*)cString; Index: src/OFString.m ================================================================== --- src/OFString.m +++ src/OFString.m @@ -32,10 +32,12 @@ #endif #import "OFString.h" #import "OFArray.h" #import "OFFile.h" +#import "OFURL.h" +#import "OFHTTPRequest.h" #import "OFAutoreleasePool.h" #import "OFExceptions.h" #import "macros.h" #import "of_asprintf.h" @@ -305,10 +307,22 @@ encoding: (of_string_encoding_t)encoding { return [[[self alloc] initWithContentsOfFile: path encoding: encoding] autorelease]; } + ++ stringWithContentsOfURL: (OFURL*)url +{ + return [[[self alloc] initWithContentsOfURL: url] autorelease]; +} + ++ stringWithContentsOfURL: (OFURL*)url + encoding: (of_string_encoding_t)encoding +{ + return [[[self alloc] initWithContentsOfURL: url + encoding: encoding] autorelease]; +} - init { self = [super init]; @@ -630,16 +644,17 @@ } - initWithContentsOfFile: (OFString*)path encoding: (of_string_encoding_t)encoding { + char *tmp; + struct stat s; + self = [super init]; @try { OFFile *file; - char *tmp; - struct stat s; if (stat([path cString], &s) == -1) @throw [OFInitializationFailedException newWithClass: isa]; @@ -649,24 +664,67 @@ @try { tmp = [self allocMemoryWithSize: s.st_size]; [file readExactlyNBytes: s.st_size intoBuffer: tmp]; - - self = [self initWithCString: tmp - encoding: encoding - length: s.st_size]; - - [self freeMemory: tmp]; } @finally { [file release]; } } @catch (id e) { [self release]; @throw e; } + self = [self initWithCString: tmp + encoding: encoding + length: s.st_size]; + [self freeMemory: tmp]; + + return self; +} + +- initWithContentsOfURL: (OFURL*)url +{ + return [self initWithContentsOfURL: url + encoding: OF_STRING_ENCODING_UTF_8]; +} + +- initWithContentsOfURL: (OFURL*)url + encoding: (of_string_encoding_t)encoding +{ + OFAutoreleasePool *pool; + OFHTTPRequest *req; + OFHTTPRequestResult *res; + Class c; + + c = isa; + [self release]; + self = nil; + + pool = [[OFAutoreleasePool alloc] init]; + + if ([[url scheme] isEqual: @"file"]) { + self = [[c alloc] initWithContentsOfFile: [url path] + encoding: encoding]; + [pool release]; + return self; + } + + req = [OFHTTPRequest request]; + [req setURL: url]; + res = [req result]; + + if ([res statusCode] != 200) + @throw [OFHTTPRequestFailedException + newWithClass: [req class] + HTTPRequest: req + statusCode: [res statusCode]]; + + self = [[c alloc] initWithCString: (char*)[[res data] cArray] + encoding: encoding + length: [[res data] count]]; + [pool release]; return self; } - (const char*)cString { Index: tests/OFStringTests.m ================================================================== --- tests/OFStringTests.m +++ tests/OFStringTests.m @@ -19,10 +19,11 @@ #include #include #import "OFString.h" #import "OFArray.h" +#import "OFURL.h" #import "OFAutoreleasePool.h" #import "OFExceptions.h" #import "TestsAppDelegate.h" @@ -121,10 +122,16 @@ TEST(@"+[stringWithContentsOfFile:encoding]", (s[1] = [OFString stringWithContentsOfFile: @"testfile.txt" encoding: OF_STRING_ENCODING_ISO_8859_1]) && [s[1] isEqual: @"testäöü"]) + + TEST(@"+[stringWithContentsOfURL:encoding]", (s[1] = [OFString + stringWithContentsOfURL: [OFURL URLWithString: + @"file://testfile.txt"] + encoding: OF_STRING_ENCODING_ISO_8859_1]) && + [s[1] isEqual: @"testäöü"]) TEST(@"-[appendCStringWithLength:]", R([s[0] appendCString: "foobarqux" + 3 withLength: 3]) && [s[0] isEqual: @"foobar"])