ObjFW  Check-in [c7630df319]

Overview
Comment:Add +[stringWithContentsOfFile:] to OFString.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: c7630df319ca11bead117c2d11cc9be4c54c863ad4f518f6482c4ae6e49d0e17
User & Date: js on 2010-04-11 02:49:49
Other Links: manifest | tags
Context
2010-04-11
16:37
Fix manually closing a TCP socket and improve automatic closing. check-in: cecf1d4bf7 user: js tags: trunk
02:49
Add +[stringWithContentsOfFile:] to OFString. check-in: c7630df319 user: js tags: trunk
2010-04-10
23:52
Add +[stringWithPath:] to OFString. check-in: 48190e2a91 user: js tags: trunk
Changes

Modified src/OFString.h from [7aecf0bec8] to [fe46512cb7].

112
113
114
115
116
117
118



















119
120
121
122
123
124
125
 * Creates a new OFString from another string.
 *
 * \param str A string to initialize the OFString with
 * \return A new autoreleased OFString
 */
+ stringWithString: (OFString*)str;




















/**
 * Initializes an already allocated OFString.
 *
 * \return An initialized OFString
 */
- init;








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
 * Creates a new OFString from another string.
 *
 * \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
 */
- init;

210
211
212
213
214
215
216




















217
218
219
220
221
222
223
 * Initializes an already allocated OFString with another string.
 *
 * \param str A string to initialize the OFString with
 * \return An initialized OFString
 */
- initWithString: (OFString*)str;





















/**
 * \return The OFString as a UTF-8 encoded C string
 */
- (const char*)cString;

/**
 * \return The length of the string in Unicode characters







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
 * Initializes an already allocated OFString with another string.
 *
 * \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;

/**
 * \return The length of the string in Unicode characters

Modified src/OFString.m from [4ec189c3b9] to [4bb745a125].

13
14
15
16
17
18
19

20
21
22
23
24
25
26
27

28
29
30
31
32
33
34

#define _GNU_SOURCE
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#ifdef HAVE_MADVISE
#include <sys/mman.h>
#else
#define madvise(addr, len, advise)
#endif

#import "OFString.h"
#import "OFArray.h"

#import "OFAutoreleasePool.h"
#import "OFExceptions.h"
#import "macros.h"

#import "asprintf.h"
#import "unicode.h"








>

|

|




>







13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

#define _GNU_SOURCE
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/stat.h>
#ifdef HAVE_MADVISE
# include <sys/mman.h>
#else
# 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"
#import "unicode.h"

273
274
275
276
277
278
279












280
281
282
283
284
285
286
	return ret;
}

+ stringWithString: (OFString*)str
{
	return [[[self alloc] initWithString: str] autorelease];
}













- init
{
	[super init];

	string = NULL;








>
>
>
>
>
>
>
>
>
>
>
>







275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
	return ret;
}

+ 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];

	string = NULL;

620
621
622
623
624
625
626


















































627
628
629
630
631
632
633
		 * Compiler bug? Anyway, [self dealloc] will do here as we
		 * don't reimplement dealloc.
		 */
		free(string);
		[self dealloc];
		@throw e;
	}



















































	return self;
}

- (const char*)cString
{
	return string;







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
		 * Compiler bug? Anyway, [self dealloc] will do here as we
		 * don't reimplement dealloc.
		 */
		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
{
	return string;

Modified tests/Makefile from [091f700834] to [4710ac3d3c].

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
	if [ -z "${IPHONE_HOST}" ]; then \
		echo "Please set IPHONE_HOST to the hostname of your iPhone!"; \
		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 \
		${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} \
		'cd ${IPHONE_TMP} && ldid -S tests && ./tests'

include ../buildsys.mk

CPPFLAGS += -I../src -I.. -DSTDOUT
LIBS := -L../src -lobjfw ${LIBS}
LD = ${OBJC}







|












55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
	if [ -z "${IPHONE_HOST}" ]; then \
		echo "Please set IPHONE_HOST to the hostname of your iPhone!"; \
		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.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} \
		'cd ${IPHONE_TMP} && ldid -S tests && ./tests'

include ../buildsys.mk

CPPFLAGS += -I../src -I.. -DSTDOUT
LIBS := -L../src -lobjfw ${LIBS}
LD = ${OBJC}

Modified tests/OFHashesTests.m from [9cfde3ad08] to [09b698ac10].

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

@implementation TestsAppDelegate (OFHashesTests)
- (void)hashesTests
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFMD5Hash *md5;
	OFSHA1Hash *sha1;
	OFFile *f = [OFFile fileWithPath: @"testfile"
				    mode: @"rb"];

	TEST(@"+[md5Hash]", (md5 = [OFMD5Hash md5Hash]))

	TEST(@"+[sha1Hash]", (sha1 = [OFSHA1Hash sha1Hash]))

	while (![f atEndOfStream]) {







|







31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

@implementation TestsAppDelegate (OFHashesTests)
- (void)hashesTests
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFMD5Hash *md5;
	OFSHA1Hash *sha1;
	OFFile *f = [OFFile fileWithPath: @"testfile.bin"
				    mode: @"rb"];

	TEST(@"+[md5Hash]", (md5 = [OFMD5Hash md5Hash]))

	TEST(@"+[sha1Hash]", (sha1 = [OFSHA1Hash sha1Hash]))

	while (![f atEndOfStream]) {

Modified tests/OFStringTests.m from [0740a8d2f7] to [b29e32f526].

99
100
101
102
103
104
105





106
107
108
109
110
111
112
	TEST(@"-[lower]", [[s[0] lower] isEqual: @"3𝄞1€sät"] &&
	    [[s[1] lower] isEqual: @"abc"])

	TEST(@"+[stringWithCString:length:]",
	    (s[0] = [OFMutableString stringWithCString: "foobar"
					      length: 3]) &&
	    [s[0] isEqual: @"foo"])






	TEST(@"-[appendCStringWithLength:]",
	    [[s[0] appendCString: "foobarqux" + 3
		      withLength: 3] isEqual: @"foobar"])

	EXPECT_EXCEPTION(@"Detection of invalid UTF-8 encoding #1",
	    OFInvalidEncodingException,







>
>
>
>
>







99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
	TEST(@"-[lower]", [[s[0] lower] isEqual: @"3𝄞1€sät"] &&
	    [[s[1] lower] isEqual: @"abc"])

	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"])

	EXPECT_EXCEPTION(@"Detection of invalid UTF-8 encoding #1",
	    OFInvalidEncodingException,

Deleted tests/testfile version [ac78121630].

cannot compute difference between binary files

Added tests/testfile.bin version [ac78121630].

cannot compute difference between binary files

Added tests/testfile.txt version [11bc6d44ee].



>
1
testäöü