annotate src/song_change/formatter.c @ 3085:ac0af6b39272

Introduce new GIO plugin to buildsystem. stdio is now deprecated. Thoughts: - getc()/ungetc() should be moved to VFS core now
author William Pitcock <nenolod@atheme.org>
date Wed, 29 Apr 2009 20:58:36 -0500
parents da77e1d2ca40
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2480
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /* Audacious
f88dd72c9156 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
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 *
f88dd72c9156 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
f88dd72c9156 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,
f88dd72c9156 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
f88dd72c9156 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
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 *
f88dd72c9156 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
f88dd72c9156 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
f88dd72c9156 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.
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12 *
f88dd72c9156 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,
f88dd72c9156 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
f88dd72c9156 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
f88dd72c9156 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.
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
17 *
f88dd72c9156 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
f88dd72c9156 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>.
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20 *
f88dd72c9156 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
f88dd72c9156 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.
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
23 */
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
25 #include "config.h"
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27 #include <glib.h>
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28 #include <string.h>
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29 #include "formatter.h"
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
30
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31 Formatter *
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
32 formatter_new(void)
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33 {
f88dd72c9156 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);
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
35
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36 formatter_associate(formatter, '%', "%");
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37 return formatter;
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38 }
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
40 void
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
41 formatter_destroy(Formatter * formatter)
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
42 {
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43 int i;
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44
f88dd72c9156 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++)
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46 if (formatter->values[i])
f88dd72c9156 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]);
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48
f88dd72c9156 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);
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50 }
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52 void
f88dd72c9156 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)
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
54 {
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55 formatter_dissociate(formatter, id);
f88dd72c9156 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);
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57 }
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59 void
f88dd72c9156 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)
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
61 {
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62 if (formatter->values[id])
f88dd72c9156 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]);
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64 formatter->values[id] = 0;
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65 }
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67 gchar *
2489
da77e1d2ca40 De-constify.
Matti Hamalainen <ccr@tnsp.org>
parents: 2480
diff changeset
68 formatter_format(Formatter * formatter, gchar *format)
2480
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 {
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 gchar *p, *q, *buffer;
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71 gint len;
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72
f88dd72c9156 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++)
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 if (*p == '%') {
f88dd72c9156 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])
f88dd72c9156 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]);
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77 else if (!*p) {
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78 len += 1;
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79 p--;
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80 }
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 else
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 len += 2;
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83 }
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84 else
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85 len++;
f88dd72c9156 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);
f88dd72c9156 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++)
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88 if (*p == '%') {
f88dd72c9156 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]) {
f88dd72c9156 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);
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91 q += strlen(q);
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92 }
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93 else {
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94 *q++ = '%';
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95 if (*p != '\0')
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96 *q++ = *p;
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97 else
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98 p--;
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
99 }
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100 }
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101 else
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102 *q++ = *p;
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103 *q = 0;
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104 return buffer;
f88dd72c9156 Moved formatter stuff to the only plugin that actually used it.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105 }