ObjFW  Check-in [c5c3df3abe]

Overview
Comment:MorphOS: Name variable handle instead of fd
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: c5c3df3abe59453472e27a361b1a267cdc2a033451d801f8b426f81fd0470df8
User & Date: js on 2017-05-30 22:01:45
Other Links: manifest | tags
Context
2017-06-03
12:46
Make of_(l)stat() private to OFFileManager check-in: 4ebeb7e3a2 user: js tags: trunk
2017-05-30
22:01
MorphOS: Name variable handle instead of fd check-in: c5c3df3abe user: js tags: trunk
2017-05-29
21:55
macros.h: Add OF_ALIGNAS check-in: c2a2107f79 user: js tags: trunk
Changes

Modified src/OFFile.h from [1fb9cda36b] to [0b74319ed1].

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
typedef struct __stat64 of_stat_t;
#elif defined(OF_HAVE_OFF64_T)
typedef struct stat64 of_stat_t;
#else
typedef struct stat of_stat_t;
#endif





/*!
 * @class OFFile OFFile.h ObjFW/OFFile.h
 *
 * @brief A class which provides methods to read and write files.
 */
@interface OFFile: OFSeekableStream
{
#if !defined(OF_MORPHOS) || defined(OF_IXEMUL)
	int _fd;
#else
	long _fd;
	bool _append;
#endif
	bool _atEndOfStream;
}

/*!
 * @brief Creates a new OFFile with the specified path and mode.







>
>
>
>










|







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
typedef struct __stat64 of_stat_t;
#elif defined(OF_HAVE_OFF64_T)
typedef struct stat64 of_stat_t;
#else
typedef struct stat of_stat_t;
#endif

#if defined(OF_MORPHOS) && !defined(OF_IXEMUL)
typedef long BPTR;
#endif

/*!
 * @class OFFile OFFile.h ObjFW/OFFile.h
 *
 * @brief A class which provides methods to read and write files.
 */
@interface OFFile: OFSeekableStream
{
#if !defined(OF_MORPHOS) || defined(OF_IXEMUL)
	int _fd;
#else
	BPTR _handle;
	bool _append;
#endif
	bool _atEndOfStream;
}

/*!
 * @brief Creates a new OFFile with the specified path and mode.
73
74
75
76
77
78
79

80
81
82
83
84
85
86
87










88
89
90
91
92
93
94
 *	       `a+`           | read-write, create, append
 *	       `ab+` or `a+b` | read-write, create, append, binary
 * @return A new autoreleased OFFile
 */
+ (instancetype)fileWithPath: (OFString *)path
			mode: (OFString *)mode;


/*!
 * @brief Creates a new OFFile with the specified file descriptor.
 *
 * @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
 */
+ (instancetype)fileWithFileDescriptor: (int)fd;











- init OF_UNAVAILABLE;

/*!
 * @brief Initializes an already allocated OFFile.
 *
 * @param path The path to the file to open as a string







>




|



>
>
>
>
>
>
>
>
>
>







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
 *	       `a+`           | read-write, create, append
 *	       `ab+` or `a+b` | read-write, create, append, binary
 * @return A new autoreleased OFFile
 */
+ (instancetype)fileWithPath: (OFString *)path
			mode: (OFString *)mode;

#if !defined(OF_MORPHOS) || defined(OF_IXEMUL)
/*!
 * @brief Creates a new OFFile with the specified file descriptor.
 *
 * @param fd A file descriptor, returned from for example open().
 *	     It is closed when the OFFile object is deallocated!
 * @return A new autoreleased OFFile
 */
+ (instancetype)fileWithFileDescriptor: (int)fd;
#else
/*!
 * @brief Creates a new OFFile with the specified handle.
 *
 * @param handle A handle, returned from for example Open().
 *		 It is closed when the OFFile object is deallocated!
 * @return A new autoreleased OFFile
 */
+ (instancetype)fileWithHandle: (BPTR)handle;
#endif

- init OF_UNAVAILABLE;

/*!
 * @brief Initializes an already allocated OFFile.
 *
 * @param path The path to the file to open as a string
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
 *	       `a+`           | read-write, create, append
 *	       `ab+` or `a+b` | read-write, create, append, binary
 * @return An initialized OFFile
 */
- initWithPath: (OFString *)path
	  mode: (OFString *)mode;


