X-Git-Url: https://git.datadissipation.net/libcext/blobdiff_plain/e778bd0bcc20f7bc4fe76e3c16f83a5a498dcd25..fd4273e5b7249057309c65c78ec1bd766bed03fe:/asprintf.h diff --git a/asprintf.h b/asprintf.h index dc944df..a22a6fb 100644 --- a/asprintf.h +++ b/asprintf.h @@ -1,22 +1,56 @@ +/* + * This is free and unencumbered software released into the public domain. + * + * Anyone is free to copy, modify, publish, use, compile, sell, or + * distribute this software, either in source code form or as a compiled + * binary, for any purpose, commercial or non-commercial, and by any + * means. + * + * In jurisdictions that recognize copyright laws, the author or authors + * of this software dedicate any and all copyright interest in the + * software to the public domain. We make this dedication for the benefit + * of the public at large and to the detriment of our heirs and + * successors. We intend this dedication to be an overt act of + * relinquishment in perpetuity of all present and future rights to this + * software under copyright law. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * For more information, please refer to + */ + /* https://git.datadissipation.net */ #ifndef ASPRINTF_H_ #define ASPRINTF_H_ 1 - #ifdef ASPRINTF_INCLUDE_LIBC_ + #ifdef ASPRINTF_INCLUDE_LIBC #include #include #include #endif #ifndef HAVE_ASPRINTF - int vasprintf_(char **strp, const char *fmt, va_list ap); - int asprintf_(char **strp, const char *fmt, ...); + #define HAVE_ASPRINTF 1 + int i_vasprintf_(char **restrict strp, const char *restrict fmt, va_list ap); + int i_asprintf_(char **restrict strp, const char *restrict fmt, ...); /* * Credit for a big part of the macro madness: * Jens Gustedt * https://gustedt.wordpress.com/2010/06/08/detect-empty-macro-arguments/ */ - #define vasprintf(s, f, a) vasprintf_(s, f, a) + + /* + * This isn't strict C99, because C99 requires at least one argument in "..." + * for macros. Still, I haven't encountered a compiler that won't preprocess + * this right (out of the popular ones, MSVC wasn't tested) + */ + #define vasprintf(s, f, a) i_vasprintf_(s, f, a) #define I_COND_COMMA_(...) , #define I_PICK_ARG_(_1, _2, ARG, ...) ARG #define I_HAS_COMMA_(...) I_PICK_ARG_(__VA_ARGS__, 1, 0) @@ -47,14 +81,14 @@ #define I_GET_ARG_(...) I_PICK_ARG_(__VA_ARGS__) #define asprintf(s, f, ...)\ - asprintf_(s, f I_PASTE_(I_ISEMPTY_(I_GET_ARG_(, , __VA_ARGS__))) __VA_ARGS__) + i_asprintf_(s, f I_PASTE_(I_ISEMPTY_(I_GET_ARG_(, , __VA_ARGS__))) __VA_ARGS__) #define I_EMPTY_1 #define I_EMPTY_0 I_COND_COMMA_() - #endif + #endif /* !HAVE_ASPRINTF */ - #ifdef ASPRINTF_IMPLEMENTATION_ + #ifdef ASPRINTF_IMPLEMENTATION int - vasprintf_(char **strp, const char *fmt, va_list ap) + i_vasprintf_(char **restrict strp, const char *restrict fmt, va_list ap) { int size, ret; va_list tp; @@ -81,16 +115,16 @@ } int - asprintf_(char **strp, const char *fmt, ...) + i_asprintf_(char **restrict strp, const char *restrict fmt, ...) { int ret; va_list ap; va_start(ap, fmt); - ret = vasprintf(strp, fmt, ap); + ret = i_vasprintf_(strp, fmt, ap); va_end(ap); return ret; } - #endif -#endif + #endif /* ASPRINTF_IMPLEMENTATION */ +#endif /* !ASPRINTF_H_ */