1 | //===-- Starting point 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 | #include "src/stdio/scanf_core/scanf_main.h" |
10 | |
11 | #include "src/__support/arg_list.h" |
12 | #include "src/stdio/scanf_core/converter.h" |
13 | #include "src/stdio/scanf_core/core_structs.h" |
14 | #include "src/stdio/scanf_core/parser.h" |
15 | #include "src/stdio/scanf_core/reader.h" |
16 | |
17 | #include <stddef.h> |
18 | |
19 | namespace LIBC_NAMESPACE { |
20 | namespace scanf_core { |
21 | |
22 | int scanf_main(Reader *reader, const char *__restrict str, |
23 | internal::ArgList &args) { |
24 | Parser<internal::ArgList> parser(str, args); |
25 | int ret_val = READ_OK; |
26 | int conversions = 0; |
27 | for (FormatSection cur_section = parser.get_next_section(); |
28 | !cur_section.raw_string.empty() && ret_val == READ_OK; |
29 | cur_section = parser.get_next_section()) { |
30 | if (cur_section.has_conv) { |
31 | ret_val = convert(reader, to_conv: cur_section); |
32 | // The %n (current position) conversion doesn't increment the number of |
33 | // assignments. |
34 | if (cur_section.conv_name != 'n') |
35 | conversions += ret_val == READ_OK ? 1 : 0; |
36 | } else { |
37 | ret_val = raw_match(reader, raw_string: cur_section.raw_string); |
38 | } |
39 | } |
40 | |
41 | return conversions; |
42 | } |
43 | |
44 | } // namespace scanf_core |
45 | } // namespace LIBC_NAMESPACE |
46 |