Mercurial > audlegacy
annotate src/audacious/strings.c @ 2559:b474ecb5bde4 trunk
[svn] revise str_to_utf8():
- new utf8 validator using libguess DFA has been implemented. str_to_utf8() tries utf8 validation first.
- default conversion from ISO-8859-1 is enabled regardless of chardet.
- libguess and librcd is always compiled in.
- some libguess cleanups.
| author | yaz |
|---|---|
| date | Wed, 21 Feb 2007 04:25:12 -0800 |
| parents | 063374a51105 |
| children | e35538325145 |
| 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 */ | |
| 197 if (!str) | |
| 198 return NULL; | |
| 199 | |
| 200 /* Note: Currently, playlist calls this function repeatedly, even | |
| 201 * if the string is already converted into utf-8. | |
| 202 * chardet_to_utf8() would convert a valid utf-8 string into a | |
| 203 * different utf-8 string, if fallback encodings were supplied and | |
| 2559 | 204 * the given string could be treated as a string in one of |
| 205 * fallback encodings. To avoid this, g_utf8_validate() had been | |
| 206 * used at the top of evaluation. | |
| 207 */ | |
| 208 | |
| 209 /* Note 2: g_utf8_validate() has so called encapsulated utf-8 | |
| 210 * problem, thus chardet_to_utf8() took the place of that. | |
| 2313 | 211 */ |
| 2559 | 212 |
| 213 /* Note 3: As introducing madplug, the problem of conversion from | |
| 214 * ISO-8859-1 to UTF-8 arose. This may be coped with g_convert() | |
| 215 * located near the end of chardet_to_utf8(), but it requires utf8 | |
| 216 * validation guard where g_utf8_validate() was. New | |
| 217 * dfa_validate_utf8() employs libguess' DFA engine to validate | |
| 218 * utf-8 and can properly distinguish examples of encapsulated | |
| 219 * utf-8. It is considered to be safe to use as a guard. | |
| 220 */ | |
| 221 | |
| 222 /* already UTF-8? */ | |
| 223 if (dfa_validate_utf8(str, strlen(str))) | |
| 224 return g_strdup(str); | |
| 225 | |
| 2313 | 226 /* chardet encoding detector */ |
| 227 if ((out_str = chardet_to_utf8(str, strlen(str), NULL, NULL, NULL))) | |
| 228 return out_str; | |
| 229 | |
| 230 /* assume encoding associated with locale */ | |
| 231 if ((out_str = g_locale_to_utf8(str, -1, NULL, NULL, NULL))) | |
| 232 return out_str; | |
| 233 | |
| 234 /* all else fails, we mask off character codes >= 128, | |
| 235 replace with '?' */ | |
| 236 return str_to_utf8_fallback(str); | |
| 237 } | |
| 238 | |
| 239 | |
| 240 const gchar * | |
| 241 str_skip_chars(const gchar * str, const gchar * chars) | |
| 242 { | |
| 243 while (strchr(chars, *str)) | |
| 244 str++; | |
| 245 return str; | |
| 246 } | |
| 247 | |
| 248 gchar * | |
| 249 convert_title_text(gchar * title) | |
| 250 { | |
| 251 g_return_val_if_fail(title != NULL, NULL); | |
| 252 | |
| 253 if (cfg.convert_slash) | |
| 254 str_replace_char(title, '\\', '/'); | |
| 255 | |
| 256 if (cfg.convert_underscore) | |
| 257 str_replace_char(title, '_', ' '); | |
| 258 | |
| 259 if (cfg.convert_twenty) | |
| 260 str_twenty_to_space(title); | |
| 261 | |
| 262 return title; | |
| 263 } | |
| 264 | |
| 265 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
|
266 gsize *arg_bytes_read, gsize *arg_bytes_write, |
|
ad1d7687814c
[svn] made strings.h for existing strings.c, cleanups
mf0102
parents:
2332
diff
changeset
|
267 GError **arg_error) |
| 2313 | 268 { |
| 269 #ifdef USE_CHARDET | |
| 270 char *det = NULL, *encoding = NULL; | |
| 271 #endif | |
| 272 gchar *ret = NULL; | |
| 273 gsize *bytes_read, *bytes_write; | |
| 274 GError **error; | |
| 275 gsize my_bytes_read, my_bytes_write; | |
| 276 | |
| 277 bytes_read = arg_bytes_read ? arg_bytes_read : &my_bytes_read; | |
| 278 bytes_write = arg_bytes_write ? arg_bytes_write : &my_bytes_write; | |
| 279 error = arg_error ? arg_error : NULL; | |
| 280 | |
| 281 #ifdef USE_CHARDET | |
| 282 if(cfg.chardet_detector) | |
| 283 det = cfg.chardet_detector; | |
| 284 | |
| 285 if(det){ | |
| 286 if(!strncasecmp("japanese", det, sizeof("japanese"))) { | |
| 287 encoding = (char *)guess_jp(str, strlen(str)); | |
| 288 if (!encoding) | |
| 289 goto fallback; | |
| 290 } else if(!strncasecmp("taiwanese", det, sizeof("taiwanese"))) { | |
| 291 encoding = (char *)guess_tw(str, strlen(str)); | |
| 292 if (!encoding) | |
| 293 goto fallback; | |
| 294 } else if(!strncasecmp("chinese", det, sizeof("chinese"))) { | |
| 295 encoding = (char *)guess_cn(str, strlen(str)); | |
| 296 if (!encoding) | |
| 297 goto fallback; | |
| 298 } else if(!strncasecmp("korean", det, sizeof("korean"))) { | |
| 299 encoding = (char *)guess_kr(str, strlen(str)); | |
| 300 if (!encoding) | |
| 301 goto fallback; | |
| 302 } else if(!strncasecmp("russian", det, sizeof("russian"))) { | |
| 303 rcd_russian_charset res = rcdGetRussianCharset(str, strlen(str)); | |
| 304 switch(res) { | |
| 305 case RUSSIAN_CHARSET_WIN: | |
| 306 encoding = "CP1251"; | |
| 307 break; | |
| 308 case RUSSIAN_CHARSET_ALT: | |
| 309 encoding = "CP866"; | |
| 310 break; | |
| 311 case RUSSIAN_CHARSET_KOI: | |
| 312 encoding = "KOI8-R"; | |
| 313 break; | |
| 314 case RUSSIAN_CHARSET_UTF8: | |
| 315 encoding = "UTF-8"; | |
| 316 break; | |
| 317 } | |
| 318 if (!encoding) | |
| 319 goto fallback; | |
| 320 #ifdef HAVE_UDET | |
| 321 } else if (!strncasecmp("universal", det, sizeof("universal"))) { | |
| 322 encoding = (char *)detectCharset((char *)str, strlen(str)); | |
| 323 if (!encoding) | |
| 324 goto fallback; | |
| 325 #endif | |
| 326 } else /* none, invalid */ | |
| 327 goto fallback; | |
| 328 | |
| 329 ret = g_convert(str, len, "UTF-8", encoding, bytes_read, bytes_write, error); | |
| 330 } | |
| 331 | |
| 332 fallback: | |
| 333 #endif | |
| 334 if(!ret && cfg.chardet_fallback){ | |
| 335 gchar **encs=NULL, **enc=NULL; | |
| 336 encs = g_strsplit_set(cfg.chardet_fallback, " ,:;|/", 0); | |
| 337 | |
| 338 if(encs){ | |
| 339 enc = encs; | |
| 340 for(enc=encs; *enc ; enc++){ | |
| 341 ret = g_convert(str, len, "UTF-8", *enc, bytes_read, bytes_write, error); | |
| 342 if(len == *bytes_read){ | |
| 343 break; | |
| 344 } | |
| 345 } | |
| 346 g_strfreev(encs); | |
| 347 } | |
| 348 } | |
| 349 | |
| 350 if(!ret){ | |
| 351 ret = g_convert(str, len, "UTF-8", "ISO-8859-1", bytes_read, bytes_write, error); | |
| 352 } | |
| 353 | |
| 354 if(ret){ | |
| 355 if(g_utf8_validate(ret, -1, NULL)) | |
| 356 return ret; | |
| 357 else { | |
| 358 g_free(ret); | |
| 359 ret = NULL; | |
| 360 } | |
| 361 } | |
| 362 | |
| 363 return NULL; /* if I have no idea, return NULL. */ | |
| 364 } |
