Update, sync with libidn.texi.
[libidn.git] / examples / example3.c
bloba6314d22444545840041464fc9a0cfeed1ec90e7
1 /* example3.c --- Example ToASCII() 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_ascii_lz() */
29 * Compiling using libtool and pkg-config is recommended:
31 * $ libtool cc -o example3 example3.c `pkg-config --cflags --libs libidn`
32 * $ ./example3
33 * Input domain encoded as `ISO-8859-1': www.räksmörgåsª.example
34 * Read string (length 23): 77 77 77 2e 72 e4 6b 73 6d f6 72 67 e5 73 aa 2e 65 78 61 6d 70 6c 65
35 * ACE label (length 33): 'www.xn--rksmrgsa-0zap8p.example'
36 * 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
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_ascii_lz (buf, &p, 0);
62 if (rc != IDNA_SUCCESS)
64 printf ("ToASCII() 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;