comparison lib/protocols.c @ 423:4cee2498761d

2004-3-15 Brian Masney <masneyb@gftp.org> * lib/gftp.h - if _FILE_OFFSET_BITS is > 32 and _LARGEFILE_SOURCE is not defined, define it * lib/protocols.c (gftp_string_{to|from}_utf8 - don't alter the contents of the remote_charsets option. Instead, make a copy of that and alter that copy
author masneyb
date Tue, 16 Mar 2004 02:11:56 +0000
parents 38bfc112ab46
children 1d45758e5cf5
comparison
equal deleted inserted replaced
422:38bfc112ab46 423:4cee2498761d
346 346
347 347
348 #if GLIB_MAJOR_VERSION > 1 348 #if GLIB_MAJOR_VERSION > 1
349 349
350 static char * 350 static char *
351 _gftp_get_next_charset (char *remote_charsets, char *orig_str, char **curpos) 351 _gftp_get_next_charset (char **curpos)
352 { 352 {
353 char *ret, *endpos; 353 char *ret, *endpos;
354 354
355 if (**curpos == '\0') 355 if (**curpos == '\0')
356 return (NULL); 356 return (NULL);
357 357
358 ret = *curpos; 358 ret = *curpos;
359 if (*curpos != remote_charsets)
360 {
361 *orig_str = *(*curpos - 1);
362 *(*curpos - 1) = ',';
363 }
364
365 if ((endpos = strchr (*curpos, ',')) == NULL) 359 if ((endpos = strchr (*curpos, ',')) == NULL)
366 *curpos += strlen (*curpos); 360 *curpos += strlen (*curpos);
367 else 361 else
368 { 362 {
369 *endpos = '\0'; 363 *endpos = '\0';
370 364 *curpos = endpos + 1;
371 if (*orig_str != '\0')
372 *curpos = endpos + 1;
373 else
374 *curpos = endpos;
375 } 365 }
376 366
377 return (ret); 367 return (ret);
378 }
379
380
381 static void
382 _gftp_restore_charset_string (char **remote_charsets, char orig_str, char **curpos)
383 {
384 if (*curpos != *remote_charsets)
385 *(*curpos - 1) = orig_str;
386 } 368 }
387 369
388 370
389 char * 371 char *
390 gftp_string_to_utf8 (gftp_request * request, const char *str) 372 gftp_string_to_utf8 (gftp_request * request, const char *str)
391 { 373 {
392 char *ret, *remote_charsets, *stpos, *cur_charset, orig_str; 374 char *ret, *remote_charsets, *stpos, *cur_charset, *tempstr;
393 gsize bread, bwrite; 375 gsize bread, bwrite;
394 GError * error; 376 GError * error;
395 377
396 if (request == NULL) 378 if (request == NULL)
397 return (NULL); 379 return (NULL);
400 return (g_convert_with_iconv (str, -1, request->iconv, &bread, &bwrite, 382 return (g_convert_with_iconv (str, -1, request->iconv, &bread, &bwrite,
401 &error)); 383 &error));
402 else if (g_utf8_validate (str, -1, NULL)) 384 else if (g_utf8_validate (str, -1, NULL))
403 return (NULL); 385 return (NULL);
404 386
405 gftp_lookup_request_option (request, "remote_charsets", &remote_charsets); 387 gftp_lookup_request_option (request, "remote_charsets", &tempstr);
406 if (*remote_charsets == '\0') 388 if (*tempstr == '\0')
407 { 389 {
408 error = NULL; 390 error = NULL;
409 if ((ret = g_locale_to_utf8 (str, -1, &bread, &bwrite, &error)) != NULL) 391 if ((ret = g_locale_to_utf8 (str, -1, &bread, &bwrite, &error)) != NULL)
410 return (ret); 392 return (ret);
411 393
412 return (NULL); 394 return (NULL);
413 } 395 }
414 396
397 remote_charsets = g_strdup (tempstr);
415 ret = NULL; 398 ret = NULL;
416 stpos = remote_charsets; 399 stpos = remote_charsets;
417 while ((cur_charset = _gftp_get_next_charset (remote_charsets, &orig_str, 400 while ((cur_charset = _gftp_get_next_charset (&stpos)) != NULL)
418 &stpos)) != NULL)
419 { 401 {
420 if ((request->iconv = g_iconv_open ("UTF-8", cur_charset)) == (GIConv) -1) 402 if ((request->iconv = g_iconv_open ("UTF-8", cur_charset)) == (GIConv) -1)
421 continue; 403 continue;
422 404
423 error = NULL; 405 error = NULL;
429 continue; 411 continue;
430 } 412 }
431 else 413 else
432 { 414 {
433 request->iconv_initialized = 1; 415 request->iconv_initialized = 1;
434 _gftp_restore_charset_string (&remote_charsets, *cur_charset, &stpos);
435 break; 416 break;
436 } 417 }
437 } 418 }
419
420 g_free (remote_charsets);
438 421
439 return (ret); 422 return (ret);
440 } 423 }
441 424
442 425
443 char * 426 char *
444 gftp_string_from_utf8 (gftp_request * request, const char *str) 427 gftp_string_from_utf8 (gftp_request * request, const char *str)
445 { 428 {
446 char *ret, *remote_charsets, *stpos, *cur_charset, orig_str; 429 char *ret, *remote_charsets, *stpos, *cur_charset, *tempstr;
447 gsize bread, bwrite; 430 gsize bread, bwrite;
448 GError * error; 431 GError * error;
449 432
450 if (request == NULL) 433 if (request == NULL)
451 return (NULL); 434 return (NULL);
454 return (g_convert_with_iconv (str, -1, request->iconv, &bread, &bwrite, 437 return (g_convert_with_iconv (str, -1, request->iconv, &bread, &bwrite,
455 &error)); 438 &error));
456 else if (g_utf8_validate (str, -1, NULL)) 439 else if (g_utf8_validate (str, -1, NULL))
457 return (NULL); 440 return (NULL);
458 441
459 gftp_lookup_request_option (request, "remote_charsets", &remote_charsets); 442 gftp_lookup_request_option (request, "remote_charsets", &tempstr);
460 if (*remote_charsets == '\0') 443 if (*tempstr == '\0')
461 { 444 {
462 error = NULL; 445 error = NULL;
463 if ((ret = g_locale_from_utf8 (str, -1, &bread, &bwrite, &error)) != NULL) 446 if ((ret = g_locale_from_utf8 (str, -1, &bread, &bwrite, &error)) != NULL)
464 return (ret); 447 return (ret);
465 448
466 return (NULL); 449 return (NULL);
467 } 450 }
468 451
452 remote_charsets = g_strdup (tempstr);
469 ret = NULL; 453 ret = NULL;
470 stpos = remote_charsets; 454 stpos = remote_charsets;
471 while ((cur_charset = _gftp_get_next_charset (remote_charsets, &orig_str, 455 while ((cur_charset = _gftp_get_next_charset (&stpos)) != NULL)
472 &stpos)) != NULL)
473 { 456 {
474 if ((request->iconv = g_iconv_open (cur_charset, "UTF-8")) == (GIConv) -1) 457 if ((request->iconv = g_iconv_open (cur_charset, "UTF-8")) == (GIConv) -1)
475 continue; 458 continue;
476 459
477 error = NULL; 460 error = NULL;
483 continue; 466 continue;
484 } 467 }
485 else 468 else
486 { 469 {
487 request->iconv_initialized = 1; 470 request->iconv_initialized = 1;
488 _gftp_restore_charset_string (&remote_charsets, *cur_charset, &stpos);
489 break; 471 break;
490 } 472 }
491 } 473 }
474
475 g_free (remote_charsets);
492 476
493 return (ret); 477 return (ret);
494 } 478 }
495 479
496 #else 480 #else