src/cx/printf.h

Mon, 18 Dec 2023 14:14:47 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 18 Dec 2023 14:14:47 +0100
changeset 759
475335643af4
parent 693
494d9b20b99e
child 805
26500fc24058
permissions
-rw-r--r--

increase version number to 3.1

remove per-file version information
from Doxygen output

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@599 47 /**
universe@599 48 * A \c fprintf like function which writes the output to a stream by
universe@599 49 * using a write_func.
universe@599 50 *
universe@599 51 * @param stream the stream the data is written to
universe@599 52 * @param wfc the write function
universe@599 53 * @param fmt format string
universe@599 54 * @param ... additional arguments
universe@599 55 * @return the total number of bytes written
universe@599 56 */
universe@635 57 __attribute__((__nonnull__(1, 2, 3), __format__(printf, 3, 4)))
universe@635 58 int cx_fprintf(
universe@635 59 void *stream,
universe@635 60 cx_write_func wfc,
universe@635 61 char const *fmt,
universe@635 62 ...
universe@635 63 );
universe@599 64
universe@599 65 /**
universe@599 66 * A \c vfprintf like function which writes the output to a stream by
universe@599 67 * using a write_func.
universe@599 68 *
universe@599 69 * @param stream the stream the data is written to
universe@599 70 * @param wfc the write function
universe@599 71 * @param fmt format string
universe@599 72 * @param ap argument list
universe@599 73 * @return the total number of bytes written
universe@599 74 * @see cx_fprintf()
universe@599 75 */
universe@635 76 __attribute__((__nonnull__))
universe@635 77 int cx_vfprintf(
universe@635 78 void *stream,
universe@635 79 cx_write_func wfc,
universe@635 80 char const *fmt,
universe@635 81 va_list ap
universe@635 82 );
universe@599 83
universe@599 84 /**
universe@599 85 * A \c asprintf like function which allocates space for a string
universe@599 86 * the result is written to.
universe@599 87 *
universe@599 88 * \note The resulting string is guaranteed to be zero-terminated.
universe@599 89 *
universe@599 90 * @param allocator the CxAllocator used for allocating the string
universe@599 91 * @param fmt format string
universe@599 92 * @param ... additional arguments
universe@599 93 * @return the formatted string
universe@599 94 * @see cx_strfree_a()
universe@599 95 */
universe@635 96 __attribute__((__nonnull__(1, 2), __format__(printf, 2, 3)))
universe@635 97 cxmutstr cx_asprintf_a(
universe@693 98 CxAllocator const *allocator,
universe@635 99 char const *fmt,
universe@635 100 ...
universe@635 101 );
universe@599 102
universe@599 103 /**
universe@599 104 * A \c asprintf like function which allocates space for a string
universe@599 105 * the result is written to.
universe@599 106 *
universe@599 107 * \note The resulting string is guaranteed to be zero-terminated.
universe@599 108 *
universe@599 109 * @param fmt format string
universe@599 110 * @param ... additional arguments
universe@599 111 * @return the formatted string
universe@599 112 * @see cx_strfree()
universe@599 113 */
universe@599 114 #define cx_asprintf(fmt, ...) \
universe@599 115 cx_asprintf_a(cxDefaultAllocator, fmt, __VA_ARGS__)
universe@599 116
universe@599 117 /**
universe@599 118 * A \c vasprintf like function which allocates space for a string
universe@599 119 * the result is written to.
universe@599 120 *
universe@599 121 * \note The resulting string is guaranteed to be zero-terminated.
universe@599 122 *
universe@599 123 * @param allocator the CxAllocator used for allocating the string
universe@599 124 * @param fmt format string
universe@599 125 * @param ap argument list
universe@599 126 * @return the formatted string
universe@599 127 * @see cx_asprintf_a()
universe@599 128 */
universe@635 129 __attribute__((__nonnull__))
universe@635 130 cxmutstr cx_vasprintf_a(
universe@693 131 CxAllocator const *allocator,
universe@635 132 char const *fmt,
universe@635 133 va_list ap
universe@635 134 );
universe@599 135
universe@599 136 /**
universe@599 137 * A \c vasprintf like function which allocates space for a string
universe@599 138 * the result is written to.
universe@599 139 *
universe@599 140 * \note The resulting string is guaranteed to be zero-terminated.
universe@599 141 *
universe@599 142 * @param fmt format string
universe@599 143 * @param ap argument list
universe@599 144 * @return the formatted string
universe@599 145 * @see cx_asprintf()
universe@599 146 */
universe@599 147 #define cx_vasprintf(fmt, ap) cx_vasprintf_a(cxDefaultAllocator, fmt, ap)
universe@599 148
universe@599 149 /**
universe@599 150 * A \c printf like function which writes the output to a CxBuffer.
universe@599 151 *
universe@635 152 * @param buffer a pointer to the buffer the data is written to
universe@599 153 * @param fmt the format string
universe@599 154 * @param ... additional arguments
universe@599 155 * @return the total number of bytes written
universe@599 156 * @see ucx_fprintf()
universe@599 157 */
universe@599 158 #define cx_bprintf(buffer, fmt, ...) cx_fprintf((CxBuffer*)buffer, \
universe@599 159 (cx_write_func) cxBufferWrite, fmt, __VA_ARGS__)
universe@599 160
universe@599 161 #ifdef __cplusplus
universe@599 162 } // extern "C"
universe@599 163 #endif
universe@599 164
universe@599 165 #endif //UCX_PRINTF_H

mercurial