1 /* Copyright (C) 1998, 1999, 2000 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. */
26 #include <locale/localeinfo.h>
27 #include <wcsmbsload.h>
28 #include <bits/libc-lock.h>
29 #include <iconv/gconv_int.h>
32 /* Last loaded locale for LC_CTYPE. We initialize for the C locale
33 which is enabled at startup. */
34 extern const struct locale_data _nl_C_LC_CTYPE;
35 const struct locale_data *__wcsmbs_last_locale = &_nl_C_LC_CTYPE;
38 /* These are the descriptions for the default conversion functions. */
39 static struct __gconv_step to_wc =
41 .__shlib_handle = NULL,
44 .__from_name = "ANSI_X3.4-1968//",
45 .__to_name = "INTERNAL",
46 .__fct = __gconv_transform_ascii_internal,
49 .__min_needed_from = 1,
50 .__max_needed_from = 1,
57 static struct __gconv_step to_mb =
59 .__shlib_handle = NULL,
62 .__from_name = "INTERNAL",
63 .__to_name = "ANSI_X3.4-1968//",
64 .__fct = __gconv_transform_internal_ascii,
67 .__min_needed_from = 4,
68 .__max_needed_from = 4,
76 /* For the default locale we only have to handle ANSI_X3.4-1968. */
77 struct gconv_fcts __wcsmbs_gconv_fcts =
84 static inline struct __gconv_step *
85 getfct (const char *to, const char *from)
88 struct __gconv_step *result;
92 if (__gconv_find_transform (to, from, &result, &nsteps, 0) != __GCONV_OK)
93 /* Loading the conversion step is not possible. */
96 /* Count the number of stateful conversions. Since we will only
97 have one 'mbstate_t' object available we can only deal with one
98 stateful conversion. */
100 for (cnt = 0; cnt < nsteps; ++cnt)
101 if (result[cnt].__stateful)
105 /* We cannot handle this case. */
106 __gconv_close_transform (result, nsteps);
114 /* Extract from the given locale name the character set portion. Since
115 only the XPG form of the name includes this information we don't have
116 to take care for the CEN form. */
117 #define extract_charset_name(str) \
119 const char *cp = str; \
120 char *result = NULL; \
122 cp += strcspn (cp, "@.+,"); \
125 const char *endp = ++cp; \
126 while (*endp != '\0' && *endp != '@') \
129 result = strndupa (cp, endp - cp); \
135 /* The gconv functions expects the name to be complete, including the
136 trailing shashes if necessary. */
137 #define norm_add_slashes(str) \
139 const char *cp = str; \
144 while (*cp != '\0') \
148 tmp = result = alloca (cp - str + 3); \
150 while (*cp != '\0') \
151 *tmp++ = _toupper (*cp++); \
163 /* We must modify global data. */
164 __libc_lock_define_initialized (static, lock)
167 /* Load conversion functions for the currently selected locale. */
170 __wcsmbs_load_conv (const struct locale_data *new_category)
172 /* Acquire the lock. */
173 __libc_lock_lock (lock);
175 /* We should repest the test since while we waited some other thread
176 might have run this function. */
177 if (__wcsmbs_last_locale != new_category)
179 if (new_category->name == _nl_C_name) /* Yes, pointer comparison. */
182 __wcsmbs_gconv_fcts.towc = &to_wc;
183 __wcsmbs_gconv_fcts.tomb = &to_mb;
187 /* We must find the real functions. */
188 const char *charset_name;
189 const char *complete_name;
190 struct __gconv_step *new_towc;
191 struct __gconv_step *new_tomb;
193 /* Free the old conversions. */
194 __gconv_close_transform (__wcsmbs_gconv_fcts.tomb, 1);
195 __gconv_close_transform (__wcsmbs_gconv_fcts.towc, 1);
197 /* Get name of charset of the locale. */
198 charset_name = new_category->values[_NL_ITEM_INDEX(CODESET)].string;
200 /* Normalize the name and add the slashes necessary for a
202 complete_name = norm_add_slashes (charset_name);
204 new_towc = getfct ("INTERNAL", complete_name);
205 if (new_towc != NULL)
206 new_tomb = getfct (complete_name, "INTERNAL");
208 /* If any of the conversion functions is not available we don't
209 use any since this would mean we cannot convert back and
211 if (new_towc == NULL || new_tomb == NULL)
213 if (new_towc != NULL)
214 __gconv_close_transform (new_towc, 1);
219 __wcsmbs_gconv_fcts.tomb = new_tomb;
220 __wcsmbs_gconv_fcts.towc = new_towc;
223 /* Set last-used variable for current locale. */
224 __wcsmbs_last_locale = new_category;
227 __libc_lock_unlock (lock);
231 /* Clone the current conversion function set. */
234 __wcsmbs_clone_conv (struct gconv_fcts *copy)
236 /* First make sure the function table is up-to-date. */
237 update_conversion_ptrs ();
239 /* Make sure the data structures remain the same until we are finished. */
240 __libc_lock_lock (lock);
243 *copy = __wcsmbs_gconv_fcts;
245 /* Now increment the usage counters. */
246 if (copy->towc->__shlib_handle != NULL)
247 ++copy->towc->__counter;
248 if (copy->tomb->__shlib_handle != NULL)
249 ++copy->tomb->__counter;
251 __libc_lock_unlock (lock);
255 /* Clone the current conversion function set. */
258 __wcsmbs_named_conv (struct gconv_fcts *copy, const char *name)
260 copy->towc = getfct ("INTERNAL", name);
261 if (copy->towc != NULL)
263 copy->tomb = getfct (name, "INTERNAL");
264 if (copy->tomb == NULL)
265 __gconv_close_transform (copy->towc, 1);
268 if (copy->towc == NULL || copy->tomb == NULL)
271 /* Now increment the usage counters. */
272 if (copy->towc->__shlib_handle != NULL)
273 ++copy->towc->__counter;
274 if (copy->tomb->__shlib_handle != NULL)
275 ++copy->tomb->__counter;