From 54262ed0902a67f8e5335d73594fbcc12cf892ba Mon Sep 17 00:00:00 2001 From: Pekka Riikonen Date: Tue, 6 Nov 2007 15:31:31 +0000 Subject: [PATCH] Added '%@' formatting to silc_snprintf and friends. --- CHANGES.RUNTIME | 11 +++++++++++ lib/silcutil/silcsnprintf.c | 18 ++++++++++++++++++ lib/silcutil/silcsnprintf.h | 31 +++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) diff --git a/CHANGES.RUNTIME b/CHANGES.RUNTIME index 2a62d8f9..ec67f058 100644 --- a/CHANGES.RUNTIME +++ b/CHANGES.RUNTIME @@ -1,3 +1,14 @@ +Tue Nov 6 17:09:42 EET 2007 Pekka Riikonen + + * Added '%@' formatting to silc_snprintf and variants. It + can be used to render data and structures. Affected files + are lib/silcutil/silcsnprintf.[ch]. + +Sat Oct 27 18:12:40 EEST 2007 Pekka Riikonen + + * Added silc_net_tcp_create_listener2. Affected files are + lib/silcutil/silcnet.h and platform specific implementation. + Sat Sep 1 12:09:32 EEST 2007 Pekka Riikonen * Rewrote parts of the SILC Atomic API to not use volatile diff --git a/lib/silcutil/silcsnprintf.c b/lib/silcutil/silcsnprintf.c index 501625ab..d49b6d1f 100644 --- a/lib/silcutil/silcsnprintf.c +++ b/lib/silcutil/silcsnprintf.c @@ -128,6 +128,7 @@ static size_t dopr(char *buffer, size_t maxlen, const char *format, int cflags; size_t currlen; va_list args; + SilcSnprintfRender render; silc_va_copy(args, args_in); @@ -354,6 +355,23 @@ static size_t dopr(char *buffer, size_t maxlen, const char *format, /* not supported yet, treat as next char */ ch = *format++; break; + case '@': + /* Renderer function */ + render = va_arg (args, SilcSnprintfRender); + if (render) { + void *ptr = va_arg (args, void *); + if (ptr) { + strvalue = render (ptr); + if (strvalue) { + if (max == -1) + max = strlen(strvalue); + if (min > 0 && max >= 0 && min > max) max = min; + fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max); + silc_free(strvalue); + } + } + } + break; default: /* Unknown, skip */ break; diff --git a/lib/silcutil/silcsnprintf.h b/lib/silcutil/silcsnprintf.h index 00ae8c2c..abc1e751 100644 --- a/lib/silcutil/silcsnprintf.h +++ b/lib/silcutil/silcsnprintf.h @@ -29,6 +29,34 @@ #ifndef SILCSNPRINTF_H #define SILCSNPRINTF_H +/****f* silcutil/SilcSnprintf/SilcSnprintfRender + * + * SYNOPSIS + * + * typedef char *(*SilcSnprintfRender)(void *data); + * + * DESCRIPTION + * + * Snprintf rendering function. This function can be used with '%@' + * formatting character. The `data' is rendered into a string and + * allocated string is returned. If NULL is returned the rendering + * is skipped and ignored. If the returned string does not fit to + * the destination buffer it may be truncated. + * + * EXAMPLE + * + * char *id_render(void *data) + * { + * ...render... + * return id_string; + * } + * + * // Call id_render function to render the 'client_id'. + * silc_snprintf(buf, sizeof(buf), "Client ID %@", id_render, client_id); + * + ***/ +typedef char *(*SilcSnprintfRender)(void *data); + /****f* silcutil/SilcSnprintf/silc_snprintf * * SYNOPSIS @@ -42,6 +70,9 @@ * snprintf(3) and printf(3) formatting. Returns the number of character * in `str' or negative value on error. * + * This also supports '%@' formatting to render data and structures + * using SilcSnprintfRender. + * ***/ int silc_snprintf(char *str, size_t count, const char *fmt, ...); -- 2.24.0