Back to index

glibc  2.9
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  */
00021 
00022 #if 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 LOCALE_WORKS 1
00044 #endif
00045 
00046 #if LOCALE_WORKS
00047 # include <langinfo.h>
00048 # include <locale.h>
00049 #endif
00050 
00051 #ifdef _LIBC
00052 # define stringprep_locale_charset() nl_langinfo (CODESET)
00053 #else
00054 
00074 const char *
00075 stringprep_locale_charset (void)
00076 {
00077   const char *charset = getenv ("CHARSET");      /* flawfinder: ignore */
00078 
00079   if (charset && *charset)
00080     return charset;
00081 
00082 # ifdef LOCALE_WORKS
00083   charset = nl_langinfo (CODESET);
00084 
00085   if (charset && *charset)
00086     return charset;
00087 # endif
00088 
00089   return "ASCII";
00090 }
00091 #endif
00092 
00105 char *
00106 stringprep_convert (const char *str,
00107                   const char *to_codeset, const char *from_codeset)
00108 {
00109 #if HAVE_ICONV
00110   return iconv_string (str, from_codeset, to_codeset);
00111 #else
00112   char *p;
00113   fprintf (stderr, "libidn: warning: libiconv not installed, cannot "
00114           "convert data to UTF-8\n");
00115   p = malloc (strlen (str) + 1);
00116   if (!p)
00117     return NULL;
00118   return strcpy (p, str);
00119 #endif
00120 }
00121 
00132 char *
00133 stringprep_locale_to_utf8 (const char *str)
00134 {
00135   return stringprep_convert (str, "UTF-8", stringprep_locale_charset ());
00136 }
00137 
00148 char *
00149 stringprep_utf8_to_locale (const char *str)
00150 {
00151   return stringprep_convert (str, stringprep_locale_charset (), "UTF-8");
00152 }