/*!
 * @brief 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 int of_stat(OFString *path, of_stat_t *buffer);
extern int of_lstat(OFString *path, of_stat_t *buffer);
#ifdef __cplusplus
}
#endif

OF_ASSUME_NONNULL_END







>




|
>


>
>
>
>
>
>
>
>
>
>












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
150
151
152
153
154
155
156
157
158
159
160
161
 *	       `a+`           | read-write, create, append
 *	       `ab+` or `a+b` | read-write, create, append, binary
 * @return An initialized OFFile
 */
- initWithPath: (OFString *)path
	  mode: (OFString *)mode;

#if !defined(OF_MORPHOS) || defined(OF_IXEMUL)
/*!
 * @brief Initializes an already allocated OFFile.
 *
 * @param fd A file descriptor, returned from for example open().
 *	     It is closed when the OFFile object is deallocated!
 * @return An initialized OFFile
 */
- initWithFileDescriptor: (int)fd;
#else
/*!
 * @brief Initializes an already allocated OFFile.
 *
 * @param handle A handle, returned from for example Open().
 *		 It is closed when the OFFile object is deallocated!
 * @return An initialized OFFile
 */
- initWithHandle: (BPTR)handle;
#endif
@end

#ifdef __cplusplus
extern "C" {
#endif
extern int of_stat(OFString *path, of_stat_t *buffer);
extern int of_lstat(OFString *path, of_stat_t *buffer);
#ifdef __cplusplus
}
#endif

OF_ASSUME_NONNULL_END

Modified src/OFFile.m from [08f6a9dc6a] to [e2526b07ea].

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
# include <windows.h>
#endif

#if defined(OF_MORPHOS) && !defined(OF_IXEMUL)
# define BOOL EXEC_BOOL
# include <proto/dos.h>
# undef BOOL
# define INVALID_FD 0
# define close(fd) Close(fd)
#endif

#ifdef OF_WII
# define BOOL OGC_BOOL
# include <fat.h>
# undef BOOL
#endif

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

#ifndef INVALID_FD
# define INVALID_FD -1
#endif

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







<
<













<
<
<
<







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
# include <windows.h>
#endif

#if defined(OF_MORPHOS) && !defined(OF_IXEMUL)
# define BOOL EXEC_BOOL
# include <proto/dos.h>
# undef BOOL


#endif

#ifdef OF_WII
# define BOOL OGC_BOOL
# include <fat.h>
# undef BOOL
#endif

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





#ifndef O_BINARY
# define O_BINARY 0
#endif
#ifndef O_CLOEXEC
# define O_CLOEXEC 0
#endif
#ifndef O_EXCL
196
197
198
199
200
201
202

203
204
205




206
207

208
209
210
211
212
213
214
+ (instancetype)fileWithPath: (OFString *)path
			mode: (OFString *)mode
{
	return [[[self alloc] initWithPath: path
				      mode: mode] autorelease];
}


+ (instancetype)fileWithFileDescriptor: (int)filedescriptor
{
	return [[[self alloc]




	    initWithFileDescriptor: filedescriptor] autorelease];
}


- init
{
	OF_INVALID_INIT_METHOD
}

- initWithPath: (OFString *)path







>
|

|
>
>
>
>
|

>







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
+ (instancetype)fileWithPath: (OFString *)path
			mode: (OFString *)mode
{
	return [[[self alloc] initWithPath: path
				      mode: mode] autorelease];
}

#if !defined(OF_MORPHOS) || defined(OF_IXEMUL)
+ (instancetype)fileWithFileDescriptor: (int)fd
{
	return [[[self alloc] initWithFileDescriptor: fd] autorelease];
}
#else
+ (instancetype)fileWithHandle: (BPTR)handle
{
	return [[[self alloc] initWithHandle: handle] autorelease];
}
#endif

- init
{
	OF_INVALID_INIT_METHOD
}

- initWithPath: (OFString *)path
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
			    exceptionWithPath: path
					 mode: mode
					errNo: errno];
#else
		if ((flags = parseMode([mode UTF8String], &_append)) == -1)
			@throw [OFInvalidArgumentException exception];

		if ((_fd = Open([path cStringWithEncoding:
		    [OFLocalization encoding]], flags)) == INVALID_FD)
			@throw [OFOpenItemFailedException
			    exceptionWithPath: path
					 mode: mode];

		if (_append) {
			if (Seek64(_fd, 0, OFFSET_END) == -1)
				@throw [OFOpenItemFailedException
				    exceptionWithPath: path
						 mode: mode];
		}
