ucx/cx/printf.h

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

mercurial