Warning: This file is not a C or C++ file. It does not have highlighting.

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/__support/macros/config.h"
15#include "src/__support/macros/properties/architectures.h"
16#include "src/stdio/scanf_core/reader.h"
17#include "src/stdio/scanf_core/scanf_main.h"
18
19#if defined(LIBC_TARGET_ARCH_IS_GPU)
20#include "src/stdio/ferror.h"
21#include "src/stdio/getc.h"
22#include "src/stdio/ungetc.h"
23#endif
24
25#include "hdr/types/FILE.h"
26#include <stddef.h>
27
28namespace LIBC_NAMESPACE_DECL {
29
30namespace internal {
31
32#if defined(LIBC_TARGET_ARCH_IS_GPU)
33// The GPU build provides FILE access through the host operating system's
34// library. So here we simply use the public entrypoints like in the SYSTEM_FILE
35// interface. Entrypoints should normally not call others, this is an exception.
36// FIXME: We do not acquire any locks here, so this is not thread safe.
37LIBC_INLINE void flockfile(::FILE *) { return; }
38
39LIBC_INLINE void funlockfile(::FILE *) { return; }
40
41LIBC_INLINE int ferror_unlocked(::FILE *f) { return LIBC_NAMESPACE::ferror(f); }
42
43LIBC_INLINE int getc(::FILE *f) { return LIBC_NAMESPACE::getc(f); }
44
45LIBC_INLINE void ungetc(int c, ::FILE *f) { LIBC_NAMESPACE::ungetc(c, f); }
46
47#elif !defined(LIBC_COPT_STDIO_USE_SYSTEM_FILE)
48
49LIBC_INLINE void flockfile(FILE *f) {
50 reinterpret_cast<LIBC_NAMESPACE::File *>(f)->lock();
51}
52
53LIBC_INLINE void funlockfile(FILE *f) {
54 reinterpret_cast<LIBC_NAMESPACE::File *>(f)->unlock();
55}
56
57LIBC_INLINE int ferror_unlocked(FILE *f) {
58 return reinterpret_cast<LIBC_NAMESPACE::File *>(f)->error_unlocked();
59}
60
61LIBC_INLINE int getc(FILE *f) {
62 unsigned char c;
63 auto result =
64 reinterpret_cast<LIBC_NAMESPACE::File *>(f)->read_unlocked(&c, 1);
65 size_t r = result.value;
66 if (result.has_error() || r != 1)
67 return '\0';
68
69 return c;
70}
71
72LIBC_INLINE void ungetc(int c, FILE *f) {
73 reinterpret_cast<LIBC_NAMESPACE::File *>(f)->ungetc_unlocked(c);
74}
75
76#else // defined(LIBC_COPT_STDIO_USE_SYSTEM_FILE)
77
78// Since ungetc_unlocked isn't always available, we don't acquire the lock for
79// system files.
80LIBC_INLINE void flockfile(::FILE *) { return; }
81
82LIBC_INLINE void funlockfile(::FILE *) { return; }
83
84LIBC_INLINE int ferror_unlocked(::FILE *f) { return ::ferror(f); }
85
86LIBC_INLINE int getc(::FILE *f) { return ::getc(f); }
87
88LIBC_INLINE void ungetc(int c, ::FILE *f) { ::ungetc(c, f); }
89
90#endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE
91
92} // namespace internal
93
94namespace scanf_core {
95
96class StreamReader : public Reader<StreamReader> {
97 ::FILE *stream;
98
99public:
100 LIBC_INLINE StreamReader(::FILE *stream) : stream(stream) {}
101
102 LIBC_INLINE char getc() {
103 return static_cast<char>(internal::getc(static_cast<FILE *>(stream)));
104 }
105 LIBC_INLINE void ungetc(int c) {
106 internal::ungetc(c, static_cast<FILE *>(stream));
107 }
108};
109
110LIBC_INLINE int vfscanf_internal(::FILE *__restrict stream,
111 const char *__restrict format,
112 internal::ArgList &args) {
113 internal::flockfile(stream);
114 scanf_core::StreamReader reader(stream);
115 int retval = scanf_core::scanf_main(&reader, format, args);
116 if (retval == 0 && internal::ferror_unlocked(stream))
117 retval = EOF;
118 internal::funlockfile(stream);
119
120 return retval;
121}
122} // namespace scanf_core
123} // namespace LIBC_NAMESPACE_DECL
124
125#endif // LLVM_LIBC_SRC_STDIO_SCANF_CORE_VFSCANF_INTERNAL_H
126

Warning: This file is not a C or C++ file. It does not have highlighting.

source code of libc/src/stdio/scanf_core/vfscanf_internal.h