ObjFW  Check-in [4814266280]

Overview
Comment:More style improvements.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 48142662806418e29c58a855ca71d0ca7602a1dc2641143ffb263c3bfd938093
User & Date: js on 2011-04-22 18:00:08
Other Links: manifest | tags
Context
2011-04-22
18:22
Fix double-retain in OFList. check-in: 58d10be52f user: js tags: trunk
18:00
More style improvements. check-in: 4814266280 user: js tags: trunk
16:53
Style improvements in OF(Mutable)Dictionary. check-in: da0d602dc8 user: js tags: trunk
Changes

Modified src/OFDataArray+Hashing.m from [8e2c5834df] to [c3dc8494cf].

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

@implementation OFDataArray (Hashing)
- (OFString*)MD5Hash
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFMD5Hash *hash = [OFMD5Hash MD5Hash];
	uint8_t *digest;
	char cRet[OF_MD5_DIGEST_SIZE * 2];
	size_t i;

	[hash updateWithBuffer: data
			ofSize: count * itemSize];
	digest = [hash digest];

	for (i = 0; i < OF_MD5_DIGEST_SIZE; i++) {
		uint8_t high, low;

		high = digest[i] >> 4;
		low  = digest[i] & 0x0F;

		cRet[i * 2] = (high > 9 ? high - 10 + 'a' : high + '0');
		cRet[i * 2 + 1] = (low > 9 ? low - 10 + 'a' : low + '0');
	}

	[pool release];

	return [OFString stringWithCString: cRet
				    length: 32];
}

- (OFString*)SHA1Hash
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFMD5Hash *hash = [OFSHA1Hash SHA1Hash];
	uint8_t *digest;
	char cRet[OF_SHA1_DIGEST_SIZE * 2];
	size_t i;

	[hash updateWithBuffer: data
			ofSize: count * itemSize];
	digest = [hash digest];

	for (i = 0; i < OF_SHA1_DIGEST_SIZE; i++) {
		uint8_t high, low;

		high = digest[i] >> 4;
		low  = digest[i] & 0x0F;

		cRet[i * 2] = (high > 9 ? high - 10 + 'a' : high + '0');
		cRet[i * 2 + 1] = (low > 9 ? low - 10 + 'a' : low + '0');
	}

	[pool release];

	return [OFString stringWithCString: cRet
				    length: 40];
}
@end







|












|
|




|








|












|
|




|



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

@implementation OFDataArray (Hashing)
- (OFString*)MD5Hash
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFMD5Hash *hash = [OFMD5Hash MD5Hash];
	uint8_t *digest;
	char retCString[OF_MD5_DIGEST_SIZE * 2];
	size_t i;

	[hash updateWithBuffer: data
			ofSize: count * itemSize];
	digest = [hash digest];

	for (i = 0; i < OF_MD5_DIGEST_SIZE; i++) {
		uint8_t high, low;

		high = digest[i] >> 4;
		low  = digest[i] & 0x0F;

		retCString[i * 2] = (high > 9 ? high - 10 + 'a' : high + '0');
		retCString[i * 2 + 1] = (low > 9 ? low - 10 + 'a' : low + '0');
	}

	[pool release];

	return [OFString stringWithCString: retCString
				    length: 32];
}

- (OFString*)SHA1Hash
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFMD5Hash *hash = [OFSHA1Hash SHA1Hash];
	uint8_t *digest;
	char retCString[OF_SHA1_DIGEST_SIZE * 2];
	size_t i;

	[hash updateWithBuffer: data
			ofSize: count * itemSize];
	digest = [hash digest];

	for (i = 0; i < OF_SHA1_DIGEST_SIZE; i++) {
		uint8_t high, low;

		high = digest[i] >> 4;
		low  = digest[i] & 0x0F;

		retCString[i * 2] = (high > 9 ? high - 10 + 'a' : high + '0');
		retCString[i * 2 + 1] = (low > 9 ? low - 10 + 'a' : low + '0');
	}

	[pool release];

	return [OFString stringWithCString: retCString
				    length: 40];
}
@end

Modified src/OFFile.h from [7b3dba70a0] to [f7c069ca7f].

30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#endif

/**
 * \brief A class which provides functions to read, write and manipulate files.
 */
@interface OFFile: OFSeekableStream
{
	int  fd;
	BOOL closable;
	BOOL eos;
}

/**
 * \param path The path to the file to open as a string
 * \param mode The mode in which the file should be opened as a string
 * \return A new autoreleased OFFile
 */
+ fileWithPath: (OFString*)path
	  mode: (OFString*)mode;

/**
 * \param fd A file descriptor, returned from for example open().
 *	     It is not closed when the OFFile object is deallocated!
 * \return A new autoreleased OFFile
 */
+ fileWithFileDescriptor: (int)fd;

/**
 * \return The path of the current working directory
 */
+ (OFString*)currentDirectoryPath;

/**







|

|











|
|


|







30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#endif

/**
 * \brief A class which provides functions to read, write and manipulate files.
 */
@interface OFFile: OFSeekableStream
{
	int  fileDescriptor;
	BOOL closable;
	BOOL isAtEndOfStream;
}

/**
 * \param path The path to the file to open as a string
 * \param mode The mode in which the file should be opened as a string
 * \return A new autoreleased OFFile
 */
+ fileWithPath: (OFString*)path
	  mode: (OFString*)mode;

/**
 * \param fileDescriptor A file descriptor, returned from for example open().
 *			 It is not closed when the OFFile object is deallocated!
 * \return A new autoreleased OFFile
 */
+ fileWithFileDescriptor: (int)fileDescriptor;

/**
 * \return The path of the current working directory
 */
+ (OFString*)currentDirectoryPath;

/**
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
		  toOwner: (OFString*)owner
		    group: (OFString*)group;
#endif

/**
 * Copies a file.
 *
 * \param from The file to copy
 * \param to The destination path
 */
+ (void)copyFileAtPath: (OFString*)from
		toPath: (OFString*)to;

/**
 * Renames a file.
 *
 * \param from The file to rename
 * \param to The new name
 */
+ (void)renameFileAtPath: (OFString*)from
		  toPath: (OFString*)to;

/**
 * Deletes a file.
 *
 * \param path The path to the file of which should be deleted as a string
 */
+ (void)deleteFileAtPath: (OFString*)path;







|
|

|
|




|
|

|
|







125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
		  toOwner: (OFString*)owner
		    group: (OFString*)group;
#endif

/**
 * Copies a file.
 *
 * \param source The file to copy
 * \param destination The destination path
 */
+ (void)copyFileAtPath: (OFString*)source
		toPath: (OFString*)destination;

/**
 * Renames a file.
 *
 * \param source The file to rename
 * \param destination The new name
 */
+ (void)renameFileAtPath: (OFString*)source
		  toPath: (OFString*)destination;

/**
 * Deletes a file.
 *
 * \param path The path to the file of which should be deleted as a string
 */
+ (void)deleteFileAtPath: (OFString*)path;
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214

#ifndef _WIN32
/**
 * Hardlinks a file.
 *
 * Not available on Windows.
 *
 * \param src The path to the file of which should be linked as a string
 * \param dest The path to where the file should be linked as a string
 */
+ (void)linkFileAtPath: (OFString*)src
		toPath: (OFString*)dest;
#endif

#if !defined(_WIN32) && !defined(_PSP)
/**
 * Symlinks a file.
 *
 * Not available on Windows.
 *
 * \param src The path to the file of which should be symlinked as a string
 * \param dest The path to where the file should be symlinked as a string
 */
+ (void)symlinkFileAtPath: (OFString*)src
		   toPath: (OFString*)dest;
#endif

/**
 * Initializes an already allocated OFFile.
 *
 * \param path The path to the file to open as a string
 * \param mode The mode in which the file should be opened as a string
 * \return An initialized OFFile
 */
- initWithPath: (OFString*)path
	  mode: (OFString*)mode;

/**
 * Initializes an already allocated OFFile.
 *
 * \param fd A file descriptor, returned from for example open().
 *	     It is not closed when the OFFile object is deallocated!
 */
- initWithFileDescriptor: (int)fd;
@end

#ifdef __cplusplus
extern "C" {
#endif
extern OFFile *of_stdin;
extern OFFile *of_stdout;
extern OFFile *of_stderr;
#ifdef __cplusplus
}
#endif







|
|

|
|








|
|

|
|















|
|

|











160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214

#ifndef _WIN32
/**
 * Hardlinks a file.
 *
 * Not available on Windows.
 *
 * \param source The path to the file of which should be linked as a string
 * \param destination The path to where the file should be linked as a string
 */
+ (void)linkFileAtPath: (OFString*)source
		toPath: (OFString*)destination;
#endif

#if !defined(_WIN32) && !defined(_PSP)
/**
 * Symlinks a file.
 *
 * Not available on Windows.
 *
 * \param source The path to the file of which should be symlinked as a string
 * \param destination The path to where the file should be symlinked as a string
 */
+ (void)symlinkFileAtPath: (OFString*)source
		   toPath: (OFString*)destination;
#endif

/**
 * Initializes an already allocated OFFile.
 *
 * \param path The path to the file to open as a string
 * \param mode The mode in which the file should be opened as a string
 * \return An initialized OFFile
 */
- initWithPath: (OFString*)path
	  mode: (OFString*)mode;

/**
 * Initializes an already allocated OFFile.
 *
 * \param fileDescriptor A file descriptor, returned from for example open().
 *			 It is not closed when the OFFile object is deallocated!
 */
- initWithFileDescriptor: (int)fileDescriptor;
@end

#ifdef __cplusplus
extern "C" {
#endif
extern OFFile *of_stdin;
extern OFFile *of_stdout;
extern OFFile *of_stderr;
#ifdef __cplusplus
}
#endif

Modified src/OFFile.m from [696b6c1432] to [437b42878c].

47
48
49
50
51
52
53

54
55
56
57
58
59
60
#import "OFCreateDirectoryFailedException.h"
#import "OFDeleteDirectoryFailedException.h"
#import "OFDeleteFileFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFLinkFailedException.h"
#import "OFNotImplementedException.h"
#import "OFOpenFileFailedException.h"

