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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2426
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
1 /* Audacious
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
2 * Copyright (C) 2005-2007 Audacious team
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
3 *
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
4 * XMMS - Cross-platform multimedia player
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
5 * Copyright (C) 1998-2003 Peter Alm, Mikael Alm, Olle Hallnas,
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
6 * Thomas Nilsson and 4Front Technologies
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
7 * Copyright (C) 1999-2003 Haavard Kvaalen
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
8 *
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
9 * This program is free software; you can redistribute it and/or modify
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
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
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
12 *
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
13 * This program is distributed in the hope that it will be useful,
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
16 * GNU General Public License for more details.
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
17 *
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
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
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
23 */
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
24
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
25 #ifdef HAVE_CONFIG_H
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
26 # include "config.h"
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
27 #endif
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
28
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
29 #include <glib.h>
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
30 #include <string.h>
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
31 #include "formatter.h"
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
32
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
33 /**
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
34 * formatter_new:
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
35 *
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
36 * Factory for #Formatter objects.
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
37 *
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
38 * Return value: A #Formatter object.
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
39 **/
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
40 Formatter *
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
41 formatter_new(void)
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
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
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
44
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
45 formatter_associate(formatter, '%', "%");
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
46 return formatter;
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
47 }
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
48
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
49 /**
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
50 * formatter_destroy:
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
51 * @formatter: A #Formatter object to destroy.
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
52 *
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
53 * Destroys #Formatter objects.
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
54 **/
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
55 void
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
56 formatter_destroy(Formatter * formatter)
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
57 {
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
58 int i;
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
59
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
60 for (i = 0; i < 256; i++)
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
61 if (formatter->values[i])
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
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
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
65 }
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
66
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
67 /**
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
68 * formatter_associate:
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
69 * @formatter: A #Formatter object to use.
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
70 * @id: The character to use for replacement.
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
71 * @value: The value to replace with.
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
72 *
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
73 * Adds a id->replacement set to the formatter's stack.
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
74 **/
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
75 void
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
76 formatter_associate(Formatter * formatter, guchar id, char *value)
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
77 {
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
78 formatter_dissociate(formatter, id);
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
79 formatter->values[id] = g_strdup(value);
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
80 }
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
81
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
82 /**
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
83 * formatter_dissociate:
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
84 * @formatter: A #Formatter object to use.
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
85 * @id: The id to remove the id->replacement mapping for.
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
86 *
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
87 * Removes an id->replacement mapping from the formatter's stack.
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
88 **/
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
89 void
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
90 formatter_dissociate(Formatter * formatter, guchar id)
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
91 {
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
92 if (formatter->values[id])
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
93 g_free(formatter->values[id]);
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
94 formatter->values[id] = 0;
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
95 }
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
96
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
97 /**
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
98 * formatter_format:
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
99 * @formatter: A #Formatter object to use.
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
100 * @format: A string to format.
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
101 *
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
102 * Performs id->replacement substitution on a string.
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
103 *
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
104 * Returns: The formatted string.
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
105 **/
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
106 gchar *
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
107 formatter_format(Formatter * formatter, char *format)
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
108 {
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
109 char *p, *q, *buffer;
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
110 int len;
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
111
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
112 for (p = format, len = 0; *p; p++)
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
113 if (*p == '%') {
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
114 if (formatter->values[(int) *++p])
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
115 len += strlen(formatter->values[(int) *p]);
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
116 else if (!*p) {
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
117 len += 1;
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
118 p--;
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
119 }
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
120 else
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
121 len += 2;
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
122 }
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
123 else
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
124 len++;
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
125 buffer = g_malloc(len + 1);
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
126 for (p = format, q = buffer; *p; p++)
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
127 if (*p == '%') {
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
128 if (formatter->values[(int) *++p]) {
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
129 strcpy(q, formatter->values[(int) *p]);
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
130 q += strlen(q);
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
131 }
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
132 else {
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
133 *q++ = '%';
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
134 if (*p != '\0')
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
135 *q++ = *p;
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
136 else
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
137 p--;
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
138 }
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
139 }
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
140 else
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
141 *q++ = *p;
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
142 *q = 0;
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
143 return buffer;
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
144 }