src/cx/printf.h

Sat, 05 Nov 2022 17:17:17 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 05 Nov 2022 17:17:17 +0100
changeset 599
6536a9a75b71
child 635
d4845058239a
permissions
-rw-r--r--

#222 add printf-like functions

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 * \version 3.0
universe@599 34 * \copyright 2-Clause BSD License
universe@599 35 */
universe@599 36
universe@599 37 #ifndef UCX_PRINTF_H
universe@599 38 #define UCX_PRINTF_H
universe@599 39
universe@599 40 #include "common.h"
universe@599 41 #include "string.h"
universe@599 42 #include <stdarg.h>
universe@599 43
universe@599 44 #ifdef __cplusplus
universe@599 45 extern "C" {
universe@599 46 #endif
universe@599 47
universe@599 48 /**
universe@599 49 * A \c fprintf like function which writes the output to a stream by
universe@599 50 * using a write_func.
universe@599 51 *
universe@599 52 * @param stream the stream the data is written to
universe@599 53 * @param wfc the write function
universe@599 54 * @param fmt format string
universe@599 55 * @param ... additional arguments
universe@599 56 * @return the total number of bytes written
universe@599 57 */
universe@599 58 int cx_fprintf(void *stream, cx_write_func wfc, char const *fmt, ...);
universe@599 59
universe@599 60 /**
universe@599 61 * A \c vfprintf like function which writes the output to a stream by
universe@599 62 * using a write_func.
universe@599 63 *
universe@599 64 * @param stream the stream the data is written to
universe@599 65 * @param wfc the write function
universe@599 66 * @param fmt format string
universe@599 67 * @param ap argument list
universe@599 68 * @return the total number of bytes written
universe@599 69 * @see cx_fprintf()
universe@599 70 */
universe@599 71 int cx_vfprintf(void *stream, cx_write_func wfc, char const *fmt, va_list ap);
universe@599 72
universe@599 73 /**
universe@599 74 * A \c asprintf like function which allocates space for a string
universe@599 75 * the result is written to.
universe@599 76 *
universe@599 77 * \note The resulting string is guaranteed to be zero-terminated.
universe@599 78 *
universe@599 79 * @param allocator the CxAllocator used for allocating the string
universe@599 80 * @param fmt format string
universe@599 81 * @param ... additional arguments
universe@599 82 * @return the formatted string
universe@599 83 * @see cx_strfree_a()
universe@599 84 */
universe@599 85 cxmutstr cx_asprintf_a(CxAllocator *allocator, char const *fmt, ...);
universe@599 86
universe@599 87 /**
universe@599 88 * A \c asprintf like function which allocates space for a string
universe@599 89 * the result is written to.
universe@599 90 *
universe@599 91 * \note The resulting string is guaranteed to be zero-terminated.
universe@599 92 *
universe@599 93 * @param fmt format string
universe@599 94 * @param ... additional arguments
universe@599 95 * @return the formatted string
universe@599 96 * @see cx_strfree()
universe@599 97 */
universe@599 98 #define cx_asprintf(fmt, ...) \
universe@599 99 cx_asprintf_a(cxDefaultAllocator, fmt, __VA_ARGS__)
universe@599 100
universe@599 101 /**
universe@599 102 * A \c vasprintf like function which allocates space for a string
universe@599 103 * the result is written to.
universe@599 104 *
universe@599 105 * \note The resulting string is guaranteed to be zero-terminated.
universe@599 106 *
universe@599 107 * @param allocator the CxAllocator used for allocating the string
universe@599 108 * @param fmt format string
universe@599 109 * @param ap argument list
universe@599 110 * @return the formatted string
universe@599 111 * @see cx_asprintf_a()
universe@599 112 */
universe@599 113 cxmutstr cx_vasprintf_a(CxAllocator *allocator, char const *fmt, va_list ap);
universe@599 114
universe@599 115 /**
universe@599 116 * A \c vasprintf like function which allocates space for a string
universe@599 117 * the result is written to.
universe@599 118 *
universe@599 119 * \note The resulting string is guaranteed to be zero-terminated.
universe@599 120 *
universe@599 121 * @param fmt format string
universe@599 122 * @param ap argument list
universe@599 123 * @return the formatted string
universe@599 124 * @see cx_asprintf()
universe@599 125 */
universe@599 126 #define cx_vasprintf(fmt, ap) cx_vasprintf_a(cxDefaultAllocator, fmt, ap)
universe@599 127
universe@599 128 /**
universe@599 129 * A \c printf like function which writes the output to a CxBuffer.
universe@599 130 *
universe@599 131 * @param buffer the buffer the data is written to
universe@599 132 * @param fmt the format string
universe@599 133 * @param ... additional arguments
universe@599 134 * @return the total number of bytes written
universe@599 135 * @see ucx_fprintf()
universe@599 136 */
universe@599 137 #define cx_bprintf(buffer, fmt, ...) cx_fprintf((CxBuffer*)buffer, \
universe@599 138 (cx_write_func) cxBufferWrite, fmt, __VA_ARGS__)
universe@599 139
universe@599 140 #ifdef __cplusplus
universe@599 141 } // extern "C"
universe@599 142 #endif
universe@599 143
universe@599 144 #endif //UCX_PRINTF_H

mercurial