comparison lib/protocols.c @ 842:0d6fba16c7db

2006-11-2 Brian Masney <masneyb@gftp.org> * lib/gftp.h lib/protocols.c - added _do_convert_string(), which is the common code that was in gftp_string_to_utf8() and gftp_string_from_utf8().
author masneyb
date Fri, 03 Nov 2006 02:01:16 +0000
parents 57aad6d4b304
children 8263cc35c027
comparison
equal deleted inserted replaced
841:94f87e3c92b2 842:0d6fba16c7db
159 #if GLIB_MAJOR_VERSION > 1 159 #if GLIB_MAJOR_VERSION > 1
160 if (request->iconv_initialized) 160 if (request->iconv_initialized)
161 { 161 {
162 g_iconv_close (request->iconv); 162 g_iconv_close (request->iconv);
163 request->iconv_initialized = 0; 163 request->iconv_initialized = 0;
164 g_free (request->iconv_charset);
165 request->iconv_charset = NULL;
164 } 166 }
165 #endif 167 #endif
166 168
167 request->cached = 0; 169 request->cached = 0;
168 if (request->disconnect == NULL) 170 if (request->disconnect == NULL)
412 414
413 static /*@null@*/ char * 415 static /*@null@*/ char *
414 _gftp_get_next_charset (char **curpos) 416 _gftp_get_next_charset (char **curpos)
415 { 417 {
416 char *ret, *endpos; 418 char *ret, *endpos;
419 size_t len, retlen;
417 420
418 if (**curpos == '\0') 421 if (**curpos == '\0')
419 return (NULL); 422 return (NULL);
420 423
421 ret = *curpos; 424 for (; **curpos == ' ' || **curpos == '\t'; (*curpos)++);
425
422 if ((endpos = strchr (*curpos, ',')) == NULL) 426 if ((endpos = strchr (*curpos, ',')) == NULL)
423 *curpos += strlen (*curpos); 427 len = strlen (*curpos);
424 else 428 else
425 { 429 len = endpos - *curpos + 1;
426 *endpos = '\0'; 430
427 *curpos = endpos + 1; 431 for (retlen = len - 1;
428 } 432 (*curpos)[retlen - 1] == ' ' || (*curpos)[retlen - 1] == '\t';
433 retlen--);
434
435 ret = g_malloc0 (retlen + 1);
436 memcpy (ret, *curpos, retlen);
437
438 for (*curpos += len; **curpos == ','; (*curpos)++);
429 439
430 return (ret); 440 return (ret);
431 } 441 }
432 442
433 443
444 static void
445 _do_show_iconv_error (const char *str, char *charset, int from_utf8,
446 GError * error)
447 {
448 const char *fromset, *toset;
449
450 if (from_utf8)
451 {
452 fromset = "UTF-8";
453 toset = charset;
454 }
455 else
456 {
457 fromset = charset;
458 toset = "UTF-8";
459 }
460
461 printf (_("Error converting string '%s' from character set %s to character set %s: %s\n"),
462 str, fromset, toset, error->message);
463 }
464
465
434 /*@null@*/ char * 466 /*@null@*/ char *
435 gftp_string_to_utf8 (gftp_request * request, const char *str, size_t *dest_len) 467 _do_convert_string (gftp_request * request, const char *str,
436 { 468 size_t *dest_len, int from_utf8)
437 char *ret, *remote_charsets, *stpos, *cur_charset, *tempstr; 469 {
438 GError * error = NULL; 470 char *remote_charsets, *ret, *fromset, *toset, *stpos, *cur_charset;
471 GError * error;
439 gsize bread; 472 gsize bread;
440 473
441 if (request == NULL) 474 if (request == NULL)
442 return (NULL); 475 return (NULL);
443 476
444 if (g_utf8_validate (str, -1, NULL)) 477 if (g_utf8_validate (str, -1, NULL) != from_utf8)
445 return (NULL); 478 return (NULL);
446 else if (request->iconv_initialized) 479
447 { 480 error = NULL;
448 ret = g_convert_with_iconv (str, -1, request->iconv, &bread, dest_len, 481 gftp_lookup_request_option (request, "remote_charsets", &remote_charsets);
482 if (*remote_charsets == '\0' || request->use_local_encoding)
483 {
484 if (from_utf8)
485 ret = g_locale_from_utf8 (str, -1, &bread, dest_len, &error);
486 else
487 ret = g_locale_to_utf8 (str, -1, &bread, dest_len, &error);
488
489 if (ret == NULL)
490 _do_show_iconv_error (str, request->iconv_charset, from_utf8, error);
491
492 return (ret);
493 }
494
495 if (request->iconv_initialized)
496 {
497 ret = g_convert_with_iconv (str, -1, request->iconv, &bread, dest_len,
449 &error); 498 &error);
450 if (ret == NULL) 499 if (ret == NULL)
451 printf (_("Error converting string '%s' from character set %s to character set %s: %s\n"), 500 _do_show_iconv_error (str, request->iconv_charset, from_utf8, error);
452 str, _("<unknown>"), "UTF-8", error->message);
453 501
454 return (ret); 502 return (ret);
455 } 503 }
456 504
457 gftp_lookup_request_option (request, "remote_charsets", &tempstr);
458 if (*tempstr == '\0')
459 {
460 error = NULL;
461 if ((ret = g_locale_to_utf8 (str, -1, &bread, dest_len, &error)) != NULL)
462 return (ret);
463
464 /* Don't use request->logging_function since the strings must be in UTF-8
465 for the GTK+ 2.x port */
466 printf (_("Error converting string '%s' to UTF-8 from current locale: %s\n"),
467 str, error->message);
468 return (NULL);
469 }
470
471 remote_charsets = g_strdup (tempstr);
472 ret = NULL;
473 stpos = remote_charsets; 505 stpos = remote_charsets;
474 while ((cur_charset = _gftp_get_next_charset (&stpos)) != NULL) 506 while ((cur_charset = _gftp_get_next_charset (&stpos)) != NULL)
475 { 507 {
476 if ((request->iconv = g_iconv_open ("UTF-8", cur_charset)) == (GIConv) -1) 508 if (from_utf8)
477 continue; 509 {
510 fromset = "UTF-8";
511 toset = cur_charset;
512 }
513 else
514 {
515 fromset = cur_charset;
516 toset = "UTF-8";
517 }
518
519 if ((request->iconv = g_iconv_open (toset, fromset)) == (GIConv) -1)
520 {
521 g_free (cur_charset);
522 continue;
523 }
478 524
479 error = NULL; 525 error = NULL;
480 if ((ret = g_convert_with_iconv (str, -1, request->iconv, &bread, 526 if ((ret = g_convert_with_iconv (str, -1, request->iconv, &bread,
481 dest_len, &error)) == NULL) 527 dest_len, &error)) == NULL)
482 { 528 {
483 printf (_("Error converting string '%s' from character set %s to character set %s: %s\n"),
484 str, cur_charset, "UTF-8", error->message);
485
486 g_iconv_close (request->iconv); 529 g_iconv_close (request->iconv);
487 request->iconv = NULL; 530 request->iconv = NULL;
488 continue; 531 _do_show_iconv_error (str, cur_charset, from_utf8, error);
489 } 532 g_free (cur_charset);
490 else 533 }
491 { 534
492 request->iconv_initialized = 1; 535 request->iconv_initialized = 1;
493 break; 536 request->iconv_charset = cur_charset;
494 } 537 return (ret);
495 } 538 }
496 539
497 g_free (remote_charsets); 540 return (NULL);
498 541 }
499 return (ret); 542
543 /*@null@*/ char *
544 gftp_string_to_utf8 (gftp_request * request, const char *str, size_t *dest_len)
545 {
546 return (_do_convert_string (request, str, dest_len, 0));
500 } 547 }
501 548
502 549
503 char * 550 char *
504 gftp_string_from_utf8 (gftp_request * request, const char *str, 551 gftp_string_from_utf8 (gftp_request * request, const char *str,
505 size_t *dest_len) 552 size_t *dest_len)
506 { 553 {
507 char *ret, *remote_charsets, *stpos, *cur_charset, *tempstr; 554 return (_do_convert_string (request, str, dest_len, 1));
508 GError * error = NULL;
509 gsize bread;
510
511 if (request == NULL)
512 return (NULL);
513
514 /* FIXME - use request->use_local_encoding */
515
516 /* If the string isn't in UTF-8 format, assume it is already in the current
517 locale... */
518 if (!g_utf8_validate (str, -1, NULL))
519 return (NULL);
520 else if (request->iconv_initialized)
521 {
522 ret = g_convert_with_iconv (str, -1, request->iconv, &bread, dest_len,
523 &error);
524 if (ret == NULL)
525 printf (_("Error converting string '%s' from character set %s to character set %s: %s\n"),
526 str, "UTF-8", _("<unknown>"), error->message);
527
528 return (ret);
529 }
530
531 gftp_lookup_request_option (request, "remote_charsets", &tempstr);
532 if (*tempstr == '\0')
533 {
534 error = NULL;
535 if ((ret = g_locale_from_utf8 (str, -1, &bread, dest_len,
536 &error)) != NULL)
537 return (ret);
538
539 /* Don't use request->logging_function since the strings must be in UTF-8
540 for the GTK+ 2.x port */
541 printf (_("Error converting string '%s' to current locale from UTF-8: %s\n"),
542 str, error->message);
543 return (NULL);
544 }
545
546 remote_charsets = g_strdup (tempstr);
547 ret = NULL;
548 stpos = remote_charsets;
549 while ((cur_charset = _gftp_get_next_charset (&stpos)) != NULL)
550 {
551 if ((request->iconv = g_iconv_open (cur_charset, "UTF-8")) == (GIConv) -1)
552 continue;
553
554 error = NULL;
555 if ((ret = g_convert_with_iconv (str, -1, request->iconv, &bread,
556 dest_len, &error)) == NULL)
557 {
558 printf (_("Error converting string '%s' from character set %s to character set %s: %s\n"),
559 str, "UTF-8", cur_charset, error->message);
560
561 g_iconv_close (request->iconv);
562 request->iconv = NULL;
563 continue;
564 }
565 else
566 {
567 request->iconv_initialized = 1;
568 break;
569 }
570 }
571
572 g_free (remote_charsets);
573
574 return (ret);
575 } 555 }
576 556
577 #else 557 #else
578 558
579 char * 559 char *