#import "OFReadFailedException.h"
#import "OFRenameFileFailedException.h"
#import "OFSeekFailedException.h"
#import "OFSymlinkFailedException.h"
#import "OFWriteFailedException.h"

#import "macros.h"







>







47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#import "OFCreateDirectoryFailedException.h"
#import "OFDeleteDirectoryFailedException.h"
#import "OFDeleteFileFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFLinkFailedException.h"
#import "OFNotImplementedException.h"
#import "OFOpenFileFailedException.h"
#import "OFOutOfMemoryException.h"
#import "OFReadFailedException.h"
#import "OFRenameFileFailedException.h"
#import "OFSeekFailedException.h"
#import "OFSymlinkFailedException.h"
#import "OFWriteFailedException.h"

#import "macros.h"
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
145
146
147
148
	if (!strcmp(mode, "ab+") || !strcmp(mode, "a+b"))
		return O_RDWR | O_CREAT | O_APPEND | O_BINARY;

	return -1;
}

void
of_log(OFConstantString *fmt, ...)
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFDate *date;
	OFString *date_str, *me, *msg;
	va_list args;

	date = [OFDate date];
	date_str = [date localDateStringWithFormat: @"%Y-%m-%d %H:%M:%S"];
	me = [[OFApplication programName] lastPathComponent];

	va_start(args, fmt);
	msg = [[[OFString alloc] initWithFormat: fmt
				      arguments: args] autorelease];
	va_end(args);

	[of_stderr writeFormat: @"[%@.%03d %@(%d)] %@\n", date_str,
				[date microsecond] / 1000, me, getpid(), msg];

	[pool release];
}

@interface OFFileSingleton: OFFile
@end







|



|
|


|


|
|
|
|

|







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
145
146
147
148
149
	if (!strcmp(mode, "ab+") || !strcmp(mode, "a+b"))
		return O_RDWR | O_CREAT | O_APPEND | O_BINARY;

	return -1;
}

void
of_log(OFConstantString *format, ...)
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFDate *date;
	OFString *dateString, *me, *msg;
	va_list arguments;

	date = [OFDate date];
	dateString = [date localDateStringWithFormat: @"%Y-%m-%d %H:%M:%S"];
	me = [[OFApplication programName] lastPathComponent];

	va_start(arguments, format);
	msg = [[[OFString alloc] initWithFormat: format
				      arguments: arguments] autorelease];
	va_end(arguments);

	[of_stderr writeFormat: @"[%@.%03d %@(%d)] %@\n", dateString,
				[date microsecond] / 1000, me, getpid(), msg];

	[pool release];
}

@interface OFFileSingleton: OFFile
@end
166
167
168
169
170
171
172
173
174
175

176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
+ fileWithPath: (OFString*)path
	  mode: (OFString*)mode
{
	return [[[self alloc] initWithPath: path
				      mode: mode] autorelease];
}

+ fileWithFileDescriptor: (int)fd_
{
	return [[[self alloc] initWithFileDescriptor: fd_] autorelease];

}

+ (OFString*)currentDirectoryPath
{
	OFString *ret;
	char *buf = getcwd(NULL, 0);

	@try {
		ret = [OFString stringWithCString: buf];
	} @finally {
		free(buf);
	}

	return ret;
}

+ (BOOL)fileExistsAtPath: (OFString*)path
{







|

|
>





|


|

|







167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
+ fileWithPath: (OFString*)path
	  mode: (OFString*)mode
{
	return [[[self alloc] initWithPath: path
				      mode: mode] autorelease];
}

+ fileWithFileDescriptor: (int)filedescriptor
{
	return [[[self alloc]
	    initWithFileDescriptor: filedescriptor] autorelease];
}

+ (OFString*)currentDirectoryPath
{
	OFString *ret;
	char *buffer = getcwd(NULL, 0);

	@try {
		ret = [OFString stringWithCString: buffer];
	} @finally {
		free(buffer);
	}

	return ret;
}

+ (BOOL)fileExistsAtPath: (OFString*)path
{
260
261
262
263
264
265
266

267
268
269
270
271
272
273
274
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
301
	} @finally {
		closedir(dir);
	}
#else
	HANDLE handle;
	WIN32_FIND_DATA fd;


	path = [path stringByAppendingString: @"\\*"];

	if ((handle = FindFirstFile([path cString], &fd)) ==
	    INVALID_HANDLE_VALUE)
		@throw [OFOpenFileFailedException newWithClass: self
							  path: path
							  mode: @"r"];

	@try {
		pool = [[OFAutoreleasePool alloc] init];

		do {
			OFString *file;

			if (!strcmp(fd.cFileName, ".") ||
			    !strcmp(fd.cFileName, ".."))
				continue;

			file = [OFString stringWithCString: fd.cFileName];
			[files addObject: file];

			[pool releaseObjects];
		} while (FindNextFile(handle, &fd));

		[pool release];
	} @finally {
		FindClose(handle);
	}


#endif

	/*
	 * Class swizzle the array to be immutable. We declared the return type
	 * to be OFArray*, so it can't be modified anyway. But not swizzling it
	 * would create a real copy each time -[copy] is called.
	 */







>









|











|


|



>
>







262
263
264
265
266
267
268
269
270
271
272
273
274
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
301
302
303
304
305
306
	} @finally {
		closedir(dir);
	}
#else
	HANDLE handle;
	WIN32_FIND_DATA fd;

	pool = [[OFAutoreleasePool alloc] init];
	path = [path stringByAppendingString: @"\\*"];

	if ((handle = FindFirstFile([path cString], &fd)) ==
	    INVALID_HANDLE_VALUE)
		@throw [OFOpenFileFailedException newWithClass: self
							  path: path
							  mode: @"r"];

	@try {
		OFAutoreleasePool pool2 = [[OFAutoreleasePool alloc] init];

		do {
			OFString *file;

			if (!strcmp(fd.cFileName, ".") ||
			    !strcmp(fd.cFileName, ".."))
				continue;

			file = [OFString stringWithCString: fd.cFileName];
			[files addObject: file];

			[pool2 releaseObjects];
		} while (FindNextFile(handle, &fd));

		[pool2 release];
	} @finally {
		FindClose(handle);
	}

	[pool release];
#endif

	/*
	 * Class swizzle the array to be immutable. We declared the return type
	 * to be OFArray*, so it can't be modified anyway. But not swizzling it
	 * would create a real copy each time -[copy] is called.
	 */
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
{
# ifndef _WIN32
	if (chmod([path cString], mode))
		@throw [OFChangeFileModeFailedException newWithClass: self
								path: path
								mode: mode];
# else
	DWORD attrs = GetFileAttributes([path cString]);

	if (attrs == INVALID_FILE_ATTRIBUTES)
		@throw [OFChangeFileModeFailedException newWithClass: self
								path: path
								mode: mode];

	if ((mode / 100) & 2)
		attrs &= ~FILE_ATTRIBUTE_READONLY;
	else
		attrs |= FILE_ATTRIBUTE_READONLY;

	if (!SetFileAttributes([path cString], attrs))
		@throw [OFChangeFileModeFailedException newWithClass: self
								path: path
								mode: mode];
# endif
}
#endif








|

|





|

|

|







321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
{
# ifndef _WIN32
	if (chmod([path cString], mode))
		@throw [OFChangeFileModeFailedException newWithClass: self
								path: path
								mode: mode];
# else
	DWORD attributes = GetFileAttributes([path cString]);

	if (attributes == INVALID_FILE_ATTRIBUTES)
		@throw [OFChangeFileModeFailedException newWithClass: self
								path: path
								mode: mode];

	if ((mode / 100) & 2)
		attributes &= ~FILE_ATTRIBUTE_READONLY;
	else
		attributes |= FILE_ATTRIBUTE_READONLY;

	if (!SetFileAttributes([path cString], attributes))
		@throw [OFChangeFileModeFailedException newWithClass: self
								path: path
								mode: mode];
# endif
}
#endif

368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424

425
426
427
428
429

430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449

450
451
452
453
454
455

456
457
458
459
460
461
462
463


464
465
466

467
468
469
470
471
472
473
474
475
476


477
478
479
480
481
482
483

# ifdef OF_THREADS
	[mutex lock];

	@try {
# endif
		if (owner != nil) {
			struct passwd *pw;

			if ((pw = getpwnam([owner cString])) == NULL)
				@throw [OFChangeFileOwnerFailedException
				    newWithClass: self
					    path: path
					   owner: owner
					   group: group];

			uid = pw->pw_uid;
		}

		if (group != nil) {
			struct group *gr;

			if ((gr = getgrnam([group cString])) == NULL)
				@throw [OFChangeFileOwnerFailedException
				    newWithClass: self
					    path: path
					   owner: owner
					   group: group];

			gid = gr->gr_gid;
		}
# ifdef OF_THREADS
	} @finally {
		[mutex unlock];
	}
# endif

	if (chown([path cString], uid, gid))
		@throw [OFChangeFileOwnerFailedException newWithClass: self
								 path: path
								owner: owner
								group: group];
}
#endif

+ (void)copyFileAtPath: (OFString*)from
		toPath: (OFString*)to
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	BOOL override;
	OFFile *src;
	OFFile *dest;
	char buf[4096];

	if ([self directoryExistsAtPath: to]) {
		OFString *filename = [from lastPathComponent];
		to = [OFString stringWithPath: to, filename, nil];

	}

	override = [self fileExistsAtPath: to];

	src = nil;

	dest = nil;

	@try {
		src = [OFFile fileWithPath: from
				      mode: @"rb"];
		dest = [OFFile fileWithPath: to
				       mode: @"wb"];

		while (![src isAtEndOfStream]) {
			size_t len = [src readNBytes: 4096
					  intoBuffer: buf];
			[dest writeNBytes: len
			       fromBuffer: buf];
		}

#if !defined(_WIN32) && !defined(_PSP)
		if (!override) {
			struct stat s;

			if (fstat(src->fd, &s) == 0)

				fchmod(dest->fd, s.st_mode);
		}
#endif
	} @finally {
		[src close];
		[dest close];

	}

	[pool release];
}

