src/cx/printf.h

Sun, 14 Jan 2024 13:13:12 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 14 Jan 2024 13:13:12 +0100
changeset 805
26500fc24058
parent 759
475335643af4
child 810
85859399a0cc
permissions
-rw-r--r--

add constant for reading out printf sbo size - relates to #343

universe@599 1 /*
universe@599 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@599 3 *
universe@599 4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
universe@599 5 *
universe@599 6 * Redistribution and use in source and binary forms, with or without
universe@599 7 * modification, are permitted provided that the following conditions are met:
universe@599 8 *
universe@599 9 * 1. Redistributions of source code must retain the above copyright
universe@599 10 * notice, this list of conditions and the following disclaimer.
universe@599 11 *
universe@599 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@599 13 * notice, this list of conditions and the following disclaimer in the
universe@599 14 * documentation and/or other materials provided with the distribution.
universe@599 15 *
universe@599 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@599 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@599 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@599 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@599 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@599 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@599 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@599 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@599 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@599 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@599 26 * POSSIBILITY OF SUCH DAMAGE.
universe@599 27 */
universe@599 28 /**
universe@599 29 * \file printf.h
universe@599 30 * \brief Wrapper for write functions with a printf-like interface.
universe@599 31 * \author Mike Becker
universe@599 32 * \author Olaf Wintermann
universe@599 33 * \copyright 2-Clause BSD License
universe@599 34 */
universe@599 35
universe@599 36 #ifndef UCX_PRINTF_H
universe@599 37 #define UCX_PRINTF_H
universe@599 38
universe@599 39 #include "common.h"
universe@599 40 #include "string.h"
universe@599 41 #include <stdarg.h>
universe@599 42
universe@599 43 #ifdef __cplusplus
universe@599 44 extern "C" {
universe@599 45 #endif
universe@599 46
universe@805 47
universe@805 48 /**
universe@805 49 * The maximum string length that fits into stack memory.
universe@805 50 */
universe@805 51 extern unsigned const cx_printf_sbo_size;
universe@805 52
universe@599 53 /**
universe@599 54 * A \c fprintf like function which writes the output to a stream by
universe@599 55 * using a write_func.
universe@599 56 *
universe@599 57 * @param stream the stream the data is written to
universe@599 58 * @param wfc the write function
universe@599 59 * @param fmt format string
universe@599 60 * @param ... additional arguments
universe@599 61 * @return the total number of bytes written
universe@599 62 */
universe@635 63 __attribute__((__nonnull__(1, 2, 3), __format__(printf, 3, 4)))
universe@635 64 int cx_fprintf(
universe@635 65 void *stream,
universe@635 66 cx_write_func wfc,
universe@635 67 char const *fmt,
universe@635 68 ...
universe@635 69 );
universe@599 70
universe@599 71 /**
universe@599 72 * A \c vfprintf like function which writes the output to a stream by
universe@599 73 * using a write_func.
universe@599 74 *
universe@599 75 * @param stream the stream the data is written to
universe@599 76 * @param wfc the write function
universe@599 77 * @param fmt format string
universe@599 78 * @param ap argument list
universe@599 79 * @return the total number of bytes written
universe@599 80 * @see cx_fprintf()
universe@599 81 */
universe@635 82 __attribute__((__nonnull__))
universe@635 83 int cx_vfprintf(
universe@635 84 void *stream,
universe@635 85 cx_write_func wfc,
universe@635 86 char const *fmt,
universe@635 87 va_list ap
universe@635 88 );
universe@599 89
universe@599 90 /**
universe@599 91 * A \c asprintf like function which allocates space for a string
universe@599 92 * the result is written to.
universe@599 93 *
universe@599 94 * \note The resulting string is guaranteed to be zero-terminated.
universe@599 95 *
universe@599 96 * @param allocator the CxAllocator used for allocating the string
universe@599 97 * @param fmt format string
universe@599 98 * @param ... additional arguments
universe@599 99 * @return the formatted string
universe@599 100 * @see cx_strfree_a()
universe@599 101 */
universe@635 102 __attribute__((__nonnull__(1, 2), __format__(printf, 2, 3)))
universe@635 103 cxmutstr cx_asprintf_a(
universe@693 104 CxAllocator const *allocator,
universe@635 105 char const *fmt,
universe@635 106 ...
universe@635 107 );
universe@599 108
universe@599 109 /**
universe@599 110 * A \c asprintf like function which allocates space for a string
universe@599 111 * the result is written to.
universe@599 112 *
universe@599 113 * \note The resulting string is guaranteed to be zero-terminated.
universe@599 114 *
universe@599 115 * @param fmt format string
universe@599 116 * @param ... additional arguments
universe@599 117 * @return the formatted string
universe@599 118 * @see cx_strfree()
universe@599 119 */
universe@599 120 #define cx_asprintf(fmt, ...) \
universe@599 121 cx_asprintf_a(cxDefaultAllocator, fmt, __VA_ARGS__)
universe@599 122
universe@599 123 /**
universe@599 124 * A \c vasprintf like function which allocates space for a string
universe@599 125 * the result is written to.
universe@599 126 *
universe@599 127 * \note The resulting string is guaranteed to be zero-terminated.
universe@599 128 *
universe@599 129 * @param allocator the CxAllocator used for allocating the string
universe@599 130 * @param fmt format string
universe@599 131 * @param ap argument list
universe@599 132 * @return the formatted string
universe@599 133 * @see cx_asprintf_a()
universe@599 134 */
universe@635 135 __attribute__((__nonnull__))
universe@635 136 cxmutstr cx_vasprintf_a(
universe@693 137 CxAllocator const *allocator,
universe@635 138 char const *fmt,
universe@635 139 va_list ap
universe@635 140 );
universe@599 141
universe@599 142 /**
universe@599 143 * A \c vasprintf like function which allocates space for a string
universe@599 144 * the result is written to.
universe@599 145 *
universe@599 146 * \note The resulting string is guaranteed to be zero-terminated.
universe@599 147 *
universe@599 148 * @param fmt format string
universe@599 149 * @param ap argument list
universe@599 150 * @return the formatted string
universe@599 151 * @see cx_asprintf()
universe@599 152 */
universe@599 153 #define cx_vasprintf(fmt, ap) cx_vasprintf_a(cxDefaultAllocator, fmt, ap)
universe@599 154
universe@599 155 /**
universe@599 156 * A \c printf like function which writes the output to a CxBuffer.
universe@599 157 *
universe@635 158 * @param buffer a pointer to the buffer the data is written to
universe@599 159 * @param fmt the format string
universe@599 160 * @param ... additional arguments
universe@599 161 * @return the total number of bytes written
universe@599 162 * @see ucx_fprintf()
universe@599 163 */
universe@599 164 #define cx_bprintf(buffer, fmt, ...) cx_fprintf((CxBuffer*)buffer, \
universe@599 165 (cx_write_func) cxBufferWrite, fmt, __VA_ARGS__)
universe@599 166
universe@599 167 #ifdef __cplusplus
universe@599 168 } // extern "C"
universe@599 169 #endif
universe@599 170
universe@599 171 #endif //UCX_PRINTF_H

mercurial