Mercurial > gftp.yaz
comparison lib/charset-conv.c @ 989:c99b134c6185
[mq]: charset2
author | Yoshiki Yazawa <yaz@honeyplanet.jp> |
---|---|
date | Mon, 01 Feb 2010 15:17:58 +0900 |
parents | 63555c9744c2 |
children |
comparison
equal
deleted
inserted
replaced
988:63555c9744c2 | 989:c99b134c6185 |
---|---|
17 /* along with this program; if not, write to the Free Software */ | 17 /* along with this program; if not, write to the Free Software */ |
18 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA */ | 18 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA */ |
19 /*****************************************************************************/ | 19 /*****************************************************************************/ |
20 | 20 |
21 #include "gftp.h" | 21 #include "gftp.h" |
22 #include <langinfo.h> | |
22 static const char cvsid[] = "$Id: protocols.c 952 2008-01-24 23:31:26Z masneyb $"; | 23 static const char cvsid[] = "$Id: protocols.c 952 2008-01-24 23:31:26Z masneyb $"; |
23 | |
24 #if GLIB_MAJOR_VERSION > 1 | |
25 | 24 |
26 static /*@null@*/ char * | 25 static /*@null@*/ char * |
27 _gftp_get_next_charset (char **curpos) | 26 _gftp_get_next_charset (char **curpos) |
28 { | 27 { |
29 char *ret, *endpos; | 28 char *ret, *endpos; |
72 | 71 |
73 printf (_("Error converting string '%s' from character set %s to character set %s: %s\n"), | 72 printf (_("Error converting string '%s' from character set %s to character set %s: %s\n"), |
74 str, fromset, toset, error->message); | 73 str, fromset, toset, error->message); |
75 } | 74 } |
76 | 75 |
77 /*@null@*/ static char * | 76 |
78 _do_convert_string (gftp_request * request, int is_filename, int force_local, | 77 char * |
79 const char *str, size_t *dest_len, int from_utf8) | 78 gftp_string_to_utf8 (gftp_request *request, const char *str, size_t *dest_len) |
80 { | 79 { |
81 char *remote_charset = NULL, *default_charset = NULL, *ret, *fromset, | 80 gchar *remote_charset = NULL, *default_charset = NULL, *ret; |
82 *toset, *stpos, *cur_charset; | 81 GError *error = NULL; |
83 GError * error; | 82 |
84 gsize bread; | 83 gftp_lookup_global_option ("default_charset", &default_charset); |
85 | 84 if(!default_charset) |
86 if (request == NULL) | 85 default_charset = "UTF-8"; |
87 return (NULL); | 86 |
88 | 87 if(request) |
89 if (g_utf8_validate (str, -1, NULL) != from_utf8) | 88 remote_charset = request->remote_charset; |
90 return (NULL); | 89 if(!remote_charset) |
91 | 90 remote_charset = default_charset; |
92 error = NULL; | 91 |
93 // gftp_lookup_request_option (request, "remote_charset", &remote_charset); | 92 ret = g_convert_with_fallback(str, -1, "UTF-8", remote_charset, "?", NULL, dest_len, &error); |
94 remote_charset = request->remote_charset; | 93 |
95 gftp_lookup_global_option ("default_charset", &default_charset); | 94 return ret; |
96 if(!remote_charset) | 95 } |
97 remote_charset = (default_charset && *default_charset != '\0') | 96 |
98 ? default_charset : "UTF8"; | 97 |
99 | 98 char * |
100 if (*remote_charset == '\0' || request->use_local_encoding || | 99 gftp_string_from_utf8 (gftp_request *request, int force_local, const char *str, |
101 force_local == 1) | |
102 { | |
103 if (from_utf8) | |
104 { | |
105 if (is_filename) | |
106 ret = g_filename_from_utf8 (str, -1, &bread, dest_len, &error); | |
107 else | |
108 ret = g_locale_from_utf8 (str, -1, &bread, dest_len, &error); | |
109 } | |
110 else | |
111 { | |
112 if (is_filename) | |
113 ret = g_filename_to_utf8 (str, -1, &bread, dest_len, &error); | |
114 else | |
115 ret = g_locale_to_utf8 (str, -1, &bread, dest_len, &error); | |
116 } | |
117 | |
118 if (ret == NULL) | |
119 _do_show_iconv_error (str, request->iconv_charset, from_utf8, error); | |
120 | |
121 return (ret); | |
122 } | |
123 | |
124 if (from_utf8) | |
125 { | |
126 if (request->iconv_from_initialized) | |
127 { | |
128 ret = g_convert_with_iconv (str, -1, request->iconv_from, &bread, dest_len, | |
129 &error); | |
130 if (ret == NULL) | |
131 _do_show_iconv_error (str, request->iconv_charset, from_utf8, error); | |
132 | |
133 return (ret); | |
134 } | |
135 } | |
136 else | |
137 { | |
138 if (request->iconv_to_initialized) | |
139 { | |
140 ret = g_convert_with_iconv (str, -1, request->iconv_to, &bread, dest_len, | |
141 &error); | |
142 if (ret == NULL) | |
143 _do_show_iconv_error (str, request->iconv_charset, from_utf8, error); | |
144 | |
145 return (ret); | |
146 } | |
147 } | |
148 | |
149 stpos = remote_charset; | |
150 while ((cur_charset = _gftp_get_next_charset (&stpos)) != NULL) | |
151 { | |
152 if (from_utf8) | |
153 { | |
154 fromset = "UTF-8"; | |
155 toset = cur_charset; | |
156 if ((request->iconv_from = g_iconv_open (toset, fromset)) == (GIConv) -1) | |
157 { | |
158 g_free (cur_charset); | |
159 continue; | |
160 } | |
161 | |
162 error = NULL; | |
163 if ((ret = g_convert_with_iconv (str, -1, request->iconv_from, &bread, | |
164 dest_len, &error)) == NULL) | |
165 { | |
166 g_iconv_close (request->iconv_from); | |
167 request->iconv_from = NULL; | |
168 _do_show_iconv_error (str, cur_charset, from_utf8, error); | |
169 g_free (cur_charset); | |
170 continue; | |
171 } | |
172 | |
173 request->iconv_from_initialized = 1; | |
174 } | |
175 else | |
176 { | |
177 fromset = cur_charset; | |
178 toset = "UTF-8"; | |
179 if ((request->iconv_to = g_iconv_open (toset, fromset)) == (GIConv) -1) | |
180 { | |
181 g_free (cur_charset); | |
182 continue; | |
183 } | |
184 | |
185 error = NULL; | |
186 if ((ret = g_convert_with_iconv (str, -1, request->iconv_to, &bread, | |
187 dest_len, &error)) == NULL) | |
188 { | |
189 g_iconv_close (request->iconv_to); | |
190 request->iconv_to = NULL; | |
191 _do_show_iconv_error (str, cur_charset, from_utf8, error); | |
192 g_free (cur_charset); | |
193 continue; | |
194 } | |
195 | |
196 request->iconv_to_initialized = 1; | |
197 } | |
198 | |
199 request->iconv_charset = cur_charset; | |
200 return (ret); | |
201 } | |
202 | |
203 return (NULL); | |
204 } | |
205 | |
206 | |
207 char * | |
208 gftp_string_to_utf8 (gftp_request * request, const char *str, size_t *dest_len) | |
209 { | |
210 return (_do_convert_string (request, 0, 0, str, dest_len, 0)); | |
211 } | |
212 | |
213 | |
214 char * | |
215 gftp_string_from_utf8 (gftp_request * request, int force_local, const char *str, | |
216 size_t *dest_len) | 100 size_t *dest_len) |
217 { | 101 { |
218 return (_do_convert_string (request, 0, force_local, str, dest_len, 1)); | 102 gchar *remote_charset = NULL, *default_charset = NULL, *ret; |
219 } | 103 GError *error = NULL; |
220 | 104 |
221 | 105 gftp_lookup_global_option ("default_charset", &default_charset); |
222 char * | 106 if(!default_charset) |
223 gftp_filename_to_utf8 (gftp_request * request, const char *str, | 107 default_charset = "UTF-8"; |
108 | |
109 if(request) | |
110 remote_charset = request->remote_charset; | |
111 if(!remote_charset) | |
112 remote_charset = default_charset; | |
113 | |
114 ret = g_convert_with_fallback(str, -1, remote_charset, "UTF-8", "?", NULL, dest_len, &error); | |
115 | |
116 return ret; | |
117 } | |
118 | |
119 | |
120 char * | |
121 gftp_filename_to_utf8 (gftp_request *request, const char *str, | |
224 size_t *dest_len) | 122 size_t *dest_len) |
225 { | 123 { |
226 return (_do_convert_string (request, 1, 0, str, dest_len, 0)); | 124 char *codeset = nl_langinfo(CODESET); |
227 } | 125 gchar *ret; |
228 | 126 GError *error = NULL; |
229 | 127 |
230 char * | 128 ret = g_filename_to_utf8 (str, -1, NULL, dest_len, &error); |
231 gftp_filename_from_utf8 (gftp_request * request, const char *str, | 129 if(!ret) { |
130 error = NULL; | |
131 ret = g_convert_with_fallback(str, -1, "UTF-8", codeset, "?", NULL, dest_len, &error); | |
132 } | |
133 | |
134 if(!ret) { | |
135 error = NULL; | |
136 ret = g_strdup(str); | |
137 } | |
138 | |
139 return ret; | |
140 } | |
141 | |
142 | |
143 char * | |
144 gftp_filename_from_utf8 (gftp_request *request, const char *str, | |
232 size_t *dest_len) | 145 size_t *dest_len) |
233 { | 146 { |
234 return (_do_convert_string (request, 1, 0, str, dest_len, 1)); | 147 char *codeset = nl_langinfo(CODESET); |
235 } | 148 gchar *ret; |
236 | 149 GError *error = NULL; |
237 | 150 |
238 char * | 151 ret = g_filename_from_utf8 (str, -1, NULL, dest_len, &error); |
239 gftp_remote_filename_to_utf8 (gftp_request * request, const char *str, | 152 if(!ret) { |
153 error = NULL; | |
154 ret = g_convert_with_fallback(str, -1, codeset, "UTF-8", "?", NULL, dest_len, &error); | |
155 } | |
156 | |
157 return ret; | |
158 } | |
159 | |
160 | |
161 char * | |
162 gftp_remote_filename_to_utf8 (gftp_request *request, const char *str, | |
240 size_t *dest_len) | 163 size_t *dest_len) |
241 { | 164 { |
242 char *remote_charset = NULL, *default_charset = NULL; | 165 char *remote_charset = NULL, *default_charset = NULL; |
243 GError *error = NULL; | 166 gchar *ret; |
244 gchar *ret = NULL; | 167 GError *error = NULL; |
245 | 168 |
246 if(request == NULL) | 169 if(request == NULL) |
247 return (NULL); | 170 return (NULL); |
248 | 171 |
249 /* get remote_charset */ | 172 /* get remote_charset */ |
250 // gftp_lookup_request_option (request, "remote_charset", &remote_charset); | |
251 remote_charset = request->remote_charset; | 173 remote_charset = request->remote_charset; |
252 gftp_lookup_global_option ("default_charset", &default_charset); | 174 gftp_lookup_global_option ("default_charset", &default_charset); |
253 if(!remote_charset) | 175 if(!remote_charset) |
254 remote_charset = (default_charset && *default_charset != '\0') | 176 remote_charset = (default_charset && *default_charset != '\0') |
255 ? default_charset : "UTF8"; | 177 ? default_charset : "UTF8"; |
256 | 178 |
257 ret = g_convert(str, -1, "UTF8", remote_charset, NULL, dest_len, error); | 179 ret = g_convert(str, -1, "UTF8", remote_charset, NULL, dest_len, &error); |
258 return ret; | 180 return ret; |
259 } | 181 } |
260 | 182 |
261 | 183 |
262 char * | 184 char * |
263 gftp_remote_filename_from_utf8 (gftp_request *request, const char *str, | 185 gftp_remote_filename_from_utf8 (gftp_request *request, const char *str, |
264 size_t *dest_len) | 186 size_t *dest_len) |
265 { | 187 { |
266 char *remote_charset = NULL, *default_charset = NULL; | 188 char *remote_charset = NULL, *default_charset = NULL; |
267 GError *error = NULL; | 189 gchar *ret; |
268 gchar *ret = NULL; | 190 GError *error = NULL; |
269 | 191 |
270 if(request == NULL) | 192 if(request == NULL) |
271 return (NULL); | 193 return (NULL); |
272 | 194 |
273 if(!g_utf8_validate (str, -1, NULL)) | 195 if(!g_utf8_validate (str, -1, NULL)) |
274 return (NULL); | 196 return (NULL); |
275 | 197 |
276 /* get remote_charset */ | 198 /* get remote_charset */ |
277 // gftp_lookup_request_option (request, "remote_charset", &remote_charset); | |
278 remote_charset = request->remote_charset; | 199 remote_charset = request->remote_charset; |
279 gftp_lookup_global_option ("default_charset", &default_charset); | 200 gftp_lookup_global_option ("default_charset", &default_charset); |
280 if(!remote_charset) | 201 if(!remote_charset) |
281 remote_charset = (default_charset && *default_charset != '\0') | 202 remote_charset = (default_charset && *default_charset != '\0') |
282 ? default_charset : "UTF8"; | 203 ? default_charset : "UTF8"; |
283 | 204 |
284 ret = g_convert(str, -1, remote_charset, "UTF8", NULL, dest_len, error); | 205 ret = g_convert(str, -1, remote_charset, "UTF8", NULL, dest_len, &error); |
285 return ret; | 206 return ret; |
286 } | 207 } |
287 | |
288 #else | |
289 | |
290 char * | |
291 gftp_string_to_utf8 (gftp_request * request, const char *str, size_t dest_len) | |
292 { | |
293 return (NULL); | |
294 } | |
295 | |
296 | |
297 char * | |
298 gftp_string_from_utf8 (gftp_request * request, int force_local, const char *str, | |
299 size_t dest_len) | |
300 { | |
301 return (NULL); | |
302 } | |
303 | |
304 | |
305 char * | |
306 gftp_filename_to_utf8 (gftp_request * request, const char *str, size_t dest_len) | |
307 { | |
308 return (NULL); | |
309 } | |
310 | |
311 | |
312 char * | |
313 gftp_filename_from_utf8 (gftp_request * request, int force_local, | |
314 const char *str, size_t dest_len) | |
315 { | |
316 return (NULL); | |
317 } | |
318 | |
319 char * | |
320 gftp_remote_filename_to_utf8 (gftp_request * request, const char *str, size_t dest_len) | |
321 { | |
322 return (NULL); | |
323 } | |
324 | |
325 | |
326 char * | |
327 gftp_remote_filename_from_utf8 (gftp_request * request, int force_local, | |
328 const char *str, size_t dest_len) | |
329 { | |
330 return (NULL); | |
331 } | |
332 | |
333 #endif | |
334 | |
335 |