#endif
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}


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

	_fd = fd;

	return self;
}











- (bool)lowlevelIsAtEndOfStream
{

	if (_fd == INVALID_FD)
		return true;





	return _atEndOfStream;
}

- (size_t)lowlevelReadIntoBuffer: (void *)buffer
			  length: (size_t)length
{
	ssize_t ret;


	if (_fd == INVALID_FD || _atEndOfStream)
		@throw [OFReadFailedException exceptionWithObject: self
						  requestedLength: length];

#if defined(OF_WINDOWS)





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

	if ((ret = read(_fd, buffer, (unsigned int)length)) < 0)
		@throw [OFReadFailedException exceptionWithObject: self
						  requestedLength: length
							    errNo: errno];





#elif defined(OF_MORPHOS) && !defined(OF_IXEMUL)
	if (length > LONG_MAX)
		@throw [OFOutOfRangeException exception];

	if ((ret = Read(_fd, buffer, length)) < 0)
		@throw [OFReadFailedException exceptionWithObject: self
						  requestedLength: length];
#else
	if ((ret = read(_fd, buffer, length)) < 0)
		@throw [OFReadFailedException exceptionWithObject: self
						  requestedLength: length
							    errNo: errno];
#endif

	if (ret == 0)
		_atEndOfStream = true;

	return ret;
}

- (void)lowlevelWriteBuffer: (const void *)buffer
		     length: (size_t)length
{

	if (_fd == INVALID_FD || _atEndOfStream)
		@throw [OFWriteFailedException exceptionWithObject: self
						   requestedLength: length];




#if defined(OF_WINDOWS)





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

	if (write(_fd, buffer, (int)length) != (int)length)
		@throw [OFWriteFailedException exceptionWithObject: self
						   requestedLength: length
							     errNo: errno];

#elif defined(OF_MORPHOS) && !defined(OF_IXEMUL)



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

	if (_append) {
		if (Seek64(_fd, 0, OFFSET_END) == -1)
			@throw [OFWriteFailedException
			    exceptionWithObject: self
				requestedLength: length];
	}

	if (Write(_fd, (void *)buffer, length) != (LONG)length)
		@throw [OFWriteFailedException exceptionWithObject: self
						   requestedLength: length];
#else
	if (length > SSIZE_MAX)
		@throw [OFOutOfRangeException exception];

	if (write(_fd, buffer, length) != (ssize_t)length)
		@throw [OFWriteFailedException exceptionWithObject: self
						   requestedLength: length
							     errNo: errno];
#endif
}

- (of_offset_t)lowlevelSeekToOffset: (of_offset_t)offset
			     whence: (int)whence
{
	of_offset_t ret;


	if (_fd == INVALID_FD)
		@throw [OFSeekFailedException exceptionWithStream: self
							   offset: offset
							   whence: whence];

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

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





	switch (whence) {
	case SEEK_SET:
		ret = Seek64(_fd, offset, OFFSET_BEGINNING);
		break;
	case SEEK_CUR:
		ret = Seek64(_fd, offset, OFFSET_CURRENT);
		break;
	case SEEK_END:
		ret = Seek64(_fd, offset, OFFSET_END);
		break;
	default:
		ret = -1;
		break;
	}

	if (ret == -1)







|
|





|













>








>
>
>
>
>
>
>
>
>
>



>
|

>
>
>
>









>
|



|
>
>
>
>
>







>
>
>
>
>
|



|


<
<
<
<
<











>
|


>
>
>

<
>
>
>
>
>







>
|
>
>
>




|





|


<
<
<
<
<
<
<
<








>
|




<














>
>
>
>
>


|


|


|







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
			    exceptionWithPath: path
					 mode: mode
					errNo: errno];
#else
		if ((flags = parseMode([mode UTF8String], &_append)) == -1)
			@throw [OFInvalidArgumentException exception];

		if ((_handle = Open([path cStringWithEncoding:
		    [OFLocalization encoding]], flags)) == 0)
			@throw [OFOpenItemFailedException
			    exceptionWithPath: path
					 mode: mode];

		if (_append) {
			if (Seek64(_handle, 0, OFFSET_END) == -1)
				@throw [OFOpenItemFailedException
				    exceptionWithPath: path
						 mode: mode];
		}
