Use AC_LIBTOOL_WIN32_DLL.
[libidn.git] / lib / toutf8.c
blob3301e1e530063a1004e594b8ded0c01c45676548
1 /* toutf8.c --- Convert strings from system locale into UTF-8.
2 * Copyright (C) 2002, 2003, 2004, 2005 Simon Josefsson
4 * This file is part of GNU Libidn.
6 * GNU Libidn is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * GNU Libidn is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with GNU Libidn; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
26 /* Get prototypes. */
27 #include "stringprep.h"
29 /* Get fprintf. */
30 #include <stdio.h>
32 /* Get getenv. */
33 #include <stdlib.h>
35 /* Get strlen. */
36 #include <string.h>
38 /* Get iconv_string. */
39 #include "iconvme.h"
41 #ifdef _LIBC
42 # define HAVE_ICONV 1
43 # define HAVE_LOCALE_H 1
44 # define HAVE_LANGINFO_CODESET 1
45 #endif
47 #if HAVE_LOCALE_H
48 # include <locale.h>
49 #endif
51 #if HAVE_LANGINFO_CODESET
52 # include <langinfo.h>
53 #endif
55 #ifdef _LIBC
56 # define stringprep_locale_charset() nl_langinfo (CODESET)
57 #else
58 /**
59 * stringprep_locale_charset - return charset used in current locale
61 * Find out current locale charset. The function respect the CHARSET
62 * environment variable, but typically uses nl_langinfo(CODESET) when
63 * it is supported. It fall back on "ASCII" if CHARSET isn't set and
64 * nl_langinfo isn't supported or return anything.
66 * Note that this function return the application's locale's preferred
67 * charset (or thread's locale's preffered charset, if your system
68 * support thread-specific locales). It does not return what the
69 * system may be using. Thus, if you receive data from external
70 * sources you cannot in general use this function to guess what
71 * charset it is encoded in. Use stringprep_convert from the external
72 * representation into the charset returned by this function, to have
73 * data in the locale encoding.
75 * Return value: Return the character set used by the current locale.
76 * It will never return NULL, but use "ASCII" as a fallback.
77 **/
78 const char *
79 stringprep_locale_charset (void)
81 const char *charset = getenv ("CHARSET"); /* flawfinder: ignore */
83 if (charset && *charset)
84 return charset;
86 # ifdef HAVE_LANGINFO_CODESET
87 charset = nl_langinfo (CODESET);
89 if (charset && *charset)
90 return charset;
91 # endif
93 return "ASCII";
95 #endif
97 /**
98 * stringprep_convert - encode string using new character set
99 * @str: input zero-terminated string.
100 * @to_codeset: name of destination character set.
101 * @from_codeset: name of origin character set, as used by @str.
103 * Convert the string from one character set to another using the
104 * system's iconv() function.
106 * Return value: Returns newly allocated zero-terminated string which
107 * is @str transcoded into to_codeset.
109 char *
110 stringprep_convert (const char *str,
111 const char *to_codeset, const char *from_codeset)
113 #if HAVE_ICONV
114 return iconv_string (str, from_codeset, to_codeset);
115 #else
116 char *p;
117 fprintf (stderr, "libidn: warning: libiconv not installed, cannot "
118 "convert data to UTF-8\n");
119 p = malloc (strlen (str) + 1);
120 if (!p)
121 return NULL;
122 return strcpy (p, str);
123 #endif
127 * stringprep_locale_to_utf8 - convert locale encoded string to UTF-8
128 * @str: input zero terminated string.
130 * Convert string encoded in the locale's character set into UTF-8 by
131 * using stringprep_convert().
133 * Return value: Returns newly allocated zero-terminated string which
134 * is @str transcoded into UTF-8.
136 char *
137 stringprep_locale_to_utf8 (const char *str)
139 return stringprep_convert (str, "UTF-8", stringprep_locale_charset ());
143 * stringprep_utf8_to_locale - encode UTF-8 string to locale encoding
144 * @str: input zero terminated string.
146 * Convert string encoded in UTF-8 into the locale's character set by
147 * using stringprep_convert().
149 * Return value: Returns newly allocated zero-terminated string which
150 * is @str transcoded into the locale's character set.
152 char *
153 stringprep_utf8_to_locale (const char *str)
155 return stringprep_convert (str, stringprep_locale_charset (), "UTF-8");