|
| 1 | +//===-- Implementation of fprintf for baremetal -----------------*- 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/fprintf.h" |
| 10 | + |
| 11 | +#include "hdr/types/FILE.h" |
| 12 | +#include "src/__support/arg_list.h" |
| 13 | +#include "src/__support/macros/config.h" |
| 14 | +#include "src/stdio/baremetal/write_utils.h" |
| 15 | +#include "src/stdio/printf_core/printf_main.h" |
| 16 | + |
| 17 | +#include <stdarg.h> |
| 18 | + |
| 19 | +namespace LIBC_NAMESPACE_DECL { |
| 20 | + |
| 21 | +LLVM_LIBC_FUNCTION(int, fprintf, |
| 22 | + (::FILE *__restrict stream, const char *__restrict format, |
| 23 | + ...)) { |
| 24 | + va_list vlist; |
| 25 | + va_start(vlist, format); |
| 26 | + internal::ArgList args(vlist); // This holder class allows for easier copying |
| 27 | + // and pointer semantics, as well as handling |
| 28 | + // destruction automatically. |
| 29 | + va_end(vlist); |
| 30 | + static constexpr size_t BUFF_SIZE = 1024; |
| 31 | + char buffer[BUFF_SIZE]; |
| 32 | + |
| 33 | + printf_core::WriteBuffer<printf_core::WriteMode::FLUSH_TO_STREAM> wb( |
| 34 | + buffer, BUFF_SIZE, get_write_hook(stream), nullptr); |
| 35 | + printf_core::Writer<printf_core::WriteMode::FLUSH_TO_STREAM> writer(wb); |
| 36 | + |
| 37 | + int retval = printf_core::printf_main(&writer, format, args); |
| 38 | + |
| 39 | + int flushval = wb.overflow_write(""); |
| 40 | + if (flushval != printf_core::WRITE_OK) |
| 41 | + retval = flushval; |
| 42 | + |
| 43 | + return retval; |
| 44 | +} |
| 45 | + |
| 46 | +} // namespace LIBC_NAMESPACE_DECL |
0 commit comments