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