Warning: This file is not a C or C++ file. It does not have highlighting.
1 | //===-- Core Structures for scanf ------------------------------*- 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_CORE_STRUCTS_H |
10 | #define LLVM_LIBC_SRC_STDIO_SCANF_CORE_CORE_STRUCTS_H |
11 | |
12 | #include "src/__support/CPP/bitset.h" |
13 | #include "src/__support/CPP/string_view.h" |
14 | #include "src/__support/macros/config.h" |
15 | |
16 | #include <inttypes.h> |
17 | #include <stddef.h> |
18 | |
19 | namespace LIBC_NAMESPACE_DECL { |
20 | namespace scanf_core { |
21 | |
22 | // These length modifiers match the length modifiers in the format string, which |
23 | // is why they are formatted differently from the rest of the file. |
24 | enum class LengthModifier { hh, h, l, ll, j, z, t, L, NONE }; |
25 | |
26 | enum FormatFlags : uint8_t { |
27 | NONE = 0x00, |
28 | NO_WRITE = 0x01, // * |
29 | ALLOCATE = 0x02, // m |
30 | }; |
31 | |
32 | struct FormatSection { |
33 | bool has_conv; |
34 | |
35 | cpp::string_view raw_string; |
36 | |
37 | // Format Specifier Values |
38 | FormatFlags flags = FormatFlags::NONE; |
39 | LengthModifier length_modifier = LengthModifier::NONE; |
40 | int max_width = -1; |
41 | |
42 | // output_ptr is nullptr if and only if the NO_WRITE flag is set. |
43 | void *output_ptr = nullptr; |
44 | |
45 | char conv_name; |
46 | |
47 | cpp::bitset<256> scan_set; |
48 | |
49 | LIBC_INLINE bool operator==(const FormatSection &other) { |
50 | if (has_conv != other.has_conv) |
51 | return false; |
52 | |
53 | if (raw_string != other.raw_string) |
54 | return false; |
55 | |
56 | if (has_conv) { |
57 | if (!((static_cast<uint8_t>(flags) == |
58 | static_cast<uint8_t>(other.flags)) && |
59 | (max_width == other.max_width) && |
60 | (length_modifier == other.length_modifier) && |
61 | (conv_name == other.conv_name))) |
62 | return false; |
63 | |
64 | // If the pointers are used, then they should be equal. If the NO_WRITE |
65 | // flag is set or the conversion is %, then the pointers are not used. |
66 | // If the pointers are used and they are not equal, return false. |
67 | |
68 | if (!(((flags & FormatFlags::NO_WRITE) != 0) || (conv_name == '%') || |
69 | (output_ptr == other.output_ptr))) |
70 | return false; |
71 | |
72 | if (conv_name == '[') |
73 | return scan_set == other.scan_set; |
74 | } |
75 | return true; |
76 | } |
77 | }; |
78 | |
79 | enum ErrorCodes : int { |
80 | // This is the value to be returned by conversions when no error has occurred. |
81 | READ_OK = 0, |
82 | // These are the scanf return values for when an error has occurred. They are |
83 | // all negative, and should be distinct. |
84 | FILE_READ_ERROR = -1, |
85 | FILE_STATUS_ERROR = -2, |
86 | MATCHING_FAILURE = -3, |
87 | ALLOCATION_FAILURE = -4, |
88 | }; |
89 | } // namespace scanf_core |
90 | } // namespace LIBC_NAMESPACE_DECL |
91 | |
92 | #endif // LLVM_LIBC_SRC_STDIO_SCANF_CORE_CORE_STRUCTS_H |
93 |
Warning: This file is not a C or C++ file. It does not have highlighting.