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