#endif
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

#if !defined(OF_MORPHOS) || defined(OF_IXEMUL)
- initWithFileDescriptor: (int)fd
{
	self = [super init];

	_fd = fd;

	return self;
}
#else
- initWithHandle: (BPTR)handle
{
	self = [super init];

	_handle = handle;

	return self;
}
#endif

- (bool)lowlevelIsAtEndOfStream
{
#if !defined(OF_MORPHOS) || defined(OF_IXEMUL)
	if (_fd == -1)
		return true;
#else
	if (_handle == 0)
		return true;
#endif

	return _atEndOfStream;
}

- (size_t)lowlevelReadIntoBuffer: (void *)buffer
			  length: (size_t)length
{
	ssize_t ret;

#if !defined(OF_MORPHOS) || defined(OF_IXEMUL)
	if (_fd == -1 || _atEndOfStream)
		@throw [OFReadFailedException exceptionWithObject: self
						  requestedLength: length];

# ifndef OF_WINDOWS
	if ((ret = read(_fd, buffer, length)) < 0)
		@throw [OFReadFailedException exceptionWithObject: self
						  requestedLength: length
							    errNo: errno];
# else
	if (length > UINT_MAX)
		@throw [OFOutOfRangeException exception];

	if ((ret = read(_fd, buffer, (unsigned int)length)) < 0)
		@throw [OFReadFailedException exceptionWithObject: self
						  requestedLength: length
							    errNo: errno];
# endif
#else
	if (_handle == 0 || _atEndOfStream)
		@throw [OFReadFailedException exceptionWithObject: self
						  requestedLength: length];

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

	if ((ret = Read(_handle, buffer, length)) < 0)
		@throw [OFReadFailedException exceptionWithObject: self
						  requestedLength: length];





#endif

	if (ret == 0)
		_atEndOfStream = true;

	return ret;
}

- (void)lowlevelWriteBuffer: (const void *)buffer
		     length: (size_t)length
{
#if !defined(OF_MORPHOS) || defined(OF_IXEMUL)
	if (_fd == -1 || _atEndOfStream)
		@throw [OFWriteFailedException exceptionWithObject: self
						   requestedLength: length];
# ifndef OF_WINDOWS
	if (length > SSIZE_MAX)
		@throw [OFOutOfRangeException exception];


	if (write(_fd, buffer, length) != (ssize_t)length)
		@throw [OFWriteFailedException exceptionWithObject: self
						   requestedLength: length
							     errNo: errno];
# else
	if (length > INT_MAX)
		@throw [OFOutOfRangeException exception];

	if (write(_fd, buffer, (int)length) != (int)length)
		@throw [OFWriteFailedException exceptionWithObject: self
						   requestedLength: length
							     errNo: errno];
# endif
#else
	if (_handle == 0 || _atEndOfStream)
		@throw [OFWriteFailedException exceptionWithObject: self
						   requestedLength: length];
	if (length > LONG_MAX)
		@throw [OFOutOfRangeException exception];

	if (_append) {
		if (Seek64(_handle, 0, OFFSET_END) == -1)
			@throw [OFWriteFailedException
			    exceptionWithObject: self
				requestedLength: length];
	}

	if (Write(_handle, (void *)buffer, length) != (LONG)length)
		@throw [OFWriteFailedException exceptionWithObject: self
						   requestedLength: length];








#endif
}

