libidn  1.25
toutf8.c
Go to the documentation of this file.
00001 /* toutf8.c --- Convert strings from system locale into UTF-8.
00002    Copyright (C) 2002-2012 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 either:
00008 
00009      * the GNU Lesser General Public License as published by the Free
00010        Software Foundation; either version 3 of the License, or (at
00011        your option) any later version.
00012 
00013    or
00014 
00015      * the GNU General Public License as published by the Free
00016        Software Foundation; either version 2 of the License, or (at
00017        your option) any later version.
00018 
00019    or both in parallel, as here.
00020 
00021    GNU Libidn is distributed in the hope that it will be useful,
00022    but WITHOUT ANY WARRANTY; without even the implied warranty of
00023    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00024    General Public License for more details.
00025 
00026    You should have received copies of the GNU General Public License and
00027    the GNU Lesser General Public License along with this program.  If
00028    not, see <http://www.gnu.org/licenses/>. */
00029 
00030 #ifdef HAVE_CONFIG_H
00031 # include "config.h"
00032 #endif
00033 
00034 /* Get prototypes. */
00035 #include "stringprep.h"
00036 
00037 /* Get fprintf. */
00038 #include <stdio.h>
00039 
00040 /* Get getenv. */
00041 #include <stdlib.h>
00042 
00043 /* Get strlen. */
00044 #include <string.h>
00045 
00046 /* Get iconv_string. */
00047 #include "striconv.h"
00048 
00049 #ifdef _LIBC
00050 # define HAVE_ICONV 1
00051 # define HAVE_LOCALE_H 1
00052 # define HAVE_LANGINFO_CODESET 1
00053 #endif
00054 
00055 #include <locale.h>
00056 
00057 #ifdef HAVE_LANGINFO_CODESET
00058 # include <langinfo.h>
00059 #endif
00060 
00061 #ifdef _LIBC
00062 # define stringprep_locale_charset() nl_langinfo (CODESET)
00063 #else
00064 
00084 const char *
00085 stringprep_locale_charset (void)
00086 {
00087   const char *charset = getenv ("CHARSET");     /* flawfinder: ignore */
00088 
00089   if (charset && *charset)
00090     return charset;
00091 
00092 # ifdef HAVE_LANGINFO_CODESET
00093   charset = nl_langinfo (CODESET);
00094 
00095   if (charset && *charset)
00096     return charset;
00097 # endif
00098 
00099   return "ASCII";
00100 }
00101 #endif
00102 
00115 char *
00116 stringprep_convert (const char *str,
00117                     const char *to_codeset, const char *from_codeset)
00118 {
00119 #if HAVE_ICONV
00120   return str_iconv (str, from_codeset, to_codeset);
00121 #else
00122   char *p;
00123   fprintf (stderr, "libidn: warning: libiconv not installed, cannot "
00124            "convert data to UTF-8\n");
00125   p = malloc (strlen (str) + 1);
00126   if (!p)
00127     return NULL;
00128   return strcpy (p, str);
00129 #endif
00130 }
00131 
00142 char *
00143 stringprep_locale_to_utf8 (const char *str)
00144 {
00145   return stringprep_convert (str, "UTF-8", stringprep_locale_charset ());
00146 }
00147 
00158 char *
00159 stringprep_utf8_to_locale (const char *str)
00160 {
00161   return stringprep_convert (str, stringprep_locale_charset (), "UTF-8");
00162 }