diff util.c @ 511:291e3caa91a0

Move logParseFilename() to nn_log_parse_filename() under util.c and rename logFileOpen() and logFileClose().
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 05 Jun 2012 19:28:34 +0300
parents b6f6c989deab
children ef5a2aa8382b
line wrap: on
line diff
--- a/util.c	Sun Jun 03 06:33:28 2012 +0300
+++ b/util.c	Tue Jun 05 19:28:34 2012 +0300
@@ -718,3 +718,48 @@
     th_free(tuple->str);
     th_free(tuple);
 }
+
+
+#define VPUTCH(CH)  th_vputch(&bufData, &bufSize, &bufLen, CH)
+#define VPUTS(STR)  th_vputs(&bufData, &bufSize, &bufLen, STR)
+
+char *nn_log_parse_filename(const char *fmt, int id)
+{
+    size_t bufSize = strlen(fmt) + TH_BUFGROW, bufLen = 0;
+    char *bufData = th_malloc(bufSize);
+    char tmpBuf[32];
+    const char *s = fmt;
+
+    while (*s)
+    {
+        if (*s == '%')
+        {
+            s++;
+            switch (*s)
+            {
+            case 'i':
+                snprintf(tmpBuf, sizeof(tmpBuf), "%05d", id);
+                VPUTS(tmpBuf);
+                break;
+
+            case 'd':
+                snprintf(tmpBuf, sizeof(tmpBuf), "%d", id);
+                VPUTS(tmpBuf);
+                break;
+
+            case '%':
+                VPUTCH('%');
+                break;
+            }
+            s++;
+        }
+        else
+        {
+            VPUTCH(*s);
+            s++;
+        }
+    }
+
+    VPUTCH(0);
+    return bufData;
+}