xref: /llvm-project/libc/src/stdio/printf_core/printf_main.cpp (revision 5ff3ff33ff930e4ec49da7910612d8a41eb068cb)
1e1c54d4dSMichael Jones //===-- Starting point for printf -------------------------------*- C++ -*-===//
2e1c54d4dSMichael Jones //
3e1c54d4dSMichael Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e1c54d4dSMichael Jones // See https://llvm.org/LICENSE.txt for license information.
5e1c54d4dSMichael Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e1c54d4dSMichael Jones //
7e1c54d4dSMichael Jones //===----------------------------------------------------------------------===//
8e1c54d4dSMichael Jones 
9e1c54d4dSMichael Jones #include "src/stdio/printf_core/printf_main.h"
10e1c54d4dSMichael Jones 
11e1c54d4dSMichael Jones #include "src/__support/arg_list.h"
12*5ff3ff33SPetr Hosek #include "src/__support/macros/config.h"
13e1c54d4dSMichael Jones #include "src/stdio/printf_core/converter.h"
14e1c54d4dSMichael Jones #include "src/stdio/printf_core/core_structs.h"
15e1c54d4dSMichael Jones #include "src/stdio/printf_core/parser.h"
16e1c54d4dSMichael Jones #include "src/stdio/printf_core/writer.h"
17e1c54d4dSMichael Jones 
18e1c54d4dSMichael Jones #include <stddef.h>
19e1c54d4dSMichael Jones 
20*5ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL {
21e1c54d4dSMichael Jones namespace printf_core {
22e1c54d4dSMichael Jones 
23e1c54d4dSMichael Jones int printf_main(Writer *writer, const char *__restrict str,
24e1c54d4dSMichael Jones                 internal::ArgList &args) {
25e0be78beSJoseph Huber   Parser<internal::ArgList> parser(str, args);
262e6eccfeSMichael Jones   int result = 0;
27e1c54d4dSMichael Jones   for (FormatSection cur_section = parser.get_next_section();
28096463d0SMichael Jones        !cur_section.raw_string.empty();
29096463d0SMichael Jones        cur_section = parser.get_next_section()) {
30e1c54d4dSMichael Jones     if (cur_section.has_conv)
312e6eccfeSMichael Jones       result = convert(writer, cur_section);
32e1c54d4dSMichael Jones     else
33096463d0SMichael Jones       result = writer->write(cur_section.raw_string);
342e6eccfeSMichael Jones 
352e6eccfeSMichael Jones     if (result < 0)
362e6eccfeSMichael Jones       return result;
37e1c54d4dSMichael Jones   }
38e1c54d4dSMichael Jones 
39e1c54d4dSMichael Jones   return writer->get_chars_written();
40e1c54d4dSMichael Jones }
41e1c54d4dSMichael Jones 
42e1c54d4dSMichael Jones } // namespace printf_core
43*5ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL
44