toutf8.c

Go to the documentation of this file.
00001 /* toutf8.c --- Convert strings from system locale into UTF-8.
00002  * Copyright (C) 2002, 2003, 2004, 2005  Simon Josefsson
00003  *
00004  * This file is part of GNU Libidn.
00005  *
00006  * GNU Libidn is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * GNU Libidn is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with GNU Libidn; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
00019  *
00020  */
00021 
00022 #ifdef HAVE_CONFIG_H
00023 # include "config.h"
00024 #endif
00025 
00026 /* Get prototypes. */
00027 #include "stringprep.h"
00028 
00029 /* Get fprintf. */
00030 #include <stdio.h>
00031 
00032 /* Get getenv. */
00033 #include <stdlib.h>
00034 
00035 /* Get strlen. */
00036 #include <string.h>
00037 
00038 /* Get iconv_string. */
00039 #include "iconvme.h"
00040 
00041 #ifdef _LIBC
00042 # define HAVE_ICONV 1
00043 # define HAVE_LOCALE_H 1
00044 # define HAVE_LANGINFO_CODESET 1
00045 #endif
00046 
00047 #if HAVE_LOCALE_H
00048 # include <locale.h>
00049 #endif
00050 
00051 #if HAVE_LANGINFO_CODESET
00052 # include <langinfo.h>
00053 #endif
00054 
00055 #ifdef _LIBC
00056 # define stringprep_locale_charset() nl_langinfo (CODESET)
00057 #else
00058 
00078 const char *
00079 stringprep_locale_charset (void)
00080 {
00081   const char *charset = getenv ("CHARSET");     /* flawfinder: ignore */
00082 
00083   if (charset && *charset)
00084     return charset;
00085 
00086 # ifdef HAVE_LANGINFO_CODESET
00087   charset = nl_langinfo (CODESET);
00088 
00089   if (charset && *charset)
00090     return charset;
00091 # endif
00092 
00093   return "ASCII";
00094 }
00095 #endif
00096 
00109 char *
00110 stringprep_convert (const char *str,
00111                     const char *to_codeset, const char *from_codeset)
00112 {
00113 #if HAVE_ICONV
00114   return iconv_string (str, from_codeset, to_codeset);
00115 #else
00116   char *p;
00117   fprintf (stderr, "libidn: warning: libiconv not installed, cannot "
00118            "convert data to UTF-8\n");
00119   p = malloc (strlen (str) + 1);
00120   if (!p)
00121     return NULL;
00122   return strcpy (p, str);
00123 #endif
00124 }
00125 
00136 char *
00137 stringprep_locale_to_utf8 (const char *str)
00138 {
00139   return stringprep_convert (str, "UTF-8", stringprep_locale_charset ());
00140 }
00141 
00152 char *
00153 stringprep_utf8_to_locale (const char *str)
00154 {
00155   return stringprep_convert (str, stringprep_locale_charset (), "UTF-8");
00156 }

Generated on Wed Sep 13 10:20:31 2006 for libidn by  doxygen 1.4.7