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//TRANSLIT",
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//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
=
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 /* We must modify global data. */
136 __libc_lock_define_initialized (static, lock
)
139 /* Load conversion functions for the currently selected locale. */
142 __wcsmbs_load_conv (const struct locale_data
*new_category
)
144 /* Acquire the lock. */
145 __libc_lock_lock (lock
);
147 /* We should repeat the test since while we waited some other thread
148 might have run this function. */
149 if (__builtin_expect (__wcsmbs_last_locale
!= new_category
, 1))
151 if (new_category
->name
== _nl_C_name
) /* Yes, pointer comparison. */
154 __wcsmbs_gconv_fcts
.towc
= &to_wc
;
155 __wcsmbs_gconv_fcts
.tomb
= &to_mb
;
159 /* We must find the real functions. */
160 const char *charset_name
;
161 const char *complete_name
;
162 struct __gconv_step
*new_towc
;
163 struct __gconv_step
*new_tomb
;
166 /* Free the old conversions. */
167 __gconv_close_transform (__wcsmbs_gconv_fcts
.tomb
, 1);
168 __gconv_close_transform (__wcsmbs_gconv_fcts
.towc
, 1);
170 /* Get name of charset of the locale. */
171 charset_name
= new_category
->values
[_NL_ITEM_INDEX(CODESET
)].string
;
173 /* Does the user want transliteration? */
174 use_translit
= new_category
->use_translit
;
176 /* Normalize the name and add the slashes necessary for a
178 complete_name
= norm_add_slashes (charset_name
,
179 use_translit
? "TRANSLIT" : NULL
);
181 /* It is not necessary to use transliteration in this direction
182 since the internal character set is supposed to be able to
183 represent all others. */
184 new_towc
= getfct ("INTERNAL", complete_name
);
185 new_tomb
= (new_towc
!= NULL
186 ? getfct (complete_name
, "INTERNAL") : NULL
);
188 /* If any of the conversion functions is not available we don't
189 use any since this would mean we cannot convert back and
191 if (new_towc
== NULL
|| new_tomb
== NULL
)
193 if (new_towc
!= NULL
)
194 __gconv_close_transform (new_towc
, 1);
199 __wcsmbs_gconv_fcts
.tomb
= new_tomb
;
200 __wcsmbs_gconv_fcts
.towc
= new_towc
;
203 /* Set last-used variable for current locale. */
204 __wcsmbs_last_locale
= new_category
;
207 __libc_lock_unlock (lock
);
211 /* Clone the current conversion function set. */
214 __wcsmbs_clone_conv (struct gconv_fcts
*copy
)
216 /* First make sure the function table is up-to-date. */
217 update_conversion_ptrs ();
219 /* Make sure the data structures remain the same until we are finished. */
220 __libc_lock_lock (lock
);
223 *copy
= __wcsmbs_gconv_fcts
;
225 /* Now increment the usage counters. */
226 if (copy
->towc
->__shlib_handle
!= NULL
)
227 ++copy
->towc
->__counter
;
228 if (copy
->tomb
->__shlib_handle
!= NULL
)
229 ++copy
->tomb
->__counter
;
231 __libc_lock_unlock (lock
);
235 /* Clone the current conversion function set. */
238 __wcsmbs_named_conv (struct gconv_fcts
*copy
, const char *name
)
240 copy
->towc
= getfct ("INTERNAL", name
);
241 if (copy
->towc
!= NULL
)
243 copy
->tomb
= getfct (name
, "INTERNAL");
244 if (copy
->tomb
== NULL
)
245 __gconv_close_transform (copy
->towc
, 1);
248 if (copy
->towc
== NULL
|| copy
->tomb
== NULL
)
251 /* Now increment the usage counters. */
252 if (copy
->towc
->__shlib_handle
!= NULL
)
253 ++copy
->towc
->__counter
;
254 if (copy
->tomb
->__shlib_handle
!= NULL
)
255 ++copy
->tomb
->__counter
;