annotate src/song_change/formatter.c @ 2487:729134979b7b

Moved formatter stuff to the only plugin that actually used it.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 31 Mar 2008 08:14:09 +0300
parents
children da77e1d2ca40
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2487
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /* Audacious
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 * Copyright (C) 2005-2007 Audacious team
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 *
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
4 * XMMS - Cross-platform multimedia player
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 * Copyright (C) 1998-2003 Peter Alm, Mikael Alm, Olle Hallnas,
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 * Thomas Nilsson and 4Front Technologies
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 * Copyright (C) 1999-2003 Haavard Kvaalen
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 *
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 * This program is free software; you can redistribute it and/or modify
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10 * it under the terms of the GNU General Public License as published by
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11 * the Free Software Foundation; under version 3 of the License.
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12 *
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13 * This program is distributed in the hope that it will be useful,
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16 * GNU General Public License for more details.
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
17 *
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
18 * You should have received a copy of the GNU General Public License
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
19 * along with this program. If not, see <http://www.gnu.org/licenses>.
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20 *
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21 * The Audacious team does not consider modular code linking to
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22 * Audacious or using our public API to be a derived work.
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
23 */
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
25 #include "config.h"
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27 #include <glib.h>
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28 #include <string.h>
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29 #include "formatter.h"
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
30
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31 Formatter *
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
32 formatter_new(void)
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33 {
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
34 Formatter *formatter = g_slice_new0(Formatter);
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
35
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36 formatter_associate(formatter, '%', "%");
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37 return formatter;
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38 }
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
40 void
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
41 formatter_destroy(Formatter * formatter)
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
42 {
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43 int i;
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45 for (i = 0; i < 256; i++)
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46 if (formatter->values[i])
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47 g_free(formatter->values[i]);
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49 g_slice_free(Formatter, formatter);
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50 }
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52 void
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53 formatter_associate(Formatter * formatter, const guchar id, const gchar *value)
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
54 {
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55 formatter_dissociate(formatter, id);
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56 formatter->values[id] = g_strdup(value);
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57 }
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59 void
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60 formatter_dissociate(Formatter * formatter, const guchar id)
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
61 {
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62 if (formatter->values[id])
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63 g_free(formatter->values[id]);
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64 formatter->values[id] = 0;
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65 }
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67 gchar *
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68 formatter_format(Formatter * formatter, const gchar *format)
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 {
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 gchar *p, *q, *buffer;
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71 gint len;
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73 for (p = format, len = 0; *p; p++)
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 if (*p == '%') {
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 if (formatter->values[(int) *++p])
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76 len += strlen(formatter->values[(int) *p]);
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77 else if (!*p) {
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78 len += 1;
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79 p--;
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80 }
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 else
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 len += 2;
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83 }
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84 else
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85 len++;
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86 buffer = g_malloc(len + 1);
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87 for (p = format, q = buffer; *p; p++)
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88 if (*p == '%') {
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89 if (formatter->values[(int) *++p]) {
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90 g_strlcpy(q, formatter->values[(int) *p], len - 1);
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91 q += strlen(q);
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92 }
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93 else {
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94 *q++ = '%';
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95 if (*p != '\0')
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96 *q++ = *p;
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97 else
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98 p--;
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
99 }
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100 }
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101 else
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102 *q++ = *p;
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103 *q = 0;
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104 return buffer;
729134979b7b Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105 }