# HG changeset patch # User Matti Hamalainen # Date 1352611997 -7200 # Node ID 8a52651bb88d92f5067ba3f32eba541e5989d47f # Parent ffacb78d9b9f9fd5ab979af7257d7ff1050e66c6 Improve log filename parser, should now accept some of the standard printf() style modifiers, such as "%05d", etc. diff -r ffacb78d9b9f -r 8a52651bb88d util.c --- 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), ©[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), ©[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; }