1 /* Copyright (C) 1998, 1999, 2000, 2001 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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
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
= (char *) "ANSI_X3.4-1968//TRANSLIT",
45 .__to_name
= (char *) "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
= (char *) "INTERNAL",
63 .__to_name
= (char *) "ANSI_X3.4-1968//TRANSLIT",
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
=
86 static inline struct __gconv_step
*
87 getfct (const char *to
, const char *from
, size_t *nstepsp
)
90 struct __gconv_step
*result
;
96 if (__gconv_find_transform (to
, from
, &result
, &nsteps
, 0) != __GCONV_OK
)
97 /* Loading the conversion step is not possible. */
100 /* Maybe it is someday necessary to allow more than one step.
101 Currently this is not the case since the conversions handled here
102 are from and to INTERNAL and there always is a converted for
103 that. It the directly following code is enabled the libio
104 functions will have to allocate appropriate __gconv_step_data
105 elements instead of only one. */
107 /* Count the number of stateful conversions. Since we will only
108 have one 'mbstate_t' object available we can only deal with one
109 stateful conversion. */
111 for (cnt
= 0; cnt
< nsteps
; ++cnt
)
112 if (result
[cnt
].__stateful
)
119 /* We cannot handle this case. */
120 __gconv_close_transform (result
, nsteps
);
130 /* Extract from the given locale name the character set portion. Since
131 only the XPG form of the name includes this information we don't have
132 to take care for the CEN form. */
133 #define extract_charset_name(str) \
135 const char *cp = str; \
136 char *result = NULL; \
138 cp += strcspn (cp, "@.+,"); \
141 const char *endp = ++cp; \
142 while (*endp != '\0' && *endp != '@') \
145 result = strndupa (cp, endp - cp); \
151 /* We must modify global data. */
152 __libc_lock_define_initialized (static, lock
)
155 /* Load conversion functions for the currently selected locale. */
158 __wcsmbs_load_conv (const struct locale_data
*new_category
)
160 /* Acquire the lock. */
161 __libc_lock_lock (lock
);
163 /* We should repeat the test since while we waited some other thread
164 might have run this function. */
165 if (__builtin_expect (__wcsmbs_last_locale
!= new_category
, 1))
167 if (new_category
->name
== _nl_C_name
) /* Yes, pointer comparison. */
170 __wcsmbs_gconv_fcts
.towc
= &to_wc
;
171 __wcsmbs_gconv_fcts
.tomb
= &to_mb
;
175 /* We must find the real functions. */
176 const char *charset_name
;
177 const char *complete_name
;
178 struct __gconv_step
*new_towc
;
179 size_t new_towc_nsteps
;
180 struct __gconv_step
*new_tomb
;
181 size_t new_tomb_nsteps
;
184 /* Free the old conversions. */
185 if (__wcsmbs_gconv_fcts
.tomb
!= &to_mb
)
186 __gconv_close_transform (__wcsmbs_gconv_fcts
.tomb
,
187 __wcsmbs_gconv_fcts
.tomb_nsteps
);
188 if (__wcsmbs_gconv_fcts
.towc
!= &to_wc
)
189 __gconv_close_transform (__wcsmbs_gconv_fcts
.towc
,
190 __wcsmbs_gconv_fcts
.towc_nsteps
);
192 /* Get name of charset of the locale. */
193 charset_name
= new_category
->values
[_NL_ITEM_INDEX(CODESET
)].string
;
195 /* Does the user want transliteration? */
196 use_translit
= new_category
->use_translit
;
198 /* Normalize the name and add the slashes necessary for a
200 complete_name
= norm_add_slashes (charset_name
,
201 use_translit
? "TRANSLIT" : NULL
);
203 /* It is not necessary to use transliteration in this direction
204 since the internal character set is supposed to be able to
205 represent all others. */
206 new_towc
= getfct ("INTERNAL", complete_name
, &new_towc_nsteps
);
207 new_tomb
= (new_towc
!= NULL
208 ? getfct (complete_name
, "INTERNAL", &new_tomb_nsteps
)
211 /* If any of the conversion functions is not available we don't
212 use any since this would mean we cannot convert back and
214 if (new_towc
== NULL
|| new_tomb
== NULL
)
216 if (new_towc
!= NULL
)
217 __gconv_close_transform (new_towc
, 1);
222 __wcsmbs_gconv_fcts
.tomb
= new_tomb
;
223 __wcsmbs_gconv_fcts
.tomb_nsteps
= new_tomb_nsteps
;
224 __wcsmbs_gconv_fcts
.towc
= new_towc
;
225 __wcsmbs_gconv_fcts
.towc_nsteps
= new_towc_nsteps
;
228 /* Set last-used variable for current locale. */
229 __wcsmbs_last_locale
= new_category
;
232 __libc_lock_unlock (lock
);
236 /* Clone the current conversion function set. */
239 __wcsmbs_clone_conv (struct gconv_fcts
*copy
)
241 /* First make sure the function table is up-to-date. */
242 update_conversion_ptrs ();
244 /* Make sure the data structures remain the same until we are finished. */
245 __libc_lock_lock (lock
);
248 *copy
= __wcsmbs_gconv_fcts
;
250 /* Now increment the usage counters. */
251 if (copy
->towc
->__shlib_handle
!= NULL
)
252 ++copy
->towc
->__counter
;
253 if (copy
->tomb
->__shlib_handle
!= NULL
)
254 ++copy
->tomb
->__counter
;
256 __libc_lock_unlock (lock
);
260 /* Get converters for named charset. */
263 __wcsmbs_named_conv (struct gconv_fcts
*copy
, const char *name
)
265 copy
->towc
= getfct ("INTERNAL", name
, ©
->towc_nsteps
);
266 if (copy
->towc
!= NULL
)
268 copy
->tomb
= getfct (name
, "INTERNAL", ©
->tomb_nsteps
);
269 if (copy
->tomb
== NULL
)
270 __gconv_close_transform (copy
->towc
, copy
->towc_nsteps
);
273 return copy
->towc
== NULL
|| copy
->tomb
== NULL
? 1 : 0;
277 /* Free all resources if necessary. */
278 static void __attribute__ ((unused
))
281 if (__wcsmbs_gconv_fcts
.tomb
!= &to_mb
)
283 struct __gconv_step
*old
= __wcsmbs_gconv_fcts
.tomb
;
284 size_t nold
= __wcsmbs_gconv_fcts
.tomb_nsteps
;
285 __wcsmbs_gconv_fcts
.tomb
= &to_mb
;
286 __wcsmbs_gconv_fcts
.tomb_nsteps
= 1;
287 __gconv_release_cache (old
, nold
);
290 if (__wcsmbs_gconv_fcts
.towc
!= &to_wc
)
292 struct __gconv_step
*old
= __wcsmbs_gconv_fcts
.towc
;
293 size_t nold
= __wcsmbs_gconv_fcts
.towc_nsteps
;
294 __wcsmbs_gconv_fcts
.towc
= &to_wc
;
295 __wcsmbs_gconv_fcts
.towc_nsteps
= 1;
296 __gconv_release_cache (old
, nold
);
300 text_set_element (__libc_subfreeres
, free_mem
);