Mercurial > audlegacy
annotate src/audacious/strings.c @ 3209:41a91ed36bfa trunk
Use new combined libguess interface.
| author | William Pitcock <nenolod@atheme-project.org> |
|---|---|
| date | Wed, 01 Aug 2007 08:12:03 -0500 |
| parents | f1c756f39e6c |
| children | 040243a50bd3 |
| 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" |
| 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 */ | |
|
3116
a2d234851527
Switching from g_return_val_if_fail() to a more quiet return NULL
Christian Birchinger <joker@netswarm.net>
parents:
2787
diff
changeset
|
197 /* 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
|
198 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
|
199 return NULL; |
| 2313 | 200 |
| 201 /* Note: Currently, playlist calls this function repeatedly, even | |
| 202 * if the string is already converted into utf-8. | |
| 203 * chardet_to_utf8() would convert a valid utf-8 string into a | |
| 204 * different utf-8 string, if fallback encodings were supplied and | |
| 2559 | 205 * the given string could be treated as a string in one of |
| 206 * fallback encodings. To avoid this, g_utf8_validate() had been | |
| 207 * used at the top of evaluation. | |
| 208 */ | |
| 209 | |
| 210 /* Note 2: g_utf8_validate() has so called encapsulated utf-8 | |
| 211 * problem, thus chardet_to_utf8() took the place of that. | |
| 2313 | 212 */ |
| 2559 | 213 |
| 214 /* Note 3: As introducing madplug, the problem of conversion from | |
| 215 * ISO-8859-1 to UTF-8 arose. This may be coped with g_convert() | |
| 216 * located near the end of chardet_to_utf8(), but it requires utf8 | |
| 217 * validation guard where g_utf8_validate() was. New | |
| 218 * dfa_validate_utf8() employs libguess' DFA engine to validate | |
| 219 * utf-8 and can properly distinguish examples of encapsulated | |
| 220 * utf-8. It is considered to be safe to use as a guard. | |
| 221 */ | |
| 222 | |
| 223 /* already UTF-8? */ | |
| 224 if (dfa_validate_utf8(str, strlen(str))) | |
| 225 return g_strdup(str); | |
| 226 | |
| 2313 | 227 /* chardet encoding detector */ |
| 228 if ((out_str = chardet_to_utf8(str, strlen(str), NULL, NULL, NULL))) | |
| 229 return out_str; | |
| 230 | |
| 231 /* assume encoding associated with locale */ | |
| 232 if ((out_str = g_locale_to_utf8(str, -1, NULL, NULL, NULL))) | |
| 233 return out_str; | |
| 234 | |
| 235 /* all else fails, we mask off character codes >= 128, | |
| 236 replace with '?' */ | |
| 237 return str_to_utf8_fallback(str); | |
| 238 } | |
| 239 | |
| 240 | |
| 241 const gchar * | |
| 242 str_skip_chars(const gchar * str, const gchar * chars) | |
| 243 { | |
| 244 while (strchr(chars, *str)) | |
| 245 str++; | |
| 246 return str; | |
| 247 } | |
| 248 | |
| 249 gchar * | |
| 250 convert_title_text(gchar * title) | |
| 251 { | |
| 252 g_return_val_if_fail(title != NULL, NULL); | |
| 253 | |
| 254 if (cfg.convert_slash) | |
| 255 str_replace_char(title, '\\', '/'); | |
| 256 | |
| 257 if (cfg.convert_underscore) | |
| 258 str_replace_char(title, '_', ' '); | |
| 259 | |
| 260 if (cfg.convert_twenty) | |
| 261 str_twenty_to_space(title); | |
| 262 | |
| 263 return title; | |
| 264 } | |
| 265 | |
| 266 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
|
267 gsize *arg_bytes_read, gsize *arg_bytes_write, |
|
ad1d7687814c
[svn] made strings.h for existing strings.c, cleanups
mf0102
parents:
2332
diff
changeset
|
268 GError **arg_error) |
| 2313 | 269 { |
| 270 #ifdef USE_CHARDET | |
| 271 char *det = NULL, *encoding = NULL; | |
| 272 #endif | |
| 273 gchar *ret = NULL; | |
| 274 gsize *bytes_read, *bytes_write; | |
| 275 GError **error; | |
| 276 gsize my_bytes_read, my_bytes_write; | |
| 277 | |
| 278 bytes_read = arg_bytes_read ? arg_bytes_read : &my_bytes_read; | |
| 279 bytes_write = arg_bytes_write ? arg_bytes_write : &my_bytes_write; | |
| 280 error = arg_error ? arg_error : NULL; | |
| 281 | |
|
2787
e35538325145
[svn] - add some assertions to chardet_to_utf8() to try to trace bad g_convert() calls
nenolod
parents:
2559
diff
changeset
|
282 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
|
283 |
| 2313 | 284 #ifdef USE_CHARDET |
| 285 if(cfg.chardet_detector) | |
| 286 det = cfg.chardet_detector; | |
| 287 | |
|
3209
41a91ed36bfa
Use new combined libguess interface.
William Pitcock <nenolod@atheme-project.org>
parents:
3123
diff
changeset
|
288 guess_init(); |
|
41a91ed36bfa
Use new combined libguess interface.
William Pitcock <nenolod@atheme-project.org>
parents:
3123
diff
changeset
|
289 |
| 2313 | 290 if(det){ |
|
3209
41a91ed36bfa
Use new combined libguess interface.
William Pitcock <nenolod@atheme-project.org>
parents:
3123
diff
changeset
|
291 encoding = (char *) guess_encoding(str, strlen(str), det); |
|
41a91ed36bfa
Use new combined libguess interface.
William Pitcock <nenolod@atheme-project.org>
parents:
3123
diff
changeset
|
292 if (!encoding) |
| 2313 | 293 goto fallback; |
| 294 | |
| 295 ret = g_convert(str, len, "UTF-8", encoding, bytes_read, bytes_write, error); | |
| 296 } | |
| 297 | |
| 298 fallback: | |
| 299 #endif | |
| 300 if(!ret && cfg.chardet_fallback){ | |
| 301 gchar **encs=NULL, **enc=NULL; | |
| 302 encs = g_strsplit_set(cfg.chardet_fallback, " ,:;|/", 0); | |
| 303 | |
| 304 if(encs){ | |
| 305 enc = encs; | |
| 306 for(enc=encs; *enc ; enc++){ | |
| 307 ret = g_convert(str, len, "UTF-8", *enc, bytes_read, bytes_write, error); | |
| 308 if(len == *bytes_read){ | |
| 309 break; | |
| 310 } | |
| 311 } | |
| 312 g_strfreev(encs); | |
| 313 } | |
| 314 } | |
| 315 | |
| 316 if(!ret){ | |
| 317 ret = g_convert(str, len, "UTF-8", "ISO-8859-1", bytes_read, bytes_write, error); | |
| 318 } | |
| 319 | |
| 320 if(ret){ | |
| 321 if(g_utf8_validate(ret, -1, NULL)) | |
| 322 return ret; | |
| 323 else { | |
| 324 g_free(ret); | |
| 325 ret = NULL; | |
| 326 } | |
| 327 } | |
| 328 | |
| 329 return NULL; /* if I have no idea, return NULL. */ | |
| 330 } |
