Mercurial > audlegacy
annotate src/audacious/formatter.c @ 3981:ab4962c133c7
Handle native endianness.
author | William Pitcock <nenolod@atheme.org> |
---|---|
date | Thu, 22 Nov 2007 05:01:37 -0600 |
parents | 331bfc72ef66 |
children | 10e7c823462d |
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 { | |
3514
331bfc72ef66
Convert some things to use GSlice.
William Pitcock <nenolod@atheme.org>
parents:
3123
diff
changeset
|
43 Formatter *formatter = g_slice_new0(Formatter); |
2426 | 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]); | |
3514
331bfc72ef66
Convert some things to use GSlice.
William Pitcock <nenolod@atheme.org>
parents:
3123
diff
changeset
|
63 |
331bfc72ef66
Convert some things to use GSlice.
William Pitcock <nenolod@atheme.org>
parents:
3123
diff
changeset
|
64 g_slice_free(Formatter, formatter); |
2426 | 65 } |
66 | |
67 /** | |
68 * formatter_associate: | |
69 * @formatter: A #Formatter object to use. | |
70 * @id: The character to use for replacement. | |
71 * @value: The value to replace with. | |
72 * | |
73 * Adds a id->replacement set to the formatter's stack. | |
74 **/ | |
75 void | |
76 formatter_associate(Formatter * formatter, guchar id, char *value) | |
77 { | |
78 formatter_dissociate(formatter, id); | |
79 formatter->values[id] = g_strdup(value); | |
80 } | |
81 | |
82 /** | |
83 * formatter_dissociate: | |
84 * @formatter: A #Formatter object to use. | |
85 * @id: The id to remove the id->replacement mapping for. | |
86 * | |
87 * Removes an id->replacement mapping from the formatter's stack. | |
88 **/ | |
89 void | |
90 formatter_dissociate(Formatter * formatter, guchar id) | |
91 { | |
92 if (formatter->values[id]) | |
93 g_free(formatter->values[id]); | |
94 formatter->values[id] = 0; | |
95 } | |
96 | |
97 /** | |
98 * formatter_format: | |
99 * @formatter: A #Formatter object to use. | |
100 * @format: A string to format. | |
101 * | |
102 * Performs id->replacement substitution on a string. | |
103 * | |
104 * Returns: The formatted string. | |
105 **/ | |
106 gchar * | |
107 formatter_format(Formatter * formatter, char *format) | |
108 { | |
109 char *p, *q, *buffer; | |
110 int len; | |
111 | |
112 for (p = format, len = 0; *p; p++) | |
113 if (*p == '%') { | |
114 if (formatter->values[(int) *++p]) | |
115 len += strlen(formatter->values[(int) *p]); | |
116 else if (!*p) { | |
117 len += 1; | |
118 p--; | |
119 } | |
120 else | |
121 len += 2; | |
122 } | |
123 else | |
124 len++; | |
125 buffer = g_malloc(len + 1); | |
126 for (p = format, q = buffer; *p; p++) | |
127 if (*p == '%') { | |
128 if (formatter->values[(int) *++p]) { | |
129 strcpy(q, formatter->values[(int) *p]); | |
130 q += strlen(q); | |
131 } | |
132 else { | |
133 *q++ = '%'; | |
134 if (*p != '\0') | |
135 *q++ = *p; | |
136 else | |
137 p--; | |
138 } | |
139 } | |
140 else | |
141 *q++ = *p; | |
142 *q = 0; | |
143 return buffer; | |
144 } |