- (of_offset_t)lowlevelSeekToOffset: (of_offset_t)offset
			     whence: (int)whence
{
	of_offset_t ret;

#if !defined(OF_MORPHOS) || defined(OF_IXEMUL)
	if (_fd == -1)
		@throw [OFSeekFailedException exceptionWithStream: self
							   offset: offset
							   whence: whence];


# if defined(OF_WINDOWS)
	ret = _lseeki64(_fd, offset, whence);
# elif defined(OF_HAVE_OFF64_T)
	ret = lseek64(_fd, offset, whence);
# else
	ret = lseek(_fd, offset, whence);
# endif

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

	switch (whence) {
	case SEEK_SET:
		ret = Seek64(_handle, offset, OFFSET_BEGINNING);
		break;
	case SEEK_CUR:
		ret = Seek64(_handle, offset, OFFSET_CURRENT);
		break;
	case SEEK_END:
		ret = Seek64(_handle, offset, OFFSET_END);
		break;
	default:
		ret = -1;
		break;
	}

	if (ret == -1)
419
420
421
422
423
424
425

426
427
428
429






430
431
432
433
434
435
436
437
438
439
440
441
{
	return _fd;
}
#endif

- (void)close
{

	if (_fd != INVALID_FD)
		close(_fd);

	_fd = INVALID_FD;







	[super close];
}

- (void)dealloc
{
	if (_fd != INVALID_FD)
		close(_fd);

	[super dealloc];
}
@end







>
|


|
>
>
>
>
>
>






<
|




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
{
	return _fd;
}
#endif

- (void)close
{
#if !defined(OF_MORPHOS) || defined(OF_IXEMUL)
	if (_fd != -1)
		close(_fd);

	_fd = -1;
#else
	if (_handle != 0)
		Close(_handle);

	_handle = 0;
#endif

	[super close];
}

- (void)dealloc
{

	[self close];

	[super dealloc];
}
@end

Modified src/OFStdIOStream+Private.h from [68f2152466] to [f4f520c039].

18
19
20
21
22
23
24
25
26
27
28
29
30
31

OF_ASSUME_NONNULL_BEGIN

@interface OFStdIOStream ()
#if !defined(OF_MORPHOS) || defined(OF_IXEMUL)
- (instancetype)of_initWithFileDescriptor: (int)fd OF_METHOD_FAMILY(init);
#else
- (instancetype)of_initWithFileDescriptor: (long)handle
				 closable: (bool)closable
    OF_METHOD_FAMILY(init);
#endif
@end

OF_ASSUME_NONNULL_END







|
|
<




18
19
20
21
22
23
24
25
26

27
28
29
30

OF_ASSUME_NONNULL_BEGIN

@interface OFStdIOStream ()
#if !defined(OF_MORPHOS) || defined(OF_IXEMUL)
- (instancetype)of_initWithFileDescriptor: (int)fd OF_METHOD_FAMILY(init);
#else
- (instancetype)of_initWithHandle: (BPTR)handle
			 closable: (bool)closable OF_METHOD_FAMILY(init);

#endif
@end

OF_ASSUME_NONNULL_END

Modified src/OFStdIOStream.h from [6bad6405ef] to [20df83b805].

14
15
16
17
18
19
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
 * file.
 */

#import "OFStream.h"

OF_ASSUME_NONNULL_BEGIN





/*!
 * @class OFStdIOStream OFStdIOStream.h ObjFW/OFStdIOStream.h
 *
 * @brief A class for providing standard input, output and error as OFStream.
 *
 * The global variables @ref of_stdin, @ref of_stdout and @ref of_stderr are
 * instances of this class and need no initialization.
 */
#ifdef OF_STDIO_STREAM_WIN32_CONSOLE_H
OF_SUBCLASSING_RESTRICTED
#endif
@interface OFStdIOStream: OFStream
{
#if !defined(OF_MORPHOS) || defined(OF_IXEMUL)
	int _fd;
#else
	long _fd;
	bool _closable;
#endif
	bool _atEndOfStream;
}

- init OF_UNAVAILABLE;








>
>
>
>
















|







14
15
16
17
18
19
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
 * file.
 */

#import "OFStream.h"

OF_ASSUME_NONNULL_BEGIN

#if defined(OF_MORPHOS) && !defined(OF_IXEMUL)
typedef long BPTR;
#endif

/*!
 * @class OFStdIOStream OFStdIOStream.h ObjFW/OFStdIOStream.h
 *
 * @brief A class for providing standard input, output and error as OFStream.
 *
 * The global variables @ref of_stdin, @ref of_stdout and @ref of_stderr are
 * instances of this class and need no initialization.
 */
#ifdef OF_STDIO_STREAM_WIN32_CONSOLE_H
OF_SUBCLASSING_RESTRICTED
#endif
@interface OFStdIOStream: OFStream
{
#if !defined(OF_MORPHOS) || defined(OF_IXEMUL)
	int _fd;
#else
	BPTR _handle;
	bool _closable;
#endif
	bool _atEndOfStream;
}

- init OF_UNAVAILABLE;

Modified src/OFStdIOStream.m from [1049054022] to [f28a278b7e].

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
#import "OFWriteFailedException.h"

#if defined(OF_MORPHOS) && !defined(OF_IXEMUL)
# define BOOL EXEC_BOOL
# include <exec/execbase.h>
# include <proto/dos.h>
# undef BOOL

# define INVALID_FD 0
# define getpid() ((int)SysBase->ThisTask)
# define read(fd, buf, len) Read(fd, buf, len)
# define write(fd, buf, len) Write(fd, buf, len)

extern struct ExecBase *SysBase;
#endif

#ifndef INVALID_FD
# define INVALID_FD -1
#endif

/* References for static linking */
#ifdef OF_WINDOWS
void
_reference_to_OFStdIOStream_Win32Console(void)
{
	[OFStdIOStream_Win32Console class];
}







<
<

<
<
<



<
<
<
<







40
41
42
43
44
45
46


47



48
49
50




51
52
53
54
55
56
57
#import "OFWriteFailedException.h"

#if defined(OF_MORPHOS) && !defined(OF_IXEMUL)
# define BOOL EXEC_BOOL
# include <exec/execbase.h>
# include <proto/dos.h>
# undef BOOL


# define getpid() ((int)SysBase->ThisTask)



extern struct ExecBase *SysBase;
#endif





/* References for static linking */
#ifdef OF_WINDOWS
void
_reference_to_OFStdIOStream_Win32Console(void)
{
	[OFStdIOStream_Win32Console class];
}
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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
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
	of_stderr = [[OFStdIOStream alloc] of_initWithFileDescriptor: 2];
# else
	BPTR input = Input(), output = Output();
	BPTR error = ((struct Process *)SysBase->ThisTask)->pr_CES;
	bool inputClosable = false, outputClosable = false,
	    errorClosable = false;

	if (input == INVALID_FD) {
		input = Open("*", MODE_OLDFILE);
		inputClosable = true;
	}

	if (output == INVALID_FD) {
		output = Open("*", MODE_OLDFILE);
		outputClosable = true;
	}

	if (error == INVALID_FD) {
		error = Open("*", MODE_OLDFILE);
		errorClosable = true;
	}

	of_stdin = [[OFStdIOStream alloc]
	    of_initWithFileDescriptor: input
			     closable: inputClosable];
	of_stdout = [[OFStdIOStream alloc]
	    of_initWithFileDescriptor: output
			     closable: outputClosable];
	of_stderr = [[OFStdIOStream alloc]
	    of_initWithFileDescriptor: error
			     closable: errorClosable];
# endif
}
#endif

