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
|
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
|
-
+
+
+
+
+
+
-
-
-
+
+
+
+
|
# include <sys/stat.h>
#endif
#import "OFFile.h"
#import "OFLocale.h"
#import "OFString.h"
#import "OFSystemInfo.h"
#import "OFURL.h"
#import "OFURI.h"
#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFNotOpenException.h"
#import "OFOpenItemFailedException.h"
#import "OFOutOfMemoryException.h"
#import "OFOutOfRangeException.h"
#import "OFReadFailedException.h"
#import "OFSeekFailedException.h"
#import "OFWriteFailedException.h"
#ifdef OF_WINDOWS
# include <windows.h>
#endif
#ifdef OF_AMIGAOS
# include <proto/exec.h>
# include <proto/dos.h>
#endif
#ifdef OF_WII
# include <fat.h>
#endif
#ifdef OF_NINTENDO_DS
# include <stdbool.h>
# include <filesystem.h>
#endif
#ifdef OF_AMIGAOS
# include <proto/exec.h>
# include <proto/dos.h>
#ifdef OF_NINTENDO_SWITCH
# define id nx_id
# include <switch.h>
# undef id
#endif
#ifndef O_BINARY
# define O_BINARY 0
#endif
#ifndef O_CLOEXEC
# define O_CLOEXEC 0
|
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
|
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
|
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
|
#endif
#ifdef OF_NINTENDO_DS
if (!nitroFSInit(NULL))
@throw [OFInitializationFailedException
exceptionWithClass: self];
#endif
#ifdef OF_NINTENDO_SWITCH
if (R_SUCCEEDED(romfsInit()))
/*
* Errors are intentionally ignored, as it's possible we just
* have no romfs.
*/
atexit((void (*)(void))romfsExit);
#endif
}
+ (instancetype)fileWithPath: (OFString *)path mode: (OFString *)mode
{
return [[[self alloc] initWithPath: path mode: mode] autorelease];
}
+ (instancetype)fileWithURL: (OFURL *)URL mode: (OFString *)mode
{
return [[[self alloc] initWithURL: URL mode: mode] autorelease];
}
+ (instancetype)fileWithHandle: (OFFileHandle)handle
{
return [[[self alloc] initWithHandle: handle] autorelease];
}
- (instancetype)init
{
OF_INVALID_INIT_METHOD
}
- (instancetype)initWithPath: (OFString *)path mode: (OFString *)mode
{
OFFileHandle handle;
@try {
void *pool = objc_autoreleasePoolPush();
int flags;
|
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
|
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
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
|
closeHandle(handle);
@throw e;
}
return self;
}
- (instancetype)initWithURL: (OFURL *)URL mode: (OFString *)mode
{
void *pool = objc_autoreleasePoolPush();
OFString *fileSystemRepresentation;
@try {
fileSystemRepresentation = URL.fileSystemRepresentation;
} @catch (id e) {
[self release];
@throw e;
}
self = [self initWithPath: fileSystemRepresentation mode: mode];
objc_autoreleasePoolPop(pool);
return self;
}
- (instancetype)initWithHandle: (OFFileHandle)handle
{
self = [super init];
_handle = handle;
return self;
}
- (instancetype)init
{
OF_INVALID_INIT_METHOD
}
- (bool)lowlevelIsAtEndOfStream
{
if (_handle == OFInvalidFileHandle)
@throw [OFNotOpenException exceptionWithObject: self];
return _atEndOfStream;
|
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
|
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
|
-
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
+
-
+
-
+
-
+
-
+
|
bytesWritten: 0
errNo: errno];
#endif
return (size_t)bytesWritten;
}
- (OFFileOffset)lowlevelSeekToOffset: (OFFileOffset)offset whence: (int)whence
- (OFStreamOffset)lowlevelSeekToOffset: (OFStreamOffset)offset
whence: (OFSeekWhence)whence
{
OFFileOffset ret;
OFStreamOffset ret;
if (_handle == OFInvalidFileHandle)
@throw [OFNotOpenException exceptionWithObject: self];
#ifndef OF_AMIGAOS
int translatedWhence;
switch (whence) {
case OFSeekSet:
translatedWhence = SEEK_SET;
break;
case OFSeekCurrent:
translatedWhence = SEEK_CUR;
break;
case OFSeekEnd:
translatedWhence = SEEK_END;
break;
default:
@throw [OFSeekFailedException exceptionWithStream: self
offset: offset
whence: whence
errNo: EINVAL];
}
# if defined(OF_WINDOWS)
ret = _lseeki64(_handle, offset, whence);
ret = _lseeki64(_handle, offset, translatedWhence);
# elif defined(HAVE_LSEEK64)
ret = lseek64(_handle, offset, whence);
ret = lseek64(_handle, offset, translatedWhence);
# else
ret = lseek(_handle, offset, whence);
ret = lseek(_handle, offset, translatedWhence);
# endif
if (ret == -1)
@throw [OFSeekFailedException exceptionWithStream: self
offset: offset
whence: whence
errNo: errno];
#else
LONG translatedWhence;
switch (whence) {
case SEEK_SET:
case OFSeekSet:
translatedWhence = OFFSET_BEGINNING;
break;
case SEEK_CUR:
case OFSeekCurrent:
translatedWhence = OFFSET_CURRENT;
break;
case SEEK_END:
case OFSeekEnd:
translatedWhence = OFFSET_END;
break;
default:
@throw [OFSeekFailedException exceptionWithStream: self
offset: offset
whence: whence
errNo: EINVAL];
|