Index: src/OFString.h ================================================================== --- src/OFString.h +++ src/OFString.h @@ -114,10 +114,29 @@ * \param str A string to initialize the OFString with * \return A new autoreleased OFString */ + stringWithString: (OFString*)str; +/** + * Creates a new OFString with the contents of the specified file. + * + * \param path The path to the file + * \return A new autoreleased OFString + */ ++ stringWithContentsOfFile: (OFString*)path; + +/** + * Creates a new OFString with the contents of the specified file in the + * specified encoding. + * + * \param path The path to the file + * \param encoding The encoding of the file + * \return A new autoreleased OFString + */ ++ stringWithContentsOfFile: (OFString*)path + encoding: (enum of_string_encoding)encoding; + /** * Initializes an already allocated OFString. * * \return An initialized OFString */ @@ -212,10 +231,30 @@ * \param str A string to initialize the OFString with * \return An initialized OFString */ - initWithString: (OFString*)str; +/** + * Initializes an already allocated OFString with the contents of the specified + * file in the specified encoding. + * + * \param path The path to the file + * \return An initialized OFString + */ +- initWithContentsOfFile: (OFString*)path; + +/** + * Initializes an already allocated OFString with the contents of the specified + * file in the specified encoding. + * + * \param path The path to the file + * \param encoding The encoding of the file + * \return An initialized OFString + */ +- initWithContentsOfFile: (OFString*)path + encoding: (enum of_string_encoding)encoding; + /** * \return The OFString as a UTF-8 encoded C string */ - (const char*)cString; Index: src/OFString.m ================================================================== --- src/OFString.m +++ src/OFString.m @@ -15,18 +15,20 @@ #include #include #include #include +#include #ifdef HAVE_MADVISE -#include +# include #else -#define madvise(addr, len, advise) +# define madvise(addr, len, advise) #endif #import "OFString.h" #import "OFArray.h" +#import "OFFile.h" #import "OFAutoreleasePool.h" #import "OFExceptions.h" #import "macros.h" #import "asprintf.h" @@ -275,10 +277,22 @@ + stringWithString: (OFString*)str { return [[[self alloc] initWithString: str] autorelease]; } + ++ stringWithContentsOfFile: (OFString*)path +{ + return [[[self alloc] initWithContentsOfFile: path] autorelease]; +} + ++ stringWithContentsOfFile: (OFString*)path + encoding: (enum of_string_encoding)encoding +{ + return [[[self alloc] initWithContentsOfFile: path + encoding: encoding] autorelease]; +} - init { [super init]; @@ -622,10 +636,60 @@ */ free(string); [self dealloc]; @throw e; } + + return self; +} + +- initWithContentsOfFile: (OFString*)path +{ + return [self initWithContentsOfFile: path + encoding: OF_STRING_ENCODING_UTF_8]; +} + +- initWithContentsOfFile: (OFString*)path + encoding: (enum of_string_encoding)encoding +{ + OFFile *file = nil; + char *tmp; + struct stat s; + + if (stat([path cString], &s) == -1) { + Class c = isa; + [super dealloc]; + @throw [OFInitializationFailedException newWithClass: c]; + } + + if ((tmp = malloc(s.st_size)) == NULL) { + Class c = isa; + [super dealloc]; + @throw [OFOutOfMemoryException newWithClass: c + size: s.st_size]; + } + + @try { + file = [[OFFile alloc] initWithPath: path + mode: @"rb"]; + [file readExactlyNBytes: s.st_size + intoBuffer: tmp]; + } @catch (OFException *e) { + free(tmp); + [super dealloc]; + @throw e; + } @finally { + [file release]; + } + + @try { + self = [self initWithCString: tmp + encoding: encoding + length: s.st_size]; + } @finally { + free(tmp); + } return self; } - (const char*)cString Index: tests/Makefile ================================================================== --- tests/Makefile +++ tests/Makefile @@ -57,11 +57,11 @@ exit 1; \ fi echo "Uploading files to iPhone ${IPHONE_HOST} at ${IPHONE_TMP}..." ssh ${IPHONE_USER}@${IPHONE_HOST} \ 'rm -fr ${IPHONE_TMP} && mkdir -p ${IPHONE_TMP}/plugin' - scp -q ../src/libobjfw.dylib tests testfile \ + scp -q ../src/libobjfw.dylib tests testfile.bin testfile.txt \ ${IPHONE_USER}@${IPHONE_HOST}:${IPHONE_TMP}/ scp -q plugin/TestPlugin.impl \ ${IPHONE_USER}@${IPHONE_HOST}:${IPHONE_TMP}/plugin/ echo "Signing and running tests binary on iPhone ${IPHONE_HOST}..." ssh ${IPHONE_USER}@${IPHONE_HOST} \ Index: tests/OFHashesTests.m ================================================================== --- tests/OFHashesTests.m +++ tests/OFHashesTests.m @@ -33,11 +33,11 @@ - (void)hashesTests { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; OFMD5Hash *md5; OFSHA1Hash *sha1; - OFFile *f = [OFFile fileWithPath: @"testfile" + OFFile *f = [OFFile fileWithPath: @"testfile.bin" mode: @"rb"]; TEST(@"+[md5Hash]", (md5 = [OFMD5Hash md5Hash])) TEST(@"+[sha1Hash]", (sha1 = [OFSHA1Hash sha1Hash])) Index: tests/OFStringTests.m ================================================================== --- tests/OFStringTests.m +++ tests/OFStringTests.m @@ -101,10 +101,15 @@ TEST(@"+[stringWithCString:length:]", (s[0] = [OFMutableString stringWithCString: "foobar" length: 3]) && [s[0] isEqual: @"foo"]) + + TEST(@"+[stringWithContentsOfFile:encoding]", (s[1] = [OFString + stringWithContentsOfFile: @"testfile.txt" + encoding: OF_STRING_ENCODING_ISO_8859_1]) && + [s[1] isEqual: @"testäöü"]) TEST(@"-[appendCStringWithLength:]", [[s[0] appendCString: "foobarqux" + 3 withLength: 3] isEqual: @"foobar"]) DELETED tests/testfile Index: tests/testfile ================================================================== --- tests/testfile +++ tests/testfile cannot compute difference between binary files ADDED tests/testfile.bin Index: tests/testfile.bin ================================================================== --- tests/testfile.bin +++ tests/testfile.bin cannot compute difference between binary files ADDED tests/testfile.txt Index: tests/testfile.txt ================================================================== --- tests/testfile.txt +++ tests/testfile.txt @@ -0,0 +1,1 @@ +testäöü