Mstdlib-1.24.0
m_fmt.h
1/* The MIT License (MIT)
2 *
3 * Copyright (c) 2015 Monetra Technologies, LLC.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
24#ifndef __M_FMT_H__
25#define __M_FMT_H__
26
27#include <stdio.h>
28#include <stdarg.h>
29
30#include <mstdlib/base/m_defs.h>
31#include <mstdlib/base/m_types.h>
32#include <mstdlib/base/m_fs.h>
33
34/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
35
36__BEGIN_DECLS
37
38M_BEGIN_IGNORE_REDECLARATIONS
39#if M_BLACKLIST_FUNC == 1
40/* normal */
41# ifdef printf
42# undef printf
43# else
44 M_DEPRECATED_FOR(M_printf, int printf(const char *format, ...))
45# endif
46
47# ifdef fprintf
48# undef fprintf
49# else
50 M_DEPRECATED_FOR(M_fprintf, int fprintf(FILE *stream, const char *format, ...))
51# endif
52
53# ifdef sprintf
54# undef sprintf
55# else
56 M_DEPRECATED_FOR(M_sprintf, int sprintf(char *str, const char *format, ...))
57# endif
58
59# ifdef snprintf
60# undef snprintf
61# else
62 M_DEPRECATED_FOR(M_snprintf, int snprintf(char *str, size_t size, const char *format, ...))
63# endif
64
65/* var args */
66# ifdef vprintf
67# undef vprintf
68# else
69 M_DEPRECATED_FOR(M_vprintf, int vprintf(const char *format, va_list ap))
70# endif
71
72# ifdef vfprintf
73# undef vfprintf
74# else
75 M_DEPRECATED_FOR(M_vfprintf, int vfprintf(FILE *stream, const char *format, va_list ap))
76# endif
77
78# ifdef vsprintf
79# undef vsprintf
80# else
81 M_DEPRECATED_FOR(M_vsprintf, int vsprintf(char *str, const char *format, va_list ap))
82# endif
83
84# ifdef vsnprintf
85# undef vsnprintf
86# else
87 M_DEPRECATED_FOR(M_vsnprintf, int vsnprintf(char *str, size_t size, const char *format, va_list ap))
88# endif
89
90/* allocating */
91# ifdef asprintf
92# undef asprintf
93# else
94 M_DEPRECATED_FOR(M_asprintf, int asprintf(char **ret, const char *fmt, ...) M_PRINTF(2,3))
95# endif
96
97# ifdef vasprintf
98# undef vasprintf
99# else
100 M_DEPRECATED_FOR(M_vasprintf, int vasprintf(char **ret, const char *fmt, va_list ap))
101# endif
102#endif
103M_END_IGNORE_REDECLARATIONS
104
105/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
106
107#ifdef _WIN64
108# define M_PRIuPTR "llu"
109#else
110# define M_PRIuPTR "lu"
111#endif
112
113#define M_PRIu64 "llu"
114#define M_PRId64 "lld"
115
116/*! \addtogroup m_fmt Format String Functions
117 * \ingroup m_string
118 *
119 * Formatted String output.
120 *
121 * %<character> is used to denote the data type of the function arguments. Arguments are passed
122 * after the format string. flags and other modifiers are specified between the % and conversion
123 * characters. E.g. %<behavior><character>
124 *
125 * # Supported features
126 *
127 * ## Flags
128 *
129 * Flag | Description
130 * -----|------------
131 * '-' | Left justify output. Default is to right justify. Overrides the '0' flag if both are set.
132 * '+' | Always add the sign (+-) for numeric output. Default is only to add sign for negative. Overrides the ' ' flag if both are set.
133 * '#' | Add the appropriate prefix to the output of numerics. 0x or 0X for Hex. 0 for Octal.
134 * ' ' | Use a space as if it were the sign for positive numbers.
135 * '0' | Pad numerics with 0. Default padding is space (' ').
136 *
137 * ## Width and precision
138 *
139 * A decimal (.) separated value can be specified to control the width and precision of the argument.
140 * <width>.<precision>.
141 *
142 * The width is the minimum output size. Padding will be added if the output would be smaller
143 * than the width. If the output size exceeds the width, the width is ignored and the full input
144 * will be output.
145 *
146 * Precision for strings controls the length that should be output. If the value is larger than the length
147 * of the string, the string length will be used. E.g. ("%.2s", "abc") will result in "ab" for the output.
148 *
149 * Precision for floating point determines the number of decimal places to output. The default is 6.
150 * It's recommended the maximum precision specified be no large than 14 digits. Digits over 14 can have
151 * platform specific rounding differences.
152 *
153 * Width and precision are both optional. You can specify one, the other, or both. E.g. "%.2s", "%8.s".
154 *
155 * A '*' can be used instead of a decmail value and will read the size from an argument. The argument is an
156 * int. The arguments are read right to left. E.g. ("%*.*s", 4, 2, "abc") will result in " ab".
157 *
158 * ## Size modifiers
159 *
160 * Specify the data size of a given argument.
161 *
162 * Modifier | Description
163 * ---------|------------
164 * hh | Size of char. 8 bit.
165 * h | Size of short. 16 bit.
166 * l | Size of long. 8 or 16 bit (system dependant).
167 * ll | Size of long long. 64 bit.
168 * I, z | size of size_t. Based on system size. 32 or 64 bit.
169 * I64 | 64 bit.
170 * I32 | 32 bit.
171 *
172 * ## Conversion
173 *
174 * Specifies the data type of the argument.
175 *
176 * Type | Description
177 * -----------------|------------
178 * d, i | Signed integer.
179 * o, O | Unsigned integer. Output as octal.
180 * u | Unsigned integer.
181 * x, X | Unsigned integer. Output as hex. 'x' outputs lowercase, 'X' outputs uppercase.
182 * p, P | Unsigned pointer. Output as hex. 'p' outputs lowercase, 'P' outputs uppercase.
183 * e, E, f, F, g, G | Double. All will output in the form [-]ddd.ddd. Default 6 decimal digits unless otherwise precision is otherwise specified.
184 * c | Signed character.
185 * s | String (const char *).
186 *
187 * @{
188 */
189
190
191/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
192
193/*! Output format string to FILE.
194 *
195 * \param[in] stream FILE stream.
196 * \param[in] fmt Format string.
197 * \param[in] ap arguments.
198 *
199 * \return Number of characters output. -1 on fatal error.
200 */
201M_API ssize_t M_vfprintf(FILE *stream, const char *fmt, va_list ap);
202
203
204/*! Output format string to FILE (varargs).
205 *
206 * \see M_vfprintf
207 */
208M_API ssize_t M_fprintf(FILE *stream, const char *fmt, ...) M_PRINTF(2,3) M_WARN_NONNULL(1) M_WARN_NONNULL(2);
209
210
211/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
212
213/*! Output format string to mstdlib file descriptor.
214 *
215 * \param[in] fd mstdlib file descriptor.
216 * \param[in] fmt Format string.
217 * \param[in] ap arguments.
218 *
219 * \return Number of characters output. -1 on fatal error.
220 */
221M_API ssize_t M_vmdprintf(M_fs_file_t *fd, const char *fmt, va_list ap);
222
223
224/*! Output format string to mstdlib file descriptor (varargs).
225 *
226 * \see M_vmdprintf
227 */
228M_API ssize_t M_mdprintf(M_fs_file_t *fd, const char *fmt, ...) M_PRINTF(2,3) M_WARN_NONNULL(2);
229
230
231/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
232
233/*! Output format string to OS file descriptor.
234 *
235 * \param[in] fd OS file descriptor.
236 * \param[in] fmt Format string.
237 * \param[in] ap arguments.
238 *
239 * \return Number of characters output. -1 on fatal error.
240 */
241M_API ssize_t M_vdprintf(int fd, const char *fmt, va_list ap);
242
243
244/*! Output format string to OS file descriptor (varargs).
245 *
246 * \see M_vdprintf
247 */
248M_API ssize_t M_dprintf(int fd, const char *fmt, ...) M_PRINTF(2,3) M_WARN_NONNULL(2);
249
250
251/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
252
253/*! Output format string to stdout.
254 *
255 * \param[in] fmt Format string.
256 * \param[in] ap arguments.
257 *
258 * \return Number of characters output. -1 on fatal error.
259 */
260M_API ssize_t M_vprintf(const char *fmt, va_list ap);
261
262
263/*! Output format string to stdout (varargs).
264 *
265 * \see M_vprintf
266 */
267M_API ssize_t M_printf(const char *fmt, ...) M_PRINTF(1,2) M_WARN_NONNULL(1);
268
269
270/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
271
272/*! Output format string to pre-allocated string buffer.
273 *
274 * Output is NULL terminated.
275 *
276 * The output will not exceed size of buffer - 1. 1 byte is reserved for the NULL terminator.
277 *
278 * \param[in] str Storage location for string.
279 * \param[in] size Size of location.
280 * \param[in] fmt Format string.
281 * \param[in] ap arguments.
282 *
283 * \return The length of the fully formatted string. If the size of the buffer is smaller
284 * than the length the string is truncated but the returned length is not. To
285 * determine truncation check is this return against the str buffer.
286 */
287M_API size_t M_vsnprintf(char *str, size_t size, const char *fmt, va_list ap);
288
289
290/*! Output format string to pre-allocated string buffer.
291 *
292 * \see M_vsnprintf
293 */
294M_API size_t M_snprintf(char *buf, size_t size, const char *fmt, ...) M_PRINTF(3,4) M_WARN_NONNULL(3);
295
296
297/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
298
299/*! Output format string to a newly allocated string buffer.
300 *
301 * Output is NULL terminated.
302 *
303 * \param[out] ret Allocated string.
304 * \param[in] fmt Format string.
305 * \param[in] ap arguments.
306 *
307 * \return Number of characters output.
308 */
309M_API size_t M_vasprintf(char **ret, const char *fmt, va_list ap);
310
311
312/*! Output format string to a newly allocated string buffer (varargs).
313 *
314 * \see M_vasprintf
315 */
316M_API size_t M_asprintf(char **ret, const char *fmt, ...) M_PRINTF(2,3) M_WARN_NONNULL(1) M_WARN_NONNULL(2);
317
318
319/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
320
321/*! Output format string to an M_buf buffer.
322 *
323 * Output is NULL terminated.
324 *
325 * \param[in] buf Buffer
326 * \param[in] fmt Format string.
327 * \param[in] ap arguments.
328 *
329 * \return Number of characters output.
330 */
331M_API size_t M_vbprintf(M_buf_t *buf, const char *fmt, va_list ap);
332
333
334/*! Output format string to an M_buf buffer.
335 *
336 * \see M_vbprintf
337 */
338M_API size_t M_bprintf(M_buf_t *buf, const char *fmt, ...) M_PRINTF(2,3) M_WARN_NONNULL(1) M_WARN_NONNULL(2);
339
340
341/*! @} */
342
343__END_DECLS
344
345/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
346
347#endif /* __M_FMT_H__ */
struct M_buf M_buf_t
Definition: m_buf.h:77
ssize_t M_mdprintf(M_fs_file_t *fd, const char *fmt,...)
size_t M_snprintf(char *buf, size_t size, const char *fmt,...)
ssize_t M_fprintf(FILE *stream, const char *fmt,...)
size_t M_vasprintf(char **ret, const char *fmt, va_list ap)
ssize_t M_vprintf(const char *fmt, va_list ap)
size_t M_vsnprintf(char *str, size_t size, const char *fmt, va_list ap)
ssize_t M_dprintf(int fd, const char *fmt,...)
ssize_t M_vdprintf(int fd, const char *fmt, va_list ap)
ssize_t M_vmdprintf(M_fs_file_t *fd, const char *fmt, va_list ap)
ssize_t M_vfprintf(FILE *stream, const char *fmt, va_list ap)
size_t M_asprintf(char **ret, const char *fmt,...)
ssize_t M_printf(const char *fmt,...)
size_t M_vbprintf(M_buf_t *buf, const char *fmt, va_list ap)
size_t M_bprintf(M_buf_t *buf, const char *fmt,...)
struct M_fs_file M_fs_file_t
Definition: m_fs.h:132