51 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLVM_LIBC_SRC_STDIO_SCANF_CORE_SCANF_MAIN_H10#define LLVM_LIBC_SRC_STDIO_SCANF_CORE_SCANF_MAIN_H11 12#include "src/__support/arg_list.h"13#include "src/__support/macros/config.h"14#include "src/stdio/scanf_core/converter.h"15#include "src/stdio/scanf_core/core_structs.h"16#include "src/stdio/scanf_core/parser.h"17#include "src/stdio/scanf_core/reader.h"18 19#include <stddef.h>20 21namespace LIBC_NAMESPACE_DECL {22namespace scanf_core {23 24template <typename T>25int scanf_main(Reader<T> *reader, const char *__restrict str,26 internal::ArgList &args) {27 Parser<internal::ArgList> parser(str, args);28 int ret_val = READ_OK;29 int conversions = 0;30 for (FormatSection cur_section = parser.get_next_section();31 !cur_section.raw_string.empty() && ret_val == READ_OK;32 cur_section = parser.get_next_section()) {33 if (cur_section.has_conv) {34 ret_val = convert(reader, cur_section);35 // The %n (current position) conversion doesn't increment the number of36 // assignments.37 if (cur_section.conv_name != 'n')38 conversions += ret_val == READ_OK ? 1 : 0;39 } else {40 ret_val = raw_match(reader, cur_section.raw_string);41 }42 }43 44 return conversions;45}46 47} // namespace scanf_core48} // namespace LIBC_NAMESPACE_DECL49 50#endif // LLVM_LIBC_SRC_STDIO_SCANF_CORE_SCANF_MAIN_H51