Overview
Comment: | OFFile: Better abstraction of file handle
As a nice side effect, this adds a designated initializer to OFFile. |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
29403cc56ad771bdafbd4a1b82c40e82 |
User & Date: | js on 2017-06-04 17:39:30 |
Other Links: | manifest | tags |
Context
2017-06-04
| ||
18:16 | OFFile: Make sure to not leak handles on MorphOS check-in: 1ffd8f9f16 user: js tags: trunk | |
17:39 | OFFile: Better abstraction of file handle check-in: 29403cc56a user: js tags: trunk | |
16:42 | objfw-config: Fix a typo check-in: 2704a412cb user: js tags: trunk | |
Changes
Modified src/OFFile.h from [6e6ecd5f68] to [21ad1ce6b7].
︙ | ︙ | |||
12 13 14 15 16 17 18 | * 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. */ #import "OFSeekableStream.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 42 43 44 45 46 47 48 49 50 51 52 | * 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. */ #import "OFSeekableStream.h" #if !defined(OF_MORPHOS) || defined(OF_IXEMUL) # define OF_FILE_HANDLE_IS_FD # define OF_INVALID_FILE_HANDLE (-1) # define OF_FILE_HANDLE_IS_VALID(h) (h != -1) typedef int of_file_handle_t; #else # define BOOL EXEC_BOOL # include <proto/dos.h> # undef BOOL # define OF_INVALID_FILE_HANDLE ((of_file_handle_t){ 0, false }) # define OF_FILE_HANDLE_IS_VALID(h) (h.handle != 0) typedef struct of_file_handle_t { BPTR handle; bool append; } of_file_handle_t; #endif OF_ASSUME_NONNULL_BEGIN /*! * @class OFFile OFFile.h ObjFW/OFFile.h * * @brief A class which provides methods to read and write files. */ @interface OFFile: OFSeekableStream { of_file_handle_t _handle; bool _atEndOfStream; } /*! * @brief Creates a new OFFile with the specified path and mode. * * @param path The path to the file to open as a string |
︙ | ︙ | |||
61 62 63 64 65 66 67 | * `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; | < | | | < < < < < < < < | | < | 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | * `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 native file handle. * * @param handle A native file handle. If OF_FILE_HANDLE_IS_FD is defined, this * is a file descriptor. The handle is closed when the OFFile * object is deallocated! * @return A new autoreleased OFFile */ + (instancetype)fileWithHandle: (of_file_handle_t)handle; - init OF_UNAVAILABLE; /*! * @brief Initializes an already allocated OFFile. * * @param path The path to the file to open as a string |
︙ | ︙ | |||
108 109 110 111 112 113 114 | * `a+` | read-write, create, append * `ab+` or `a+b` | read-write, create, append, binary * @return An initialized OFFile */ - initWithPath: (OFString *)path mode: (OFString *)mode; | < | | < < < < < < < < | | < | 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | * `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 handle A native file handle. If OF_FILE_HANDLE_IS_FD is defined, this * is a file descriptor. The handle is closed when the OFFile * object is deallocated! * @return An initialized OFFile */ - initWithHandle: (of_file_handle_t)handle OF_DESIGNATED_INITIALIZER; @end OF_ASSUME_NONNULL_END |
Modified src/OFFile.m from [c22a9c9657] to [1d857a1710].
︙ | ︙ | |||
104 105 106 107 108 109 110 111 112 113 114 115 116 117 | return -1; } #else static int parseMode(const char *mode, bool *append) { if (strcmp(mode, "r") == 0) return MODE_OLDFILE; if (strcmp(mode, "w") == 0) return MODE_NEWFILE; if (strcmp(mode, "wx") == 0) return MODE_NEWFILE; if (strcmp(mode, "a") == 0) { | > > | 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | return -1; } #else static int parseMode(const char *mode, bool *append) { *append = false; if (strcmp(mode, "r") == 0) return MODE_OLDFILE; if (strcmp(mode, "w") == 0) return MODE_NEWFILE; if (strcmp(mode, "wx") == 0) return MODE_NEWFILE; if (strcmp(mode, "a") == 0) { |
︙ | ︙ | |||
173 174 175 176 177 178 179 | + (instancetype)fileWithPath: (OFString *)path mode: (OFString *)mode { return [[[self alloc] initWithPath: path mode: mode] autorelease]; } | < < < < < < | < | > | | | | > | | | > > > > > > > > > > > > > > > < < < < < < < < < < | < < < < < | < < | | < < < < < | < < < < < | > > > > | | > < | < < < < < < < < > | < | < < < | | | > > > > > > > > < | > | | | < < < < < | | | | | | > < | < < < | > | < | 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 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 433 434 435 436 437 438 439 | + (instancetype)fileWithPath: (OFString *)path mode: (OFString *)mode { return [[[self alloc] initWithPath: path mode: mode] autorelease]; } + (instancetype)fileWithHandle: (of_file_handle_t)handle { return [[[self alloc] initWithHandle: handle] autorelease]; } - init { OF_INVALID_INIT_METHOD } - initWithPath: (OFString *)path mode: (OFString *)mode { of_file_handle_t handle; @try { void *pool = objc_autoreleasePoolPush(); int flags; #if !defined(OF_MORPHOS) || defined(OF_IXEMUL) if ((flags = parseMode([mode UTF8String])) == -1) @throw [OFInvalidArgumentException exception]; flags |= O_CLOEXEC; # if defined(OF_WINDOWS) if ((handle = _wopen([path UTF16String], flags, _S_IREAD | _S_IWRITE)) == -1) # elif defined(OF_HAVE_OFF64_T) if ((handle = open64([path cStringWithEncoding: [OFLocalization encoding]], flags, 0666)) == -1) # else if ((handle = open([path cStringWithEncoding: [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 { self = [super init]; _handle = handle; return self; } - (bool)lowlevelIsAtEndOfStream { if (!OF_FILE_HANDLE_IS_VALID(_handle)) return true; return _atEndOfStream; } - (size_t)lowlevelReadIntoBuffer: (void *)buffer length: (size_t)length { ssize_t ret; if (!OF_FILE_HANDLE_IS_VALID(_handle) || _atEndOfStream) @throw [OFReadFailedException exceptionWithObject: self requestedLength: length]; #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) && !defined(OF_IXEMUL) if (length > LONG_MAX) @throw [OFOutOfRangeException exception]; if ((ret = Read(_handle.handle, buffer, length)) < 0) @throw [OFReadFailedException exceptionWithObject: self requestedLength: length]; #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 (!OF_FILE_HANDLE_IS_VALID(_handle) || _atEndOfStream) @throw [OFWriteFailedException exceptionWithObject: self requestedLength: length]; #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) && !defined(OF_IXEMUL) if (length > LONG_MAX) @throw [OFOutOfRangeException exception]; if (_handle.append) { if (Seek64(_handle.handle, 0, OFFSET_END) == -1) @throw [OFWriteFailedException exceptionWithObject: self requestedLength: length]; } if (Write(_handle.handle, (void *)buffer, length) != (LONG)length) @throw [OFWriteFailedException exceptionWithObject: self requestedLength: length]; #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 (!OF_FILE_HANDLE_IS_VALID(_handle)) @throw [OFSeekFailedException exceptionWithStream: self offset: offset whence: whence]; #if !defined(OF_MORPHOS) || defined(OF_IXEMUL) # 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) @throw [OFSeekFailedException exceptionWithStream: self offset: offset whence: whence]; #endif _atEndOfStream = false; return ret; } #ifdef OF_FILE_HANDLE_IS_FD - (int)fileDescriptorForReading { return _handle; } - (int)fileDescriptorForWriting { 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 |