- init
{
	OF_INVALID_INIT_METHOD
}

#if !defined(OF_MORPHOS) || defined(OF_IXEMUL)
- (instancetype)of_initWithFileDescriptor: (int)fd
{
	self = [super init];

	_fd = fd;

	return self;
}
#else
- (instancetype)of_initWithFileDescriptor: (long)fd
				 closable: (bool)closable
{
	self = [super init];

	_fd = fd;
	_closable = closable;

	return self;
}
#endif

- (void)dealloc
{
	[self close];

	[super dealloc];
}

- (bool)lowlevelIsAtEndOfStream
{

	if (_fd == INVALID_FD)
		return true;





	return _atEndOfStream;
}

- (size_t)lowlevelReadIntoBuffer: (void *)buffer
			  length: (size_t)length
{
	ssize_t ret;


	if (_fd == INVALID_FD || _atEndOfStream)
		@throw [OFReadFailedException exceptionWithObject: self
						  requestedLength: length];

#ifndef OF_WINDOWS
	if ((ret = read(_fd, buffer, length)) < 0)
		@throw [OFReadFailedException exceptionWithObject: self
						  requestedLength: length
							    errNo: errno];
#else
	if (length > UINT_MAX)
		@throw [OFOutOfRangeException exception];

	if ((ret = read(_fd, buffer, (unsigned int)length)) < 0)
		@throw [OFReadFailedException exceptionWithObject: self
						  requestedLength: length
							    errNo: errno];












#endif

	if (ret == 0)
		_atEndOfStream = true;

	return ret;
}

