Overview
Comment: | OFFile: Rework handle on MorphOS
The previous one didn't work, as the index was part of the handle, but |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
a9b2bd12eb8c4994885f437eef7672a1 |
User & Date: | js on 2017-06-11 02:34:04 |
Other Links: | manifest | tags |
Context
2017-06-11
| ||
03:01 | OFFileManager: Use native AmigaDOS APIs on MorphOS check-in: 769145b2a9 user: js tags: trunk | |
02:34 | OFFile: Rework handle on MorphOS check-in: a9b2bd12eb user: js tags: trunk | |
01:24 | runtime: Fix initialization of static instances check-in: 9027e1ab8d user: js tags: trunk | |
Changes
Modified src/OFFile.h from [dcb097e34e] to [d0fae452f5].
︙ | ︙ | |||
15 16 17 18 19 20 21 | */ #import "OFSeekableStream.h" #ifndef OF_MORPHOS # define OF_FILE_HANDLE_IS_FD # define OF_INVALID_FILE_HANDLE (-1) | < | < | < < < < | 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | */ #import "OFSeekableStream.h" #ifndef OF_MORPHOS # define OF_FILE_HANDLE_IS_FD # define OF_INVALID_FILE_HANDLE (-1) typedef int of_file_handle_t; #else # define BOOL EXEC_BOOL # include <proto/dos.h> # undef BOOL # define OF_INVALID_FILE_HANDLE NULL typedef struct of_file_handle *of_file_handle_t; #endif OF_ASSUME_NONNULL_BEGIN /*! * @class OFFile OFFile.h ObjFW/OFFile.h * |
︙ | ︙ |
Modified src/OFFile.m from [89ce8091b7] to [b2ec5b887b].
︙ | ︙ | |||
33 34 35 36 37 38 39 40 41 42 43 44 45 46 | #import "OFLocalization.h" #import "OFDataArray.h" #import "OFInitializationFailedException.h" #import "OFInvalidArgumentException.h" #import "OFNotOpenException.h" #import "OFOpenItemFailedException.h" #import "OFOutOfRangeException.h" #import "OFReadFailedException.h" #import "OFSeekFailedException.h" #import "OFWriteFailedException.h" #ifdef OF_WINDOWS # include <windows.h> | > | 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #import "OFLocalization.h" #import "OFDataArray.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> |
︙ | ︙ | |||
69 70 71 72 73 74 75 | #ifndef O_EXLOCK # define O_EXLOCK 0 #endif #ifndef OF_MORPHOS # define closeHandle(h) close(h) #else | > > | > > | < < | | | | | > > | | < | < | | 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 109 110 | #ifndef O_EXLOCK # define O_EXLOCK 0 #endif #ifndef OF_MORPHOS # define closeHandle(h) close(h) #else struct of_file_handle { of_file_handle_t previous, next; BPTR handle; bool append; } *firstHandle = NULL; static void closeHandle(of_file_handle_t handle) { Close(handle->handle); if (handle->previous != NULL) handle->previous->next = handle->next; if (handle->next != NULL) handle->next->previous = handle->previous; if (firstHandle == handle) firstHandle = handle->next; free(handle); } OF_DESTRUCTOR() { for (of_file_handle_t iter = firstHandle; iter != NULL; iter = iter->next) Close(iter->handle); } #endif #ifndef OF_MORPHOS static int parseMode(const char *mode) { |
︙ | ︙ | |||
189 190 191 192 193 194 195 | @implementation OFFile + (void)initialize { if (self != [OFFile class]) return; | < < < < | 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | @implementation OFFile + (void)initialize { if (self != [OFFile class]) return; #ifdef OF_WII if (!fatInitDefault()) @throw [OFInitializationFailedException exceptionWithClass: self]; #endif #ifdef OF_NINTENDO_DS |
︙ | ︙ | |||
253 254 255 256 257 258 259 | [OFLocalization encoding]], flags, 0666)) == -1) # endif @throw [OFOpenItemFailedException exceptionWithPath: path mode: mode errNo: errno]; #else | | > > > | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | < < | | | | | | | | | > > > > > > > > > > > > | 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 | [OFLocalization encoding]], flags, 0666)) == -1) # endif @throw [OFOpenItemFailedException exceptionWithPath: path mode: mode errNo: errno]; #else if ((handle = malloc(sizeof(*handle))) == NULL) @throw [OFOutOfMemoryException exceptionWithRequestedSize: sizeof(*handle)]; @try { if ((flags = parseMode([mode UTF8String], &handle->append)) == -1) @throw [OFInvalidArgumentException exception]; if ((handle->handle = Open([path cStringWithEncoding: [OFLocalization encoding]], flags)) == 0) { int errNo; switch (IoErr()) { case ERROR_OBJECT_IN_USE: case ERROR_DISK_NOT_VALIDATED: errNo = EBUSY; break; case ERROR_OBJECT_NOT_FOUND: errNo = ENOENT; break; case ERROR_DISK_WRITE_PROTECTED: errNo = EROFS; break; case ERROR_WRITE_PROTECTED: case ERROR_READ_PROTECTED: errNo = EACCES; break; default: errNo = 0; break; } @throw [OFOpenItemFailedException exceptionWithPath: path mode: mode errNo: errNo]; } if (handle->append) { if (Seek64(handle->handle, 0, OFFSET_END) == -1) { Close(handle->handle); @throw [OFOpenItemFailedException exceptionWithPath: path mode: mode errNo: EIO]; } } handle->previous = NULL; handle->next = firstHandle; if (firstHandle != NULL) firstHandle->previous = handle; firstHandle = handle; } @catch (id e) { free(handle); @throw e; } #endif objc_autoreleasePoolPop(pool); } @catch (id e) { [self release]; @throw e; |
︙ | ︙ | |||
330 331 332 333 334 335 336 | _handle = handle; return self; } - (bool)lowlevelIsAtEndOfStream { | | | | | | | | | | | | | 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 433 434 435 436 437 438 439 440 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 | _handle = handle; return self; } - (bool)lowlevelIsAtEndOfStream { if (_handle == OF_INVALID_FILE_HANDLE) @throw [OFNotOpenException exceptionWithObject: self]; return _atEndOfStream; } - (size_t)lowlevelReadIntoBuffer: (void *)buffer length: (size_t)length { ssize_t ret; if (_handle == OF_INVALID_FILE_HANDLE) @throw [OFNotOpenException exceptionWithObject: self]; #if defined(OF_WINDOWS) 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) if (length > LONG_MAX) @throw [OFOutOfRangeException exception]; if ((ret = Read(_handle->handle, buffer, length)) < 0) @throw [OFReadFailedException exceptionWithObject: self requestedLength: length errNo: EIO]; #else if ((ret = read(_handle, 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 (_handle == OF_INVALID_FILE_HANDLE) @throw [OFNotOpenException exceptionWithObject: self]; #if defined(OF_WINDOWS) if (length > INT_MAX) @throw [OFOutOfRangeException exception]; if (write(_handle, buffer, (int)length) != (int)length) @throw [OFWriteFailedException exceptionWithObject: self requestedLength: length errNo: errno]; #elif defined(OF_MORPHOS) if (length > LONG_MAX) @throw [OFOutOfRangeException exception]; if (_handle->append) { if (Seek64(_handle->handle, 0, OFFSET_END) == -1) @throw [OFWriteFailedException exceptionWithObject: self requestedLength: length errNo: EIO]; } if (Write(_handle->handle, (void *)buffer, length) != (LONG)length) @throw [OFWriteFailedException exceptionWithObject: self requestedLength: length errNo: EIO]; #else if (length > SSIZE_MAX) @throw [OFOutOfRangeException exception]; if (write(_handle, 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 (_handle == OF_INVALID_FILE_HANDLE) @throw [OFNotOpenException exceptionWithObject: self]; #ifndef OF_MORPHOS # 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 switch (whence) { case SEEK_SET: ret = Seek64(_handle->handle, offset, OFFSET_BEGINNING); break; case SEEK_CUR: ret = Seek64(_handle->handle, offset, OFFSET_CURRENT); break; case SEEK_END: ret = Seek64(_handle->handle, offset, OFFSET_END); break; default: ret = -1; break; } if (ret == -1) |
︙ | ︙ | |||
478 479 480 481 482 483 484 | { return _handle; } #endif - (void)close { | | | 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 | { return _handle; } #endif - (void)close { if (_handle != OF_INVALID_FILE_HANDLE) closeHandle(_handle); _handle = OF_INVALID_FILE_HANDLE; [super close]; } |
︙ | ︙ |