Added '%@' formatting to silc_snprintf and friends.
authorPekka Riikonen <priikone@silcnet.org>
Tue, 6 Nov 2007 15:31:31 +0000 (15:31 +0000)
committerPekka Riikonen <priikone@silcnet.org>
Tue, 6 Nov 2007 15:31:31 +0000 (15:31 +0000)
CHANGES.RUNTIME
lib/silcutil/silcsnprintf.c
lib/silcutil/silcsnprintf.h

index 2a62d8f996abc3bfda712f766a6b05687f3e7390..ec67f0586edb8a254efcdec34eb320dd03f84f9d 100644 (file)
@@ -1,3 +1,14 @@
+Tue Nov  6 17:09:42 EET 2007  Pekka Riikonen <priikone@silcnet.org>
+
+       * 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 <priikone@silcnet.org>
+
+       * 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 <priikone@silcnet.org>
 
        * Rewrote parts of the SILC Atomic API to not use volatile
index 501625abf21704d2fde80b45c65947fe00f2f165..d49b6d1fa85df441aef677f93d99f3340432b281 100644 (file)
@@ -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;
index 00ae8c2c502c155ee70641beb7dded9ed26257cc..abc1e751ab369e0d0a966c7b5c30e48183eb6131 100644 (file)
 #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, ...);