annotate src/audacious/formatter.c @ 2914:22c940e0d2dc trunk

Fix crash when invoked with a URL. Reported and initial patch by by Marc Butler <marc -at- adaptivecode.com>.
author William Pitcock <nenolod@atheme.org>
date Fri, 29 Jun 2007 01:50:14 -0500
parents 193bae6b2c8f
children 3b6d316f8b09
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
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
11 * the Free Software Foundation; under version 2 of the License.
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
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
19 * along with this program; if not, write to the Free Software
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
21 */
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
22
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
23 #ifdef HAVE_CONFIG_H
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
24 # include "config.h"
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
25 #endif
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
26
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
27 #include <glib.h>
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
28 #include <string.h>
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
29 #include "formatter.h"
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
30
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
31 /**
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
32 * formatter_new:
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
33 *
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
34 * Factory for #Formatter objects.
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
35 *
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
36 * Return value: A #Formatter object.
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
37 **/
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
38 Formatter *
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
39 formatter_new(void)
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
40 {
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
41 Formatter *formatter = g_new0(Formatter, 1);
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
42
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
43 formatter_associate(formatter, '%', "%");
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
44 return formatter;
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
45 }
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
46
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
47 /**
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
48 * formatter_destroy:
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
49 * @formatter: A #Formatter object to destroy.
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
50 *
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
51 * Destroys #Formatter objects.
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
52 **/
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
53 void
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
54 formatter_destroy(Formatter * formatter)
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
55 {
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
56 int i;
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
57
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
58 for (i = 0; i < 256; i++)
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
59 if (formatter->values[i])
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
60 g_free(formatter->values[i]);
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
61 g_free(formatter);
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
62 }
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
63
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
64 /**
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
65 * formatter_associate:
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
66 * @formatter: A #Formatter object to use.
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
67 * @id: The character to use for replacement.
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
68 * @value: The value to replace with.
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
69 *
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
70 * Adds a id->replacement set to the formatter's stack.
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
71 **/
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
72 void
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
73 formatter_associate(Formatter * formatter, guchar id, char *value)
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
74 {
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
75 formatter_dissociate(formatter, id);
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
76 formatter->values[id] = g_strdup(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
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
79 /**
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
80 * formatter_dissociate:
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
81 * @formatter: A #Formatter object to use.
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
82 * @id: The id to remove the id->replacement mapping for.
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
83 *
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
84 * Removes an id->replacement mapping from the formatter's stack.
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
85 **/
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
86 void
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
87 formatter_dissociate(Formatter * formatter, guchar id)
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
88 {
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
89 if (formatter->values[id])
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
90 g_free(formatter->values[id]);
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
91 formatter->values[id] = 0;
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
92 }
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
93
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
94 /**
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
95 * formatter_format:
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
96 * @formatter: A #Formatter object to use.
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
97 * @format: A string to format.
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
98 *
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
99 * Performs id->replacement substitution on a string.
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
100 *
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
101 * Returns: The formatted string.
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
102 **/
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
103 gchar *
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
104 formatter_format(Formatter * formatter, char *format)
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
105 {
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
106 char *p, *q, *buffer;
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
107 int len;
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
108
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
109 for (p = format, len = 0; *p; p++)
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
110 if (*p == '%') {
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
111 if (formatter->values[(int) *++p])
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
112 len += strlen(formatter->values[(int) *p]);
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
113 else if (!*p) {
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
114 len += 1;
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
115 p--;
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
116 }
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
117 else
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
118 len += 2;
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++;
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
122 buffer = g_malloc(len + 1);
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
123 for (p = format, q = buffer; *p; p++)
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
124 if (*p == '%') {
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
125 if (formatter->values[(int) *++p]) {
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
126 strcpy(q, formatter->values[(int) *p]);
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
127 q += strlen(q);
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
128 }
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
129 else {
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
130 *q++ = '%';
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
131 if (*p != '\0')
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
132 *q++ = *p;
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
133 else
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
134 p--;
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
135 }
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
136 }
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
137 else
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
138 *q++ = *p;
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
139 *q = 0;
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
140 return buffer;
193bae6b2c8f [svn] - removed xmms_ prefix from formatter
mf0102
parents:
diff changeset
141 }