Update.
[glibc.git] / wcsmbs / wcsmbsload.c
bloba8412e3bf6c9f1897d8d1d99e6ff5c0e2bd551e9
1 /* Copyright (C) 1998 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <langinfo.h>
21 #include <limits.h>
22 #include <stdlib.h>
23 #include <string.h>
25 #include <locale/localeinfo.h>
26 #include <wcsmbsload.h>
27 #include <bits/libc-lock.h>
28 #include <iconv/gconv_int.h>
31 /* Last loaded locale for LC_CTYPE. We initialize for the C locale
32 which is enabled at startup. */
33 extern const struct locale_data _nl_C_LC_CTYPE;
34 const struct locale_data *__wcsmbs_last_locale = &_nl_C_LC_CTYPE;
37 /* These are the descriptions for the default conversion functions. */
38 static struct gconv_step to_wc =
40 shlib_handle: NULL,
41 modname: NULL,
42 counter: INT_MAX,
43 from_name: "ANSI_X3.4-1968//",
44 to_name: "INTERNAL",
45 fct: __gconv_transform_ascii_internal,
46 init_fct: NULL,
47 end_fct: NULL,
48 data: NULL
51 static struct gconv_step to_mb =
53 shlib_handle: NULL,
54 modname: NULL,
55 counter: INT_MAX,
56 from_name: "INTERNAL",
57 to_name: "ANSI_X3.4-1968//",
58 fct: __gconv_transform_internal_ascii,
59 init_fct: NULL,
60 end_fct: NULL,
61 data: NULL
65 /* For the default locale we only have to handle ANSI_X3.4-1968. */
66 struct gconv_fcts __wcsmbs_gconv_fcts =
68 towc: &to_wc,
69 tomb: &to_mb
73 static inline struct gconv_step *
74 getfct (const char *to, const char *from)
76 size_t nsteps;
77 struct gconv_step *result;
79 if (__gconv_find_transform (to, from, &result, &nsteps) != GCONV_OK)
80 /* Loading the conversion step is not possible. */
81 return NULL;
83 /* We must only have one step in this conversion. */
84 if (nsteps != 1)
85 return NULL;
87 return result;
91 /* Extract from the given locale name the character set portion. Since
92 only the XPG form of the name includes this information we don't have
93 to take care for the CEN form. */
94 #define extract_charset_name(str) \
95 ({ \
96 const char *cp = str; \
97 char *result = NULL; \
99 cp += strcspn (cp, "@.+,"); \
100 if (*cp == '.') \
102 const char *endp = ++cp; \
103 while (*endp != '\0' && *endp != '@') \
104 ++endp; \
105 if (endp != cp) \
106 result = strndupa (cp, endp - cp); \
108 result; \
112 /* The gconv functions expects the name to be complete, including the
113 trailing shashes if necessary. */
114 #define add_slashes(str) \
115 ({ \
116 const char *cp = str; \
117 char *result; \
118 char *tmp; \
119 size_t cnt = 0; \
121 while (*cp != '\0') \
122 if (*cp++ == '/') \
123 ++cnt; \
125 result = alloca (cp - str + 3); \
126 tmp = __mempcpy (result, str, cp - str); \
127 if (cnt < 2) \
129 *tmp++ = '/'; \
130 if (cnt < 1) \
131 *tmp++ = '/'; \
133 *tmp = '\0'; \
134 result; \
138 /* Load conversion functions for the currently selected locale. */
139 void
140 internal_function
141 __wcsmbs_load_conv (const struct locale_data *new_category)
143 /* We must modify global data. */
144 __libc_lock_define_initialized (static, lock)
146 /* Acquire the lock. */
147 __libc_lock_lock (lock);
149 /* We should repest the test since while we waited some other thread
150 might have run this function. */
151 if (__wcsmbs_last_locale != new_category)
153 if (new_category->name == _nl_C_name) /* Yes, pointer comparison. */
155 failed:
156 __wcsmbs_gconv_fcts.towc = &to_wc;
157 __wcsmbs_gconv_fcts.tomb = &to_mb;
159 else
161 /* We must find the real functions. */
162 const char *charset_name;
163 const char *complete_name;
165 /* Get name of charset of the locale. We first examine
166 whether we have a character set mentioned in the locale
167 name. If this isn't the case we use the information from
168 the locale files. */
169 charset_name = extract_charset_name (setlocale (LC_CTYPE, NULL));
170 if (charset_name == NULL)
171 charset_name =
172 new_category->values[_NL_ITEM_INDEX(CODESET)].string;
174 /* Add the slashes necessary for a complete lookup. */
175 complete_name = add_slashes (charset_name);
177 __wcsmbs_gconv_fcts.tomb = getfct (complete_name, "INTERNAL");
178 __wcsmbs_gconv_fcts.towc = getfct ("INTERNAL", complete_name);
180 /* If any of the conversion functions is not available we don't
181 use any since this would mean we cannot convert back and
182 forth.*/
183 if (__wcsmbs_gconv_fcts.towc == NULL
184 || __wcsmbs_gconv_fcts.tomb == NULL)
185 goto failed;
188 /* Set last-used variable for current locale. */
189 __wcsmbs_last_locale = new_category;
192 __libc_lock_unlock (lock);