add getsubopt, improve override macros
[libcext.git] / asprintf.h
index a22a6fbbade4ec97be6a3212f14f0bf605bc8983..6d3341ae7c27f85aaa87bca5a839b294eff21e6e 100644 (file)
   #include <stdarg.h>
   #include <stdio.h>
   #include <stdlib.h>
- #endif
+ #endif /* ASPRINTF_INCLUDE_LIBC */
 
  #ifndef HAVE_ASPRINTF
   #define HAVE_ASPRINTF 1
+  #define asprintf  i_asprintf_
+  #define vasprintf i_vasprintf_
   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/
-   */
-
-  /*
-   * 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)
-  #define I_ISEMPTY_(...)                                                      \
-  II_ISEMPTY_(                                                                 \
-         /* test if there is just one argument, eventually an empty           \
-         one */                                                               \
-         I_HAS_COMMA_(__VA_ARGS__),                                           \
-         /* test if I_COND_COMMA_ together with the argument                  \
-         adds a comma */                                                      \
-         I_HAS_COMMA_(I_COND_COMMA_ __VA_ARGS__),                             \
-         /* test if the argument together with a parenthesis                  \
-         adds a comma */                                                      \
-         I_HAS_COMMA_(__VA_ARGS__ (/*empty*/)),                               \
-         /* test if placing it between I_COND_COMMA_ and the                  \
-         parenthesis adds a comma */                                          \
-         I_HAS_COMMA_(I_COND_COMMA_ __VA_ARGS__ (/*empty*/))                  \
-         )
-  #define I_PASTE5_(_0, _1, _2, _3, _4) _0 ## _1 ## _2 ## _3 ## _4
-  #define II_ISEMPTY_(_0, _1, _2, _3)\
-  I_HAS_COMMA_(I_PASTE5_(I_IS_EMPTY_CASE_, _0, _1, _2, _3))
-  #define I_IS_EMPTY_CASE_0001 ,
-
-  #define II_PASTE_(a) I_EMPTY_##a
-  #define I_PASTE_(a) II_PASTE_(a)
-
-  #define I_GET_ARG_(...) I_PICK_ARG_(__VA_ARGS__)
-  #define asprintf(s, f, ...)\
-  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 /* !HAVE_ASPRINTF */
 
  #ifdef ASPRINTF_IMPLEMENTATION