2045
|
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; either version 2 of the License, or
|
|
12 * (at your option) any later version.
|
|
13 *
|
|
14 * This program is distributed in the hope that it will be useful,
|
|
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
17 * GNU General Public License for more details.
|
|
18 *
|
|
19 * You should have received a copy of the GNU General Public License
|
|
20 * along with this program; if not, write to the Free Software
|
|
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
22 */
|
|
23
|
0
|
24 #ifdef HAVE_CONFIG_H
|
|
25 # include "config.h"
|
|
26 #endif
|
|
27
|
|
28 #include <glib.h>
|
|
29 #include <string.h>
|
|
30 #include "formatter.h"
|
|
31
|
2045
|
32 /**
|
|
33 * xmms_formatter_new:
|
|
34 *
|
|
35 * Factory for #Formatter objects.
|
2060
|
36 *
|
|
37 * Return value: A #Formatter object.
|
2045
|
38 **/
|
0
|
39 Formatter *
|
|
40 xmms_formatter_new(void)
|
|
41 {
|
|
42 Formatter *formatter = g_new0(Formatter, 1);
|
|
43
|
|
44 xmms_formatter_associate(formatter, '%', "%");
|
|
45 return formatter;
|
|
46 }
|
|
47
|
2045
|
48 /**
|
|
49 * xmms_formatter_destroy:
|
|
50 * @formatter: A #Formatter object to destroy.
|
|
51 *
|
|
52 * Destroys #Formatter objects.
|
|
53 **/
|
0
|
54 void
|
|
55 xmms_formatter_destroy(Formatter * formatter)
|
|
56 {
|
|
57 int i;
|
|
58
|
|
59 for (i = 0; i < 256; i++)
|
|
60 if (formatter->values[i])
|
|
61 g_free(formatter->values[i]);
|
|
62 g_free(formatter);
|
|
63 }
|
|
64
|
2045
|
65 /**
|
|
66 * xmms_formatter_associate:
|
|
67 * @formatter: A #Formatter object to use.
|
|
68 * @id: The character to use for replacement.
|
|
69 * @value: The value to replace with.
|
|
70 *
|
|
71 * Adds a id->replacement set to the formatter's stack.
|
|
72 **/
|
0
|
73 void
|
|
74 xmms_formatter_associate(Formatter * formatter, guchar id, char *value)
|
|
75 {
|
|
76 xmms_formatter_dissociate(formatter, id);
|
|
77 formatter->values[id] = g_strdup(value);
|
|
78 }
|
|
79
|
2045
|
80 /**
|
|
81 * xmms_formatter_dissociate:
|
|
82 * @formatter: A #Formatter object to use.
|
|
83 * @id: The id to remove the id->replacement mapping for.
|
|
84 *
|
|
85 * Removes an id->replacement mapping from the formatter's stack.
|
|
86 **/
|
0
|
87 void
|
|
88 xmms_formatter_dissociate(Formatter * formatter, guchar id)
|
|
89 {
|
|
90 if (formatter->values[id])
|
|
91 g_free(formatter->values[id]);
|
|
92 formatter->values[id] = 0;
|
|
93 }
|
|
94
|
2045
|
95 /**
|
|
96 * xmms_formatter_format:
|
|
97 * @formatter: A #Formatter object to use.
|
|
98 * @format: A string to format.
|
|
99 *
|
|
100 * Performs id->replacement substitution on a string.
|
|
101 *
|
|
102 * Returns: The formatted string.
|
|
103 **/
|
0
|
104 gchar *
|
|
105 xmms_formatter_format(Formatter * formatter, char *format)
|
|
106 {
|
|
107 char *p, *q, *buffer;
|
|
108 int len;
|
|
109
|
|
110 for (p = format, len = 0; *p; p++)
|
|
111 if (*p == '%') {
|
|
112 if (formatter->values[(int) *++p])
|
|
113 len += strlen(formatter->values[(int) *p]);
|
|
114 else if (!*p) {
|
|
115 len += 1;
|
|
116 p--;
|
|
117 }
|
|
118 else
|
|
119 len += 2;
|
|
120 }
|
|
121 else
|
|
122 len++;
|
|
123 buffer = g_malloc(len + 1);
|
|
124 for (p = format, q = buffer; *p; p++)
|
|
125 if (*p == '%') {
|
|
126 if (formatter->values[(int) *++p]) {
|
|
127 strcpy(q, formatter->values[(int) *p]);
|
|
128 q += strlen(q);
|
|
129 }
|
|
130 else {
|
|
131 *q++ = '%';
|
|
132 if (*p != '\0')
|
|
133 *q++ = *p;
|
|
134 else
|
|
135 p--;
|
|
136 }
|
|
137 }
|
|
138 else
|
|
139 *q++ = *p;
|
|
140 *q = 0;
|
|
141 return buffer;
|
|
142 }
|