void npformat char str size_t size const char format const char format

 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
void
npformat (char *str, size_t size, const char *format)
{
const char *p = format;
char *val;
size_t cur = 0, len;
while (*p != 0 && cur <= size)
{
if (*p != '%')
{
str[cur++] = *p++;
}
else
{
++p;
if (*p != '\0')
{
switch (*p)
{
case 'A':
val = dummy ("album");
len = strlen (val);
strncpy (str + cur, val,
(cur + len) > size ? size - cur : len);
cur += len;
break;
case 'a':
val = dummy ("artist");
len = strlen (val);
strncpy (str + cur, val,
(cur + len) > size ? size - cur : len);
cur += len;
break;
case 't':
val = dummy ("title");
len = strlen (val);
strncpy (str + cur, val,
(cur + len) > size ? size - cur : len);
cur += len;
break;
case 'n':
val = dummy ("tracknum");
len = strlen (val);
strncpy (str + cur, val,
(cur + len) > size ? size - cur : len);
cur += len;
break;
case '%':
str[cur++] = *p;
break;
default:
printf ("warning: unknown conversion type"
" character `%c' in format\n", *p);
break;
}
++p;
}
}
}
str[cur] = '\0';
}