+ (void)renameFileAtPath: (OFString*)from
		  toPath: (OFString*)to
{


	if ([self directoryExistsAtPath: to]) {
		OFString *filename = [from lastPathComponent];
		to = [OFString stringWithPath: to, filename, nil];

	}

#ifndef _WIN32
	if (rename([from cString], [to cString]))
#else
	if (!MoveFile([from cString], [to cString]))
#endif
		@throw [OFRenameFileFailedException newWithClass: self
						      sourcePath: from
						 destinationPath: to];


}

+ (void)deleteFileAtPath: (OFString*)path
{
#ifndef _WIN32
	if (unlink([path cString]))
#else







|

|






|



|

|






|















|
|



|
|
|

|
|
|
>


|

|
>
|


|
|
|
|

|
|
|
|
|






|
>
|



|
|
>





|
|

>
>
|
|
|
>



|

|


|
|
>
>







373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497

# ifdef OF_THREADS
	[mutex lock];

	@try {
# endif
		if (owner != nil) {
			struct passwd *passwd;

			if ((passwd = getpwnam([owner cString])) == NULL)
				@throw [OFChangeFileOwnerFailedException
				    newWithClass: self
					    path: path
					   owner: owner
					   group: group];

			uid = passwd->pw_uid;
		}

		if (group != nil) {
			struct group *group_;

			if ((group_ = getgrnam([group cString])) == NULL)
				@throw [OFChangeFileOwnerFailedException
				    newWithClass: self
					    path: path
					   owner: owner
					   group: group];

			gid = group_->gr_gid;
		}
# ifdef OF_THREADS
	} @finally {
		[mutex unlock];
	}
# endif

	if (chown([path cString], uid, gid))
		@throw [OFChangeFileOwnerFailedException newWithClass: self
								 path: path
								owner: owner
								group: group];
}
#endif

+ (void)copyFileAtPath: (OFString*)source
		toPath: (OFString*)destination
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	BOOL override;
	OFFile *sourceFile = nil;
	OFFile *destinationFile = nil;
	char *buffer;

	if ([self directoryExistsAtPath: destination]) {
		OFString *filename = [source lastPathComponent];
		destination = [OFString stringWithPath: destination, filename,
							nil];
	}

	override = [self fileExistsAtPath: destination];

	if ((buffer = malloc(of_pagesize)) == NULL)
		@throw [OFOutOfMemoryException newWithClass: self
					      requestedSize: of_pagesize];

	@try {
		sourceFile = [OFFile fileWithPath: source
					     mode: @"rb"];
		destinationFile = [OFFile fileWithPath: destination
						  mode: @"wb"];

		while (![sourceFile isAtEndOfStream]) {
			size_t len = [sourceFile readNBytes: of_pagesize
						 intoBuffer: buffer];
			[destinationFile writeNBytes: len
					  fromBuffer: buffer];
		}

#if !defined(_WIN32) && !defined(_PSP)
		if (!override) {
			struct stat s;

			if (fstat(sourceFile->fileDescriptor, &s) == 0)
				fchmod(destinationFile->fileDescriptor,
				    s.st_mode);
		}
#endif
	} @finally {
		[sourceFile close];
		[destinationFile close];
		free(buffer);
	}

	[pool release];
}

+ (void)renameFileAtPath: (OFString*)source
		  toPath: (OFString*)destination
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];

	if ([self directoryExistsAtPath: destination]) {
		OFString *filename = [source lastPathComponent];
		destination = [OFString stringWithPath: destination, filename,
							nil];
	}

#ifndef _WIN32
	if (rename([source cString], [destination cString]))
#else
	if (!MoveFile([source cString], [destination cString]))
#endif
		@throw [OFRenameFileFailedException newWithClass: self
						      sourcePath: source
						 destinationPath: destination];

	[pool release];
}

+ (void)deleteFileAtPath: (OFString*)path
{
#ifndef _WIN32
	if (unlink([path cString]))
#else
491
492
493
494
495
496
497
498
499
500


501
502
503

504
505
506
507
508
509


510
511
512
513
514
515
516


517
518
519

520
521
522
523
524
525


526
527
528
529
530
531
532
{
	if (rmdir([path cString]))
		@throw [OFDeleteDirectoryFailedException newWithClass: self
								 path: path];
}

#ifndef _WIN32
+ (void)linkFileAtPath: (OFString*)src
		toPath: (OFString*)dest
{


	if ([self directoryExistsAtPath: dest]) {
		OFString *filename = [src lastPathComponent];
		dest = [OFString stringWithPath: dest, filename, nil];

	}

	if (link([src cString], [dest cString]) != 0)
		@throw [OFLinkFailedException newWithClass: self
						sourcePath: src
					   destinationPath: dest];


}
#endif

#if !defined(_WIN32) && !defined(_PSP)
+ (void)symlinkFileAtPath: (OFString*)src
		   toPath: (OFString*)dest
{


	if ([self directoryExistsAtPath: dest]) {
		OFString *filename = [src lastPathComponent];
		dest = [OFString stringWithPath: dest, filename, nil];

	}

	if (symlink([src cString], [dest cString]) != 0)
		@throw [OFSymlinkFailedException newWithClass: self
						   sourcePath: src
					      destinationPath: dest];


}
#endif

- init
{
	Class c = isa;
	[self release];







|
|

>
>
|
|
|
>


|

|
|
>
>




|
|

>
>
|
|
|
>


|

|
|
>
>







505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
{
	if (rmdir([path cString]))
		@throw [OFDeleteDirectoryFailedException newWithClass: self
								 path: path];
}

#ifndef _WIN32
+ (void)linkFileAtPath: (OFString*)source
		toPath: (OFString*)destination
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];

	if ([self directoryExistsAtPath: destination]) {
		OFString *filename = [source lastPathComponent];
		destination = [OFString stringWithPath: destination, filename,
							nil];
	}

	if (link([source cString], [destination cString]) != 0)
		@throw [OFLinkFailedException newWithClass: self
						sourcePath: source
					   destinationPath: destination];

	[pool release];
}
#endif

#if !defined(_WIN32) && !defined(_PSP)
+ (void)symlinkFileAtPath: (OFString*)source
		   toPath: (OFString*)destination
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];

	if ([self directoryExistsAtPath: destination]) {
		OFString *filename = [source lastPathComponent];
		destination = [OFString stringWithPath: destination, filename,
							nil];
	}

	if (symlink([source cString], [destination cString]) != 0)
		@throw [OFSymlinkFailedException newWithClass: self
						   sourcePath: source
					      destinationPath: destination];

	[pool release];
}
#endif

