• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- Starting point for printf -------------------------------*- 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_PRINTF_CORE_PRINTF_MAIN_H
10 #define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_PRINTF_MAIN_H
11 
12 #include "src/__support/arg_list.h"
13 #include "src/__support/macros/config.h"
14 #include "src/stdio/printf_core/converter.h"
15 #include "src/stdio/printf_core/core_structs.h"
16 #include "src/stdio/printf_core/parser.h"
17 #include "src/stdio/printf_core/writer.h"
18 
19 #include <stddef.h>
20 
21 namespace LIBC_NAMESPACE_DECL {
22 namespace printf_core {
23 
24 template <WriteMode write_mode>
printf_main(Writer<write_mode> * writer,const char * __restrict str,internal::ArgList & args)25 int printf_main(Writer<write_mode> *writer, const char *__restrict str,
26                 internal::ArgList &args) {
27   Parser<internal::ArgList> parser(str, args);
28   int result = 0;
29   for (FormatSection cur_section = parser.get_next_section();
30        !cur_section.raw_string.empty();
31        cur_section = parser.get_next_section()) {
32     if (cur_section.has_conv)
33       result = convert(writer, cur_section);
34     else
35       result = writer->write(cur_section.raw_string);
36 
37     if (result < 0)
38       return result;
39   }
40 
41   return writer->get_chars_written();
42 }
43 
44 } // namespace printf_core
45 } // namespace LIBC_NAMESPACE_DECL
46 
47 #endif // LLVM_LIBC_SRC_STDIO_PRINTF_CORE_PRINTF_MAIN_H
48