ObjFW  Diff

Differences From Artifact [77e3c3a21e]:

  • File src/OFFile.m — part of check-in [fe2cbe0021] at 2018-04-22 16:13:04 on branch trunk — runtime: Define BOOL to be the same as bool

    As we define the ABI, we can just replace BOOL with bool everywhere,
    including in ObjFW itself. For the Apple platforms where BOOL and bool
    are different, this is not a problem as BOOL and bool are passed and
    returned the same way in the ABI.

    This still defines BOOL to bool for compatibility, except on AmigaOS and
    Wii, which both have its own BOOL type. (user: js, size: 10973) [annotate] [blame] [check-ins using]

To Artifact [8331bd9e6b]:


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
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
86
87







+
+
+
+
+
+
+














-
+







# include <fat.h>
#endif

#ifdef OF_NINTENDO_DS
# include <stdbool.h>
# include <filesystem.h>
#endif

#ifdef OF_AMIGAOS
# ifdef OF_AMIGAOS3
#  define INTUITION_CLASSES_H
# endif
# include <proto/dos.h>
#endif

#ifndef O_BINARY
# define O_BINARY 0
#endif
#ifndef O_CLOEXEC
# define O_CLOEXEC 0
#endif
#ifndef O_EXCL
# define O_EXCL 0
#endif
#ifndef O_EXLOCK
# define O_EXLOCK 0
#endif

#ifndef OF_MORPHOS
#ifndef OF_AMIGAOS
# define closeHandle(h) close(h)
#else
struct of_file_handle {
	of_file_handle_t previous, next;
	BPTR handle;
	bool append;
} *firstHandle = NULL;
99
100
101
102
103
104
105
106

107
108
109
110
111
112
113
106
107
108
109
110
111
112

113
114
115
116
117
118
119
120







-
+







{
	for (of_file_handle_t iter = firstHandle; iter != NULL;
	    iter = iter->next)
		Close(iter->handle);
}
#endif

#ifndef OF_MORPHOS
#ifndef OF_AMIGAOS
static int
parseMode(const char *mode)
{
	if (strcmp(mode, "r") == 0)
		return O_RDONLY;
	if (strcmp(mode, "r+") == 0)
		return O_RDWR;
205
206
207
208
209
210
211
212

213
214
215
216
217
218
219
212
213
214
215
216
217
218

219
220
221
222
223
224
225
226







-
+







{
	of_file_handle_t handle;

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

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

		flags |= O_BINARY | O_CLOEXEC;

# if defined(OF_WINDOWS)
		if ((handle = _wopen([path UTF16String], flags,
266
267
268
269
270
271
272

273
274



275
276
277
278
279
280
281
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292







+


+
+
+







				@throw [OFOpenItemFailedException
				    exceptionWithPath: path
						 mode: mode
						errNo: errNo];
			}

			if (handle->append) {
# ifdef OF_MORPHOS
				if (Seek64(handle->handle, 0,
				    OFFSET_END) == -1) {
# else
				if (Seek(handle->handle, 0, OFFSET_END) == -1) {
# endif
					Close(handle->handle);
					@throw [OFOpenItemFailedException
					    exceptionWithPath: path
							 mode: mode
							errNo: EIO];
				}
			}
359
360
361
362
363
364
365
366

367
368
369
370
371
372
373
370
371
372
373
374
375
376

377
378
379
380
381
382
383
384







-
+







	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)
#elif defined(OF_AMIGAOS)
	if (length > LONG_MAX)
		@throw [OFOutOfRangeException exception];

	if ((ret = Read(_handle->handle, buffer, length)) < 0)
		@throw [OFReadFailedException exceptionWithObject: self
						  requestedLength: length
							    errNo: EIO];
397
398
399
400
401
402
403
404

405
406
407
408
409
410

411



412
413
414
415
416
417
418
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







-
+






+

+
+
+







		@throw [OFOutOfRangeException exception];

	if ((bytesWritten = write(_handle, buffer, (int)length)) < 0)
		@throw [OFWriteFailedException exceptionWithObject: self
						   requestedLength: length
						      bytesWritten: 0
							     errNo: errno];
#elif defined(OF_MORPHOS)
#elif defined(OF_AMIGAOS)
	LONG bytesWritten;

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

	if (_handle->append) {
# ifdef OF_MORPHOS
		if (Seek64(_handle->handle, 0, OFFSET_END) == -1)
# else
		if (Seek(_handle->handle, 0, OFFSET_END) == -1)
# endif
			@throw [OFWriteFailedException
			    exceptionWithObject: self
				requestedLength: length
				   bytesWritten: 0
					  errNo: EIO];
	}

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
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







-
+














+
+


-
+


-
+


-
+


+
-
-
+
+
+


+
+
+
+
-
+







			     whence: (int)whence
{
	of_offset_t ret;

	if (_handle == OF_INVALID_FILE_HANDLE)
		@throw [OFNotOpenException exceptionWithObject: self];

#ifndef OF_MORPHOS
#ifndef OF_AMIGAOS
# 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

	if (ret == -1)
		@throw [OFSeekFailedException exceptionWithStream: self
							   offset: offset
							   whence: whence
							    errNo: errno];
#else
	LONG translatedWhence;

	switch (whence) {
	case SEEK_SET:
		ret = Seek64(_handle->handle, offset, OFFSET_BEGINNING);
		translatedWhence = OFFSET_BEGINNING;
		break;
	case SEEK_CUR:
		ret = Seek64(_handle->handle, offset, OFFSET_CURRENT);
		translatedWhence = OFFSET_CURRENT;
		break;
	case SEEK_END:
		ret = Seek64(_handle->handle, offset, OFFSET_END);
		translatedWhence = OFFSET_END;
		break;
	default:
		@throw [OFSeekFailedException exceptionWithStream: self
		ret = -1;
		break;
							   offset: offset
							   whence: whence
							    errNo: EINVAL];
	}

# ifdef OF_MORPHOS
	if ((ret = Seek64(_handle->handle, offset, translatedWhence)) == 1)
# else
	if ((ret = Seek(_handle->handle, offset, translatedWhence)) == 1)
	if (ret == -1)
# endif
		@throw [OFSeekFailedException exceptionWithStream: self
							   offset: offset
							   whence: whence
							    errNo: EINVAL];
#endif

	_atEndOfStream = false;