- init
{
	Class c = isa;
	[self release];
542
543
544
545
546
547
548
549

550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600

601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
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
	@try {
		int flags;

		if ((flags = parse_mode([mode cString])) == -1)
			@throw [OFInvalidArgumentException newWithClass: isa
							       selector: _cmd];

		if ((fd = open([path cString], flags, DEFAULT_MODE)) == -1)

			@throw [OFOpenFileFailedException newWithClass: isa
								  path: path
								  mode: mode];

		closable = YES;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithFileDescriptor: (int)fd_
{
	self = [super init];

	fd = fd_;

	return self;
}

- (BOOL)_isAtEndOfStream
{
	if (fd == -1)
		return YES;

	return eos;
}

- (size_t)_readNBytes: (size_t)size
	   intoBuffer: (char*)buf
{
	size_t ret;

	if (fd == -1 || eos)
		@throw [OFReadFailedException newWithClass: isa
						    stream: self
					     requestedSize: size];
	if ((ret = read(fd, buf, size)) == 0)
		eos = YES;

	return ret;
}

- (size_t)_writeNBytes: (size_t)size
	    fromBuffer: (const char*)buf
{
	size_t ret;

	if (fd == -1 || eos || (ret = write(fd, buf, size)) < size)

		@throw [OFWriteFailedException newWithClass: isa
						     stream: self
					      requestedSize: size];

	return ret;
}

- (void)_seekToOffset: (off_t)offset
{
	if (lseek(fd, offset, SEEK_SET) == -1)
		@throw [OFSeekFailedException newWithClass: isa
						    stream: self
						    offset: offset
						    whence: SEEK_SET];
}

- (off_t)_seekForwardWithOffset: (off_t)offset
{
	off_t ret;

	if ((ret = lseek(fd, offset, SEEK_CUR)) == -1)
		@throw [OFSeekFailedException newWithClass: isa
						    stream: self
						    offset: offset
						    whence: SEEK_CUR];

	return ret;
}

- (off_t)_seekToOffsetRelativeToEnd: (off_t)offset
{
	off_t ret;

	if ((ret = lseek(fd, offset, SEEK_END)) == -1)
		@throw [OFSeekFailedException newWithClass: isa
						    stream: self
						    offset: offset
						    whence: SEEK_END];

	return ret;
}

- (int)fileDescriptor
{
	return fd;
}

- (void)close
{
	if (fd != -1)
		close(fd);

	fd = -1;
}

- (void)dealloc
{
	if (closable && fd != -1)
		close(fd);

	[super dealloc];
}
@end

@implementation OFFileSingleton
- initWithPath: (OFString*)path







|
>













|



|






|


|


|
|

|

|


|
|
|

|


|
|

|

|
>


|

|




|








|

|





|




|

|





|




|




|
|
>
|




|
|







566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
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
	@try {
		int flags;

		if ((flags = parse_mode([mode cString])) == -1)
			@throw [OFInvalidArgumentException newWithClass: isa
							       selector: _cmd];

		if ((fileDescriptor = open([path cString], flags,
		    DEFAULT_MODE)) == -1)
			@throw [OFOpenFileFailedException newWithClass: isa
								  path: path
								  mode: mode];

		closable = YES;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithFileDescriptor: (int)fileDescriptor_
{
	self = [super init];

	fileDescriptor = fileDescriptor_;

	return self;
}

- (BOOL)_isAtEndOfStream
{
	if (fileDescriptor == -1)
		return YES;

	return isAtEndOfStream;
}

- (size_t)_readNBytes: (size_t)length
	   intoBuffer: (char*)buffer
{
	size_t retLength;

	if (fileDescriptor == -1 || isAtEndOfStream)
		@throw [OFReadFailedException newWithClass: isa
						    stream: self
					   requestedLength: length];
	if ((retLength = read(fileDescriptor, buffer, length)) == 0)
		isAtEndOfStream = YES;

	return retLength;
}

- (size_t)_writeNBytes: (size_t)length
	    fromBuffer: (const char*)buffer
{
	size_t retLength;

	if (fileDescriptor == -1 || isAtEndOfStream ||
	    (retLength = write(fileDescriptor, buffer, length)) < length)
		@throw [OFWriteFailedException newWithClass: isa
						     stream: self
					    requestedLength: length];

	return retLength;
}

- (void)_seekToOffset: (off_t)offset
{
	if (lseek(fileDescriptor, offset, SEEK_SET) == -1)
		@throw [OFSeekFailedException newWithClass: isa
						    stream: self
						    offset: offset
						    whence: SEEK_SET];
}

- (off_t)_seekForwardWithOffset: (off_t)offset
{
	off_t retOffset;

	if ((retOffset = lseek(fileDescriptor, offset, SEEK_CUR)) == -1)
		@throw [OFSeekFailedException newWithClass: isa
						    stream: self
						    offset: offset
						    whence: SEEK_CUR];

	return retOffset;
}

- (off_t)_seekToOffsetRelativeToEnd: (off_t)offset
{
	off_t retOffset;

	if ((retOffset = lseek(fileDescriptor, offset, SEEK_END)) == -1)
		@throw [OFSeekFailedException newWithClass: isa
						    stream: self
						    offset: offset
						    whence: SEEK_END];

	return retOffset;
}

- (int)fileDescriptor
{
	return fileDescriptor;
}

- (void)close
{
	if (fileDescriptor != -1)
		close(fileDescriptor);

	fileDescriptor = -1;
}

- (void)dealloc
{
	if (closable && fileDescriptor != -1)
		close(fileDescriptor);

	[super dealloc];
}
@end

@implementation OFFileSingleton
- initWithPath: (OFString*)path

Modified src/OFSeekableStream.m from [f25fa4bf66] to [693313aa14].

39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

- (void)seekToOffset: (off_t)offset
{
	[self _seekToOffset: offset];

	[self freeMemory: cache];
	cache = NULL;
	cacheLen = 0;
}

- (off_t)seekForwardWithOffset: (off_t)offset
{
	off_t ret;

	ret = [self _seekForwardWithOffset: offset - cacheLen];

	[self freeMemory: cache];
	cache = NULL;
	cacheLen = 0;

	return ret;
}

- (off_t)seekToOffsetRelativeToEnd: (off_t)offset
{
	off_t ret;

	ret = [self _seekToOffsetRelativeToEnd: offset];

	[self freeMemory: cache];
	cache = NULL;
	cacheLen = 0;

	return ret;
}
@end







|




|

|



|

|




|

|



|

|


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

- (void)seekToOffset: (off_t)offset
{
	[self _seekToOffset: offset];

	[self freeMemory: cache];
	cache = NULL;
	cacheLength = 0;
}

- (off_t)seekForwardWithOffset: (off_t)offset
{
	off_t retOffset;

	retOffset = [self _seekForwardWithOffset: offset - cacheLength];

	[self freeMemory: cache];
	cache = NULL;
	cacheLength = 0;

	return retOffset;
}

- (off_t)seekToOffsetRelativeToEnd: (off_t)offset
{
	off_t retOffset;

	retOffset = [self _seekToOffsetRelativeToEnd: offset];

	[self freeMemory: cache];
	cache = NULL;
	cacheLength = 0;

	return retOffset;
}
@end

Modified src/OFStream.h from [0de1b69c7e] to [0372616946].

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
 * not defined in the headers, but do the actual work. OFStream uses those and
 * does all the caching and other stuff. If you override these methods without
 * the _ prefix, you *WILL* break caching and get broken results!
 */
@interface OFStream: OFObject
{
	char   *cache;
	char   *wBuffer;
	size_t cacheLen, wBufferLen;
	BOOL   buffersWrites;
	BOOL   isBlocking;
}

#ifdef OF_HAVE_PROPERTIES
@property (assign, setter=setBlocking:) BOOL isBlocking;
#endif

/**
 * Returns a boolean whether the end of the stream has been reached.
 *
 * \return A boolean whether the end of the stream has been reached
 */
- (BOOL)isAtEndOfStream;

/**
 * Reads at most size bytes from the stream into a buffer.
 *
 * \param buf The buffer into which the data is read
 * \param size The size of the data that should be read at most.
 *	       The buffer MUST be at least size big!
 * \return The number of bytes read
 */
- (size_t)readNBytes: (size_t)size
	  intoBuffer: (char*)buf;

/**
 * Reads exactly size bytes from the stream into a buffer. Unlike
 * readNBytes:intoBuffer:, this method does not return when less than the
 * specified size has been read - instead, it waits until it got exactly size
 * bytes.
 *
 * WARNING: Only call this when you know that specified amount of data is
 *	    available! Otherwise you will get an exception!
 *
 * \param buf The buffer into which the data is read
 * \param size The size of the data that should be read.
 *	       The buffer MUST be EXACTLY this big!
 */
- (void)readExactlyNBytes: (size_t)size
	       intoBuffer: (char*)buf;

/**
 * Reads a uint8_t from the stream.
 *
 * WARNING: Only call this when you know that enough data is available!
 *	    Otherwise you will get an exception!
 *







|
|


















|
|
|



|


|

|





|
|


|
|







29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
 * not defined in the headers, but do the actual work. OFStream uses those and
 * does all the caching and other stuff. If you override these methods without
 * the _ prefix, you *WILL* break caching and get broken results!
 */
@interface OFStream: OFObject
{
	char   *cache;
	char   *writeBuffer;
	size_t cacheLength, writeBufferLength;
	BOOL   buffersWrites;
	BOOL   isBlocking;
}

#ifdef OF_HAVE_PROPERTIES
@property (assign, setter=setBlocking:) BOOL isBlocking;
#endif

/**
 * Returns a boolean whether the end of the stream has been reached.
 *
 * \return A boolean whether the end of the stream has been reached
 */
- (BOOL)isAtEndOfStream;

/**
 * Reads at most size bytes from the stream into a buffer.
 *
 * \param buffer The buffer into which the data is read
 * \param length The length of the data that should be read at most.
 *		 The buffer MUST be at least this big!
 * \return The number of bytes read
 */
- (size_t)readNBytes: (size_t)size
	  intoBuffer: (char*)buffer;

/**
 * Reads exactly length bytes from the stream into a buffer. Unlike
 * readNBytes:intoBuffer:, this method does not return when less than the
 * specified length has been read - instead, it waits until it got exactly length
 * bytes.
 *
 * WARNING: Only call this when you know that specified amount of data is
 *	    available! Otherwise you will get an exception!
 *
 * \param buffer The buffer into which the data is read
 * \param length The length of the data that should be read.
 *	       The buffer MUST be EXACTLY this big!
 */
- (void)readExactlyNBytes: (size_t)length
	       intoBuffer: (char*)buffer;

/**
 * Reads a uint8_t from the stream.
 *
 * WARNING: Only call this when you know that enough data is available!
 *	    Otherwise you will get an exception!
 *
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
 * Reads nitems items with the specified item size from the stream and returns
 * them in an OFDataArray.
 *
 * WARNING: Only call this when you know that enough data is available!
 *	    Otherwise you will get an exception!
 *
 * \param itemsize The size of each item
 * \param nitems The number of iteams to read
 * \return An OFDataArray with at nitems items.
 */
- (OFDataArray*)readDataArrayWithItemSize: (size_t)itemsize
				andNItems: (size_t)nitems;

/**
 * \return An OFDataArray with an item size of 1 with all the data of the
 *	   stream until the end of the stream is reached.
 */
- (OFDataArray*)readDataArrayTillEndOfStream;








|
|


|
|







150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
 * Reads nitems items with the specified item size from the stream and returns
 * them in an OFDataArray.
 *
 * WARNING: Only call this when you know that enough data is available!
 *	    Otherwise you will get an exception!
 *
 * \param itemSize The size of each item
 * \param nItems The number of iteams to read
 * \return An OFDataArray with at nitems items.
 */
- (OFDataArray*)readDataArrayWithItemSize: (size_t)itemSize
				andNItems: (size_t)nItems;

/**
 * \return An OFDataArray with an item size of 1 with all the data of the
 *	   stream until the end of the stream is reached.
 */
- (OFDataArray*)readDataArrayTillEndOfStream;

221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
 * Writes everythig in the write buffer to the stream.
 */
- (void)flushWriteBuffer;

/**
 * Writes from a buffer into the stream.
 *
 * \param buf The buffer from which the data is written to the stream
 * \param size The size of the data that should be written
 * \return The number of bytes written
 */
- (size_t)writeNBytes: (size_t)size
	   fromBuffer: (const char*)buf;

/**
 * Writes a uint8_t into the stream.
 *
 * \param int8 A uint8_t
 */
- (void)writeInt8: (uint8_t)int8;







|
|


|
|







221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
 * Writes everythig in the write buffer to the stream.
 */
- (void)flushWriteBuffer;

/**
 * Writes from a buffer into the stream.
 *
 * \param buffer The buffer from which the data is written to the stream
 * \param length The length of the data that should be written
 * \return The number of bytes written
 */
- (size_t)writeNBytes: (size_t)length
	   fromBuffer: (const char*)buffer;

/**
 * Writes a uint8_t into the stream.
 *
 * \param int8 A uint8_t
 */
- (void)writeInt8: (uint8_t)int8;
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
 * \param int64 A uint64_t
 */
- (void)writeLittleEndianInt64: (uint64_t)int64;

/**
 * Writes from an OFDataArray into the stream.
 *
 * \param dataarray The OFDataArray to write into the stream
 * \return The number of bytes written
 */
- (size_t)writeDataArray: (OFDataArray*)dataarray;

/**
 * Writes a string into the stream, without the trailing zero.
 *
 * \param str The string from which the data is written to the stream
 * \return The number of bytes written
 */
- (size_t)writeString: (OFString*)str;

/**
 * Writes a string into the stream with a trailing newline.
 *
 * \param str The string from which the data is written to the stream
 * \return The number of bytes written
 */
- (size_t)writeLine: (OFString*)str;

/**
 * Writes a formatted string into the stream.
 *
 * \param fmt A string used as format
 * \return The number of bytes written
 */
- (size_t)writeFormat: (OFString*)fmt, ...;

/**
 * Writes a formatted string into the stream.
 *
 * \param fmt A string used as format
 * \param args The arguments used in the format string
 * \return The number of bytes written
 */
- (size_t)writeFormat: (OFString*)fmt
	withArguments: (va_list)args;

/**
 * \return The number of bytes still present in the internal cache.
 */
- (size_t)pendingBytes;

/**







|


|




|


|




|


|




|


|




|
|


|
|







280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
 * \param int64 A uint64_t
 */
- (void)writeLittleEndianInt64: (uint64_t)int64;

/**
 * Writes from an OFDataArray into the stream.
 *
 * \param dataArray The OFDataArray to write into the stream
 * \return The number of bytes written
 */
- (size_t)writeDataArray: (OFDataArray*)dataArray;

/**
 * Writes a string into the stream, without the trailing zero.
 *
 * \param string The string from which the data is written to the stream
 * \return The number of bytes written
 */
- (size_t)writeString: (OFString*)string;

/**
 * Writes a string into the stream with a trailing newline.
 *
 * \param string The string from which the data is written to the stream
 * \return The number of bytes written
 */
- (size_t)writeLine: (OFString*)string;

/**
 * Writes a formatted string into the stream.
 *
 * \param format A string used as format
 * \return The number of bytes written
 */
- (size_t)writeFormat: (OFString*)format, ...;

/**
 * Writes a formatted string into the stream.
 *
 * \param format A string used as format
 * \param arguments The arguments used in the format string
 * \return The number of bytes written
 */
- (size_t)writeFormat: (OFString*)format
	withArguments: (va_list)arguments;

/**
 * \return The number of bytes still present in the internal cache.
 */
- (size_t)pendingBytes;

/**

Modified src/OFStream.m from [585894669b] to [49caf92ef8].

57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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
		@throw [OFNotImplementedException newWithClass: c
						      selector: _cmd];
	}

	self = [super init];

	cache = NULL;
	wBuffer = NULL;
	isBlocking = YES;

	return self;
}

- (BOOL)_isAtEndOfStream
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (size_t)_readNBytes: (size_t)size
	   intoBuffer: (char*)buf
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (size_t)_writeNBytes: (size_t)size
	    fromBuffer: (const char*)buf
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (BOOL)isAtEndOfStream
{
	if (cache != NULL)
		return NO;

	return [self _isAtEndOfStream];
}

- (size_t)readNBytes: (size_t)size
	  intoBuffer: (char*)buf
{
	if (cache == NULL)
		return [self _readNBytes: size
			      intoBuffer: buf];

	if (size >= cacheLen) {
		size_t ret = cacheLen;
		memcpy(buf, cache, cacheLen);

		[self freeMemory: cache];
		cache = NULL;
		cacheLen = 0;

		return ret;
	} else {
		char *tmp = [self allocMemoryWithSize: cacheLen - size];
		memcpy(tmp, cache + size, cacheLen - size);

		memcpy(buf, cache, size);

		[self freeMemory: cache];
		cache = tmp;
		cacheLen -= size;

		return size;
	}
}

- (void)readExactlyNBytes: (size_t)size
	       intoBuffer: (char*)buf
{
	size_t len = 0;

	while (len < size)
		len += [self readNBytes: size - len
			     intoBuffer: buf + len];
}

- (uint8_t)readInt8
{
	uint8_t ret;

	[self readExactlyNBytes: 1







|











|
|





|
|













|
|


|
|

|
|
|



|



|
|

|



|

|



|
|

|

|
|
|







57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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
		@throw [OFNotImplementedException newWithClass: c
						      selector: _cmd];
	}

	self = [super init];

	cache = NULL;
	writeBuffer = NULL;
	isBlocking = YES;

	return self;
}

- (BOOL)_isAtEndOfStream
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (size_t)_readNBytes: (size_t)length
	   intoBuffer: (char*)buffer
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (size_t)_writeNBytes: (size_t)length
	    fromBuffer: (const char*)buffer
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (BOOL)isAtEndOfStream
{
	if (cache != NULL)
		return NO;

	return [self _isAtEndOfStream];
}

- (size_t)readNBytes: (size_t)length
	  intoBuffer: (char*)buffer
{
	if (cache == NULL)
		return [self _readNBytes: length
			      intoBuffer: buffer];

	if (length >= cacheLength) {
		size_t ret = cacheLength;
		memcpy(buffer, cache, cacheLength);

		[self freeMemory: cache];
		cache = NULL;
		cacheLength = 0;

		return ret;
	} else {
		char *tmp = [self allocMemoryWithSize: cacheLength - length];
		memcpy(tmp, cache + length, cacheLength - length);

		memcpy(buffer, cache, length);

		[self freeMemory: cache];
		cache = tmp;
		cacheLength -= length;

		return length;
	}
}

- (void)readExactlyNBytes: (size_t)length
	       intoBuffer: (char*)buffer
{
	size_t readLength = 0;

	while (readLength < length)
		readLength += [self readNBytes: length - readLength
				    intoBuffer: buffer + readLength];
}

- (uint8_t)readInt8
{
	uint8_t ret;

	[self readExactlyNBytes: 1
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
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
263
264
265
266
267
268
269
270
271
272
273
274
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
301
302
303
304
305

306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333


334
335
336
337
338
339



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356

357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389

390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434

435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486

487
488
489
490


491
492
493
494

495
496
497

498
499



500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531

532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585

	[self readExactlyNBytes: 8
		     intoBuffer: (char*)&ret];

	return of_bswap64_if_be(ret);
}

- (OFDataArray*)readDataArrayWithItemSize: (size_t)itemsize
				andNItems: (size_t)nitems
{
	OFDataArray *da;
	char *tmp;

	da = [OFDataArray dataArrayWithItemSize: itemsize];
	tmp = [self allocMemoryForNItems: nitems
				withSize: itemsize];

	@try {
		[self readExactlyNBytes: nitems * itemsize
			     intoBuffer: tmp];

		[da addNItems: nitems
		   fromCArray: tmp];
	} @finally {
		[self freeMemory: tmp];
	}

	return da;
}

- (OFDataArray*)readDataArrayTillEndOfStream
{
	OFDataArray *a;
	char *buf;

	a = [OFDataArray dataArrayWithItemSize: 1];
	buf = [self allocMemoryWithSize: of_pagesize];

	@try {
		while (![self isAtEndOfStream]) {
			size_t size;

			size = [self readNBytes: of_pagesize
				     intoBuffer: buf];
			[a addNItems: size
			  fromCArray: buf];
		}
	} @finally {
		[self freeMemory: buf];
	}

	return a;
}

- (OFString*)readLine
{
	return [self readLineWithEncoding: OF_STRING_ENCODING_UTF_8];
}

- (OFString*)readLineWithEncoding: (of_string_encoding_t)encoding
{
	size_t i, len, ret_len;
	char *ret_c, *tmp, *tmp2;
	OFString *ret;

	/* Look if there's a line or \0 in our cache */
	if (cache != NULL) {
		for (i = 0; i < cacheLen; i++) {
			if (OF_UNLIKELY(cache[i] == '\n' ||
			    cache[i] == '\0')) {
				ret_len = i;

				if (i > 0 && cache[i - 1] == '\r')
					ret_len--;

				ret = [OFString stringWithCString: cache
							 encoding: encoding
							   length: ret_len];


				tmp = [self allocMemoryWithSize: cacheLen -
								 i - 1];
				if (tmp != NULL)
					memcpy(tmp, cache + i + 1,
					    cacheLen - i - 1);

				[self freeMemory: cache];
				cache = tmp;
				cacheLen -= i + 1;

				return ret;
			}
		}
	}

	/* Read until we get a newline or \0 */
	tmp = [self allocMemoryWithSize: of_pagesize];

	@try {
		for (;;) {
			if ([self _isAtEndOfStream]) {
				if (cache == NULL)
					return nil;

				ret_len = cacheLen;


				if (ret_len > 0 && cache[ret_len - 1] == '\r')
					ret_len--;

				ret = [OFString stringWithCString: cache
							 encoding: encoding
							   length: ret_len];

				[self freeMemory: cache];
				cache = NULL;
				cacheLen = 0;

				return ret;
			}

			len = [self _readNBytes: of_pagesize
				     intoBuffer: tmp];

			/* Look if there's a newline or \0 */
			for (i = 0; i < len; i++) {
				if (OF_UNLIKELY(tmp[i] == '\n' ||
				    tmp[i] == '\0')) {
					ret_len = cacheLen + i;
					ret_c = [self
					    allocMemoryWithSize: ret_len];

					if (cache != NULL)
						memcpy(ret_c, cache, cacheLen);
					memcpy(ret_c + cacheLen, tmp, i);



					if (ret_len > 0 &&
					    ret_c[ret_len - 1] == '\r')
						ret_len--;

					@try {



						ret = [OFString
						    stringWithCString: ret_c
							     encoding: encoding
							       length: ret_len];
					} @catch (id e) {
						/*
						 * Append data to cache to
						 * prevent loss of data due to
						 * wrong encoding.
						 */
						cache = [self
						    resizeMemory: cache
							  toSize: cacheLen +
								  len];

						if (cache != NULL)
							memcpy(cache + cacheLen,

							    tmp, len);

						cacheLen += len;

						@throw e;
					} @finally {
						[self freeMemory: ret_c];
					}

					tmp2 = [self
					    allocMemoryWithSize: len - i - 1];
					if (tmp2 != NULL)
						memcpy(tmp2, tmp + i + 1,
						    len - i - 1);

					[self freeMemory: cache];
					cache = tmp2;
					cacheLen = len - i - 1;

					return ret;
				}
			}

			/* There was no newline or \0 */
			cache = [self resizeMemory: cache
					    toSize: cacheLen + len];

			/*
			 * It's possible that cacheLen + len is 0 and thus
			 * cache was set to NULL by resizeMemory:toSize:.
			 */
			if (cache != NULL)
				memcpy(cache + cacheLen, tmp, len);


			cacheLen += len;
		}
	} @finally {
		[self freeMemory: tmp];
	}

	/* Get rid of a warning, never reached anyway */
	assert(0);
}

- (OFString*)readTillDelimiter: (OFString*)delimiter
{
	return [self readTillDelimiter: delimiter
			  withEncoding: OF_STRING_ENCODING_UTF_8];
}

- (OFString*)readTillDelimiter: (OFString*)delimiter
		  withEncoding: (of_string_encoding_t)encoding
{
	const char *delim;
	size_t i, j, delim_len, len, ret_len;
	char *ret_c, *tmp, *tmp2;
	OFString *ret;

	/* FIXME: Convert delimiter to specified charset */
	delim = [delimiter cString];
	delim_len = [delimiter cStringLength];
	j = 0;

	if (delim_len == 0)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	/* Look if there's something in our cache */
	if (cache != NULL) {
		for (i = 0; i < cacheLen; i++) {
			if (cache[i] != delim[j++])
				j = 0;

			if (j == delim_len || cache[i] == '\0') {
				if (cache[i] == '\0')
					delim_len = 1;

				ret = [OFString stringWithCString: cache

							 encoding: encoding
							   length: i + 1 -
								   delim_len];

				tmp = [self allocMemoryWithSize: cacheLen - i -
								 1];
				if (tmp != NULL)
					memcpy(tmp, cache + i + 1,
					    cacheLen - i - 1);

				[self freeMemory: cache];
				cache = tmp;
				cacheLen -= i + 1;

				return ret;
			}
		}
	}

	/* Read until we get the delimiter or \0 */
	tmp = [self allocMemoryWithSize: of_pagesize];

	@try {
		for (;;) {
			if ([self _isAtEndOfStream]) {
				if (cache == NULL)
					return nil;

				ret = [OFString stringWithCString: cache
							 encoding: encoding
							   length: cacheLen];

				[self freeMemory: cache];
				cache = NULL;
				cacheLen = 0;

				return ret;
			}

			len = [self _readNBytes: of_pagesize
				     intoBuffer: tmp];

			/* Look if there's the delimiter or \0 */
			for (i = 0; i < len; i++) {
				if (tmp[i] != delim[j++])
					j = 0;

				if (j == delim_len || tmp[i] == '\0') {
					if (tmp[i] == '\0')
						delim_len = 1;

					ret_len = cacheLen + i + 1 - delim_len;

					ret_c = [self
					    allocMemoryWithSize: ret_len];

					if (cache != NULL &&


					    cacheLen <= ret_len)
						memcpy(ret_c, cache, cacheLen);
					else if (cache != NULL)
						memcpy(ret_c, cache, ret_len);

					if (i >= delim_len)
						memcpy(ret_c + cacheLen, tmp,
						    i + 1 - delim_len);


					@try {



						ret = [OFString
						    stringWithCString: ret_c
							     encoding: encoding
							       length: ret_len];
					} @finally {
						[self freeMemory: ret_c];
					}

					tmp2 = [self
					    allocMemoryWithSize: len - i - 1];
					if (tmp2 != NULL)
						memcpy(tmp2, tmp + i + 1,
						    len - i - 1);

					[self freeMemory: cache];
					cache = tmp2;
					cacheLen = len - i - 1;

					return ret;
				}
			}

			/* Neither the delimiter nor \0 was found */
			cache = [self resizeMemory: cache
					    toSize: cacheLen + len];

			/*
			 * It's possible that cacheLen + len is 0 and thus
			 * cache was set to NULL by resizeMemory:toSize:.
			 */
			if (cache != NULL)
				memcpy(cache + cacheLen, tmp, len);


			cacheLen += len;
		}
	} @finally {
		[self freeMemory: tmp];
	}

	/* Get rid of a warning, never reached anyway */
	assert(0);
}

- (BOOL)buffersWrites
{
	return buffersWrites;
}

- (void)setBuffersWrites: (BOOL)enable
{
	buffersWrites = enable;
}

- (void)flushWriteBuffer
{
	if (wBuffer == NULL)
		return;

	[self _writeNBytes: wBufferLen
		fromBuffer: wBuffer];

	[self freeMemory: wBuffer];
	wBuffer = NULL;
	wBufferLen = 0;
}

- (size_t)writeNBytes: (size_t)size
	   fromBuffer: (const char*)buf
{
	if (!buffersWrites)
		return [self _writeNBytes: size
			       fromBuffer: buf];
	else {
		wBuffer = [self resizeMemory: wBuffer
				      toSize: wBufferLen + size];
		memcpy(wBuffer + wBufferLen, buf, size);
		wBufferLen += size;

		return size;
	}
}

- (void)writeInt8: (uint8_t)int8
{
	[self writeNBytes: 1
	       fromBuffer: (char*)&int8];







|
|




|
|
|


|


|










|
|

|
|



|

|
|
|
|


|


|









|
|




|


|


|



|

>
|
<
|
|
|


|
|







|







|

>
|
|



|



|




|
|


|
|
|
|
|
|


|
|
>
>

|
|
|


>
>
>

|

|








|
|


|
>
|

|



|


|
|
|
|
|


|
|







|






|
>

|


|















|
|
|



|
|


|





|
|


|

|

|
>
|
|
<

|
|
|
|
|


|
|







|









|



|




|
|


|
|


|
|
|

|
>
|
|


>
>
|
<

|
>
|
|
|
>


>
>
>

|

|

|


|
|
|
|
|


|
|







|






|
>

|


|


















|


|
|

|
|
|


|
|


|
|

|
|
|
|

|







201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
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
263
264
265
266
267
268
269
270
271
272
273
274
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445

446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502

503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601

	[self readExactlyNBytes: 8
		     intoBuffer: (char*)&ret];

	return of_bswap64_if_be(ret);
}

- (OFDataArray*)readDataArrayWithItemSize: (size_t)itemSize
				andNItems: (size_t)nItems
{
	OFDataArray *da;
	char *tmp;

	da = [OFDataArray dataArrayWithItemSize: itemSize];
	tmp = [self allocMemoryForNItems: nItems
				withSize: itemSize];

	@try {
		[self readExactlyNBytes: nItems * itemSize
			     intoBuffer: tmp];

		[da addNItems: nItems
		   fromCArray: tmp];
	} @finally {
		[self freeMemory: tmp];
	}

	return da;
}

- (OFDataArray*)readDataArrayTillEndOfStream
{
	OFDataArray *dataArray;
	char *buffer;

	dataArray = [OFDataArray dataArrayWithItemSize: 1];
	buffer = [self allocMemoryWithSize: of_pagesize];

	@try {
		while (![self isAtEndOfStream]) {
			size_t length;

			length = [self readNBytes: of_pagesize
				       intoBuffer: buffer];
			[dataArray addNItems: length
				  fromCArray: buffer];
		}
	} @finally {
		[self freeMemory: buffer];
	}

	return dataArray;
}

- (OFString*)readLine
{
	return [self readLineWithEncoding: OF_STRING_ENCODING_UTF_8];
}

- (OFString*)readLineWithEncoding: (of_string_encoding_t)encoding
{
	size_t i, bufferLength, retLength;
	char *retCString, *buffer, *newCache;
	OFString *ret;

	/* Look if there's a line or \0 in our cache */
	if (cache != NULL) {
		for (i = 0; i < cacheLength; i++) {
			if (OF_UNLIKELY(cache[i] == '\n' ||
			    cache[i] == '\0')) {
				retLength = i;

				if (i > 0 && cache[i - 1] == '\r')
					retLength--;

				ret = [OFString stringWithCString: cache
							 encoding: encoding
							   length: retLength];

				newCache = [self
				    allocMemoryWithSize: cacheLength - i - 1];

				if (newCache != NULL)
					memcpy(newCache, cache + i + 1,
					    cacheLength - i - 1);

				[self freeMemory: cache];
				cache = newCache;
				cacheLength -= i + 1;

				return ret;
			}
		}
	}

	/* Read until we get a newline or \0 */
	buffer = [self allocMemoryWithSize: of_pagesize];

	@try {
		for (;;) {
			if ([self _isAtEndOfStream]) {
				if (cache == NULL)
					return nil;

				retLength = cacheLength;

				if (retLength > 0 &&
				    cache[retLength - 1] == '\r')
					retLength--;

				ret = [OFString stringWithCString: cache
							 encoding: encoding
							   length: retLength];

				[self freeMemory: cache];
				cache = NULL;
				cacheLength = 0;

				return ret;
			}

			bufferLength = [self _readNBytes: of_pagesize
					      intoBuffer: buffer];

			/* Look if there's a newline or \0 */
			for (i = 0; i < bufferLength; i++) {
				if (OF_UNLIKELY(buffer[i] == '\n' ||
				    buffer[i] == '\0')) {
					retLength = cacheLength + i;
					retCString = [self
					    allocMemoryWithSize: retLength];

					if (cache != NULL)
						memcpy(retCString, cache,
						    cacheLength);
					memcpy(retCString + cacheLength,
					    buffer, i);

					if (retLength > 0 &&
					    retCString[retLength - 1] == '\r')
						retLength--;

					@try {
						char *rcs = retCString;
						size_t rl = retLength;

						ret = [OFString
						    stringWithCString: rcs
							     encoding: encoding
							       length: rl];
					} @catch (id e) {
						/*
						 * Append data to cache to
						 * prevent loss of data due to
						 * wrong encoding.
						 */
						cache = [self
						    resizeMemory: cache
							  toSize: cacheLength +
								  bufferLength];

						if (cache != NULL)
							memcpy(cache +
							    cacheLength, buffer,
							    bufferLength);

						cacheLength += bufferLength;

						@throw e;
					} @finally {
						[self freeMemory: retCString];
					}

					newCache = [self allocMemoryWithSize:
					    bufferLength - i - 1];
					if (newCache != NULL)
						memcpy(newCache, buffer + i + 1,
						    bufferLength - i - 1);

					[self freeMemory: cache];
					cache = newCache;
					cacheLength = bufferLength - i - 1;

					return ret;
				}
			}

			/* There was no newline or \0 */
			cache = [self resizeMemory: cache
					    toSize: cacheLength + bufferLength];

			/*
			 * It's possible that cacheLen + len is 0 and thus
			 * cache was set to NULL by resizeMemory:toSize:.
			 */
			if (cache != NULL)
				memcpy(cache + cacheLength, buffer,
				    bufferLength);

			cacheLength += bufferLength;
		}
	} @finally {
		[self freeMemory: buffer];
	}

	/* Get rid of a warning, never reached anyway */
	assert(0);
}

- (OFString*)readTillDelimiter: (OFString*)delimiter
{
	return [self readTillDelimiter: delimiter
			  withEncoding: OF_STRING_ENCODING_UTF_8];
}

- (OFString*)readTillDelimiter: (OFString*)delimiter
		  withEncoding: (of_string_encoding_t)encoding
{
	const char *delimiterCString;
	size_t i, j, delimiterLength, bufferLength, retLength;
	char *retCString, *buffer, *newCache;
	OFString *ret;

	/* FIXME: Convert delimiter to specified charset */
	delimiterCString = [delimiter cString];
	delimiterLength = [delimiter cStringLength];
	j = 0;

	if (delimiterLength == 0)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	/* Look if there's something in our cache */
	if (cache != NULL) {
		for (i = 0; i < cacheLength; i++) {
			if (cache[i] != delimiterCString[j++])
				j = 0;

			if (j == delimiterLength || cache[i] == '\0') {
				if (cache[i] == '\0')
					delimiterLength = 1;

				ret = [OFString
				    stringWithCString: cache
					     encoding: encoding
					      length: i + 1 - delimiterLength];


				newCache = [self allocMemoryWithSize:
				    cacheLength - i - 1];
				if (newCache != NULL)
					memcpy(newCache, cache + i + 1,
					    cacheLength - i - 1);

				[self freeMemory: cache];
				cache = newCache;
				cacheLength -= i + 1;

				return ret;
			}
		}
	}

	/* Read until we get the delimiter or \0 */
	buffer = [self allocMemoryWithSize: of_pagesize];

	@try {
		for (;;) {
			if ([self _isAtEndOfStream]) {
				if (cache == NULL)
					return nil;

				ret = [OFString stringWithCString: cache
							 encoding: encoding
							   length: cacheLength];

				[self freeMemory: cache];
				cache = NULL;
				cacheLength = 0;

				return ret;
			}

			bufferLength = [self _readNBytes: of_pagesize
					      intoBuffer: buffer];

			/* Look if there's the delimiter or \0 */
			for (i = 0; i < bufferLength; i++) {
				if (buffer[i] != delimiterCString[j++])
					j = 0;

				if (j == delimiterLength || buffer[i] == '\0') {
					if (buffer[i] == '\0')
						delimiterLength = 1;

					retLength = cacheLength + i + 1 -
					    delimiterLength;
					retCString = [self
					    allocMemoryWithSize: retLength];

					if (cache != NULL &&
					    cacheLength <= retLength)
						memcpy(retCString, cache,
						    cacheLength);

					else if (cache != NULL)
						memcpy(retCString, cache,
						    retLength);
					if (i >= delimiterLength)
						memcpy(retCString + cacheLength,
						    buffer, i + 1 -
						    delimiterLength);

					@try {
						char *rcs = retCString;
						size_t rl = retLength;

						ret = [OFString
						    stringWithCString: rcs
							     encoding: encoding
							       length: rl];
					} @finally {
						[self freeMemory: retCString];
					}

					newCache = [self allocMemoryWithSize:
					    bufferLength - i - 1];
					if (newCache != NULL)
						memcpy(newCache, buffer + i + 1,
						    bufferLength - i - 1);

					[self freeMemory: cache];
					cache = newCache;
					cacheLength = bufferLength - i - 1;

					return ret;
				}
			}

			/* Neither the delimiter nor \0 was found */
			cache = [self resizeMemory: cache
					    toSize: cacheLength + bufferLength];

			/*
			 * It's possible that cacheLen + len is 0 and thus
			 * cache was set to NULL by resizeMemory:toSize:.
			 */
			if (cache != NULL)
				memcpy(cache + cacheLength, buffer,
				    bufferLength);

			cacheLength += bufferLength;
		}
	} @finally {
		[self freeMemory: buffer];
	}

	/* Get rid of a warning, never reached anyway */
	assert(0);
}

- (BOOL)buffersWrites
{
	return buffersWrites;
}

- (void)setBuffersWrites: (BOOL)enable
{
	buffersWrites = enable;
}

- (void)flushWriteBuffer
{
	if (writeBuffer == NULL)
		return;

	[self _writeNBytes: writeBufferLength
		fromBuffer: writeBuffer];

	[self freeMemory: writeBuffer];
	writeBuffer = NULL;
	writeBufferLength = 0;
}

- (size_t)writeNBytes: (size_t)length
	   fromBuffer: (const char*)buffer
{
	if (!buffersWrites)
		return [self _writeNBytes: length
			       fromBuffer: buffer];
	else {
		writeBuffer = [self resizeMemory: writeBuffer
					  toSize: writeBufferLength + length];
		memcpy(writeBuffer + writeBufferLength, buffer, length);
		writeBufferLength += length;

		return length;
	}
}

- (void)writeInt8: (uint8_t)int8
{
	[self writeNBytes: 1
	       fromBuffer: (char*)&int8];
629
630
631
632
633
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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
{
	int64 = of_bswap64_if_be(int64);

	[self writeNBytes: 8
	       fromBuffer: (char*)&int64];
}

- (size_t)writeDataArray: (OFDataArray*)dataarray
{
	return [self writeNBytes: [dataarray count] * [dataarray itemSize]
		      fromBuffer: [dataarray cArray]];
}

- (size_t)writeString: (OFString*)str
{
	return [self writeNBytes: [str cStringLength]
		      fromBuffer: [str cString]];
}

- (size_t)writeLine: (OFString*)str
{
	size_t ret, len = [str cStringLength];
	char *buf;

	buf = [self allocMemoryWithSize: len + 1];

	@try {
		memcpy(buf, [str cString], len);
		buf[len] = '\n';

		ret = [self writeNBytes: len + 1
			     fromBuffer: buf];
	} @finally {
		[self freeMemory: buf];
	}

	return ret;
}

- (size_t)writeFormat: (OFString*)fmt, ...
{
	va_list args;
	size_t ret;

	va_start(args, fmt);
	ret = [self writeFormat: fmt
		  withArguments: args];
	va_end(args);

	return ret;
}

- (size_t)writeFormat: (OFString*)fmt
	withArguments: (va_list)args
{
	char *t;
	int len;

	if (fmt == nil)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	if ((len = of_vasprintf(&t, [fmt cString], args)) == -1)

		@throw [OFInvalidFormatException newWithClass: isa];

	@try {
		return [self writeNBytes: len
			      fromBuffer: t];
	} @finally {
		free(t);
	}

	/* Get rid of a warning, never reached anyway */
	assert(0);
}

- (size_t)pendingBytes
{
	return cacheLen;
}

- (BOOL)isBlocking
{
	return isBlocking;
}








|

|
|


|

|
|


|

|
|

|


|
|

|
|

|


|


|

|


|
|
|
|




|
|

|
|

|



|
>



|
|

|








|







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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
{
	int64 = of_bswap64_if_be(int64);

	[self writeNBytes: 8
	       fromBuffer: (char*)&int64];
}

- (size_t)writeDataArray: (OFDataArray*)dataArray
{
	return [self writeNBytes: [dataArray count] * [dataArray itemSize]
		      fromBuffer: [dataArray cArray]];
}

- (size_t)writeString: (OFString*)string
{
	return [self writeNBytes: [string cStringLength]
		      fromBuffer: [string cString]];
}

- (size_t)writeLine: (OFString*)string
{
	size_t retLength, stringLength = [string cStringLength];
	char *buffer;

	buffer = [self allocMemoryWithSize: stringLength + 1];

	@try {
		memcpy(buffer, [string cString], stringLength);
		buffer[stringLength] = '\n';

		retLength = [self writeNBytes: stringLength + 1
				   fromBuffer: buffer];
	} @finally {
		[self freeMemory: buffer];
	}

	return retLength;
}

- (size_t)writeFormat: (OFString*)format, ...
{
	va_list arguments;
	size_t ret;

	va_start(arguments, format);
	ret = [self writeFormat: format
		  withArguments: arguments];
	va_end(arguments);

	return ret;
}

- (size_t)writeFormat: (OFString*)format
	withArguments: (va_list)arguments
{
	char *cString;
	int length;

	if (format == nil)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	if ((length = of_vasprintf(&cString, [format cString],
	    arguments)) == -1)
		@throw [OFInvalidFormatException newWithClass: isa];

	@try {
		return [self writeNBytes: length
			      fromBuffer: cString];
	} @finally {
		free(cString);
	}

	/* Get rid of a warning, never reached anyway */
	assert(0);
}

- (size_t)pendingBytes
{
	return cacheLength;
}

- (BOOL)isBlocking
{
	return isBlocking;
}

Modified src/OFStreamSocket.h from [a4e5ea7f23] to [738613fdff].

25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

/**
 * \brief A class which provides functions to create and use stream sockets.
 */
@interface OFStreamSocket: OFStream
{
	int  sock;
	BOOL eos;
}

/**
 * \return A new autoreleased OFTCPSocket
 */
+ socket;
@end







|







25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

/**
 * \brief A class which provides functions to create and use stream sockets.
 */
@interface OFStreamSocket: OFStream
{
	int  sock;
	BOOL isAtEndOfStream;
}

/**
 * \return A new autoreleased OFTCPSocket
 */
+ socket;
@end

Modified src/OFStreamSocket.m from [f8648491b3] to [7c915a3b62].

58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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
+ socket
{
	return [[[self alloc] init] autorelease];
}

- (BOOL)_isAtEndOfStream
{
	return eos;
}

- (size_t)_readNBytes: (size_t)size
	   intoBuffer: (char*)buf
{
	ssize_t ret;

	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: isa
						      socket: self];

	if (eos) {
		OFReadFailedException *e;

		e = [OFReadFailedException newWithClass: isa
						 stream: self
					  requestedSize: size];
#ifndef _WIN32
		e->errNo = ENOTCONN;
#else
		e->errNo = WSAENOTCONN;
#endif

		@throw e;
	}

	if ((ret = recv(sock, buf, size, 0)) < 0)
		@throw [OFReadFailedException newWithClass: isa
						    stream: self
					     requestedSize: size];

	if (ret == 0)
		eos = YES;

	return ret;
}

- (size_t)_writeNBytes: (size_t)size
	    fromBuffer: (const char*)buf
{
	ssize_t ret;

	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: isa
						      socket: self];

	if (eos) {
		OFWriteFailedException *e;

		e = [OFWriteFailedException newWithClass: isa
						  stream: self
					   requestedSize: size];
#ifndef _WIN32
		e->errNo = ENOTCONN;
#else
		e->errNo = WSAENOTCONN;
#endif

		@throw e;
	}

	if ((ret = send(sock, buf, size, 0)) == -1)
		@throw [OFWriteFailedException newWithClass: isa
						     stream: self
					      requestedSize: size];

	return ret;
}

#ifdef _WIN32
- (void)setBlocking: (BOOL)enable
{
	u_long v = enable;
	isBlocking = enable;







|


|
|

|





|




|









|


|

|
|

|


|
|

|





|




|









|


|

|







58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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
+ socket
{
	return [[[self alloc] init] autorelease];
}

- (BOOL)_isAtEndOfStream
{
	return isAtEndOfStream;
}

- (size_t)_readNBytes: (size_t)length
	   intoBuffer: (char*)buffer
{
	ssize_t retLength;

	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: isa
						      socket: self];

	if (isAtEndOfStream) {
		OFReadFailedException *e;

		e = [OFReadFailedException newWithClass: isa
						 stream: self
					requestedLength: length];
#ifndef _WIN32
		e->errNo = ENOTCONN;
#else
		e->errNo = WSAENOTCONN;
#endif

		@throw e;
	}

	if ((retLength = recv(sock, buffer, length, 0)) < 0)
		@throw [OFReadFailedException newWithClass: isa
						    stream: self
					   requestedLength: length];

	if (retLength == 0)
		isAtEndOfStream = YES;

	return retLength;
}

- (size_t)_writeNBytes: (size_t)length
	    fromBuffer: (const char*)buffer
{
	ssize_t retLength;

	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: isa
						      socket: self];

	if (isAtEndOfStream) {
		OFWriteFailedException *e;

		e = [OFWriteFailedException newWithClass: isa
						  stream: self
					 requestedLength: length];
#ifndef _WIN32
		e->errNo = ENOTCONN;
#else
		e->errNo = WSAENOTCONN;
#endif

		@throw e;
	}

	if ((retLength = send(sock, buffer, length, 0)) == -1)
		@throw [OFWriteFailedException newWithClass: isa
						     stream: self
					    requestedLength: length];

	return retLength;
}

