libidn: Example 1
1
1 9.1 Example 1
1 =============
1
1 This example demonstrates how the stringprep functions are used.
1
1 /* example.c --- Example code showing how to use stringprep().
1 * Copyright (C) 2002-2016 Simon Josefsson
1 *
1 * This file is part of GNU Libidn.
1 *
1 * This program is free software: you can redistribute it and/or modify
1 * it under the terms of the GNU General Public License as published by
1 * the Free Software Foundation, either version 3 of the License, or
1 * (at your option) any later version.
1 *
1 * This program is distributed in the hope that it will be useful,
1 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1 * GNU General Public License for more details.
1 *
1 * You should have received a copy of the GNU General Public License
1 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1 *
1 */
1
1 #include <stdio.h>
1 #include <stdlib.h>
1 #include <string.h>
1 #include <locale.h> /* setlocale() */
1 #include <stringprep.h>
1
1 /*
1 * Compiling using libtool and pkg-config is recommended:
1 *
1 * $ libtool cc -o example example.c `pkg-config --cflags --libs libidn`
1 * $ ./example
1 * Input string encoded as `ISO-8859-1': ยช
1 * Before locale2utf8 (length 2): aa 0a
1 * Before stringprep (length 3): c2 aa 0a
1 * After stringprep (length 2): 61 0a
1 * $
1 *
1 */
1
1 int
1 main (void)
1 {
1 char buf[BUFSIZ];
1 char *p;
1 int rc;
1 size_t i;
1
1 setlocale (LC_ALL, "");
1
1 printf ("Input string encoded as `%s': ", stringprep_locale_charset ());
1 fflush (stdout);
1 if (!fgets (buf, BUFSIZ, stdin))
1 perror ("fgets");
1 buf[strlen (buf) - 1] = '\0';
1
1 printf ("Before locale2utf8 (length %ld): ", (long int) strlen (buf));
1 for (i = 0; i < strlen (buf); i++)
1 printf ("%02x ", (unsigned) buf[i] & 0xFF);
1 printf ("\n");
1
1 p = stringprep_locale_to_utf8 (buf);
1 if (p)
1 {
1 strcpy (buf, p);
1 free (p);
1 }
1 else
1 printf ("Could not convert string to UTF-8, continuing anyway...\n");
1
1 printf ("Before stringprep (length %ld): ", (long int) strlen (buf));
1 for (i = 0; i < strlen (buf); i++)
1 printf ("%02x ", (unsigned) buf[i] & 0xFF);
1 printf ("\n");
1
1 rc = stringprep (buf, BUFSIZ, 0, stringprep_nameprep);
1 if (rc != STRINGPREP_OK)
1 printf ("Stringprep failed (%d): %s\n", rc, stringprep_strerror (rc));
1 else
1 {
1 printf ("After stringprep (length %ld): ", (long int) strlen (buf));
1 for (i = 0; i < strlen (buf); i++)
1 printf ("%02x ", (unsigned) buf[i] & 0xFF);
1 printf ("\n");
1 }
1
1 return 0;
1 }
1