ObjFW  Diff

Differences From Artifact [9167fd4ad1]:

To Artifact [4cea821d7a]:


65
66
67
68
69
70
71
72

73
74
75
76
77
78
79
65
66
67
68
69
70
71

72
73
74
75
76
77
78
79







-
+







#ifndef O_EXCL
# define O_EXCL 0
#endif
#ifndef O_EXLOCK
# define O_EXLOCK 0
#endif

#if !defined(OF_MORPHOS) || defined(OF_IXEMUL)
#ifndef OF_MORPHOS
# define closeHandle(h) close(h)
#else
static OFDataArray *openHandles = nil;

static void
closeHandle(of_file_handle_t handle)
{
96
97
98
99
100
101
102
103

104
105
106
107
108
109
110
96
97
98
99
100
101
102

103
104
105
106
107
108
109
110







-
+







	size_t count = [openHandles count];

	for (size_t i = 0; i < count; i++)
		Close(handles[i]);
}
#endif

#if !defined(OF_MORPHOS) || defined(OF_IXEMUL)
#ifndef OF_MORPHOS
static int
parseMode(const char *mode)
{
	if (strcmp(mode, "r") == 0)
		return O_RDONLY;
	if (strcmp(mode, "w") == 0)
		return O_WRONLY | O_CREAT | O_TRUNC;
188
189
190
191
192
193
194
195

196
197
198
199
200
201
202
188
189
190
191
192
193
194

195
196
197
198
199
200
201
202







-
+








@implementation OFFile
+ (void)initialize
{
	if (self != [OFFile class])
		return;

#if defined(OF_MORPHOS) && !defined(OF_IXEMUL)
#ifdef OF_MORPHOS
	openHandles = [[OFDataArray alloc] initWithItemSize: sizeof(BPTR)];
#endif

#ifdef OF_WII
	if (!fatInitDefault())
		@throw [OFInitializationFailedException
		    exceptionWithClass: self];
231
232
233
234
235
236
237
238

239
240
241
242
243
244
245
231
232
233
234
235
236
237

238
239
240
241
242
243
244
245







-
+







{
	of_file_handle_t handle;

	@try {
		void *pool = objc_autoreleasePoolPush();
		int flags;

#if !defined(OF_MORPHOS) || defined(OF_IXEMUL)
#ifndef OF_MORPHOS
		if ((flags = parseMode([mode UTF8String])) == -1)
			@throw [OFInvalidArgumentException exception];

		flags |= O_CLOEXEC;

# if defined(OF_WINDOWS)
		if ((handle = _wopen([path UTF16String], flags,
327
328
329
330
331
332
333
334

335
336
337
338
339
340
341
327
328
329
330
331
332
333

334
335
336
337
338
339
340
341







-
+







	if (length > UINT_MAX)
		@throw [OFOutOfRangeException exception];

	if ((ret = read(_handle, buffer, (unsigned int)length)) < 0)
		@throw [OFReadFailedException exceptionWithObject: self
						  requestedLength: length
							    errNo: errno];
#elif defined(OF_MORPHOS) && !defined(OF_IXEMUL)
#elif defined(OF_MORPHOS)
	if (length > LONG_MAX)
		@throw [OFOutOfRangeException exception];

	if ((ret = Read(_handle.handle, buffer, length)) < 0)
		@throw [OFReadFailedException exceptionWithObject: self
						  requestedLength: length];
#else
362
363
364
365
366
367
368
369

370
371
372
373
374
375
376
362
363
364
365
366
367
368

369
370
371
372
373
374
375
376







-
+







	if (length > INT_MAX)
		@throw [OFOutOfRangeException exception];

	if (write(_handle, buffer, (int)length) != (int)length)
		@throw [OFWriteFailedException exceptionWithObject: self
						   requestedLength: length
							     errNo: errno];
#elif defined(OF_MORPHOS) && !defined(OF_IXEMUL)
#elif defined(OF_MORPHOS)
	if (length > LONG_MAX)
		@throw [OFOutOfRangeException exception];

	if (_handle.append) {
		if (Seek64(_handle.handle, 0, OFFSET_END) == -1)
			@throw [OFWriteFailedException
			    exceptionWithObject: self
397
398
399
400
401
402
403
404

405
406
407
408
409
410
411
397
398
399
400
401
402
403

404
405
406
407
408
409
410
411







-
+







	of_offset_t ret;

	if (!OF_FILE_HANDLE_IS_VALID(_handle))
		@throw [OFSeekFailedException exceptionWithStream: self
							   offset: offset
							   whence: whence];

#if !defined(OF_MORPHOS) || defined(OF_IXEMUL)
#ifndef OF_MORPHOS
# if defined(OF_WINDOWS)
	ret = _lseeki64(_handle, offset, whence);
# elif defined(OF_HAVE_OFF64_T)
	ret = lseek64(_handle, offset, whence);
# else
	ret = lseek(_handle, offset, whence);
# endif