#ifdef _WIN32
- (void)setBlocking: (BOOL)enable
{
	u_long v = enable;
	isBlocking = enable;
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: isa
						      socket: self];

	close(sock);

	sock = INVALID_SOCKET;
	eos = NO;
}

- (void)dealloc
{
	if (sock != INVALID_SOCKET)
		[self close];

	[super dealloc];
}
@end







|










154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: isa
						      socket: self];

	close(sock);

	sock = INVALID_SOCKET;
	isAtEndOfStream = NO;
}

- (void)dealloc
{
	if (sock != INVALID_SOCKET)
		[self close];

	[super dealloc];
}
@end

Modified src/exceptions/OFReadFailedException.m from [4c038c522a] to [3ef710facf].

24
25
26
27
28
29
30
31
32
33
34
35
36
@implementation OFReadFailedException
- (OFString*)description
{
	if (description != nil)
		return description;

	description = [[OFString alloc] initWithFormat:
	    @"Failed to read %zu bytes in class %@! " ERRFMT, requestedSize,
	    inClass, ERRPARAM];

	return description;
}
@end







|





24
25
26
27
28
29
30
31
32
33
34
35
36
@implementation OFReadFailedException
- (OFString*)description
{
	if (description != nil)
		return description;

	description = [[OFString alloc] initWithFormat:
	    @"Failed to read %zu bytes in class %@! " ERRFMT, requestedLength,
	    inClass, ERRPARAM];

	return description;
}
@end

