Mercurial > audlegacy
annotate src/audacious/strings.c @ 2954:cfb04f784a84 trunk
user g_strdup_printf instead of fixed-size gchar array
author | Tomasz Mon <desowin@gmail.com> |
---|---|
date | Sun, 01 Jul 2007 16:37:52 +0200 |
parents | e35538325145 |
children | a2d234851527 |
rev | line source |
---|---|
2313 | 1 /* Audacious |
2 * Copyright (C) 2005-2007 Audacious development team. | |
3 * | |
4 * BMP - Cross-platform multimedia player | |
5 * Copyright (C) 2003-2004 BMP development team. | |
6 * | |
7 * Based on XMMS: | |
8 * Copyright (C) 1998-2003 XMMS development team. | |
9 * | |
10 * This program is free software; you can redistribute it and/or modify | |
11 * it under the terms of the GNU General Public License as published by | |
12 * the Free Software Foundation; under version 2 of the License. | |
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 | |
22 * 02110-1301, USA. | |
23 */ | |
24 | |
2375
063374a51105
[svn] - config.h is necessary to conditional compilation of chardet.
yaz
parents:
2373
diff
changeset
|
25 #ifdef HAVE_CONFIG_H |
063374a51105
[svn] - config.h is necessary to conditional compilation of chardet.
yaz
parents:
2373
diff
changeset
|
26 # include "config.h" |
063374a51105
[svn] - config.h is necessary to conditional compilation of chardet.
yaz
parents:
2373
diff
changeset
|
27 #endif |
063374a51105
[svn] - config.h is necessary to conditional compilation of chardet.
yaz
parents:
2373
diff
changeset
|
28 |
2373
ad1d7687814c
[svn] made strings.h for existing strings.c, cleanups
mf0102
parents:
2332
diff
changeset
|
29 #include "strings.h" |
2313 | 30 |
31 #include <glib/gi18n.h> | |
32 #include <string.h> | |
33 #include <ctype.h> | |
34 | |
35 #include "main.h" | |
36 | |
2559 | 37 #include "../libguess/libguess.h" |
38 #include "../librcd/librcd.h" | |
2313 | 39 #ifdef HAVE_UDET |
40 #include <libudet_c.h> | |
41 #endif | |
42 | |
43 /* | |
44 * escape_shell_chars() | |
45 * | |
46 * Escapes characters that are special to the shell inside double quotes. | |
47 */ | |
48 | |
49 gchar * | |
50 escape_shell_chars(const gchar * string) | |
51 { | |
52 const gchar *special = "$`\"\\"; /* Characters to escape */ | |
53 const gchar *in = string; | |
54 gchar *out, *escaped; | |
55 gint num = 0; | |
56 | |
57 while (*in != '\0') | |
58 if (strchr(special, *in++)) | |
59 num++; | |
60 | |
61 escaped = g_malloc(strlen(string) + num + 1); | |
62 | |
63 in = string; | |
64 out = escaped; | |
65 | |
66 while (*in != '\0') { | |
67 if (strchr(special, *in)) | |
68 *out++ = '\\'; | |
69 *out++ = *in++; | |
70 } | |
71 *out = '\0'; | |
72 | |
73 return escaped; | |
74 } | |
75 | |
76 static gchar * | |
77 str_twenty_to_space(gchar * str) | |
78 { | |
79 gchar *match, *match_end; | |
80 | |
81 g_return_val_if_fail(str != NULL, NULL); | |
82 | |
83 while ((match = strstr(str, "%20"))) { | |
84 match_end = match + 3; | |
85 *match++ = ' '; | |
86 while (*match_end) | |
87 *match++ = *match_end++; | |
88 *match = 0; | |
89 } | |
90 | |
91 return str; | |
92 } | |
93 | |
94 static gchar * | |
95 str_replace_char(gchar * str, gchar old, gchar new) | |
96 { | |
97 gchar *match; | |
98 | |
99 g_return_val_if_fail(str != NULL, NULL); | |
100 | |
101 match = str; | |
102 while ((match = strchr(match, old))) | |
103 *match = new; | |
104 | |
105 return str; | |
106 } | |
107 | |
108 gchar * | |
109 str_append(gchar * str, const gchar * add_str) | |
110 { | |
111 return str_replace(str, g_strconcat(str, add_str, NULL)); | |
112 } | |
113 | |
114 gchar * | |
115 str_replace(gchar * str, gchar * new_str) | |
116 { | |
117 g_free(str); | |
118 return new_str; | |
119 } | |
120 | |
121 void | |
122 str_replace_in(gchar ** str, gchar * new_str) | |
123 { | |
124 *str = str_replace(*str, new_str); | |
125 } | |
126 | |
127 | |
128 gboolean | |
129 str_has_prefix_nocase(const gchar * str, const gchar * prefix) | |
130 { | |
131 return (strncasecmp(str, prefix, strlen(prefix)) == 0); | |
132 } | |
133 | |
134 gboolean | |
135 str_has_suffix_nocase(const gchar * str, const gchar * suffix) | |
136 { | |
137 return (strcasecmp(str + strlen(str) - strlen(suffix), suffix) == 0); | |
138 } | |
139 | |
140 gboolean | |
141 str_has_suffixes_nocase(const gchar * str, gchar * const *suffixes) | |
142 { | |
143 gchar *const *suffix; | |
144 | |
145 g_return_val_if_fail(str != NULL, FALSE); | |
146 g_return_val_if_fail(suffixes != NULL, FALSE); | |
147 | |
148 for (suffix = suffixes; *suffix; suffix++) | |
149 if (str_has_suffix_nocase(str, *suffix)) | |
150 return TRUE; | |
151 | |
152 return FALSE; | |
153 } | |
154 | |
155 gchar * | |
156 str_to_utf8_fallback(const gchar * str) | |
157 { | |
158 gchar *out_str, *convert_str, *chr; | |
159 | |
160 /* NULL in NULL out */ | |
161 if (!str) | |
162 return NULL; | |
163 | |
164 convert_str = g_strdup(str); | |
165 for (chr = convert_str; *chr; chr++) { | |
166 if (*chr & 0x80) | |
167 *chr = '?'; | |
168 } | |
169 | |
170 out_str = g_strconcat(convert_str, _(" (invalid UTF-8)"), NULL); | |
171 g_free(convert_str); | |
172 | |
173 return out_str; | |
174 } | |
175 | |
176 gchar * | |
177 filename_to_utf8(const gchar * filename) | |
178 { | |
179 gchar *out_str; | |
180 | |
181 /* NULL in NULL out */ | |
182 if (!filename) | |
183 return NULL; | |
184 | |
185 if ((out_str = g_filename_to_utf8(filename, -1, NULL, NULL, NULL))) | |
186 return out_str; | |
187 | |
188 return str_to_utf8_fallback(filename); | |
189 } | |
190 | |
191 gchar * | |
192 str_to_utf8(const gchar * str) | |
193 { | |
194 gchar *out_str; | |
195 | |
196 /* NULL in NULL out */ | |
2787
e35538325145
[svn] - add some assertions to chardet_to_utf8() to try to trace bad g_convert() calls
nenolod
parents:
2559
diff
changeset
|
197 g_return_val_if_fail(str != NULL, NULL); |
2313 | 198 |
199 /* Note: Currently, playlist calls this function repeatedly, even | |
200 * if the string is already converted into utf-8. | |
201 * chardet_to_utf8() would convert a valid utf-8 string into a | |
202 * different utf-8 string, if fallback encodings were supplied and | |
2559 | 203 * the given string could be treated as a string in one of |
204 * fallback encodings. To avoid this, g_utf8_validate() had been | |
205 * used at the top of evaluation. | |
206 */ | |
207 | |
208 /* Note 2: g_utf8_validate() has so called encapsulated utf-8 | |
209 * problem, thus chardet_to_utf8() took the place of that. | |
2313 | 210 */ |
2559 | 211 |
212 /* Note 3: As introducing madplug, the problem of conversion from | |
213 * ISO-8859-1 to UTF-8 arose. This may be coped with g_convert() | |
214 * located near the end of chardet_to_utf8(), but it requires utf8 | |
215 * validation guard where g_utf8_validate() was. New | |
216 * dfa_validate_utf8() employs libguess' DFA engine to validate | |
217 * utf-8 and can properly distinguish examples of encapsulated | |
218 * utf-8. It is considered to be safe to use as a guard. | |
219 */ | |
220 | |
221 /* already UTF-8? */ | |
222 if (dfa_validate_utf8(str, strlen(str))) | |
223 return g_strdup(str); | |
224 | |
2313 | 225 /* chardet encoding detector */ |
226 if ((out_str = chardet_to_utf8(str, strlen(str), NULL, NULL, NULL))) | |
227 return out_str; | |
228 | |
229 /* assume encoding associated with locale */ | |
230 if ((out_str = g_locale_to_utf8(str, -1, NULL, NULL, NULL))) | |
231 return out_str; | |
232 | |
233 /* all else fails, we mask off character codes >= 128, | |
234 replace with '?' */ | |
235 return str_to_utf8_fallback(str); | |
236 } | |
237 | |
238 | |
239 const gchar * | |
240 str_skip_chars(const gchar * str, const gchar * chars) | |
241 { | |
242 while (strchr(chars, *str)) | |
243 str++; | |
244 return str; | |
245 } | |
246 | |
247 gchar * | |
248 convert_title_text(gchar * title) | |
249 { | |
250 g_return_val_if_fail(title != NULL, NULL); | |
251 | |
252 if (cfg.convert_slash) | |
253 str_replace_char(title, '\\', '/'); | |
254 | |
255 if (cfg.convert_underscore) | |
256 str_replace_char(title, '_', ' '); | |
257 | |
258 if (cfg.convert_twenty) | |
259 str_twenty_to_space(title); | |
260 | |
261 return title; | |
262 } | |
263 | |
264 gchar *chardet_to_utf8(const gchar *str, gssize len, | |
2373
ad1d7687814c
[svn] made strings.h for existing strings.c, cleanups
mf0102
parents:
2332
diff
changeset
|
265 gsize *arg_bytes_read, gsize *arg_bytes_write, |
ad1d7687814c
[svn] made strings.h for existing strings.c, cleanups
mf0102
parents:
2332
diff
changeset
|
266 GError **arg_error) |
2313 | 267 { |
268 #ifdef USE_CHARDET | |
269 char *det = NULL, *encoding = NULL; | |
270 #endif | |
271 gchar *ret = NULL; | |
272 gsize *bytes_read, *bytes_write; | |
273 GError **error; | |
274 gsize my_bytes_read, my_bytes_write; | |
275 | |
276 bytes_read = arg_bytes_read ? arg_bytes_read : &my_bytes_read; | |
277 bytes_write = arg_bytes_write ? arg_bytes_write : &my_bytes_write; | |
278 error = arg_error ? arg_error : NULL; | |
279 | |
2787
e35538325145
[svn] - add some assertions to chardet_to_utf8() to try to trace bad g_convert() calls
nenolod
parents:
2559
diff
changeset
|
280 g_return_val_if_fail(str != NULL, NULL); |
e35538325145
[svn] - add some assertions to chardet_to_utf8() to try to trace bad g_convert() calls
nenolod
parents:
2559
diff
changeset
|
281 |
2313 | 282 #ifdef USE_CHARDET |
283 if(cfg.chardet_detector) | |
284 det = cfg.chardet_detector; | |
285 | |
286 if(det){ | |
287 if(!strncasecmp("japanese", det, sizeof("japanese"))) { | |
288 encoding = (char *)guess_jp(str, strlen(str)); | |
289 if (!encoding) | |
290 goto fallback; | |
291 } else if(!strncasecmp("taiwanese", det, sizeof("taiwanese"))) { | |
292 encoding = (char *)guess_tw(str, strlen(str)); | |
293 if (!encoding) | |
294 goto fallback; | |
295 } else if(!strncasecmp("chinese", det, sizeof("chinese"))) { | |
296 encoding = (char *)guess_cn(str, strlen(str)); | |
297 if (!encoding) | |
298 goto fallback; | |
299 } else if(!strncasecmp("korean", det, sizeof("korean"))) { | |
300 encoding = (char *)guess_kr(str, strlen(str)); | |
301 if (!encoding) | |
302 goto fallback; | |
303 } else if(!strncasecmp("russian", det, sizeof("russian"))) { | |
304 rcd_russian_charset res = rcdGetRussianCharset(str, strlen(str)); | |
305 switch(res) { | |
306 case RUSSIAN_CHARSET_WIN: | |
307 encoding = "CP1251"; | |
308 break; | |
309 case RUSSIAN_CHARSET_ALT: | |
310 encoding = "CP866"; | |
311 break; | |
312 case RUSSIAN_CHARSET_KOI: | |
313 encoding = "KOI8-R"; | |
314 break; | |
315 case RUSSIAN_CHARSET_UTF8: | |
316 encoding = "UTF-8"; | |
317 break; | |
318 } | |
319 if (!encoding) | |
320 goto fallback; | |
321 #ifdef HAVE_UDET | |
322 } else if (!strncasecmp("universal", det, sizeof("universal"))) { | |
323 encoding = (char *)detectCharset((char *)str, strlen(str)); | |
324 if (!encoding) | |
325 goto fallback; | |
326 #endif | |
327 } else /* none, invalid */ | |
328 goto fallback; | |
329 | |
330 ret = g_convert(str, len, "UTF-8", encoding, bytes_read, bytes_write, error); | |
331 } | |
332 | |
333 fallback: | |
334 #endif | |
335 if(!ret && cfg.chardet_fallback){ | |
336 gchar **encs=NULL, **enc=NULL; | |
337 encs = g_strsplit_set(cfg.chardet_fallback, " ,:;|/", 0); | |
338 | |
339 if(encs){ | |
340 enc = encs; | |
341 for(enc=encs; *enc ; enc++){ | |
342 ret = g_convert(str, len, "UTF-8", *enc, bytes_read, bytes_write, error); | |
343 if(len == *bytes_read){ | |
344 break; | |
345 } | |
346 } | |
347 g_strfreev(encs); | |
348 } | |
349 } | |
350 | |
351 if(!ret){ | |
352 ret = g_convert(str, len, "UTF-8", "ISO-8859-1", bytes_read, bytes_write, error); | |
353 } | |
354 | |
355 if(ret){ | |
356 if(g_utf8_validate(ret, -1, NULL)) | |
357 return ret; | |
358 else { | |
359 g_free(ret); | |
360 ret = NULL; | |
361 } | |
362 } | |
363 | |
364 return NULL; /* if I have no idea, return NULL. */ | |
365 } |