xref: /llvm-project/libc/src/stdio/printf_core/printf_main.cpp (revision 2e6eccfe34c1ff005352ca78f87bbcc4884f7371)
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 __llvm_libc {
20 namespace printf_core {
21 
22 int printf_main(Writer *writer, const char *__restrict str,
23                 internal::ArgList &args) {
24   Parser parser(str, args);
25   int result = 0;
26   for (FormatSection cur_section = parser.get_next_section();
27        cur_section.raw_len > 0; cur_section = parser.get_next_section()) {
28     if (cur_section.has_conv)
29       result = convert(writer, cur_section);
30     else
31       result = writer->write(cur_section.raw_string, cur_section.raw_len);
32 
33     if (result < 0)
34       return result;
35   }
36 
37   return writer->get_chars_written();
38 }
39 
40 } // namespace printf_core
41 } // namespace __llvm_libc
42