comparison lib/protocols.c @ 184:65eb40fb4f03

2003-6-16 Brian Masney <masneyb@gftp.org> * lib/gftp.h lib/misc.c lib/options.h lib/protocols.c src/gtk/misc-gtk.c - added remote_charsets option. Whenever a file is read in that is not in UTF-8, it will first attempt to convert it from the local charset, and if that fails, it will try each of the locales in this list. I have no idea if this even works, so if someone can test this, please let me know.
author masneyb
date Tue, 17 Jun 2003 02:02:42 +0000
parents 33b394ebba68
children cc94bd62d290
comparison
equal deleted inserted replaced
183:9d42809f9fb4 184:65eb40fb4f03
280 g_free (request->last_dir_entry); 280 g_free (request->last_dir_entry);
281 request->last_dir_entry = NULL; 281 request->last_dir_entry = NULL;
282 request->last_dir_entry_len = 0; 282 request->last_dir_entry_len = 0;
283 } 283 }
284 284
285 if (request->iconv_initialized)
286 {
287 g_iconv_close (request->iconv);
288 request->iconv_initialized = 0;
289 }
290
285 return (ret); 291 return (ret);
286 } 292 }
287 293
288 294
289 int 295 int
326 return (GFTP_EFATAL); 332 return (GFTP_EFATAL);
327 return (request->list_files (request)); 333 return (request->list_files (request));
328 } 334 }
329 335
330 336
337 #if GLIB_MAJOR_VERSION > 1
338 static char *
339 _gftp_get_next_charset (char *remote_charsets, char **curpos)
340 {
341 char *ret, *endpos;
342
343 if (**curpos == '\0')
344 return (NULL);
345
346 ret = *curpos;
347 if (*curpos != remote_charsets)
348 *(*curpos - 1) = '\0';
349
350 if ((endpos = strchr (*curpos, ' ')) == NULL)
351 *curpos += strlen (*curpos);
352 else
353 {
354 *endpos = '\0';
355 *curpos = endpos + 1;
356 }
357
358 return (ret);
359 }
360
361
362 static char *
363 gftp_string_to_utf8 (gftp_request * request, char *str)
364 {
365 char *ret, *remote_charsets, *stpos, *cur_charset;
366 gsize bread, bwrite;
367 GError * error;
368
369 if (request->iconv_initialized)
370 return (g_convert_with_iconv (str, -1, request->iconv, &bread, &bwrite,
371 &error));
372
373 gftp_lookup_request_option (request, "remote_charsets", &remote_charsets);
374 if (*remote_charsets == '\0')
375 {
376 error = NULL;
377 if ((ret = g_locale_to_utf8 (str, -1, &bread, &bwrite, &error)) != NULL)
378 return (ret);
379
380 return (NULL);
381 }
382
383 ret = NULL;
384 stpos = remote_charsets;
385 while ((cur_charset = _gftp_get_next_charset (remote_charsets,
386 &stpos)) != NULL)
387 {
388 if ((request->iconv = g_iconv_open ("UTF-8", cur_charset)) == (GIConv) -1)
389 continue;
390
391 error = NULL;
392 if ((ret = g_convert_with_iconv (str, -1, request->iconv, &bread, &bwrite,
393 &error)) == NULL)
394 {
395 g_iconv_close (request->iconv);
396 request->iconv = NULL;
397 continue;
398 }
399 else
400 {
401 request->iconv_initialized = 1;
402 break;
403 }
404 }
405
406 return (ret);
407 }
408 #endif
409
410
331 int 411 int
332 gftp_get_next_file (gftp_request * request, char *filespec, gftp_file * fle) 412 gftp_get_next_file (gftp_request * request, char *filespec, gftp_file * fle)
333 { 413 {
334 int fd, ret; 414 int fd, ret;
335 #if GLIB_MAJOR_VERSION > 1
336 gsize bread, bwrite;
337 char *tempstr;
338 GError * error;
339 #endif
340 415
341 g_return_val_if_fail (request != NULL, GFTP_EFATAL); 416 g_return_val_if_fail (request != NULL, GFTP_EFATAL);
342 417
343 if (request->get_next_file == NULL) 418 if (request->get_next_file == NULL)
344 return (GFTP_EFATAL); 419 return (GFTP_EFATAL);
354 gftp_file_destroy (fle); 429 gftp_file_destroy (fle);
355 ret = request->get_next_file (request, fle, fd); 430 ret = request->get_next_file (request, fle, fd);
356 431
357 #if GLIB_MAJOR_VERSION > 1 432 #if GLIB_MAJOR_VERSION > 1
358 if (ret >= 0 && fle->file != NULL && !g_utf8_validate (fle->file, -1, NULL)) 433 if (ret >= 0 && fle->file != NULL && !g_utf8_validate (fle->file, -1, NULL))
359 { 434 fle->utf8_file = gftp_string_to_utf8 (request, fle->file);
360 error = NULL;
361 if ((tempstr = g_locale_to_utf8 (fle->file, -1, &bread,
362 &bwrite, &error)) != NULL)
363 {
364 g_free (fle->file);
365 fle->file = tempstr;
366 }
367 else
368 g_warning ("Error when converting %s to UTF-8: %s\n", fle->file,
369 error->message);
370 }
371 #endif 435 #endif
372 436
373 if (ret >= 0 && !request->cached && request->cachefd > 0 && 437 if (ret >= 0 && !request->cached && request->cachefd > 0 &&
374 request->last_dir_entry != NULL) 438 request->last_dir_entry != NULL)
375 { 439 {