Modified src/exceptions/OFReadOrWriteFailedException.h from [6e14e8add8] to [26ee57d0e2].

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

42
43
44
45
46
47
48
49
50
51
52
53

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

/**
 * \brief An exception indicating a read or write to a stream failed.
 */
@interface OFReadOrWriteFailedException: OFException
{
	OFStream *stream;
	size_t	 requestedSize;
@public
	int	 errNo;
}

#ifdef OF_HAVE_PROPERTIES
@property (readonly, nonatomic) OFStream *stream;
@property (readonly) size_t requestedSize;
@property (readonly) int errNo;
#endif

/**
 * \param class_ The class of the object which caused the exception
 * \param stream The stream which caused the read or write failed exception
 * \param size The requested size of the data that couldn't be read / written

 * \return A new open file failed exception
 */
+  newWithClass: (Class)class_
	 stream: (OFStream*)stream
  requestedSize: (size_t)size;

/**
 * Initializes an already allocated read or write failed exception.
 *
 * \param class_ The class of the object which caused the exception
 * \param stream The stream which caused the read or write failed exception
 * \param size The requested size of the data that couldn't be read / written

 * \return A new open file failed exception
 */
- initWithClass: (Class)class_
	 stream: (OFStream*)stream
  requestedSize: (size_t)size;

