Sync with TP.
[libidn.git] / examples / example4.c
blobc07673c28802a9036533dab782d51c9836fd5b53
1 /* example4.c --- Example ToUnicode() code showing how to use Libidn.
2 * Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Simon Josefsson
4 * This file is part of GNU Libidn.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <locale.h> /* setlocale() */
25 #include <stringprep.h> /* stringprep_locale_charset() */
26 #include <idna.h> /* idna_to_unicode_lzlz() */
29 * Compiling using libtool and pkg-config is recommended:
31 * $ libtool cc -o example4 example4.c `pkg-config --cflags --libs libidn`
32 * $ ./example4
33 * Input domain encoded as `ISO-8859-1': www.xn--rksmrgsa-0zap8p.example
34 * Read string (length 33): 77 77 77 2e 78 6e 2d 2d 72 6b 73 6d 72 67 73 61 2d 30 7a 61 70 38 70 2e 65 78 61 6d 70 6c 65
35 * ACE label (length 23): 'www.räksmörgåsa.example'
36 * 77 77 77 2e 72 e4 6b 73 6d f6 72 67 e5 73 61 2e 65 78 61 6d 70 6c 65
37 * $
41 int
42 main (int argc, char *argv[])
44 char buf[BUFSIZ];
45 char *p;
46 int rc;
47 size_t i;
49 setlocale (LC_ALL, "");
51 printf ("Input domain encoded as `%s': ", stringprep_locale_charset ());
52 fflush (stdout);
53 fgets (buf, BUFSIZ, stdin);
54 buf[strlen (buf) - 1] = '\0';
56 printf ("Read string (length %d): ", strlen (buf));
57 for (i = 0; i < strlen (buf); i++)
58 printf ("%02x ", buf[i] & 0xFF);
59 printf ("\n");
61 rc = idna_to_unicode_lzlz (buf, &p, 0);
62 if (rc != IDNA_SUCCESS)
64 printf ("ToUnicode() failed (%d): %s\n", rc, idna_strerror (rc));
65 exit (1);
68 printf ("ACE label (length %d): '%s'\n", strlen (p), p);
69 for (i = 0; i < strlen (p); i++)
70 printf ("%02x ", p[i] & 0xFF);
71 printf ("\n");
73 free (p);
75 return 0;