From 0535ceeaebef0cd00aa1a1187d5dbaae461edf79 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Sat, 19 Jun 2021 22:46:41 +0200 Subject: [PATCH] Add support of string_t by M_PRINT macro. --- m-core.h | 16 +++++++++++----- m-string.h | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/m-core.h b/m-core.h index 7d02d5b9..2d5d5f85 100644 --- a/m-core.h +++ b/m-core.h @@ -1872,7 +1872,10 @@ M_BEGIN_PROTECTED_CODE const char *: "%s", \ char *: "%s", \ const void *: "%p", \ - void *: "%p") + void *: "%p" \ + M_PRINTF_FORMAT_EXTEND() ) + +#define M_PRINTF_FORMAT_EXTEND() /* Nothing in m-core (see m-string.h) */ /* IF FILE is supported */ #if M_USE_STDIO @@ -1937,11 +1940,14 @@ m_core_fopen(const char filename[], const char opt[]) #endif +/* Can be overloaded by m-string to support string output too */ +#define M_CORE_PRINTF_ARG(x) x + /* Print a C variable if it is a standard type to stdout.*/ -#define M_PRINT_ARG(x) printf(M_PRINTF_FORMAT(x), x) +#define M_PRINT_ARG(x) printf(M_PRINTF_FORMAT(x), M_CORE_PRINTF_ARG(x) ) /* Print a C variable if it is a standard type to the given file 'f'.*/ -#define M_FPRINT_ARG(f, x) fprintf(f, M_PRINTF_FORMAT(x), x) +#define M_FPRINT_ARG(f, x) fprintf(f, M_PRINTF_FORMAT(x), M_CORE_PRINTF_ARG(x)) /* Get a C variable if it is a standard type from the given file 'f'.*/ #define M_FSCAN_ARG(xptr, f) \ @@ -2007,7 +2013,7 @@ M_FSCAN_DEFAULT_TYPE_DEF(m_core_fscan_ldouble, long double, "%Lf") /* Transform a C variable into a string_t (needs m-string.h) */ #define M_GET_STRING_ARG(str, x, append) \ - (append ? string_cat_printf : string_printf) (str, M_PRINTF_FORMAT(x), x) + (append ? string_cat_printf : string_printf) (str, M_PRINTF_FORMAT(x), M_CORE_PRINTF_ARG(x)) /* No use of GET_STR if no inclusion of m-string */ #define M_GET_STR_METHOD_FOR_DEFAULT_TYPE /* */ @@ -3182,7 +3188,7 @@ m_core_parse2_enum (const char str[], const char **endptr) /* Return the content of a property named 'propname' in the PROPERTIES field of oplist or 0, if it is not defined */ -#define M_GET_OPPROPERTY(oplist, propname) \ +#define M_GET_OPPROPERTY(oplist, propname) \ M_GET_METHOD (propname, 0, M_OPFLAT M_GET_PROPERTIES oplist) /* Test if a method is present in an oplist. diff --git a/m-string.h b/m-string.h index 3c977c22..7d2dfd68 100644 --- a/m-string.h +++ b/m-string.h @@ -1906,10 +1906,27 @@ namespace m_lib { M_APPLY(M_STRING_SELECT3, string_search, string_search_str, \ v, M_IF_DEFAULT1(0, __VA_ARGS__)) +/**************** Override m-core default macros to add support of string_t ******/ + /* Internal Macro: Provide GET_STR method to default type */ #undef M_GET_STR_METHOD_FOR_DEFAULT_TYPE #define M_GET_STR_METHOD_FOR_DEFAULT_TYPE GET_STR(M_GET_STRING_ARG) +/* Internal Macro: Provide support of string_t to the macro M_PRINT in C11 */ + +// Extend the format specifier to support string_t +#undef M_PRINTF_FORMAT_EXTEND +#define M_PRINTF_FORMAT_EXTEND() \ + , string_ptr: "%s" \ + , string_srcptr: "%s" + +// Add type conversion for string_t (into a const char*) +#undef M_CORE_PRINTF_ARG +#define M_CORE_PRINTF_ARG(x) _Generic( ((void)0,(x)) \ + , string_ptr: string_get_cstr( M_AS_TYPE(string_ptr, x)) \ + , string_srcptr: string_get_cstr(M_AS_TYPE(string_srcptr,x)) \ + , default: x ) + #endif /* Internal Macro: Provide GET_STR method to enum type */ @@ -1918,6 +1935,7 @@ namespace m_lib { + /***********************************************************************/ /* */ /* BOUNDED STRING, aka char[N+1] */