bug-iconv-trans.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include <iconv.h>
  2. #include <locale.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. int
  6. main (void)
  7. {
  8. iconv_t cd;
  9. const char str[] = "ÄäÖöÜüß";
  10. const char expected[] = "AEaeOEoeUEuess";
  11. char *inptr = (char *) str;
  12. size_t inlen = strlen (str) + 1;
  13. char outbuf[500];
  14. char *outptr = outbuf;
  15. size_t outlen = sizeof (outbuf);
  16. int result = 0;
  17. size_t n;
  18. if (setlocale (LC_ALL, "de_DE.UTF-8") == NULL)
  19. {
  20. puts ("setlocale failed");
  21. return 1;
  22. }
  23. cd = iconv_open ("ANSI_X3.4-1968//TRANSLIT", "ISO-8859-1");
  24. if (cd == (iconv_t) -1)
  25. {
  26. puts ("iconv_open failed");
  27. return 1;
  28. }
  29. n = iconv (cd, &inptr, &inlen, &outptr, &outlen);
  30. if (n != 7)
  31. {
  32. if (n == (size_t) -1)
  33. printf ("iconv() returned error: %m\n");
  34. else
  35. printf ("iconv() returned %Zd, expected 7\n", n);
  36. result = 1;
  37. }
  38. if (inlen != 0)
  39. {
  40. puts ("not all input consumed");
  41. result = 1;
  42. }
  43. else if (inptr - str != strlen (str) + 1)
  44. {
  45. printf ("inptr wrong, advanced by %td\n", inptr - str);
  46. result = 1;
  47. }
  48. if (memcmp (outbuf, expected, sizeof (expected)) != 0)
  49. {
  50. printf ("result wrong: \"%.*s\", expected: \"%s\"\n",
  51. (int) (sizeof (outbuf) - outlen), outbuf, expected);
  52. result = 1;
  53. }
  54. else if (outlen != sizeof (outbuf) - sizeof (expected))
  55. {
  56. printf ("outlen wrong: %Zd, expected %Zd\n", outlen,
  57. sizeof (outbuf) - 15);
  58. result = 1;
  59. }
  60. else
  61. printf ("output is \"%s\" which is OK\n", outbuf);
  62. return result;
  63. }