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