comparison libaudacious/formatter.c @ 0:cb178e5ad177 trunk

[svn] Import audacious source.
author nenolod
date Mon, 24 Oct 2005 03:06:47 -0700
parents
children 04961c9a5242
comparison
equal deleted inserted replaced
-1:000000000000 0:cb178e5ad177
1 #ifdef HAVE_CONFIG_H
2 # include "config.h"
3 #endif
4
5 #include <glib.h>
6 #include <string.h>
7 #include "formatter.h"
8
9
10 Formatter *
11 xmms_formatter_new(void)
12 {
13 Formatter *formatter = g_new0(Formatter, 1);
14
15 xmms_formatter_associate(formatter, '%', "%");
16 return formatter;
17 }
18
19 void
20 xmms_formatter_destroy(Formatter * formatter)
21 {
22 int i;
23
24 for (i = 0; i < 256; i++)
25 if (formatter->values[i])
26 g_free(formatter->values[i]);
27 g_free(formatter);
28 }
29
30 void
31 xmms_formatter_associate(Formatter * formatter, guchar id, char *value)
32 {
33 xmms_formatter_dissociate(formatter, id);
34 formatter->values[id] = g_strdup(value);
35 }
36
37 void
38 xmms_formatter_dissociate(Formatter * formatter, guchar id)
39 {
40 if (formatter->values[id])
41 g_free(formatter->values[id]);
42 formatter->values[id] = 0;
43 }
44
45 gchar *
46 xmms_formatter_format(Formatter * formatter, char *format)
47 {
48 char *p, *q, *buffer;
49 int len;
50
51 for (p = format, len = 0; *p; p++)
52 if (*p == '%') {
53 if (formatter->values[(int) *++p])
54 len += strlen(formatter->values[(int) *p]);
55 else if (!*p) {
56 len += 1;
57 p--;
58 }
59 else
60 len += 2;
61 }
62 else
63 len++;
64 buffer = g_malloc(len + 1);
65 for (p = format, q = buffer; *p; p++)
66 if (*p == '%') {
67 if (formatter->values[(int) *++p]) {
68 strcpy(q, formatter->values[(int) *p]);
69 q += strlen(q);
70 }
71 else {
72 *q++ = '%';
73 if (*p != '\0')
74 *q++ = *p;
75 else
76 p--;
77 }
78 }
79 else
80 *q++ = *p;
81 *q = 0;
82 return buffer;
83 }