- (void)lowlevelWriteBuffer: (const void *)buffer
		     length: (size_t)length
{

	if (_fd == INVALID_FD || _atEndOfStream)
		@throw [OFWriteFailedException exceptionWithObject: self
						   requestedLength: length];

#ifndef OF_WINDOWS
	if (length > SSIZE_MAX)
		@throw [OFOutOfRangeException exception];

	if (write(_fd, (void *)buffer, length) != (ssize_t)length)
		@throw [OFWriteFailedException exceptionWithObject: self
						   requestedLength: length
							     errNo: errno];
#else
	if (length > INT_MAX)
		@throw [OFOutOfRangeException exception];

	if (write(_fd, buffer, (int)length) != (int)length)
		@throw [OFWriteFailedException exceptionWithObject: self
						   requestedLength: length
							     errNo: errno];












#endif
}

#if !defined(OF_WINDOWS) && (!defined(OF_MORPHOS) || defined(OF_IXEMUL))
- (int)fileDescriptorForReading
{
	return _fd;
}

- (int)fileDescriptorForWriting
{
	return _fd;
}
#endif

- (void)close
{
#if !defined(OF_MORPHOS) || defined(OF_IXEMUL)
	if (_fd != INVALID_FD)
		close(_fd);


#else
	if (_closable && _fd != INVALID_FD)
		Close(_fd);
#endif

	_fd = INVALID_FD;


	[super close];
}

- autorelease
{
	return self;







|




|




|




|
<
|
|
<
|
|
<
|



















|
|



|















>
|

>
>
>
>









>
|



|




|







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











>
|



|



|



|







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


















|

>
>

|
|
<

|
>







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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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
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
	of_stderr = [[OFStdIOStream alloc] of_initWithFileDescriptor: 2];
# else
	BPTR input = Input(), output = Output();
	BPTR error = ((struct Process *)SysBase->ThisTask)->pr_CES;
	bool inputClosable = false, outputClosable = false,
	    errorClosable = false;

	if (input == 0) {
		input = Open("*", MODE_OLDFILE);
		inputClosable = true;
	}

	if (output == 0) {
		output = Open("*", MODE_OLDFILE);
		outputClosable = true;
	}

	if (error == 0) {
		error = Open("*", MODE_OLDFILE);
		errorClosable = true;
	}

	of_stdin = [[OFStdIOStream alloc] of_initWithHandle: input

						   closable: inputClosable];
	of_stdout = [[OFStdIOStream alloc] of_initWithHandle: output

						    closable: outputClosable];
	of_stderr = [[OFStdIOStream alloc] of_initWithHandle: error

						    closable: errorClosable];
# endif
}
#endif

- init
{
	OF_INVALID_INIT_METHOD
}

#if !defined(OF_MORPHOS) || defined(OF_IXEMUL)
- (instancetype)of_initWithFileDescriptor: (int)fd
{
	self = [super init];

	_fd = fd;

	return self;
}
#else
- (instancetype)of_initWithHandle: (BPTR)handle
			 closable: (bool)closable
{
	self = [super init];

	_handle = handle;
	_closable = closable;

	return self;
}
#endif

- (void)dealloc
{
	[self close];

	[super dealloc];
}

- (bool)lowlevelIsAtEndOfStream
{
#if !defined(OF_MORPHOS) || defined(OF_IXEMUL)
	if (_fd == -1)
		return true;
#else
	if (_handle == 0)
		return true;
#endif

	return _atEndOfStream;
}

- (size_t)lowlevelReadIntoBuffer: (void *)buffer
			  length: (size_t)length
{
	ssize_t ret;

#if !defined(OF_MORPHOS) || defined(OF_IXEMUL)
	if (_fd == -1 || _atEndOfStream)
		@throw [OFReadFailedException exceptionWithObject: self
						  requestedLength: length];

# ifndef OF_WINDOWS
	if ((ret = read(_fd, buffer, length)) < 0)
		@throw [OFReadFailedException exceptionWithObject: self
						  requestedLength: length
							    errNo: errno];
# else
	if (length > UINT_MAX)
		@throw [OFOutOfRangeException exception];

	if ((ret = read(_fd, buffer, (unsigned int)length)) < 0)
		@throw [OFReadFailedException exceptionWithObject: self
						  requestedLength: length
							    errNo: errno];
# endif
#else
	if (_handle == 0 || _atEndOfStream)
		@throw [OFReadFailedException exceptionWithObject: self
						  requestedLength: length];

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

	if ((ret = Read(_handle, buffer, length)) < 0)
		@throw [OFReadFailedException exceptionWithObject: self
						  requestedLength: length];
#endif

	if (ret == 0)
		_atEndOfStream = true;

	return ret;
}

