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