Mercurial > audlegacy
annotate src/audacious/strings.c @ 4070:040243a50bd3
- modified playlist_load_ins_file() and playlist_load_ins_file_tuple(). path builder and ext_hash checker
have been extracted from playlist_load_ins_file_tuple() and are provided as individual functions.
- path builder is available to plugins as aud_construct_uri() and it allows container plugins to construct valid uri.
- replaced __playlist_ins_with_info_tuple() with the superset __playlist_ins_file(). it can accept both tuple and title/length pair.
- changed call dependency among playlist_load_ins_file(), playlist_load_ins_file_tuple() and __playlist_ins_file(). playlist_load_ins_file() no longer calls playlist_load_ins_file_tuple() nor builds any tuple.
- made some cleanups.
| author | Yoshiki Yazawa <yaz@cc.rim.or.jp> |
|---|---|
| date | Fri, 07 Dec 2007 01:11:25 +0900 |
| parents | 41a91ed36bfa |
| children | 9e24c8746d99 |
| 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 | |
|
4070
040243a50bd3
- modified playlist_load_ins_file() and playlist_load_ins_file_tuple(). path builder and ext_hash checker
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
3209
diff
changeset
|
76 /* replace %20 with ' ' in place */ |
| 2313 | 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 | |
|
4070
040243a50bd3
- modified playlist_load_ins_file() and playlist_load_ins_file_tuple(). path builder and ext_hash checker
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
3209
diff
changeset
|
95 /* replace drive letter with '/' in place */ |
|
040243a50bd3
- modified playlist_load_ins_file() and playlist_load_ins_file_tuple(). path builder and ext_hash checker
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
3209
diff
changeset
|
96 static gchar * |
|
040243a50bd3
- modified playlist_load_ins_file() and playlist_load_ins_file_tuple(). path builder and ext_hash checker
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
3209
diff
changeset
|
97 str_replace_drive_letter(gchar * str) |
|
040243a50bd3
- modified playlist_load_ins_file() and playlist_load_ins_file_tuple(). path builder and ext_hash checker
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
3209
diff
changeset
|
98 { |
|
040243a50bd3
- modified playlist_load_ins_file() and playlist_load_ins_file_tuple(). path builder and ext_hash checker
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
3209
diff
changeset
|
99 gchar *match, *match_end; |
|
040243a50bd3
- modified playlist_load_ins_file() and playlist_load_ins_file_tuple(). path builder and ext_hash checker
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
3209
diff
changeset
|
100 |
|
040243a50bd3
- modified playlist_load_ins_file() and playlist_load_ins_file_tuple(). path builder and ext_hash checker
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
3209
diff
changeset
|
101 g_return_val_if_fail(str != NULL, NULL); |
|
040243a50bd3
- modified playlist_load_ins_file() and playlist_load_ins_file_tuple(). path builder and ext_hash checker
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
3209
diff
changeset
|
102 |
|
040243a50bd3
- modified playlist_load_ins_file() and playlist_load_ins_file_tuple(). path builder and ext_hash checker
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
3209
diff
changeset
|
103 while ((match = strstr(str, ":\\"))) { |
|
040243a50bd3
- modified playlist_load_ins_file() and playlist_load_ins_file_tuple(). path builder and ext_hash checker
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
3209
diff
changeset
|
104 match--; |
|
040243a50bd3
- modified playlist_load_ins_file() and playlist_load_ins_file_tuple(). path builder and ext_hash checker
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
3209
diff
changeset
|
105 match_end = match + 3; |
|
040243a50bd3
- modified playlist_load_ins_file() and playlist_load_ins_file_tuple(). path builder and ext_hash checker
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
3209
diff
changeset
|
106 *match++ = '/'; |
|
040243a50bd3
- modified playlist_load_ins_file() and playlist_load_ins_file_tuple(). path builder and ext_hash checker
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
3209
diff
changeset
|
107 while (*match_end) |
|
040243a50bd3
- modified playlist_load_ins_file() and playlist_load_ins_file_tuple(). path builder and ext_hash checker
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
3209
diff
changeset
|
108 *match++ = *match_end++; |
|
040243a50bd3
- modified playlist_load_ins_file() and playlist_load_ins_file_tuple(). path builder and ext_hash checker
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
3209
diff
changeset
|
109 *match = 0; /* the end of line */ |
|
040243a50bd3
- modified playlist_load_ins_file() and playlist_load_ins_file_tuple(). path builder and ext_hash checker
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
3209
diff
changeset
|
110 } |
|
040243a50bd3
- modified playlist_load_ins_file() and playlist_load_ins_file_tuple(). path builder and ext_hash checker
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
3209
diff
changeset
|
111 |
|
040243a50bd3
- modified playlist_load_ins_file() and playlist_load_ins_file_tuple(). path builder and ext_hash checker
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
3209
diff
changeset
|
112 return str; |
|
040243a50bd3
- modified playlist_load_ins_file() and playlist_load_ins_file_tuple(). path builder and ext_hash checker
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
3209
diff
changeset
|
113 } |
|
040243a50bd3
- modified playlist_load_ins_file() and playlist_load_ins_file_tuple(). path builder and ext_hash checker
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
3209
diff
changeset
|
114 |
| 2313 | 115 static gchar * |
| 116 str_replace_char(gchar * str, gchar old, gchar new) | |
| 117 { | |
| 118 gchar *match; | |
| 119 | |
| 120 g_return_val_if_fail(str != NULL, NULL); | |
| 121 | |
| 122 match = str; | |
| 123 while ((match = strchr(match, old))) | |
| 124 *match = new; | |
| 125 | |
| 126 return str; | |
| 127 } | |
| 128 | |
| 129 gchar * | |
| 130 str_append(gchar * str, const gchar * add_str) | |
| 131 { | |
| 132 return str_replace(str, g_strconcat(str, add_str, NULL)); | |
| 133 } | |
| 134 | |
| 135 gchar * | |
| 136 str_replace(gchar * str, gchar * new_str) | |
| 137 { | |
| 138 g_free(str); | |
| 139 return new_str; | |
| 140 } | |
| 141 | |
| 142 void | |
| 143 str_replace_in(gchar ** str, gchar * new_str) | |
| 144 { | |
| 145 *str = str_replace(*str, new_str); | |
| 146 } | |
| 147 | |
| 148 | |
| 149 gboolean | |
| 150 str_has_prefix_nocase(const gchar * str, const gchar * prefix) | |
| 151 { | |
| 152 return (strncasecmp(str, prefix, strlen(prefix)) == 0); | |
| 153 } | |
| 154 | |
| 155 gboolean | |
| 156 str_has_suffix_nocase(const gchar * str, const gchar * suffix) | |
| 157 { | |
| 158 return (strcasecmp(str + strlen(str) - strlen(suffix), suffix) == 0); | |
| 159 } | |
| 160 | |
| 161 gboolean | |
| 162 str_has_suffixes_nocase(const gchar * str, gchar * const *suffixes) | |
| 163 { | |
| 164 gchar *const *suffix; | |
| 165 | |
| 166 g_return_val_if_fail(str != NULL, FALSE); | |
| 167 g_return_val_if_fail(suffixes != NULL, FALSE); | |
| 168 | |
| 169 for (suffix = suffixes; *suffix; suffix++) | |
| 170 if (str_has_suffix_nocase(str, *suffix)) | |
| 171 return TRUE; | |
| 172 | |
| 173 return FALSE; | |
| 174 } | |
| 175 | |
| 176 gchar * | |
| 177 str_to_utf8_fallback(const gchar * str) | |
| 178 { | |
| 179 gchar *out_str, *convert_str, *chr; | |
| 180 | |
| 181 /* NULL in NULL out */ | |
| 182 if (!str) | |
| 183 return NULL; | |
| 184 | |
| 185 convert_str = g_strdup(str); | |
| 186 for (chr = convert_str; *chr; chr++) { | |
| 187 if (*chr & 0x80) | |
| 188 *chr = '?'; | |
| 189 } | |
| 190 | |
| 191 out_str = g_strconcat(convert_str, _(" (invalid UTF-8)"), NULL); | |
| 192 g_free(convert_str); | |
| 193 | |
| 194 return out_str; | |
| 195 } | |
| 196 | |
| 197 gchar * | |
| 198 filename_to_utf8(const gchar * filename) | |
| 199 { | |
| 200 gchar *out_str; | |
| 201 | |
| 202 /* NULL in NULL out */ | |
| 203 if (!filename) | |
| 204 return NULL; | |
| 205 | |
| 206 if ((out_str = g_filename_to_utf8(filename, -1, NULL, NULL, NULL))) | |
| 207 return out_str; | |
| 208 | |
| 209 return str_to_utf8_fallback(filename); | |
| 210 } | |
| 211 | |
| 212 gchar * | |
| 213 str_to_utf8(const gchar * str) | |
| 214 { | |
| 215 gchar *out_str; | |
| 216 | |
| 217 /* 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
|
218 /* 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
|
219 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
|
220 return NULL; |
| 2313 | 221 |
| 222 /* Note: Currently, playlist calls this function repeatedly, even | |
| 223 * if the string is already converted into utf-8. | |
| 224 * chardet_to_utf8() would convert a valid utf-8 string into a | |
| 225 * different utf-8 string, if fallback encodings were supplied and | |
| 2559 | 226 * the given string could be treated as a string in one of |
| 227 * fallback encodings. To avoid this, g_utf8_validate() had been | |
| 228 * used at the top of evaluation. | |
| 229 */ | |
| 230 | |
| 231 /* Note 2: g_utf8_validate() has so called encapsulated utf-8 | |
| 232 * problem, thus chardet_to_utf8() took the place of that. | |
| 2313 | 233 */ |
| 2559 | 234 |
| 235 /* Note 3: As introducing madplug, the problem of conversion from | |
| 236 * ISO-8859-1 to UTF-8 arose. This may be coped with g_convert() | |
| 237 * located near the end of chardet_to_utf8(), but it requires utf8 | |
| 238 * validation guard where g_utf8_validate() was. New | |
| 239 * dfa_validate_utf8() employs libguess' DFA engine to validate | |
| 240 * utf-8 and can properly distinguish examples of encapsulated | |
| 241 * utf-8. It is considered to be safe to use as a guard. | |
| 242 */ | |
| 243 | |
| 244 /* already UTF-8? */ | |
| 245 if (dfa_validate_utf8(str, strlen(str))) | |
| 246 return g_strdup(str); | |
| 247 | |
| 2313 | 248 /* chardet encoding detector */ |
| 249 if ((out_str = chardet_to_utf8(str, strlen(str), NULL, NULL, NULL))) | |
| 250 return out_str; | |
| 251 | |
| 252 /* assume encoding associated with locale */ | |
| 253 if ((out_str = g_locale_to_utf8(str, -1, NULL, NULL, NULL))) | |
| 254 return out_str; | |
| 255 | |
| 256 /* all else fails, we mask off character codes >= 128, | |
| 257 replace with '?' */ | |
| 258 return str_to_utf8_fallback(str); | |
| 259 } | |
| 260 | |
| 261 | |
| 262 const gchar * | |
| 263 str_skip_chars(const gchar * str, const gchar * chars) | |
| 264 { | |
| 265 while (strchr(chars, *str)) | |
| 266 str++; | |
| 267 return str; | |
| 268 } | |
| 269 | |
| 270 gchar * | |
| 271 convert_title_text(gchar * title) | |
| 272 { | |
| 273 g_return_val_if_fail(title != NULL, NULL); | |
| 274 | |
| 275 if (cfg.convert_slash) | |
| 276 str_replace_char(title, '\\', '/'); | |
| 277 | |
| 278 if (cfg.convert_underscore) | |
| 279 str_replace_char(title, '_', ' '); | |
| 280 | |
| 281 if (cfg.convert_twenty) | |
| 282 str_twenty_to_space(title); | |
| 283 | |
| 284 return title; | |
| 285 } | |
| 286 | |
|
4070
040243a50bd3
- modified playlist_load_ins_file() and playlist_load_ins_file_tuple(). path builder and ext_hash checker
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
3209
diff
changeset
|
287 gchar * |
|
040243a50bd3
- modified playlist_load_ins_file() and playlist_load_ins_file_tuple(). path builder and ext_hash checker
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
3209
diff
changeset
|
288 convert_dos_path(gchar * path) |
|
040243a50bd3
- modified playlist_load_ins_file() and playlist_load_ins_file_tuple(). path builder and ext_hash checker
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
3209
diff
changeset
|
289 { |
|
040243a50bd3
- modified playlist_load_ins_file() and playlist_load_ins_file_tuple(). path builder and ext_hash checker
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
3209
diff
changeset
|
290 g_return_val_if_fail(path != NULL, NULL); |
|
040243a50bd3
- modified playlist_load_ins_file() and playlist_load_ins_file_tuple(). path builder and ext_hash checker
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
3209
diff
changeset
|
291 |
|
040243a50bd3
- modified playlist_load_ins_file() and playlist_load_ins_file_tuple(). path builder and ext_hash checker
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
3209
diff
changeset
|
292 /* replace drive letter with '/' */ |
|
040243a50bd3
- modified playlist_load_ins_file() and playlist_load_ins_file_tuple(). path builder and ext_hash checker
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
3209
diff
changeset
|
293 str_replace_drive_letter(path); |
|
040243a50bd3
- modified playlist_load_ins_file() and playlist_load_ins_file_tuple(). path builder and ext_hash checker
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
3209
diff
changeset
|
294 |
|
040243a50bd3
- modified playlist_load_ins_file() and playlist_load_ins_file_tuple(). path builder and ext_hash checker
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
3209
diff
changeset
|
295 /* replace '\' with '/' */ |
|
040243a50bd3
- modified playlist_load_ins_file() and playlist_load_ins_file_tuple(). path builder and ext_hash checker
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
3209
diff
changeset
|
296 str_replace_char(path, '\\', '/'); |
|
040243a50bd3
- modified playlist_load_ins_file() and playlist_load_ins_file_tuple(). path builder and ext_hash checker
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
3209
diff
changeset
|
297 |
|
040243a50bd3
- modified playlist_load_ins_file() and playlist_load_ins_file_tuple(). path builder and ext_hash checker
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
3209
diff
changeset
|
298 return path; |
|
040243a50bd3
- modified playlist_load_ins_file() and playlist_load_ins_file_tuple(). path builder and ext_hash checker
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
3209
diff
changeset
|
299 } |
|
040243a50bd3
- modified playlist_load_ins_file() and playlist_load_ins_file_tuple(). path builder and ext_hash checker
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
3209
diff
changeset
|
300 |
|
040243a50bd3
- modified playlist_load_ins_file() and playlist_load_ins_file_tuple(). path builder and ext_hash checker
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
3209
diff
changeset
|
301 gchar * |
|
040243a50bd3
- modified playlist_load_ins_file() and playlist_load_ins_file_tuple(). path builder and ext_hash checker
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
3209
diff
changeset
|
302 chardet_to_utf8(const gchar *str, gssize len, |
|
2373
ad1d7687814c
[svn] made strings.h for existing strings.c, cleanups
mf0102
parents:
2332
diff
changeset
|
303 gsize *arg_bytes_read, gsize *arg_bytes_write, |
|
ad1d7687814c
[svn] made strings.h for existing strings.c, cleanups
mf0102
parents:
2332
diff
changeset
|
304 GError **arg_error) |
| 2313 | 305 { |
| 306 #ifdef USE_CHARDET | |
| 307 char *det = NULL, *encoding = NULL; | |
| 308 #endif | |
| 309 gchar *ret = NULL; | |
| 310 gsize *bytes_read, *bytes_write; | |
| 311 GError **error; | |
| 312 gsize my_bytes_read, my_bytes_write; | |
| 313 | |
| 314 bytes_read = arg_bytes_read ? arg_bytes_read : &my_bytes_read; | |
| 315 bytes_write = arg_bytes_write ? arg_bytes_write : &my_bytes_write; | |
| 316 error = arg_error ? arg_error : NULL; | |
| 317 | |
|
2787
e35538325145
[svn] - add some assertions to chardet_to_utf8() to try to trace bad g_convert() calls
nenolod
parents:
2559
diff
changeset
|
318 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
|
319 |
| 2313 | 320 #ifdef USE_CHARDET |
| 321 if(cfg.chardet_detector) | |
| 322 det = cfg.chardet_detector; | |
| 323 | |
|
3209
41a91ed36bfa
Use new combined libguess interface.
William Pitcock <nenolod@atheme-project.org>
parents:
3123
diff
changeset
|
324 guess_init(); |
|
41a91ed36bfa
Use new combined libguess interface.
William Pitcock <nenolod@atheme-project.org>
parents:
3123
diff
changeset
|
325 |
| 2313 | 326 if(det){ |
|
3209
41a91ed36bfa
Use new combined libguess interface.
William Pitcock <nenolod@atheme-project.org>
parents:
3123
diff
changeset
|
327 encoding = (char *) guess_encoding(str, strlen(str), det); |
|
41a91ed36bfa
Use new combined libguess interface.
William Pitcock <nenolod@atheme-project.org>
parents:
3123
diff
changeset
|
328 if (!encoding) |
| 2313 | 329 goto fallback; |
| 330 | |
| 331 ret = g_convert(str, len, "UTF-8", encoding, bytes_read, bytes_write, error); | |
| 332 } | |
| 333 | |
| 334 fallback: | |
| 335 #endif | |
| 336 if(!ret && cfg.chardet_fallback){ | |
| 337 gchar **encs=NULL, **enc=NULL; | |
| 338 encs = g_strsplit_set(cfg.chardet_fallback, " ,:;|/", 0); | |
| 339 | |
| 340 if(encs){ | |
| 341 enc = encs; | |
| 342 for(enc=encs; *enc ; enc++){ | |
| 343 ret = g_convert(str, len, "UTF-8", *enc, bytes_read, bytes_write, error); | |
| 344 if(len == *bytes_read){ | |
| 345 break; | |
| 346 } | |
| 347 } | |
| 348 g_strfreev(encs); | |
| 349 } | |
| 350 } | |
| 351 | |
| 352 if(!ret){ | |
| 353 ret = g_convert(str, len, "UTF-8", "ISO-8859-1", bytes_read, bytes_write, error); | |
| 354 } | |
| 355 | |
| 356 if(ret){ | |
| 357 if(g_utf8_validate(ret, -1, NULL)) | |
| 358 return ret; | |
| 359 else { | |
| 360 g_free(ret); | |
| 361 ret = NULL; | |
| 362 } | |
| 363 } | |
| 364 | |
| 365 return NULL; /* if I have no idea, return NULL. */ | |
| 366 } |
