Move.
[libidn.git] / examples / example3.c
blob0c285af537338bf0fa1dfb50c562e2f1e15adeef
1 /* example3.c Example ToASCII() code showing how to use Libidn.
2 * Copyright (C) 2002, 2003, 2004 Simon Josefsson
4 * This file is part of GNU Libidn.
6 * GNU Libidn is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * GNU Libidn 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with GNU Libidn; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <locale.h> /* setlocale() */
26 #include <stringprep.h> /* stringprep_locale_charset() */
27 #include <idna.h> /* idna_to_ascii_lz() */
30 * Compiling using libtool and pkg-config is recommended:
32 * $ libtool cc -o example3 example3.c `pkg-config --cflags --libs libidn`
33 * $ ./example3
34 * Input domain encoded as `ISO-8859-1': www.räksmörgåsª.example
35 * 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
36 * ACE label (length 33): 'www.xn--rksmrgsa-0zap8p.example'
37 * 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
38 * $
42 int
43 main (int argc, char *argv[])
45 char buf[BUFSIZ];
46 char *p;
47 int rc;
48 size_t i;
50 setlocale (LC_ALL, "");
52 printf ("Input domain encoded as `%s': ", stringprep_locale_charset ());
53 fflush (stdout);
54 fgets (buf, BUFSIZ, stdin);
55 buf[strlen (buf) - 1] = '\0';
57 printf ("Read string (length %d): ", strlen (buf));
58 for (i = 0; i < strlen (buf); i++)
59 printf ("%02x ", buf[i] & 0xFF);
60 printf ("\n");
62 rc = idna_to_ascii_lz (buf, &p, 0);
63 if (rc != IDNA_SUCCESS)
65 printf ("ToASCII() failed... %d\n", rc);
66 exit (1);
69 printf ("ACE label (length %d): '%s'\n", strlen (p), p);
70 for (i = 0; i < strlen (p); i++)
71 printf ("%02x ", p[i] & 0xFF);
72 printf ("\n");
74 free (p);
76 return 0;