1 | //===-- Internal implementation header of vfscanf ---------------*- C++ -*-===// |
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 | #ifndef LLVM_LIBC_SRC_STDIO_SCANF_CORE_VFSCANF_INTERNAL_H |
10 | #define LLVM_LIBC_SRC_STDIO_SCANF_CORE_VFSCANF_INTERNAL_H |
11 | |
12 | #include "src/__support/File/file.h" |
13 | #include "src/__support/arg_list.h" |
14 | #include "src/stdio/scanf_core/reader.h" |
15 | #include "src/stdio/scanf_core/scanf_main.h" |
16 | |
17 | #include <stdio.h> |
18 | |
19 | namespace LIBC_NAMESPACE { |
20 | |
21 | namespace internal { |
22 | |
23 | #ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE |
24 | |
25 | LIBC_INLINE void flockfile(FILE *f) { |
26 | reinterpret_cast<LIBC_NAMESPACE::File *>(f)->lock(); |
27 | } |
28 | |
29 | LIBC_INLINE void funlockfile(FILE *f) { |
30 | reinterpret_cast<LIBC_NAMESPACE::File *>(f)->unlock(); |
31 | } |
32 | |
33 | LIBC_INLINE int getc(void *f) { |
34 | unsigned char c; |
35 | auto result = |
36 | reinterpret_cast<LIBC_NAMESPACE::File *>(f)->read_unlocked(&c, 1); |
37 | size_t r = result.value; |
38 | if (result.has_error() || r != 1) |
39 | return '\0'; |
40 | |
41 | return c; |
42 | } |
43 | |
44 | LIBC_INLINE void ungetc(int c, void *f) { |
45 | reinterpret_cast<LIBC_NAMESPACE::File *>(f)->ungetc_unlocked(c); |
46 | } |
47 | |
48 | LIBC_INLINE int ferror_unlocked(FILE *f) { |
49 | return reinterpret_cast<LIBC_NAMESPACE::File *>(f)->error_unlocked(); |
50 | } |
51 | |
52 | #else // defined(LIBC_COPT_STDIO_USE_SYSTEM_FILE) |
53 | |
54 | // Since ungetc_unlocked isn't always available, we don't acquire the lock for |
55 | // system files. |
56 | LIBC_INLINE void flockfile(::FILE *) { return; } |
57 | |
58 | LIBC_INLINE void funlockfile(::FILE *) { return; } |
59 | |
60 | LIBC_INLINE int getc(void *f) { return ::getc(stream: reinterpret_cast<::FILE *>(f)); } |
61 | |
62 | LIBC_INLINE void ungetc(int c, void *f) { |
63 | ::ungetc(c: c, stream: reinterpret_cast<::FILE *>(f)); |
64 | } |
65 | |
66 | LIBC_INLINE int ferror_unlocked(::FILE *f) { return ::ferror(stream: f); } |
67 | |
68 | #endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE |
69 | |
70 | } // namespace internal |
71 | |
72 | namespace scanf_core { |
73 | |
74 | LIBC_INLINE int vfscanf_internal(::FILE *__restrict stream, |
75 | const char *__restrict format, |
76 | internal::ArgList &args) { |
77 | internal::flockfile(stream); |
78 | scanf_core::Reader reader(stream, &internal::getc, internal::ungetc); |
79 | int retval = scanf_core::scanf_main(reader: &reader, str: format, args); |
80 | if (retval == 0 && internal::ferror_unlocked(f: stream)) |
81 | retval = EOF; |
82 | internal::funlockfile(stream); |
83 | |
84 | return retval; |
85 | } |
86 | } // namespace scanf_core |
87 | } // namespace LIBC_NAMESPACE |
88 | |
89 | #endif // LLVM_LIBC_SRC_STDIO_SCANF_CORE_VFSCANF_INTERNAL_H |
90 | |