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