Mercurial > audlegacy
annotate src/audacious/formatter.c @ 3190:dcc92b58f06e trunk
Un-shortcircuit some code that I didn't mean to.
author | William Pitcock <nenolod@atheme-project.org> |
---|---|
date | Fri, 27 Jul 2007 19:44:14 -0500 |
parents | f1c756f39e6c |
children | 331bfc72ef66 |
rev | line source |
---|---|
2426 | 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 | |
3121
3b6d316f8b09
GPL3 relicensing.
William Pitcock <nenolod@atheme-project.org>
parents:
2426
diff
changeset
|
11 * the Free Software Foundation; under version 3 of the License. |
2426 | 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 | |
3121
3b6d316f8b09
GPL3 relicensing.
William Pitcock <nenolod@atheme-project.org>
parents:
2426
diff
changeset
|
19 * along with this program. If not, see <http://www.gnu.org/licenses>. |
3123
f1c756f39e6c
Invoke "Plugins are not derived work" clause provided by GPL3.
William Pitcock <nenolod@atheme-project.org>
parents:
3121
diff
changeset
|
20 * |
f1c756f39e6c
Invoke "Plugins are not derived work" clause provided by GPL3.
William Pitcock <nenolod@atheme-project.org>
parents:
3121
diff
changeset
|
21 * The Audacious team does not consider modular code linking to |
f1c756f39e6c
Invoke "Plugins are not derived work" clause provided by GPL3.
William Pitcock <nenolod@atheme-project.org>
parents:
3121
diff
changeset
|
22 * Audacious or using our public API to be a derived work. |
2426 | 23 */ |
24 | |
25 #ifdef HAVE_CONFIG_H | |
26 # include "config.h" | |
27 #endif | |
28 | |
29 #include <glib.h> | |
30 #include <string.h> | |
31 #include "formatter.h" | |
32 | |
33 /** | |
34 * formatter_new: | |
35 * | |
36 * Factory for #Formatter objects. | |
37 * | |
38 * Return value: A #Formatter object. | |
39 **/ | |
40 Formatter * | |
41 formatter_new(void) | |
42 { | |
43 Formatter *formatter = g_new0(Formatter, 1); | |
44 | |
45 formatter_associate(formatter, '%', "%"); | |
46 return formatter; | |
47 } | |
48 | |
49 /** | |
50 * formatter_destroy: | |
51 * @formatter: A #Formatter object to destroy. | |
52 * | |
53 * Destroys #Formatter objects. | |
54 **/ | |
55 void | |
56 formatter_destroy(Formatter * formatter) | |
57 { | |
58 int i; | |
59 | |
60 for (i = 0; i < 256; i++) | |
61 if (formatter->values[i]) | |
62 g_free(formatter->values[i]); | |
63 g_free(formatter); | |
64 } | |
65 | |
66 /** | |
67 * formatter_associate: | |
68 * @formatter: A #Formatter object to use. | |
69 * @id: The character to use for replacement. | |
70 * @value: The value to replace with. | |
71 * | |
72 * Adds a id->replacement set to the formatter's stack. | |
73 **/ | |
74 void | |
75 formatter_associate(Formatter * formatter, guchar id, char *value) | |
76 { | |
77 formatter_dissociate(formatter, id); | |
78 formatter->values[id] = g_strdup(value); | |
79 } | |
80 | |
81 /** | |
82 * formatter_dissociate: | |
83 * @formatter: A #Formatter object to use. | |
84 * @id: The id to remove the id->replacement mapping for. | |
85 * | |
86 * Removes an id->replacement mapping from the formatter's stack. | |
87 **/ | |
88 void | |
89 formatter_dissociate(Formatter * formatter, guchar id) | |
90 { | |
91 if (formatter->values[id]) | |
92 g_free(formatter->values[id]); | |
93 formatter->values[id] = 0; | |
94 } | |
95 | |
96 /** | |
97 * formatter_format: | |
98 * @formatter: A #Formatter object to use. | |
99 * @format: A string to format. | |
100 * | |
101 * Performs id->replacement substitution on a string. | |
102 * | |
103 * Returns: The formatted string. | |
104 **/ | |
105 gchar * | |
106 formatter_format(Formatter * formatter, char *format) | |
107 { | |
108 char *p, *q, *buffer; | |
109 int len; | |
110 | |
111 for (p = format, len = 0; *p; p++) | |
112 if (*p == '%') { | |
113 if (formatter->values[(int) *++p]) | |
114 len += strlen(formatter->values[(int) *p]); | |
115 else if (!*p) { | |
116 len += 1; | |
117 p--; | |
118 } | |
119 else | |
120 len += 2; | |
121 } | |
122 else | |
123 len++; | |
124 buffer = g_malloc(len + 1); | |
125 for (p = format, q = buffer; *p; p++) | |
126 if (*p == '%') { | |
127 if (formatter->values[(int) *++p]) { | |
128 strcpy(q, formatter->values[(int) *p]); | |
129 q += strlen(q); | |
130 } | |
131 else { | |
132 *q++ = '%'; | |
133 if (*p != '\0') | |
134 *q++ = *p; | |
135 else | |
136 p--; | |
137 } | |
138 } | |
139 else | |
140 *q++ = *p; | |
141 *q = 0; | |
142 return buffer; | |
143 } |