ObjFW  Diff

Differences From Artifact [1d857a1710]:

To Artifact [9167fd4ad1]:


12
13
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
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"


#include <errno.h>

#ifdef HAVE_FCNTL_H
# include <fcntl.h>
#endif
#include "unistd_wrapper.h"

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

#import "OFFile.h"
#import "OFString.h"
#import "OFLocalization.h"


#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFOpenItemFailedException.h"
#import "OFOutOfRangeException.h"
#import "OFReadFailedException.h"
#import "OFSeekFailedException.h"







>














>







12
13
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
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#include <assert.h>
#include <errno.h>

#ifdef HAVE_FCNTL_H
# include <fcntl.h>
#endif
#include "unistd_wrapper.h"

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

#import "OFFile.h"
#import "OFString.h"
#import "OFLocalization.h"
#import "OFDataArray.h"

#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFOpenItemFailedException.h"
#import "OFOutOfRangeException.h"
#import "OFReadFailedException.h"
#import "OFSeekFailedException.h"
62
63
64
65
66
67
68































69
70
71
72
73
74
75
#endif
#ifndef O_EXCL
# define O_EXCL 0
#endif
#ifndef O_EXLOCK
# define O_EXLOCK 0
#endif
































#if !defined(OF_MORPHOS) || defined(OF_IXEMUL)
static int
parseMode(const char *mode)
{
	if (strcmp(mode, "r") == 0)
		return O_RDONLY;







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







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
#endif
#ifndef O_EXCL
# define O_EXCL 0
#endif
#ifndef O_EXLOCK
# define O_EXLOCK 0
#endif

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

static void
closeHandle(of_file_handle_t handle)
{
	if (handle.index != SIZE_MAX) {
		BPTR *handles = [openHandles items];
		size_t count = [openHandles count];

		assert(handles[handle.index] == handle.handle);

		handles[handle.index] = handles[count - 1];
		[openHandles removeItemAtIndex: count - 1];
	}

	Close(handle.handle);
}

OF_DESTRUCTOR()
{
	BPTR *handles = [openHandles items];
	size_t count = [openHandles count];

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

#if !defined(OF_MORPHOS) || defined(OF_IXEMUL)
static int
parseMode(const char *mode)
{
	if (strcmp(mode, "r") == 0)
		return O_RDONLY;
154
155
156
157
158
159
160




161
162
163
164
165
166
167
#endif

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





#ifdef OF_WII
	if (!fatInitDefault())
		@throw [OFInitializationFailedException
		    exceptionWithClass: self];
#endif








>
>
>
>







187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#endif

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

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

#ifdef OF_WII
	if (!fatInitDefault())
		@throw [OFInitializationFailedException
		    exceptionWithClass: self];
#endif

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
		    [OFLocalization encoding]], flags, 0666)) == -1)
# endif
			@throw [OFOpenItemFailedException
			    exceptionWithPath: path
					 mode: mode
					errNo: errno];
#else


		if ((flags = parseMode([mode UTF8String],
		    &handle.append)) == -1)
			@throw [OFInvalidArgumentException exception];

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




		if (handle.append) {
			if (Seek64(handle.handle, 0, OFFSET_END) == -1) {
				Close(handle.handle);
				@throw [OFOpenItemFailedException
				    exceptionWithPath: path
						 mode: mode];
			}
		}
#endif

		objc_autoreleasePoolPop(pool);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	@try {
		self = [self initWithHandle: handle];
	} @catch (id e) {
#if !defined(OF_MORPHOS) || defined(OF_IXEMUL)
		close(handle);
#else
		Close(handle.handle);
#endif
		@throw e;
	}

	return self;
}

- initWithHandle: (of_file_handle_t)handle







>
>










>
>
>


|
















<
|
<
<
<







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
		    [OFLocalization encoding]], flags, 0666)) == -1)
# endif
			@throw [OFOpenItemFailedException
			    exceptionWithPath: path
					 mode: mode
					errNo: errno];
#else
		handle.index = SIZE_MAX;

		if ((flags = parseMode([mode UTF8String],
		    &handle.append)) == -1)
			@throw [OFInvalidArgumentException exception];

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

		[openHandles addItem: &handle.handle];
		handle.index = [openHandles count] - 1;

		if (handle.append) {
			if (Seek64(handle.handle, 0, OFFSET_END) == -1) {
				closeHandle(handle);
				@throw [OFOpenItemFailedException
				    exceptionWithPath: path
						 mode: mode];
			}
		}
#endif

		objc_autoreleasePoolPop(pool);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	@try {
		self = [self initWithHandle: handle];
	} @catch (id e) {

		closeHandle(handle);



		@throw e;
	}

	return self;
}

- initWithHandle: (of_file_handle_t)handle
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
	return _handle;
}
#endif

- (void)close
{
	if (OF_FILE_HANDLE_IS_VALID(_handle))
#if !defined(OF_MORPHOS) || defined(OF_IXEMUL)
		close(_handle);
#else
		Close(_handle.handle);
#endif

	_handle = OF_INVALID_FILE_HANDLE;

	[super close];
}

- (void)dealloc
{
	[self close];

	[super dealloc];
}
@end







<
|
<
<
<













453
454
455
456
457
458
459

460



461
462
463
464
465
466
467
468
469
470
471
472
473
	return _handle;
}
#endif

- (void)close
{
	if (OF_FILE_HANDLE_IS_VALID(_handle))

		closeHandle(_handle);




	_handle = OF_INVALID_FILE_HANDLE;

	[super close];
}

- (void)dealloc
{
	[self close];

	[super dealloc];
}
@end