Mercurial > audlegacy
annotate src/audacious/strings.c @ 2404:60f1bc20c19c trunk
[svn] - hooking implementation.
example: hook_register("playback begin"); hook_call("playback begin", <PlaylistEntry>);
| author | nenolod |
|---|---|
| date | Thu, 25 Jan 2007 20:23:16 -0800 |
| parents | 063374a51105 |
| children | b474ecb5bde4 |
| 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 | |
| 37 #ifdef USE_CHARDET | |
| 38 #include "../libguess/libguess.h" | |
| 39 #include "../librcd/librcd.h" | |
| 40 #ifdef HAVE_UDET | |
| 41 #include <libudet_c.h> | |
| 42 #endif | |
| 43 #endif | |
| 44 | |
| 45 /* | |
| 46 * escape_shell_chars() | |
| 47 * | |
| 48 * Escapes characters that are special to the shell inside double quotes. | |
| 49 */ | |
| 50 | |
| 51 gchar * | |
| 52 escape_shell_chars(const gchar * string) | |
| 53 { | |
| 54 const gchar *special = "$`\"\\"; /* Characters to escape */ | |
| 55 const gchar *in = string; | |
| 56 gchar *out, *escaped; | |
| 57 gint num = 0; | |
| 58 | |
| 59 while (*in != '\0') | |
| 60 if (strchr(special, *in++)) | |
| 61 num++; | |
| 62 | |
| 63 escaped = g_malloc(strlen(string) + num + 1); | |
| 64 | |
| 65 in = string; | |
| 66 out = escaped; | |
| 67 | |
| 68 while (*in != '\0') { | |
| 69 if (strchr(special, *in)) | |
| 70 *out++ = '\\'; | |
| 71 *out++ = *in++; | |
| 72 } | |
| 73 *out = '\0'; | |
| 74 | |
| 75 return escaped; | |
| 76 } | |
| 77 | |
| 78 static gchar * | |
| 79 str_twenty_to_space(gchar * str) | |
| 80 { | |
| 81 gchar *match, *match_end; | |
| 82 | |
| 83 g_return_val_if_fail(str != NULL, NULL); | |
| 84 | |
| 85 while ((match = strstr(str, "%20"))) { | |
| 86 match_end = match + 3; | |
| 87 *match++ = ' '; | |
| 88 while (*match_end) | |
| 89 *match++ = *match_end++; | |
| 90 *match = 0; | |
| 91 } | |
| 92 | |
| 93 return str; | |
| 94 } | |
| 95 | |
| 96 static gchar * | |
| 97 str_replace_char(gchar * str, gchar old, gchar new) | |
| 98 { | |
| 99 gchar *match; | |
| 100 | |
| 101 g_return_val_if_fail(str != NULL, NULL); | |
| 102 | |
| 103 match = str; | |
| 104 while ((match = strchr(match, old))) | |
| 105 *match = new; | |
| 106 | |
| 107 return str; | |
| 108 } | |
| 109 | |
| 110 gchar * | |
| 111 str_append(gchar * str, const gchar * add_str) | |
| 112 { | |
| 113 return str_replace(str, g_strconcat(str, add_str, NULL)); | |
| 114 } | |
| 115 | |
| 116 gchar * | |
| 117 str_replace(gchar * str, gchar * new_str) | |
| 118 { | |
| 119 g_free(str); | |
| 120 return new_str; | |
| 121 } | |
| 122 | |
| 123 void | |
| 124 str_replace_in(gchar ** str, gchar * new_str) | |
| 125 { | |
| 126 *str = str_replace(*str, new_str); | |
| 127 } | |
| 128 | |
| 129 | |
| 130 gboolean | |
| 131 str_has_prefix_nocase(const gchar * str, const gchar * prefix) | |
| 132 { | |
| 133 return (strncasecmp(str, prefix, strlen(prefix)) == 0); | |
| 134 } | |
| 135 | |
| 136 gboolean | |
| 137 str_has_suffix_nocase(const gchar * str, const gchar * suffix) | |
| 138 { | |
| 139 return (strcasecmp(str + strlen(str) - strlen(suffix), suffix) == 0); | |
| 140 } | |
| 141 | |
| 142 gboolean | |
| 143 str_has_suffixes_nocase(const gchar * str, gchar * const *suffixes) | |
| 144 { | |
| 145 gchar *const *suffix; | |
| 146 | |
| 147 g_return_val_if_fail(str != NULL, FALSE); | |
| 148 g_return_val_if_fail(suffixes != NULL, FALSE); | |
| 149 | |
| 150 for (suffix = suffixes; *suffix; suffix++) | |
| 151 if (str_has_suffix_nocase(str, *suffix)) | |
| 152 return TRUE; | |
| 153 | |
| 154 return FALSE; | |
| 155 } | |
| 156 | |
| 157 gchar * | |
| 158 str_to_utf8_fallback(const gchar * str) | |
| 159 { | |
| 160 gchar *out_str, *convert_str, *chr; | |
| 161 | |
| 162 /* NULL in NULL out */ | |
| 163 if (!str) | |
| 164 return NULL; | |
| 165 | |
| 166 convert_str = g_strdup(str); | |
| 167 for (chr = convert_str; *chr; chr++) { | |
| 168 if (*chr & 0x80) | |
| 169 *chr = '?'; | |
| 170 } | |
| 171 | |
| 172 out_str = g_strconcat(convert_str, _(" (invalid UTF-8)"), NULL); | |
| 173 g_free(convert_str); | |
| 174 | |
| 175 return out_str; | |
| 176 } | |
| 177 | |
| 178 gchar * | |
| 179 filename_to_utf8(const gchar * filename) | |
| 180 { | |
| 181 gchar *out_str; | |
| 182 | |
| 183 /* NULL in NULL out */ | |
| 184 if (!filename) | |
| 185 return NULL; | |
| 186 | |
| 187 if ((out_str = g_filename_to_utf8(filename, -1, NULL, NULL, NULL))) | |
| 188 return out_str; | |
| 189 | |
| 190 return str_to_utf8_fallback(filename); | |
| 191 } | |
| 192 | |
| 193 gchar * | |
| 194 str_to_utf8(const gchar * str) | |
| 195 { | |
| 196 gchar *out_str; | |
| 197 | |
| 198 /* NULL in NULL out */ | |
| 199 if (!str) | |
| 200 return NULL; | |
| 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 | |
| 206 * the given string could be treated as a string in one of fallback | |
| 207 * encodings. To avoid this, the order of evaluation has been | |
| 208 * changed. (It might cause a drawback?) | |
| 209 */ | |
| 210 /* chardet encoding detector */ | |
| 211 if ((out_str = chardet_to_utf8(str, strlen(str), NULL, NULL, NULL))) | |
| 212 return out_str; | |
| 213 | |
| 214 /* already UTF-8? */ | |
| 215 if (g_utf8_validate(str, -1, NULL)) | |
| 216 return g_strdup(str); | |
| 217 | |
| 218 /* assume encoding associated with locale */ | |
| 219 if ((out_str = g_locale_to_utf8(str, -1, NULL, NULL, NULL))) | |
| 220 return out_str; | |
| 221 | |
| 222 /* all else fails, we mask off character codes >= 128, | |
| 223 replace with '?' */ | |
| 224 return str_to_utf8_fallback(str); | |
| 225 } | |
| 226 | |
| 227 | |
| 228 const gchar * | |
| 229 str_skip_chars(const gchar * str, const gchar * chars) | |
| 230 { | |
| 231 while (strchr(chars, *str)) | |
| 232 str++; | |
| 233 return str; | |
| 234 } | |
| 235 | |
| 236 gchar * | |
| 237 convert_title_text(gchar * title) | |
| 238 { | |
| 239 g_return_val_if_fail(title != NULL, NULL); | |
| 240 | |
| 241 if (cfg.convert_slash) | |
| 242 str_replace_char(title, '\\', '/'); | |
| 243 | |
| 244 if (cfg.convert_underscore) | |
| 245 str_replace_char(title, '_', ' '); | |
| 246 | |
| 247 if (cfg.convert_twenty) | |
| 248 str_twenty_to_space(title); | |
| 249 | |
| 250 return title; | |
| 251 } | |
| 252 | |
| 253 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
|
254 gsize *arg_bytes_read, gsize *arg_bytes_write, |
|
ad1d7687814c
[svn] made strings.h for existing strings.c, cleanups
mf0102
parents:
2332
diff
changeset
|
255 GError **arg_error) |
| 2313 | 256 { |
| 257 #ifdef USE_CHARDET | |
| 258 char *det = NULL, *encoding = NULL; | |
| 259 #endif | |
| 260 gchar *ret = NULL; | |
| 261 gsize *bytes_read, *bytes_write; | |
| 262 GError **error; | |
| 263 gsize my_bytes_read, my_bytes_write; | |
| 264 | |
| 265 bytes_read = arg_bytes_read ? arg_bytes_read : &my_bytes_read; | |
| 266 bytes_write = arg_bytes_write ? arg_bytes_write : &my_bytes_write; | |
| 267 error = arg_error ? arg_error : NULL; | |
| 268 | |
| 269 #ifdef USE_CHARDET | |
| 270 if(cfg.chardet_detector) | |
| 271 det = cfg.chardet_detector; | |
| 272 | |
| 273 if(det){ | |
| 274 if(!strncasecmp("japanese", det, sizeof("japanese"))) { | |
| 275 encoding = (char *)guess_jp(str, strlen(str)); | |
| 276 if (!encoding) | |
| 277 goto fallback; | |
| 278 } else if(!strncasecmp("taiwanese", det, sizeof("taiwanese"))) { | |
| 279 encoding = (char *)guess_tw(str, strlen(str)); | |
| 280 if (!encoding) | |
| 281 goto fallback; | |
| 282 } else if(!strncasecmp("chinese", det, sizeof("chinese"))) { | |
| 283 encoding = (char *)guess_cn(str, strlen(str)); | |
| 284 if (!encoding) | |
| 285 goto fallback; | |
| 286 } else if(!strncasecmp("korean", det, sizeof("korean"))) { | |
| 287 encoding = (char *)guess_kr(str, strlen(str)); | |
| 288 if (!encoding) | |
| 289 goto fallback; | |
| 290 } else if(!strncasecmp("russian", det, sizeof("russian"))) { | |
| 291 rcd_russian_charset res = rcdGetRussianCharset(str, strlen(str)); | |
| 292 switch(res) { | |
| 293 case RUSSIAN_CHARSET_WIN: | |
| 294 encoding = "CP1251"; | |
| 295 break; | |
| 296 case RUSSIAN_CHARSET_ALT: | |
| 297 encoding = "CP866"; | |
| 298 break; | |
| 299 case RUSSIAN_CHARSET_KOI: | |
| 300 encoding = "KOI8-R"; | |
| 301 break; | |
| 302 case RUSSIAN_CHARSET_UTF8: | |
| 303 encoding = "UTF-8"; | |
| 304 break; | |
| 305 } | |
| 306 if (!encoding) | |
| 307 goto fallback; | |
| 308 #ifdef HAVE_UDET | |
| 309 } else if (!strncasecmp("universal", det, sizeof("universal"))) { | |
| 310 encoding = (char *)detectCharset((char *)str, strlen(str)); | |
| 311 if (!encoding) | |
| 312 goto fallback; | |
| 313 #endif | |
| 314 } else /* none, invalid */ | |
| 315 goto fallback; | |
| 316 | |
| 317 ret = g_convert(str, len, "UTF-8", encoding, bytes_read, bytes_write, error); | |
| 318 } | |
| 319 | |
| 320 fallback: | |
| 321 #endif | |
| 322 if(!ret && cfg.chardet_fallback){ | |
| 323 gchar **encs=NULL, **enc=NULL; | |
| 324 encs = g_strsplit_set(cfg.chardet_fallback, " ,:;|/", 0); | |
| 325 | |
| 326 if(encs){ | |
| 327 enc = encs; | |
| 328 for(enc=encs; *enc ; enc++){ | |
| 329 ret = g_convert(str, len, "UTF-8", *enc, bytes_read, bytes_write, error); | |
| 330 if(len == *bytes_read){ | |
| 331 break; | |
| 332 } | |
| 333 } | |
| 334 g_strfreev(encs); | |
| 335 } | |
| 336 } | |
| 337 | |
| 338 #ifdef USE_CHARDET | |
| 339 /* many tag libraries return 2byte latin1 utf8 character as | |
| 340 converted 8bit iso-8859-1 character, if they are asked to return | |
| 341 latin1 string. | |
| 342 */ | |
| 343 if(!ret){ | |
| 344 ret = g_convert(str, len, "UTF-8", "ISO-8859-1", bytes_read, bytes_write, error); | |
| 345 } | |
| 346 #endif | |
| 347 | |
| 348 if(ret){ | |
| 349 if(g_utf8_validate(ret, -1, NULL)) | |
| 350 return ret; | |
| 351 else { | |
| 352 g_free(ret); | |
| 353 ret = NULL; | |
| 354 } | |
| 355 } | |
| 356 | |
| 357 return NULL; /* if I have no idea, return NULL. */ | |
| 358 } |
