changeset 539:8a52651bb88d

Improve log filename parser, should now accept some of the standard printf() style modifiers, such as "%05d", etc.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 11 Nov 2012 07:33:17 +0200
parents ffacb78d9b9f
children 658c188101a6
files util.c
diffstat 1 files changed, 34 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/util.c	Thu Nov 08 19:06:32 2012 +0200
+++ b/util.c	Sun Nov 11 07:33:17 2012 +0200
@@ -722,41 +722,52 @@
 
 char *nn_log_parse_filename(const char *fmt, int id)
 {
-    size_t bufSize = strlen(fmt) + TH_BUFGROW, bufLen = 0;
+    size_t bufSize = strlen(fmt) + TH_BUFGROW, bufLen = 0, pos = 0;
     char *bufData = th_malloc(bufSize);
-    char tmpBuf[32];
-    const char *s = fmt;
+    char *copy = th_strdup(fmt);
 
-    while (*s)
+    while (fmt[pos])
     {
-        if (*s == '%')
+        if (fmt[pos] == '%')
         {
-            s++;
-            switch (*s)
+            char tmpBuf[64];
+            size_t start = pos++;
+
+            if (fmt[pos] == '-')
+                pos++;
+
+            while (isdigit(fmt[pos])) pos++;
+
+            switch (fmt[pos])
             {
-            case 'i':
-                snprintf(tmpBuf, sizeof(tmpBuf), "%05d", id);
-                VPUTS(tmpBuf);
-                break;
+                case 'i':
+                case 'd':
+                    copy[pos + 1] = 0;
+                    snprintf(tmpBuf, sizeof(tmpBuf), &copy[start], id);
+                    VPUTS(tmpBuf);
+                    break;
 
-            case 'd':
-                snprintf(tmpBuf, sizeof(tmpBuf), "%d", id);
-                VPUTS(tmpBuf);
-                break;
+                case 's':
+                    copy[pos + 1] = 0;
+//                    snprintf(tmpBuf, sizeof(tmpBuf), &copy[start], );
+                    VPUTS(tmpBuf);
+                    break;
 
-            case '%':
-                VPUTCH('%');
-                break;
+                case '%':
+                    VPUTCH('%');
+                    break;
+
+                default:
+                    goto error;
             }
-            s++;
         }
         else
-        {
-            VPUTCH(*s);
-            s++;
-        }
+            VPUTCH(fmt[pos]);
+        pos++;
     }
 
+error:
     VPUTCH(0);
+    th_free(copy);
     return bufData;
 }