improve comment
[libidn.git] / example.c
blob054feb7a54c8809e8adca56e73f81ea895c7bc83
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 main(int argc, char *argv[])
21 char buf[BUFSIZ];
22 char *p;
23 int rc, i;
25 printf("Input string encoded as `%s': ",
26 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;