/**
 * \return The stream which caused the read or write failed exception
 */
- (OFStream*)stream;

/**
 * \return The requested size of the data that couldn't be read / written
 */
- (size_t)requestedSize;

/**
 * \return The errno from when the exception was created
 */
- (int)errNo;
@end







|






|






|
>


|
|
|






|
>


|
|
|







|

|






20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

/**
 * \brief An exception indicating a read or write to a stream failed.
 */
@interface OFReadOrWriteFailedException: OFException
{
	OFStream *stream;
	size_t	 requestedLength;
@public
	int	 errNo;
}

#ifdef OF_HAVE_PROPERTIES
@property (readonly, nonatomic) OFStream *stream;
@property (readonly) size_t requestedLength;
@property (readonly) int errNo;
#endif

/**
 * \param class_ The class of the object which caused the exception
 * \param stream The stream which caused the read or write failed exception
 * \param length The requested length of the data that couldn't be read /
 *		 written
 * \return A new open file failed exception
 */
+    newWithClass: (Class)class_
	   stream: (OFStream*)stream
  requestedLength: (size_t)length;

/**
 * Initializes an already allocated read or write failed exception.
 *
 * \param class_ The class of the object which caused the exception
 * \param stream The stream which caused the read or write failed exception
 * \param length The requested length of the data that couldn't be read /
 *		 written
 * \return A new open file failed exception
 */
