Mercurial > audlegacy
annotate src/audacious/strings.c @ 4606:6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
where string data should be UTF-8 but might not be. In case of valid UTF-8
a copy of string is returned. If string is NOT valid UTF-8, a call backtrace
is printed and str_to_utf() is called instead.
| author | Matti Hamalainen <ccr@tnsp.org> |
|---|---|
| date | Wed, 04 Jun 2008 23:17:47 +0300 |
| parents | 6e323e395886 |
| children | 829c30fc87ba |
| 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 | |
|
4474
6e323e395886
Fix some random #includes.
Matti Hamalainen <ccr@tnsp.org>
parents:
4453
diff
changeset
|
38 #ifdef USE_CHARDET |
| 2559 | 39 #include "../libguess/libguess.h" |
|
4474
6e323e395886
Fix some random #includes.
Matti Hamalainen <ccr@tnsp.org>
parents:
4453
diff
changeset
|
40 # ifdef HAVE_UDET |
|
6e323e395886
Fix some random #includes.
Matti Hamalainen <ccr@tnsp.org>
parents:
4453
diff
changeset
|
41 # include <libudet_c.h> |
|
6e323e395886
Fix some random #includes.
Matti Hamalainen <ccr@tnsp.org>
parents:
4453
diff
changeset
|
42 # endif |
| 2313 | 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 | |
|
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
|
78 /* replace %20 with ' ' in place */ |
| 2313 | 79 static gchar * |
| 80 str_twenty_to_space(gchar * str) | |
| 81 { | |
| 82 gchar *match, *match_end; | |
| 83 | |
| 84 g_return_val_if_fail(str != NULL, NULL); | |
| 85 | |
| 86 while ((match = strstr(str, "%20"))) { | |
| 87 match_end = match + 3; | |
| 88 *match++ = ' '; | |
| 89 while (*match_end) | |
| 90 *match++ = *match_end++; | |
| 91 *match = 0; | |
| 92 } | |
| 93 | |
| 94 return str; | |
| 95 } | |
| 96 | |
|
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
|
97 /* 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
|
98 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
|
99 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
|
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 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
|
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 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
|
104 |
|
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 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
|
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 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
|
108 *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
|
109 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
|
110 *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
|
111 *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
|
112 } |
|
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 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
|
115 } |
|
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
|
116 |
| 2313 | 117 static gchar * |
| 118 str_replace_char(gchar * str, gchar old, gchar new) | |
| 119 { | |
| 120 gchar *match; | |
| 121 | |
| 122 g_return_val_if_fail(str != NULL, NULL); | |
| 123 | |
| 124 match = str; | |
| 125 while ((match = strchr(match, old))) | |
| 126 *match = new; | |
| 127 | |
| 128 return str; | |
| 129 } | |
| 130 | |
| 131 gchar * | |
| 132 str_append(gchar * str, const gchar * add_str) | |
| 133 { | |
| 134 return str_replace(str, g_strconcat(str, add_str, NULL)); | |
| 135 } | |
| 136 | |
| 137 gchar * | |
| 138 str_replace(gchar * str, gchar * new_str) | |
| 139 { | |
| 140 g_free(str); | |
| 141 return new_str; | |
| 142 } | |
| 143 | |
| 144 void | |
| 145 str_replace_in(gchar ** str, gchar * new_str) | |
| 146 { | |
| 147 *str = str_replace(*str, new_str); | |
| 148 } | |
| 149 | |
| 150 | |
| 151 gboolean | |
| 152 str_has_prefix_nocase(const gchar * str, const gchar * prefix) | |
| 153 { | |
|
4201
5f92bee6cd5b
possible fix for (Bugzilla #35), waiting for some feedback from Tarmaq...
Cristi Magherusan <majeru@atheme.org>
parents:
4089
diff
changeset
|
154 /* strncasecmp causes segfaults when str is NULL*/ |
|
5f92bee6cd5b
possible fix for (Bugzilla #35), waiting for some feedback from Tarmaq...
Cristi Magherusan <majeru@atheme.org>
parents:
4089
diff
changeset
|
155 return (str && (strncasecmp(str, prefix, strlen(prefix)) == 0)); |
| 2313 | 156 } |
| 157 | |
| 158 gboolean | |
| 159 str_has_suffix_nocase(const gchar * str, const gchar * suffix) | |
| 160 { | |
| 161 return (strcasecmp(str + strlen(str) - strlen(suffix), suffix) == 0); | |
| 162 } | |
| 163 | |
| 164 gboolean | |
| 165 str_has_suffixes_nocase(const gchar * str, gchar * const *suffixes) | |
| 166 { | |
| 167 gchar *const *suffix; | |
| 168 | |
| 169 g_return_val_if_fail(str != NULL, FALSE); | |
| 170 g_return_val_if_fail(suffixes != NULL, FALSE); | |
| 171 | |
| 172 for (suffix = suffixes; *suffix; suffix++) | |
| 173 if (str_has_suffix_nocase(str, *suffix)) | |
| 174 return TRUE; | |
| 175 | |
| 176 return FALSE; | |
| 177 } | |
| 178 | |
| 179 gchar * | |
| 180 str_to_utf8_fallback(const gchar * str) | |
| 181 { | |
| 182 gchar *out_str, *convert_str, *chr; | |
| 183 | |
| 184 /* NULL in NULL out */ | |
| 185 if (!str) | |
| 186 return NULL; | |
| 187 | |
| 188 convert_str = g_strdup(str); | |
| 189 for (chr = convert_str; *chr; chr++) { | |
| 190 if (*chr & 0x80) | |
| 191 *chr = '?'; | |
| 192 } | |
| 193 | |
| 194 out_str = g_strconcat(convert_str, _(" (invalid UTF-8)"), NULL); | |
| 195 g_free(convert_str); | |
| 196 | |
| 197 return out_str; | |
| 198 } | |
| 199 | |
|
4089
9e24c8746d99
- introduce new API functions uri_to_display_basename() and uri_to_display_dirname(). each function derives utf8 encoded basename or dirname from given uri respectively.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
4070
diff
changeset
|
200 /* convert name of absolute path in local file system encoding into utf8 string */ |
| 2313 | 201 gchar * |
| 202 filename_to_utf8(const gchar * filename) | |
| 203 { | |
| 204 gchar *out_str; | |
| 205 | |
| 206 /* NULL in NULL out */ | |
| 207 if (!filename) | |
| 208 return NULL; | |
| 209 | |
| 210 if ((out_str = g_filename_to_utf8(filename, -1, NULL, NULL, NULL))) | |
| 211 return out_str; | |
| 212 | |
| 213 return str_to_utf8_fallback(filename); | |
| 214 } | |
| 215 | |
|
4089
9e24c8746d99
- introduce new API functions uri_to_display_basename() and uri_to_display_dirname(). each function derives utf8 encoded basename or dirname from given uri respectively.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
4070
diff
changeset
|
216 /* derives basename from uri. basename is in utf8 */ |
|
9e24c8746d99
- introduce new API functions uri_to_display_basename() and uri_to_display_dirname(). each function derives utf8 encoded basename or dirname from given uri respectively.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
4070
diff
changeset
|
217 gchar * |
|
9e24c8746d99
- introduce new API functions uri_to_display_basename() and uri_to_display_dirname(). each function derives utf8 encoded basename or dirname from given uri respectively.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
4070
diff
changeset
|
218 uri_to_display_basename(const gchar * uri) |
|
9e24c8746d99
- introduce new API functions uri_to_display_basename() and uri_to_display_dirname(). each function derives utf8 encoded basename or dirname from given uri respectively.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
4070
diff
changeset
|
219 { |
|
9e24c8746d99
- introduce new API functions uri_to_display_basename() and uri_to_display_dirname(). each function derives utf8 encoded basename or dirname from given uri respectively.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
4070
diff
changeset
|
220 gchar *realfn, *utf8fn, *basename; |
|
9e24c8746d99
- introduce new API functions uri_to_display_basename() and uri_to_display_dirname(). each function derives utf8 encoded basename or dirname from given uri respectively.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
4070
diff
changeset
|
221 |
|
9e24c8746d99
- introduce new API functions uri_to_display_basename() and uri_to_display_dirname(). each function derives utf8 encoded basename or dirname from given uri respectively.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
4070
diff
changeset
|
222 g_return_val_if_fail(uri, NULL); |
|
9e24c8746d99
- introduce new API functions uri_to_display_basename() and uri_to_display_dirname(). each function derives utf8 encoded basename or dirname from given uri respectively.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
4070
diff
changeset
|
223 |
|
9e24c8746d99
- introduce new API functions uri_to_display_basename() and uri_to_display_dirname(). each function derives utf8 encoded basename or dirname from given uri respectively.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
4070
diff
changeset
|
224 realfn = g_filename_from_uri(uri, NULL, NULL); |
|
9e24c8746d99
- introduce new API functions uri_to_display_basename() and uri_to_display_dirname(). each function derives utf8 encoded basename or dirname from given uri respectively.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
4070
diff
changeset
|
225 utf8fn = g_filename_display_name(realfn ? realfn : uri); // guaranteed to be non-NULL |
|
9e24c8746d99
- introduce new API functions uri_to_display_basename() and uri_to_display_dirname(). each function derives utf8 encoded basename or dirname from given uri respectively.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
4070
diff
changeset
|
226 basename = g_path_get_basename(utf8fn); |
|
9e24c8746d99
- introduce new API functions uri_to_display_basename() and uri_to_display_dirname(). each function derives utf8 encoded basename or dirname from given uri respectively.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
4070
diff
changeset
|
227 |
|
9e24c8746d99
- introduce new API functions uri_to_display_basename() and uri_to_display_dirname(). each function derives utf8 encoded basename or dirname from given uri respectively.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
4070
diff
changeset
|
228 g_free(realfn); g_free(utf8fn); |
|
9e24c8746d99
- introduce new API functions uri_to_display_basename() and uri_to_display_dirname(). each function derives utf8 encoded basename or dirname from given uri respectively.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
4070
diff
changeset
|
229 |
|
9e24c8746d99
- introduce new API functions uri_to_display_basename() and uri_to_display_dirname(). each function derives utf8 encoded basename or dirname from given uri respectively.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
4070
diff
changeset
|
230 return basename; |
|
9e24c8746d99
- introduce new API functions uri_to_display_basename() and uri_to_display_dirname(). each function derives utf8 encoded basename or dirname from given uri respectively.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
4070
diff
changeset
|
231 } |
|
9e24c8746d99
- introduce new API functions uri_to_display_basename() and uri_to_display_dirname(). each function derives utf8 encoded basename or dirname from given uri respectively.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
4070
diff
changeset
|
232 |
|
9e24c8746d99
- introduce new API functions uri_to_display_basename() and uri_to_display_dirname(). each function derives utf8 encoded basename or dirname from given uri respectively.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
4070
diff
changeset
|
233 /* derives dirname from uri. dirname is in utf8 */ |
|
9e24c8746d99
- introduce new API functions uri_to_display_basename() and uri_to_display_dirname(). each function derives utf8 encoded basename or dirname from given uri respectively.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
4070
diff
changeset
|
234 gchar * |
|
9e24c8746d99
- introduce new API functions uri_to_display_basename() and uri_to_display_dirname(). each function derives utf8 encoded basename or dirname from given uri respectively.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
4070
diff
changeset
|
235 uri_to_display_dirname(const gchar * uri) |
|
9e24c8746d99
- introduce new API functions uri_to_display_basename() and uri_to_display_dirname(). each function derives utf8 encoded basename or dirname from given uri respectively.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
4070
diff
changeset
|
236 { |
|
9e24c8746d99
- introduce new API functions uri_to_display_basename() and uri_to_display_dirname(). each function derives utf8 encoded basename or dirname from given uri respectively.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
4070
diff
changeset
|
237 gchar *realfn, *utf8fn, *dirname; |
|
9e24c8746d99
- introduce new API functions uri_to_display_basename() and uri_to_display_dirname(). each function derives utf8 encoded basename or dirname from given uri respectively.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
4070
diff
changeset
|
238 |
|
9e24c8746d99
- introduce new API functions uri_to_display_basename() and uri_to_display_dirname(). each function derives utf8 encoded basename or dirname from given uri respectively.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
4070
diff
changeset
|
239 g_return_val_if_fail(uri, NULL); |
|
9e24c8746d99
- introduce new API functions uri_to_display_basename() and uri_to_display_dirname(). each function derives utf8 encoded basename or dirname from given uri respectively.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
4070
diff
changeset
|
240 |
|
9e24c8746d99
- introduce new API functions uri_to_display_basename() and uri_to_display_dirname(). each function derives utf8 encoded basename or dirname from given uri respectively.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
4070
diff
changeset
|
241 realfn = g_filename_from_uri(uri, NULL, NULL); |
|
9e24c8746d99
- introduce new API functions uri_to_display_basename() and uri_to_display_dirname(). each function derives utf8 encoded basename or dirname from given uri respectively.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
4070
diff
changeset
|
242 utf8fn = g_filename_display_name(realfn ? realfn : uri); // guaranteed to be non-NULL |
|
9e24c8746d99
- introduce new API functions uri_to_display_basename() and uri_to_display_dirname(). each function derives utf8 encoded basename or dirname from given uri respectively.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
4070
diff
changeset
|
243 dirname = g_path_get_dirname(utf8fn); |
|
9e24c8746d99
- introduce new API functions uri_to_display_basename() and uri_to_display_dirname(). each function derives utf8 encoded basename or dirname from given uri respectively.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
4070
diff
changeset
|
244 |
|
9e24c8746d99
- introduce new API functions uri_to_display_basename() and uri_to_display_dirname(). each function derives utf8 encoded basename or dirname from given uri respectively.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
4070
diff
changeset
|
245 g_free(realfn); g_free(utf8fn); |
|
9e24c8746d99
- introduce new API functions uri_to_display_basename() and uri_to_display_dirname(). each function derives utf8 encoded basename or dirname from given uri respectively.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
4070
diff
changeset
|
246 |
|
9e24c8746d99
- introduce new API functions uri_to_display_basename() and uri_to_display_dirname(). each function derives utf8 encoded basename or dirname from given uri respectively.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
4070
diff
changeset
|
247 return dirname; |
|
9e24c8746d99
- introduce new API functions uri_to_display_basename() and uri_to_display_dirname(). each function derives utf8 encoded basename or dirname from given uri respectively.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
4070
diff
changeset
|
248 } |
|
9e24c8746d99
- introduce new API functions uri_to_display_basename() and uri_to_display_dirname(). each function derives utf8 encoded basename or dirname from given uri respectively.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
4070
diff
changeset
|
249 |
| 2313 | 250 gchar * |
| 251 str_to_utf8(const gchar * str) | |
| 252 { | |
| 253 gchar *out_str; | |
| 254 | |
| 255 /* 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
|
256 /* 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
|
257 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
|
258 return NULL; |
| 2313 | 259 |
| 260 /* Note: Currently, playlist calls this function repeatedly, even | |
| 261 * if the string is already converted into utf-8. | |
| 262 * chardet_to_utf8() would convert a valid utf-8 string into a | |
| 263 * different utf-8 string, if fallback encodings were supplied and | |
| 2559 | 264 * the given string could be treated as a string in one of |
| 265 * fallback encodings. To avoid this, g_utf8_validate() had been | |
| 266 * used at the top of evaluation. | |
| 267 */ | |
| 268 | |
| 269 /* Note 2: g_utf8_validate() has so called encapsulated utf-8 | |
| 270 * problem, thus chardet_to_utf8() took the place of that. | |
| 2313 | 271 */ |
| 2559 | 272 |
| 273 /* Note 3: As introducing madplug, the problem of conversion from | |
| 274 * ISO-8859-1 to UTF-8 arose. This may be coped with g_convert() | |
| 275 * located near the end of chardet_to_utf8(), but it requires utf8 | |
| 276 * validation guard where g_utf8_validate() was. New | |
| 277 * dfa_validate_utf8() employs libguess' DFA engine to validate | |
| 278 * utf-8 and can properly distinguish examples of encapsulated | |
| 279 * utf-8. It is considered to be safe to use as a guard. | |
| 280 */ | |
| 281 | |
| 282 /* already UTF-8? */ | |
| 283 if (dfa_validate_utf8(str, strlen(str))) | |
| 284 return g_strdup(str); | |
| 285 | |
| 2313 | 286 /* chardet encoding detector */ |
| 287 if ((out_str = chardet_to_utf8(str, strlen(str), NULL, NULL, NULL))) | |
| 288 return out_str; | |
| 289 | |
| 290 /* assume encoding associated with locale */ | |
| 291 if ((out_str = g_locale_to_utf8(str, -1, NULL, NULL, NULL))) | |
| 292 return out_str; | |
| 293 | |
| 294 /* all else fails, we mask off character codes >= 128, | |
| 295 replace with '?' */ | |
| 296 return str_to_utf8_fallback(str); | |
| 297 } | |
| 298 | |
| 299 | |
|
4606
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
300 /* This function is here to ASSERT that a given string IS valid UTF-8. |
|
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
301 * If it is, a copy of the string is returned (use g_free() to deallocate it.) |
|
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
302 * |
|
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
303 * However, if the string is NOT valid UTF-8, a warning is printed and a |
|
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
304 * callstack backtrace is printed in order to see where the problem occured. |
|
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
305 * |
|
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
306 * This is a temporary measure for removing useless str_to_utf8 etc. calls |
|
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
307 * and will be eventually removed... |
|
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
308 * -- ccr |
|
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
309 */ |
|
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
310 #include <execinfo.h> |
|
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
311 |
|
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
312 gchar * |
|
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
313 str_assert_utf8(const gchar * str) |
|
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
314 { |
|
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
315 /* NULL in NULL out */ |
|
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
316 if (!str) |
|
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
317 return NULL; |
|
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
318 |
|
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
319 /* already UTF-8? */ |
|
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
320 if (!g_utf8_validate(str, -1, NULL)) { |
|
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
321 gint i, nsymbols; |
|
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
322 const gint nsymmax = 50; |
|
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
323 void *addrbuf[nsymmax]; |
|
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
324 gchar **symbols; |
|
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
325 nsymbols = backtrace(addrbuf, nsymmax); |
|
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
326 symbols = backtrace_symbols(addrbuf, nsymbols); |
|
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
327 |
|
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
328 fprintf(stderr, "WARNING! String '%s' was not UTF-8! Backtrace (%d):\n", str, nsymbols); |
|
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
329 |
|
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
330 for (i = 0; i < nsymbols; i++) |
|
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
331 fprintf(stderr, "#%d > %s\n", i, symbols[i]); |
|
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
332 |
|
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
333 free(symbols); |
|
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
334 |
|
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
335 return str_to_utf8(str); |
|
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
336 } else |
|
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
337 return g_strdup(str); |
|
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
338 } |
|
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
339 |
|
6b76f8589f5d
Added a temporary function str_assert_utf8() for finding points in code
Matti Hamalainen <ccr@tnsp.org>
parents:
4474
diff
changeset
|
340 |
| 2313 | 341 const gchar * |
| 342 str_skip_chars(const gchar * str, const gchar * chars) | |
| 343 { | |
| 344 while (strchr(chars, *str)) | |
| 345 str++; | |
| 346 return str; | |
| 347 } | |
| 348 | |
| 349 gchar * | |
| 350 convert_title_text(gchar * title) | |
| 351 { | |
| 352 g_return_val_if_fail(title != NULL, NULL); | |
| 353 | |
| 354 if (cfg.convert_slash) | |
| 355 str_replace_char(title, '\\', '/'); | |
| 356 | |
| 357 if (cfg.convert_underscore) | |
| 358 str_replace_char(title, '_', ' '); | |
| 359 | |
| 360 if (cfg.convert_twenty) | |
| 361 str_twenty_to_space(title); | |
| 362 | |
| 363 return title; | |
| 364 } | |
| 365 | |
|
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
|
366 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
|
367 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
|
368 { |
|
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
|
369 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
|
370 |
|
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
|
371 /* 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
|
372 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
|
373 |
|
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
|
374 /* 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
|
375 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
|
376 |
|
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
|
377 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
|
378 } |
|
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
|
379 |
|
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
|
380 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
|
381 chardet_to_utf8(const gchar *str, gssize len, |
|
2373
ad1d7687814c
[svn] made strings.h for existing strings.c, cleanups
mf0102
parents:
2332
diff
changeset
|
382 gsize *arg_bytes_read, gsize *arg_bytes_write, |
|
ad1d7687814c
[svn] made strings.h for existing strings.c, cleanups
mf0102
parents:
2332
diff
changeset
|
383 GError **arg_error) |
| 2313 | 384 { |
| 385 #ifdef USE_CHARDET | |
| 386 char *det = NULL, *encoding = NULL; | |
| 387 #endif | |
| 388 gchar *ret = NULL; | |
| 389 gsize *bytes_read, *bytes_write; | |
| 390 GError **error; | |
| 391 gsize my_bytes_read, my_bytes_write; | |
| 392 | |
| 393 bytes_read = arg_bytes_read ? arg_bytes_read : &my_bytes_read; | |
| 394 bytes_write = arg_bytes_write ? arg_bytes_write : &my_bytes_write; | |
| 395 error = arg_error ? arg_error : NULL; | |
| 396 | |
|
2787
e35538325145
[svn] - add some assertions to chardet_to_utf8() to try to trace bad g_convert() calls
nenolod
parents:
2559
diff
changeset
|
397 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
|
398 |
| 2313 | 399 #ifdef USE_CHARDET |
| 400 if(cfg.chardet_detector) | |
| 401 det = cfg.chardet_detector; | |
| 402 | |
|
3209
41a91ed36bfa
Use new combined libguess interface.
William Pitcock <nenolod@atheme-project.org>
parents:
3123
diff
changeset
|
403 guess_init(); |
|
41a91ed36bfa
Use new combined libguess interface.
William Pitcock <nenolod@atheme-project.org>
parents:
3123
diff
changeset
|
404 |
| 2313 | 405 if(det){ |
|
3209
41a91ed36bfa
Use new combined libguess interface.
William Pitcock <nenolod@atheme-project.org>
parents:
3123
diff
changeset
|
406 encoding = (char *) guess_encoding(str, strlen(str), det); |
|
41a91ed36bfa
Use new combined libguess interface.
William Pitcock <nenolod@atheme-project.org>
parents:
3123
diff
changeset
|
407 if (!encoding) |
| 2313 | 408 goto fallback; |
| 409 | |
| 410 ret = g_convert(str, len, "UTF-8", encoding, bytes_read, bytes_write, error); | |
| 411 } | |
| 412 | |
| 413 fallback: | |
| 414 #endif | |
| 415 if(!ret && cfg.chardet_fallback){ | |
| 416 gchar **encs=NULL, **enc=NULL; | |
| 417 encs = g_strsplit_set(cfg.chardet_fallback, " ,:;|/", 0); | |
| 418 | |
| 419 if(encs){ | |
| 420 enc = encs; | |
| 421 for(enc=encs; *enc ; enc++){ | |
| 422 ret = g_convert(str, len, "UTF-8", *enc, bytes_read, bytes_write, error); | |
| 423 if(len == *bytes_read){ | |
| 424 break; | |
| 425 } | |
| 426 } | |
| 427 g_strfreev(encs); | |
| 428 } | |
| 429 } | |
| 430 | |
| 431 if(!ret){ | |
| 432 ret = g_convert(str, len, "UTF-8", "ISO-8859-1", bytes_read, bytes_write, error); | |
| 433 } | |
| 434 | |
| 435 if(ret){ | |
| 436 if(g_utf8_validate(ret, -1, NULL)) | |
| 437 return ret; | |
| 438 else { | |
| 439 g_free(ret); | |
| 440 ret = NULL; | |
| 441 } | |
| 442 } | |
| 443 | |
| 444 return NULL; /* if I have no idea, return NULL. */ | |
| 445 } |
