Add.
[libidn.git] / examples / example.c
blob7cb1f46df21ab9140eb2ddb55a09575808df1f7b
1 /* example.c --- Example code showing how to use stringprep().
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>
29 * Compiling using libtool and pkg-config is recommended:
31 * $ libtool cc -o example example.c `pkg-config --cflags --libs libidn`
32 * $ ./example
33 * Input string encoded as `ISO-8859-1': ยช
34 * Before locale2utf8 (length 2): aa 0a
35 * Before stringprep (length 3): c2 aa 0a
36 * After stringprep (length 2): 61 0a
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 string encoded as `%s': ", stringprep_locale_charset ());
52 fflush (stdout);
53 fgets (buf, BUFSIZ, stdin);
55 printf ("Before locale2utf8 (length %d): ", strlen (buf));
56 for (i = 0; i < strlen (buf); i++)
57 printf ("%02x ", buf[i] & 0xFF);
58 printf ("\n");
60 p = stringprep_locale_to_utf8 (buf);
61 if (p)
63 strcpy (buf, p);
64 free (p);
66 else
67 printf ("Could not convert string to UTF-8, continuing anyway...\n");
69 printf ("Before stringprep (length %d): ", strlen (buf));
70 for (i = 0; i < strlen (buf); i++)
71 printf ("%02x ", buf[i] & 0xFF);
72 printf ("\n");
74 rc = stringprep (buf, BUFSIZ, 0, stringprep_nameprep);
75 if (rc != STRINGPREP_OK)
76 printf ("Stringprep failed (%d): %s\n", rc, stringprep_strerror (rc));
77 else
79 printf ("After stringprep (length %d): ", strlen (buf));
80 for (i = 0; i < strlen (buf); i++)
81 printf ("%02x ", buf[i] & 0xFF);
82 printf ("\n");
85 return 0;