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