Mercurial > hg > nnchat
comparison util.c @ 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 | ef5a2aa8382b |
children | 44f67ec5e945 |
comparison
equal
deleted
inserted
replaced
538:ffacb78d9b9f | 539:8a52651bb88d |
---|---|
720 #define VPUTCH(CH) th_vputch(&bufData, &bufSize, &bufLen, CH) | 720 #define VPUTCH(CH) th_vputch(&bufData, &bufSize, &bufLen, CH) |
721 #define VPUTS(STR) th_vputs(&bufData, &bufSize, &bufLen, STR) | 721 #define VPUTS(STR) th_vputs(&bufData, &bufSize, &bufLen, STR) |
722 | 722 |
723 char *nn_log_parse_filename(const char *fmt, int id) | 723 char *nn_log_parse_filename(const char *fmt, int id) |
724 { | 724 { |
725 size_t bufSize = strlen(fmt) + TH_BUFGROW, bufLen = 0; | 725 size_t bufSize = strlen(fmt) + TH_BUFGROW, bufLen = 0, pos = 0; |
726 char *bufData = th_malloc(bufSize); | 726 char *bufData = th_malloc(bufSize); |
727 char tmpBuf[32]; | 727 char *copy = th_strdup(fmt); |
728 const char *s = fmt; | 728 |
729 | 729 while (fmt[pos]) |
730 while (*s) | 730 { |
731 { | 731 if (fmt[pos] == '%') |
732 if (*s == '%') | 732 { |
733 { | 733 char tmpBuf[64]; |
734 s++; | 734 size_t start = pos++; |
735 switch (*s) | 735 |
736 { | 736 if (fmt[pos] == '-') |
737 case 'i': | 737 pos++; |
738 snprintf(tmpBuf, sizeof(tmpBuf), "%05d", id); | 738 |
739 VPUTS(tmpBuf); | 739 while (isdigit(fmt[pos])) pos++; |
740 break; | 740 |
741 | 741 switch (fmt[pos]) |
742 case 'd': | 742 { |
743 snprintf(tmpBuf, sizeof(tmpBuf), "%d", id); | 743 case 'i': |
744 VPUTS(tmpBuf); | 744 case 'd': |
745 break; | 745 copy[pos + 1] = 0; |
746 | 746 snprintf(tmpBuf, sizeof(tmpBuf), ©[start], id); |
747 case '%': | 747 VPUTS(tmpBuf); |
748 VPUTCH('%'); | 748 break; |
749 break; | 749 |
750 } | 750 case 's': |
751 s++; | 751 copy[pos + 1] = 0; |
752 // snprintf(tmpBuf, sizeof(tmpBuf), ©[start], ); | |
753 VPUTS(tmpBuf); | |
754 break; | |
755 | |
756 case '%': | |
757 VPUTCH('%'); | |
758 break; | |
759 | |
760 default: | |
761 goto error; | |
762 } | |
752 } | 763 } |
753 else | 764 else |
754 { | 765 VPUTCH(fmt[pos]); |
755 VPUTCH(*s); | 766 pos++; |
756 s++; | 767 } |
757 } | 768 |
758 } | 769 error: |
759 | |
760 VPUTCH(0); | 770 VPUTCH(0); |
771 th_free(copy); | |
761 return bufData; | 772 return bufData; |
762 } | 773 } |