1 //===-- String writer 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-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_WRITER_H 10 #define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_WRITER_H 11 12 #include <stddef.h> 13 14 namespace __llvm_libc { 15 namespace printf_core { 16 17 using WriteFunc = void (*)(void *, const char *__restrict, size_t); 18 19 class Writer final { 20 // output is a pointer to the string or file that the writer is meant to write 21 // to. 22 void *output; 23 24 // raw_write is a function that, when called on output with a char* and 25 // length, will copy the number of bytes equal to the length from the char* 26 // onto the end of output. 27 WriteFunc raw_write; 28 29 size_t max_length; 30 size_t chars_written; 31 32 public: 33 Writer(void *output, WriteFunc raw_write, size_t max_length); 34 35 // write will copy length bytes from new_string into output using 36 // raw_write, unless that would cause more bytes than max_length to be 37 // written. It always increments chars_written by length. 38 void write(const char *new_string, size_t length); 39 40 // write_chars will copy length copies of new_char into output using raw_write 41 // unless that would cause more bytes than max_length to be written. It always 42 // increments chars_written by length. 43 void write_chars(char new_char, size_t length); 44 45 size_t get_chars_written(); 46 }; 47 48 } // namespace printf_core 49 } // namespace __llvm_libc 50 51 #endif // LLVM_LIBC_SRC_STDIO_PRINTF_CORE_WRITER_H 52