add getopt_long
[single-header-libcext.git] / asprintf.h
1 /*
2 * This is free and unencumbered software released into the public domain.
3 *
4 * Anyone is free to copy, modify, publish, use, compile, sell, or
5 * distribute this software, either in source code form or as a compiled
6 * binary, for any purpose, commercial or non-commercial, and by any
7 * means.
8 *
9 * In jurisdictions that recognize copyright laws, the author or authors
10 * of this software dedicate any and all copyright interest in the
11 * software to the public domain. We make this dedication for the benefit
12 * of the public at large and to the detriment of our heirs and
13 * successors. We intend this dedication to be an overt act of
14 * relinquishment in perpetuity of all present and future rights to this
15 * software under copyright law.
16
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 *
25 * For more information, please refer to <https://unlicense.org>
26 */
27
28 /* https://git.datadissipation.net */
29 #ifndef ASPRINTF_H_
30 #define ASPRINTF_H_ 1
31
32 #ifdef ASPRINTF_INCLUDE_LIBC
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #endif
37
38 #ifndef HAVE_ASPRINTF
39 #define HAVE_ASPRINTF 1
40 int i_vasprintf_(char **restrict strp, const char *restrict fmt, va_list ap);
41 int i_asprintf_(char **restrict strp, const char *restrict fmt, ...);
42 /*
43 * Credit for a big part of the macro madness:
44 * Jens Gustedt
45 * https://gustedt.wordpress.com/2010/06/08/detect-empty-macro-arguments/
46 */
47
48 /*
49 * This isn't strict C99, because C99 requires at least one argument in "..."
50 * for macros. Still, I haven't encountered a compiler that won't preprocess
51 * this right (out of the popular ones, MSVC wasn't tested)
52 */
53 #define vasprintf(s, f, a) i_vasprintf_(s, f, a)
54 #define I_COND_COMMA_(...) ,
55 #define I_PICK_ARG_(_1, _2, ARG, ...) ARG
56 #define I_HAS_COMMA_(...) I_PICK_ARG_(__VA_ARGS__, 1, 0)
57
58 #define I_ISEMPTY_(...) \
59 II_ISEMPTY_( \
60 /* test if there is just one argument, eventually an empty \
61 one */ \
62 I_HAS_COMMA_(__VA_ARGS__), \
63 /* test if I_COND_COMMA_ together with the argument \
64 adds a comma */ \
65 I_HAS_COMMA_(I_COND_COMMA_ __VA_ARGS__), \
66 /* test if the argument together with a parenthesis \
67 adds a comma */ \
68 I_HAS_COMMA_(__VA_ARGS__ (/*empty*/)), \
69 /* test if placing it between I_COND_COMMA_ and the \
70 parenthesis adds a comma */ \
71 I_HAS_COMMA_(I_COND_COMMA_ __VA_ARGS__ (/*empty*/)) \
72 )
73
74 #define I_PASTE5_(_0, _1, _2, _3, _4) _0 ## _1 ## _2 ## _3 ## _4
75 #define II_ISEMPTY_(_0, _1, _2, _3)\
76 I_HAS_COMMA_(I_PASTE5_(I_IS_EMPTY_CASE_, _0, _1, _2, _3))
77 #define I_IS_EMPTY_CASE_0001 ,
78
79 #define II_PASTE_(a) I_EMPTY_##a
80 #define I_PASTE_(a) II_PASTE_(a)
81
82 #define I_GET_ARG_(...) I_PICK_ARG_(__VA_ARGS__)
83 #define asprintf(s, f, ...)\
84 i_asprintf_(s, f I_PASTE_(I_ISEMPTY_(I_GET_ARG_(, , __VA_ARGS__))) __VA_ARGS__)
85 #define I_EMPTY_1
86 #define I_EMPTY_0 I_COND_COMMA_()
87 #endif /* !HAVE_ASPRINTF */
88
89 #ifdef ASPRINTF_IMPLEMENTATION
90 int
91 i_vasprintf_(char **restrict strp, const char *restrict fmt, va_list ap)
92 {
93 int size, ret;
94 va_list tp;
95
96 va_copy(tp, ap);
97 size = vsnprintf(NULL, 0, fmt, tp);
98 va_end(tp);
99
100 if (size < 0) {
101 return -1;
102 }
103 *strp = malloc(size + 1);
104 if (*strp == NULL) {
105 return -1;
106 }
107
108 ret = vsnprintf(*strp, size + 1, fmt, ap);
109 if (ret < 0) {
110 free(*strp);
111 return -1;
112 }
113
114 return ret;
115 }
116
117 int
118 i_asprintf_(char **restrict strp, const char *restrict fmt, ...)
119 {
120 int ret;
121 va_list ap;
122
123 va_start(ap, fmt);
124 ret = i_vasprintf_(strp, fmt, ap);
125 va_end(ap);
126
127 return ret;
128 }
129 #endif /* ASPRINTF_IMPLEMENTATION */
130 #endif /* !ASPRINTF_H_ */