Initiate name change for savannah project registration.
[libidn.git] / example.c
blobc9b43b2c21851a1a8b1b897ff53d5cbf3b772741
1 /* example.c Example code showing how to use stringprep().
2 * Copyright (C) 2002 Simon Josefsson
4 * This file is part of Libstringprep.
6 * Libstringprep 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 * Libstringprep 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 Libstringprep; 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 <stringprep_nameprep.h>
28 * Compiling using libtool and pkg-config is recommended:
30 * $ libtool cc -o example example.c `pkg-config --cflags --libs libstringprep`
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, i;
47 printf ("Input string encoded as `%s': ", stringprep_locale_charset ());
48 fflush (stdout);
49 fgets (buf, BUFSIZ, stdin);
51 printf ("Before locale2utf8 (length %d): ", strlen (buf));
52 for (i = 0; i < strlen (buf); i++)
53 printf ("%02x ", buf[i] & 0xFF);
54 printf ("\n");
56 p = stringprep_locale_to_utf8 (buf);
57 if (p)
59 strcpy (buf, p);
60 free (p);
62 else
63 printf ("Could not convert string to UTF-8, continuing anyway...\n");
65 printf ("Before stringprep (length %d): ", strlen (buf));
66 for (i = 0; i < strlen (buf); i++)
67 printf ("%02x ", buf[i] & 0xFF);
68 printf ("\n");
70 rc = stringprep (buf, BUFSIZ, 0, stringprep_nameprep);
71 if (rc != STRINGPREP_OK)
72 printf ("Stringprep failed with rc %d...\n", rc);
73 else
75 printf ("After stringprep (length %d): ", strlen (buf));
76 for (i = 0; i < strlen (buf); i++)
77 printf ("%02x ", buf[i] & 0xFF);
78 printf ("\n");
81 return 0;