include stdio include stdlib include string char dummy const char tag

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *
dummy (const char *tag)
{
if (strcmp (tag, "artist") == 0)
{
return "<ARTIST>";
}
else if (strcmp (tag, "title") == 0)
{
return "<TITLE>";
}
else if (strcmp (tag, "album") == 0)
{
return "<ALBUM>";
}
else if (strcmp (tag, "tracknum") == 0)
{
return "<TRACKNUM>";
}
return NULL;
}
void
npformat (char *str, size_t size, const char *format)
{
const char *p = format;
size_t cur = 0, len, ssize = size - 1;
char *val;
while (*p != '\0' && cur <= ssize)
{
if (*p != '%')
{
str[cur++] = *p++;
}
else
{
++p;
if (*p != '\0')
{
if (*p != '%')
{
switch (*p++)
{
case 'A':
val = dummy ("album");
break;
case 'a':
val = dummy ("artist");
break;
case 't':
val = dummy ("title");
break;
case 'n':
val = dummy ("tracknum");
break;
default:
fprintf (stderr, "warning: unknow conversion type"
" character `%c' in format\n", *(p - 1));
continue;
}
len = strlen (val);
if ((len + cur) > ssize)
{
len = ssize - cur;
}
strncpy (str + cur, val, len);
cur += len;
}
else
{
str[cur++] = *p;
}
++p;
}
}
}
str[cur] = '\0';
}
int
main (int argc, char **argv)
{
char formated[5];
npformat (formated, 5, "%%G %a - %t (%A) %%Q");
printf ("%s\n", formated);
return 0;
}