- (void)lowlevelWriteBuffer: (const void *)buffer
		     length: (size_t)length
{
#if !defined(OF_MORPHOS) || defined(OF_IXEMUL)
	if (_fd == -1 || _atEndOfStream)
		@throw [OFWriteFailedException exceptionWithObject: self
						   requestedLength: length];

# ifndef OF_WINDOWS
	if (length > SSIZE_MAX)
		@throw [OFOutOfRangeException exception];

	if (write(_fd, buffer, length) != (ssize_t)length)
		@throw [OFWriteFailedException exceptionWithObject: self
						   requestedLength: length
							     errNo: errno];
# else
	if (length > INT_MAX)
		@throw [OFOutOfRangeException exception];

	if (write(_fd, buffer, (int)length) != (int)length)
		@throw [OFWriteFailedException exceptionWithObject: self
						   requestedLength: length
							     errNo: errno];
# endif
#else
	if (_handle == 0 || _atEndOfStream)
		@throw [OFWriteFailedException exceptionWithObject: self
						   requestedLength: length];

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

	if (Write(_handle, (void *)buffer, length) != (LONG)length)
		@throw [OFWriteFailedException exceptionWithObject: self
						   requestedLength: length];
#endif
}

#if !defined(OF_WINDOWS) && (!defined(OF_MORPHOS) || defined(OF_IXEMUL))
- (int)fileDescriptorForReading
{
	return _fd;
}

- (int)fileDescriptorForWriting
{
	return _fd;
}
#endif

- (void)close
{
#if !defined(OF_MORPHOS) || defined(OF_IXEMUL)
	if (_fd != -1)
		close(_fd);

	_fd = -1;
#else
	if (_closable && _handle != 0)
		Close(_handle);


	_handle = 0;
#endif

	[super close];
}

- autorelease
{
	return self;
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
- (unsigned int)retainCount
{
	return OF_RETAIN_COUNT_MAX;
}

- (int)columns
{
#if defined(HAVE_SYS_IOCTL_H) && defined(TIOCGWINSZ)

	struct winsize ws;

	if (ioctl(_fd, TIOCGWINSZ, &ws) != 0)
		return -1;

	return ws.ws_col;
#else
	return -1;
#endif
}

- (int)rows
{
#if defined(HAVE_SYS_IOCTL_H) && defined(TIOCGWINSZ)

	struct winsize ws;

	if (ioctl(_fd, TIOCGWINSZ, &ws) != 0)
		return -1;

	return ws.ws_row;
#else
	return -1;
#endif
}
@end







|
>













|
>











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
- (unsigned int)retainCount
{
	return OF_RETAIN_COUNT_MAX;
}

- (int)columns
{
#if defined(HAVE_SYS_IOCTL_H) && defined(TIOCGWINSZ) && \
    (!defined(OF_MORPHOS) || defined(OF_IXEMUL))
	struct winsize ws;

	if (ioctl(_fd, TIOCGWINSZ, &ws) != 0)
		return -1;

	return ws.ws_col;
#else
	return -1;
#endif
}

- (int)rows
{
#if defined(HAVE_SYS_IOCTL_H) && defined(TIOCGWINSZ) && \
    (!defined(OF_MORPHOS) || defined(OF_IXEMUL))
	struct winsize ws;

	if (ioctl(_fd, TIOCGWINSZ, &ws) != 0)
		return -1;

	return ws.ws_row;
#else
	return -1;
#endif
}
@end

Modified src/OFString.m from [37a16908bb] to [fd491800a0].

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#if defined(HAVE_STRTOF_L) || defined(HAVE_STRTOD_L)
# include <locale.h>
#endif
#ifdef HAVE_XLOCALE_H
# include <xlocale.h>
#endif

#ifdef OF_HAVE_FILES
# include <sys/stat.h>
#endif

#import "OFString.h"
#import "OFString_UTF8.h"
#import "OFString_UTF8+Private.h"
#import "OFArray.h"







|







24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#if defined(HAVE_STRTOF_L) || defined(HAVE_STRTOD_L)
# include <locale.h>
#endif
#ifdef HAVE_XLOCALE_H
# include <xlocale.h>
#endif

#ifdef HAVE_SYS_STAT_H
# include <sys/stat.h>
#endif

#import "OFString.h"
#import "OFString_UTF8.h"
#import "OFString_UTF8+Private.h"
#import "OFArray.h"