|
| 1 | +//===--- A platform independent cookie file class -------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "hdr/errno_macros.h" |
| 10 | +#include "hdr/types/cookie_io_functions_t.h" |
| 11 | +#include "hdr/types/off_t.h" |
| 12 | +#include "src/__support/CPP/new.h" |
| 13 | +#include "src/__support/File/file.h" |
| 14 | +#include "src/__support/macros/config.h" |
| 15 | + |
| 16 | +namespace LIBC_NAMESPACE_DECL { |
| 17 | + |
| 18 | +class CookieFile : public LIBC_NAMESPACE::File { |
| 19 | + void *cookie; |
| 20 | + cookie_io_functions_t ops; |
| 21 | + |
| 22 | + LIBC_INLINE static FileIOResult cookie_write(File *f, const void *data, |
| 23 | + size_t size); |
| 24 | + LIBC_INLINE static FileIOResult cookie_read(File *f, void *data, size_t size); |
| 25 | + LIBC_INLINE static ErrorOr<off_t> cookie_seek(File *f, off_t offset, |
| 26 | + int whence); |
| 27 | + LIBC_INLINE static int cookie_close(File *f); |
| 28 | + |
| 29 | +public: |
| 30 | + LIBC_INLINE CookieFile(void *c, cookie_io_functions_t cops, uint8_t *buffer, |
| 31 | + size_t bufsize, int buffer_mode, File::ModeFlags mode) |
| 32 | + : File(&cookie_write, &cookie_read, &CookieFile::cookie_seek, |
| 33 | + &cookie_close, buffer, bufsize, buffer_mode, |
| 34 | + true /* File owns buffer */, mode), |
| 35 | + cookie(c), ops(cops) {} |
| 36 | +}; |
| 37 | + |
| 38 | +LIBC_INLINE FileIOResult CookieFile::cookie_write(File *f, const void *data, |
| 39 | + size_t size) { |
| 40 | + auto cookie_file = reinterpret_cast<CookieFile *>(f); |
| 41 | + if (cookie_file->ops.write == nullptr) |
| 42 | + return 0; |
| 43 | + return static_cast<size_t>(cookie_file->ops.write( |
| 44 | + cookie_file->cookie, reinterpret_cast<const char *>(data), size)); |
| 45 | +} |
| 46 | + |
| 47 | +LIBC_INLINE FileIOResult CookieFile::cookie_read(File *f, void *data, |
| 48 | + size_t size) { |
| 49 | + auto cookie_file = reinterpret_cast<CookieFile *>(f); |
| 50 | + if (cookie_file->ops.read == nullptr) |
| 51 | + return 0; |
| 52 | + return static_cast<size_t>(cookie_file->ops.read( |
| 53 | + cookie_file->cookie, reinterpret_cast<char *>(data), size)); |
| 54 | +} |
| 55 | + |
| 56 | +LIBC_INLINE ErrorOr<off_t> CookieFile::cookie_seek(File *f, off_t offset, |
| 57 | + int whence) { |
| 58 | + auto cookie_file = reinterpret_cast<CookieFile *>(f); |
| 59 | + if (cookie_file->ops.seek == nullptr) { |
| 60 | + return Error(EINVAL); |
| 61 | + } |
| 62 | + off64_t offset64 = offset; |
| 63 | + int result = cookie_file->ops.seek(cookie_file->cookie, &offset64, whence); |
| 64 | + if (result == 0) |
| 65 | + return offset64; |
| 66 | + return -1; |
| 67 | +} |
| 68 | + |
| 69 | +LIBC_INLINE int CookieFile::cookie_close(File *f) { |
| 70 | + auto cookie_file = reinterpret_cast<CookieFile *>(f); |
| 71 | + if (cookie_file->ops.close == nullptr) |
| 72 | + return 0; |
| 73 | + int retval = cookie_file->ops.close(cookie_file->cookie); |
| 74 | + if (retval != 0) |
| 75 | + return retval; |
| 76 | + delete cookie_file; |
| 77 | + return 0; |
| 78 | +} |
| 79 | + |
| 80 | +} // namespace LIBC_NAMESPACE_DECL |
0 commit comments