|
| 1 | +//===-- Implementation of vprintf -------------------------------*- 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/vprintf.h" |
| 10 | +#include "src/__support/OSUtil/io.h" |
| 11 | +#include "src/__support/arg_list.h" |
| 12 | +#include "src/stdio/printf_core/core_structs.h" |
| 13 | +#include "src/stdio/printf_core/printf_main.h" |
| 14 | +#include "src/stdio/printf_core/writer.h" |
| 15 | + |
| 16 | +#include <stdarg.h> |
| 17 | + |
| 18 | +namespace LIBC_NAMESPACE { |
| 19 | + |
| 20 | +namespace { |
| 21 | + |
| 22 | +LIBC_INLINE int raw_write_hook(cpp::string_view new_str, void *) { |
| 23 | + write_to_stderr(new_str); |
| 24 | + return printf_core::WRITE_OK; |
| 25 | +} |
| 26 | + |
| 27 | +} // namespace |
| 28 | + |
| 29 | +LLVM_LIBC_FUNCTION(int, vprintf, |
| 30 | + (const char *__restrict format, va_list vlist)) { |
| 31 | + internal::ArgList args(vlist); // This holder class allows for easier copying |
| 32 | + // and pointer semantics, as well as handling |
| 33 | + // destruction automatically. |
| 34 | + constexpr size_t BUFF_SIZE = 1024; |
| 35 | + char buffer[BUFF_SIZE]; |
| 36 | + |
| 37 | + printf_core::WriteBuffer wb(buffer, BUFF_SIZE, &raw_write_hook, nullptr); |
| 38 | + printf_core::Writer writer(&wb); |
| 39 | + |
| 40 | + int retval = printf_core::printf_main(&writer, format, args); |
| 41 | + |
| 42 | + int flushval = wb.overflow_write(""); |
| 43 | + if (flushval != printf_core::WRITE_OK) |
| 44 | + retval = flushval; |
| 45 | + |
| 46 | + return retval; |
| 47 | +} |
| 48 | + |
| 49 | +} // namespace LIBC_NAMESPACE |
0 commit comments