annotate libaudacious/formatter.c @ 1672:600efc52c645 trunk

[svn] - be careful about referencing a NULL widget (e.g. headless/serveronly mode crashes on streams, bug #562)
author nenolod
date Tue, 12 Sep 2006 23:11:07 -0700
parents cb178e5ad177
children 04961c9a5242
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
1 #ifdef HAVE_CONFIG_H
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
2 # include "config.h"
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
3 #endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
4
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
5 #include <glib.h>
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
6 #include <string.h>
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
7 #include "formatter.h"
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
8
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
9
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
10 Formatter *
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
11 xmms_formatter_new(void)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
12 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
13 Formatter *formatter = g_new0(Formatter, 1);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
14
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
15 xmms_formatter_associate(formatter, '%', "%");
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
16 return formatter;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
17 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
18
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
19 void
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
20 xmms_formatter_destroy(Formatter * formatter)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
21 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
22 int i;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
23
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
24 for (i = 0; i < 256; i++)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
25 if (formatter->values[i])
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
26 g_free(formatter->values[i]);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
27 g_free(formatter);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
28 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
29
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
30 void
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
31 xmms_formatter_associate(Formatter * formatter, guchar id, char *value)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
32 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
33 xmms_formatter_dissociate(formatter, id);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
34 formatter->values[id] = g_strdup(value);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
35 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
36
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
37 void
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
38 xmms_formatter_dissociate(Formatter * formatter, guchar id)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
39 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
40 if (formatter->values[id])
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
41 g_free(formatter->values[id]);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
42 formatter->values[id] = 0;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
43 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
44
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
45 gchar *
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
46 xmms_formatter_format(Formatter * formatter, char *format)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
47 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
48 char *p, *q, *buffer;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
49 int len;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
50
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
51 for (p = format, len = 0; *p; p++)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
52 if (*p == '%') {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
53 if (formatter->values[(int) *++p])
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
54 len += strlen(formatter->values[(int) *p]);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
55 else if (!*p) {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
56 len += 1;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
57 p--;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
58 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
59 else
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
60 len += 2;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
61 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
62 else
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
63 len++;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
64 buffer = g_malloc(len + 1);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
65 for (p = format, q = buffer; *p; p++)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
66 if (*p == '%') {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
67 if (formatter->values[(int) *++p]) {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
68 strcpy(q, formatter->values[(int) *p]);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
69 q += strlen(q);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
70 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
71 else {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
72 *q++ = '%';
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
73 if (*p != '\0')
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
74 *q++ = *p;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
75 else
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
76 p--;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
77 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
78 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
79 else
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
80 *q++ = *p;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
81 *q = 0;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
82 return buffer;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
83 }