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