Fix memory leak.
[libidn.git] / example.c
blob8e619ab3fe418027b95602af4259fa3f399d725e
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:
8 * ./libtool cc -o example example.c `pkg-config --cflags --libs libstringprep`
9 */
11 int main(int argc, char *argv[])
13 char buf[BUFSIZ];
14 char *p;
15 int rc, i;
17 printf("Input string encoded as `%s': ",
18 stringprep_locale_charset ());
19 fflush(stdout);
20 fgets(buf, BUFSIZ, stdin);
22 printf("Before locale2utf (length %d): ", strlen(buf));
23 for (i=0; i < strlen(buf); i++)
24 printf("%02x ", buf[i] & 0xFF);
25 printf("\n");
27 p = stringprep_locale_to_utf8 (buf);
28 if (p)
30 strcpy(buf, p);
31 free(p);
33 else
34 printf("Could not convert string to UTF-8, continuing anyway...\n");
36 printf("Before stringprep (length %d): ", strlen(buf));
37 for (i=0; i < strlen(buf); i++)
38 printf("%02x ", buf[i] & 0xFF);
39 printf("\n");
41 rc = stringprep(buf, BUFSIZ, 0, stringprep_nameprep);
42 if (rc != STRINGPREP_OK)
43 printf("Stringprep failed with rc %d...\n", rc);
44 else
46 printf("After stringprep (length %d): ", strlen(buf));
47 for (i=0; i < strlen(buf); i++)
48 printf("%02x ", buf[i] & 0xFF);
49 printf("\n");
52 return 0;