49 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_PRINTF_MAIN_H10#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_PRINTF_MAIN_H11 12#include "src/__support/arg_list.h"13#include "src/__support/error_or.h"14#include "src/__support/macros/config.h"15#include "src/stdio/printf_core/converter.h"16#include "src/stdio/printf_core/core_structs.h"17#include "src/stdio/printf_core/parser.h"18#include "src/stdio/printf_core/writer.h"19 20#include <stddef.h>21 22namespace LIBC_NAMESPACE_DECL {23namespace printf_core {24 25template <WriteMode write_mode>26ErrorOr<size_t> printf_main(Writer<write_mode> *writer,27 const char *__restrict str,28 internal::ArgList &args) {29 Parser<internal::ArgList> parser(str, args);30 int result = 0;31 for (FormatSection cur_section = parser.get_next_section();32 !cur_section.raw_string.empty();33 cur_section = parser.get_next_section()) {34 if (cur_section.has_conv)35 result = convert(writer, cur_section);36 else37 result = writer->write(cur_section.raw_string);38 if (result < 0)39 return Error(-result);40 }41 42 return writer->get_chars_written();43}44 45} // namespace printf_core46} // namespace LIBC_NAMESPACE_DECL47 48#endif // LLVM_LIBC_SRC_STDIO_PRINTF_CORE_PRINTF_MAIN_H49