Sync with TP.
[libidn.git] / examples / example.c
blobec1f5a0b1ab02877ee63d4b8e7883aefa3a0552a
1 /* example.c --- Example code showing how to use stringprep().
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>
28 * Compiling using libtool and pkg-config is recommended:
30 * $ libtool cc -o example example.c `pkg-config --cflags --libs libidn`
31 * $ ./example
32 * Input string encoded as `ISO-8859-1': ยช
33 * Before locale2utf8 (length 2): aa 0a
34 * Before stringprep (length 3): c2 aa 0a
35 * After stringprep (length 2): 61 0a
36 * $
40 int
41 main (int argc, char *argv[])
43 char buf[BUFSIZ];
44 char *p;
45 int rc;
46 size_t i;
48 setlocale (LC_ALL, "");
50 printf ("Input string encoded as `%s': ", stringprep_locale_charset ());
51 fflush (stdout);
52 fgets (buf, BUFSIZ, stdin);
54 printf ("Before locale2utf8 (length %d): ", strlen (buf));
55 for (i = 0; i < strlen (buf); i++)
56 printf ("%02x ", buf[i] & 0xFF);
57 printf ("\n");
59 p = stringprep_locale_to_utf8 (buf);
60 if (p)
62 strcpy (buf, p);
63 free (p);
65 else
66 printf ("Could not convert string to UTF-8, continuing anyway...\n");
68 printf ("Before stringprep (length %d): ", strlen (buf));
69 for (i = 0; i < strlen (buf); i++)
70 printf ("%02x ", buf[i] & 0xFF);
71 printf ("\n");
73 rc = stringprep (buf, BUFSIZ, 0, stringprep_nameprep);
74 if (rc != STRINGPREP_OK)
75 printf ("Stringprep failed (%d): %s\n", rc, stringprep_strerror (rc));
76 else
78 printf ("After stringprep (length %d): ", strlen (buf));
79 for (i = 0; i < strlen (buf); i++)
80 printf ("%02x ", buf[i] & 0xFF);
81 printf ("\n");
84 return 0;