xref: /llvm-project/libc/src/stdio/printf_core/printf_main.cpp (revision ce9035f5bd3aa09cbd899489cdbc7f6c18acf1e3)
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 #include "src/stdio/printf_core/printf_main.h"
10 
11 #include "src/__support/arg_list.h"
12 #include "src/stdio/printf_core/converter.h"
13 #include "src/stdio/printf_core/core_structs.h"
14 #include "src/stdio/printf_core/parser.h"
15 #include "src/stdio/printf_core/writer.h"
16 
17 #include <stddef.h>
18 
19 namespace LIBC_NAMESPACE {
20 namespace printf_core {
21 
22 int printf_main(Writer *writer, const char *__restrict str,
23                 internal::ArgList &args) {
24   Parser<internal::ArgList> parser(str, args);
25   int result = 0;
26   for (FormatSection cur_section = parser.get_next_section();
27        !cur_section.raw_string.empty();
28        cur_section = parser.get_next_section()) {
29     if (cur_section.has_conv)
30       result = convert(writer, cur_section);
31     else
32       result = writer->write(cur_section.raw_string);
33 
34     if (result < 0)
35       return result;
36   }
37 
38   return writer->get_chars_written();
39 }
40 
41 } // namespace printf_core
42 } // namespace LIBC_NAMESPACE
43