-   initWithClass: (Class)class_
	   stream: (OFStream*)stream
  requestedLength: (size_t)length;

/**
 * \return The stream which caused the read or write failed exception
 */
- (OFStream*)stream;

/**
 * \return The requested length of the data that couldn't be read / written
 */
- (size_t)requestedLength;

/**
 * \return The errno from when the exception was created
 */
- (int)errNo;
@end

Modified src/exceptions/OFReadOrWriteFailedException.m from [e499bb5a9d] to [fdcd27a313].

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#import "OFStreamSocket.h"

#import "OFNotImplementedException.h"

#import "common.h"

@implementation OFReadOrWriteFailedException
+  newWithClass: (Class)class_
	 stream: (OFStream*)stream
  requestedSize: (size_t)size
{
	return [[self alloc] initWithClass: class_
				    stream: stream
			     requestedSize: size];
}

- initWithClass: (Class)class_
{
	Class c = isa;
	[self release];
	@throw [OFNotImplementedException newWithClass: c
					      selector: _cmd];
}

- initWithClass: (Class)class_
	 stream: (OFStream*)stream_
  requestedSize: (size_t)size
{
	self = [super initWithClass: class_];

	@try {
		stream = [stream_ retain];
		requestedSize = size;

		if ([class_ isSubclassOfClass: [OFStreamSocket class]])
			errNo = GET_SOCK_ERRNO;
		else
			errNo = GET_ERRNO;
	} @catch (id e) {
		return e;







|
|
|



|










|
|
|





|







21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#import "OFStreamSocket.h"

#import "OFNotImplementedException.h"

#import "common.h"

@implementation OFReadOrWriteFailedException
+    newWithClass: (Class)class_
	   stream: (OFStream*)stream
  requestedLength: (size_t)length
{
	return [[self alloc] initWithClass: class_
				    stream: stream
			   requestedLength: length];
}

- initWithClass: (Class)class_
{
	Class c = isa;
	[self release];
	@throw [OFNotImplementedException newWithClass: c
					      selector: _cmd];
}

-   initWithClass: (Class)class_
	   stream: (OFStream*)stream_
  requestedLength: (size_t)length
{
	self = [super initWithClass: class_];

	@try {
		stream = [stream_ retain];
		requestedLength = length;

		if ([class_ isSubclassOfClass: [OFStreamSocket class]])
			errNo = GET_SOCK_ERRNO;
		else
			errNo = GET_ERRNO;
	} @catch (id e) {
		return e;
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
}

- (OFStream*)stream
{
	return stream;
}

- (size_t)requestedSize
{
	return requestedSize;
}

- (int)errNo
{
	return errNo;
}
@end







|

|







71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
}

- (OFStream*)stream
{
	return stream;
}

- (size_t)requestedLength
{
	return requestedLength;
}

- (int)errNo
{
	return errNo;
}
@end

Modified src/exceptions/OFWriteFailedException.m from [655afede25] to [d5c686e01b].

24
25
26
27
28
29
30
31
32
33
34
35
36
@implementation OFWriteFailedException
- (OFString*)description
{
	if (description != nil)
		return description;

	description = [[OFString alloc] initWithFormat:
	    @"Failed to write %zu bytes in class %@! " ERRFMT, requestedSize,
	    inClass, ERRPARAM];

	return description;
}
@end







|





24
25
26
27
28
29
30
31
32
33
34
35
36
@implementation OFWriteFailedException
- (OFString*)description
{
	if (description != nil)
		return description;

	description = [[OFString alloc] initWithFormat:
	    @"Failed to write %zu bytes in class %@! " ERRFMT, requestedLength,
	    inClass, ERRPARAM];

	return description;
}
@end