Update.
[glibc.git] / wcsmbs / wcsmbsload.c
blobf5d9426c326fde709936e8d2bdd2ad8804c81996
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 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
18 02111-1307 USA. */
20 #include <ctype.h>
21 #include <langinfo.h>
22 #include <limits.h>
23 #include <stdlib.h>
24 #include <string.h>
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,
42 .__modname = NULL,
43 .__counter = INT_MAX,
44 .__from_name = "ANSI_X3.4-1968//TRANSLIT",
45 .__to_name = "INTERNAL",
46 .__fct = __gconv_transform_ascii_internal,
47 .__init_fct = NULL,
48 .__end_fct = NULL,
49 .__min_needed_from = 1,
50 .__max_needed_from = 1,
51 .__min_needed_to = 4,
52 .__max_needed_to = 4,
53 .__stateful = 0,
54 .__data = NULL
57 static struct __gconv_step to_mb =
59 .__shlib_handle = NULL,
60 .__modname = NULL,
61 .__counter = INT_MAX,
62 .__from_name = "INTERNAL",
63 .__to_name = "ANSI_X3.4-1968//TRANSLIT",
64 .__fct = __gconv_transform_internal_ascii,
65 .__init_fct = NULL,
66 .__end_fct = NULL,
67 .__min_needed_from = 4,
68 .__max_needed_from = 4,
69 .__min_needed_to = 1,
70 .__max_needed_to = 1,
71 .__stateful = 0,
72 .__data = NULL
76 /* For the default locale we only have to handle ANSI_X3.4-1968. */
77 struct gconv_fcts __wcsmbs_gconv_fcts =
79 .towc = &to_wc,
80 .tomb = &to_mb
84 static inline struct __gconv_step *
85 getfct (const char *to, const char *from)
87 size_t nsteps;
88 struct __gconv_step *result;
89 size_t nstateful;
90 size_t cnt;
92 if (__gconv_find_transform (to, from, &result, &nsteps, 0) != __GCONV_OK)
93 /* Loading the conversion step is not possible. */
94 return NULL;
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. */
99 nstateful = 0;
100 for (cnt = 0; cnt < nsteps; ++cnt)
101 if (result[cnt].__stateful)
102 ++nstateful;
103 if (nstateful > 1)
105 /* We cannot handle this case. */
106 __gconv_close_transform (result, nsteps);
107 result = NULL;
110 return result;
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) \
118 ({ \
119 const char *cp = str; \
120 char *result = NULL; \
122 cp += strcspn (cp, "@.+,"); \
123 if (*cp == '.') \
125 const char *endp = ++cp; \
126 while (*endp != '\0' && *endp != '@') \
127 ++endp; \
128 if (endp != cp) \
129 result = strndupa (cp, endp - cp); \
131 result; \
135 /* We must modify global data. */
136 __libc_lock_define_initialized (static, lock)
139 /* Load conversion functions for the currently selected locale. */
140 void
141 internal_function
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. */
153 failed:
154 __wcsmbs_gconv_fcts.towc = &to_wc;
155 __wcsmbs_gconv_fcts.tomb = &to_mb;
157 else
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;
164 int use_translit;
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
177 complete lookup. */
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
190 forth.*/
191 if (new_towc == NULL || new_tomb == NULL)
193 if (new_towc != NULL)
194 __gconv_close_transform (new_towc, 1);
196 goto failed;
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. */
212 void
213 internal_function
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);
222 /* Copy the data. */
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. */
237 internal_function
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)
249 return 1;
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;
257 return 0;