Added copying conditions.
[libidn.git] / example.c
blob0d877365d75bdfb0e6045a7c84e6da6ae7fc7231
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stringprep_nameprep.h>
6 /*
7 * Compiling using libtool and pkg-config is recommended:
9 * $ libtool cc -o example example.c `pkg-config --cflags --libs libstringprep`
10 * $ ./example
11 * Input string encoded as `ISO-8859-1': ยช
12 * Before locale2utf (length 2): aa 0a
13 * Before stringprep (length 3): c2 aa 0a
14 * After stringprep (length 2): 61 0a
15 * $
19 int
20 main (int argc, char *argv[])
22 char buf[BUFSIZ];
23 char *p;
24 int rc, i;
26 printf ("Input string encoded as `%s': ", stringprep_locale_charset ());
27 fflush (stdout);
28 fgets (buf, BUFSIZ, stdin);
30 printf ("Before locale2utf (length %d): ", strlen (buf));
31 for (i = 0; i < strlen (buf); i++)
32 printf ("%02x ", buf[i] & 0xFF);
33 printf ("\n");
35 p = stringprep_locale_to_utf8 (buf);
36 if (p)
38 strcpy (buf, p);
39 free (p);
41 else
42 printf ("Could not convert string to UTF-8, continuing anyway...\n");
44 printf ("Before stringprep (length %d): ", strlen (buf));
45 for (i = 0; i < strlen (buf); i++)
46 printf ("%02x ", buf[i] & 0xFF);
47 printf ("\n");
49 rc = stringprep (buf, BUFSIZ, 0, stringprep_nameprep);
50 if (rc != STRINGPREP_OK)
51 printf ("Stringprep failed with rc %d...\n", rc);
52 else
54 printf ("After stringprep (length %d): ", strlen (buf));
55 for (i = 0; i < strlen (buf); i++)
56 printf ("%02x ", buf[i] & 0xFF);
57 printf ("\n");
60 return 0;