Update.
[glibc.git] / wcsmbs / wcsmbsload.c
blobbf23d25cac70bd3e01a2182ec02e427f41304181
1 /* Copyright (C) 1998, 1999, 2000, 2001, 2002 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>
31 /* These are the descriptions for the default conversion functions. */
32 static struct __gconv_step to_wc =
34 .__shlib_handle = NULL,
35 .__modname = NULL,
36 .__counter = INT_MAX,
37 .__from_name = (char *) "ANSI_X3.4-1968//TRANSLIT",
38 .__to_name = (char *) "INTERNAL",
39 .__fct = __gconv_transform_ascii_internal,
40 .__init_fct = NULL,
41 .__end_fct = NULL,
42 .__min_needed_from = 1,
43 .__max_needed_from = 1,
44 .__min_needed_to = 4,
45 .__max_needed_to = 4,
46 .__stateful = 0,
47 .__data = NULL
50 static struct __gconv_step to_mb =
52 .__shlib_handle = NULL,
53 .__modname = NULL,
54 .__counter = INT_MAX,
55 .__from_name = (char *) "INTERNAL",
56 .__to_name = (char *) "ANSI_X3.4-1968//TRANSLIT",
57 .__fct = __gconv_transform_internal_ascii,
58 .__init_fct = NULL,
59 .__end_fct = NULL,
60 .__min_needed_from = 4,
61 .__max_needed_from = 4,
62 .__min_needed_to = 1,
63 .__max_needed_to = 1,
64 .__stateful = 0,
65 .__data = NULL
69 /* For the default locale we only have to handle ANSI_X3.4-1968. */
70 const struct gconv_fcts __wcsmbs_gconv_fcts_c =
72 .towc = &to_wc,
73 .towc_nsteps = 1,
74 .tomb = &to_mb,
75 .tomb_nsteps = 1
79 struct __gconv_step *
80 attribute_hidden
81 __wcsmbs_getfct (const char *to, const char *from, size_t *nstepsp)
83 size_t nsteps;
84 struct __gconv_step *result;
85 #if 0
86 size_t nstateful;
87 size_t cnt;
88 #endif
90 if (__gconv_find_transform (to, from, &result, &nsteps, 0) != __GCONV_OK)
91 /* Loading the conversion step is not possible. */
92 return NULL;
94 /* Maybe it is someday necessary to allow more than one step.
95 Currently this is not the case since the conversions handled here
96 are from and to INTERNAL and there always is a converted for
97 that. It the directly following code is enabled the libio
98 functions will have to allocate appropriate __gconv_step_data
99 elements instead of only one. */
100 #if 0
101 /* Count the number of stateful conversions. Since we will only
102 have one 'mbstate_t' object available we can only deal with one
103 stateful conversion. */
104 nstateful = 0;
105 for (cnt = 0; cnt < nsteps; ++cnt)
106 if (result[cnt].__stateful)
107 ++nstateful;
108 if (nstateful > 1)
109 #else
110 if (nsteps > 1)
111 #endif
113 /* We cannot handle this case. */
114 __gconv_close_transform (result, nsteps);
115 result = NULL;
117 else
118 *nstepsp = nsteps;
120 return result;
124 /* Extract from the given locale name the character set portion. Since
125 only the XPG form of the name includes this information we don't have
126 to take care for the CEN form. */
127 #define extract_charset_name(str) \
128 ({ \
129 const char *cp = str; \
130 char *result = NULL; \
132 cp += strcspn (cp, "@.+,"); \
133 if (*cp == '.') \
135 const char *endp = ++cp; \
136 while (*endp != '\0' && *endp != '@') \
137 ++endp; \
138 if (endp != cp) \
139 result = strndupa (cp, endp - cp); \
141 result; \
145 /* Some of the functions here must not be used while setlocale is called. */
146 __libc_lock_define (extern, __libc_setlocale_lock attribute_hidden)
148 /* Load conversion functions for the currently selected locale. */
149 void
150 internal_function
151 __wcsmbs_load_conv (struct locale_data *new_category)
153 /* Acquire the lock. */
154 __libc_lock_lock (__libc_setlocale_lock);
156 /* We should repeat the test since while we waited some other thread
157 might have run this function. */
158 if (__builtin_expect (new_category->private.ctype == NULL, 1))
160 /* We must find the real functions. */
161 const char *charset_name;
162 const char *complete_name;
163 struct gconv_fcts *new_fcts;
164 int use_translit;
166 /* Allocate the gconv_fcts structure. */
167 new_fcts = malloc (sizeof *new_fcts);
168 if (new_fcts == NULL)
169 goto failed;
171 /* Get name of charset of the locale. */
172 charset_name = new_category->values[_NL_ITEM_INDEX(CODESET)].string;
174 /* Does the user want transliteration? */
175 use_translit = new_category->use_translit;
177 /* Normalize the name and add the slashes necessary for a
178 complete lookup. */
179 complete_name = norm_add_slashes (charset_name,
180 use_translit ? "TRANSLIT" : NULL);
182 /* It is not necessary to use transliteration in this direction
183 since the internal character set is supposed to be able to
184 represent all others. */
185 new_fcts->towc = __wcsmbs_getfct ("INTERNAL", complete_name,
186 &new_fcts->towc_nsteps);
187 new_fcts->tomb = (new_fcts->towc != NULL
188 ? __wcsmbs_getfct (complete_name, "INTERNAL",
189 &new_fcts->tomb_nsteps)
190 : NULL);
192 /* If any of the conversion functions is not available we don't
193 use any since this would mean we cannot convert back and
194 forth.*/
195 if (new_fcts->tomb == NULL)
197 if (new_fcts->towc != NULL)
198 __gconv_close_transform (new_fcts->towc, new_fcts->towc_nsteps);
200 free (new_fcts);
202 failed:
203 new_category->private.ctype = &__wcsmbs_gconv_fcts_c;
205 else
207 new_category->private.ctype = new_fcts;
208 new_category->private.cleanup = &_nl_cleanup_ctype;
212 __libc_lock_unlock (__libc_setlocale_lock);
216 /* Clone the current conversion function set. */
217 void
218 internal_function
219 __wcsmbs_clone_conv (struct gconv_fcts *copy)
221 const struct gconv_fcts *orig;
223 orig = get_gconv_fcts (_NL_CURRENT_DATA (LC_CTYPE));
225 /* Copy the data. */
226 *copy = *orig;
228 /* Now increment the usage counters. */
229 if (copy->towc->__shlib_handle != NULL)
230 ++copy->towc->__counter;
231 if (copy->tomb->__shlib_handle != NULL)
232 ++copy->tomb->__counter;
236 /* Get converters for named charset. */
238 internal_function
239 __wcsmbs_named_conv (struct gconv_fcts *copy, const char *name)
241 copy->towc = __wcsmbs_getfct ("INTERNAL", name, &copy->towc_nsteps);
242 if (copy->towc != NULL)
244 copy->tomb = __wcsmbs_getfct (name, "INTERNAL", &copy->tomb_nsteps);
245 if (copy->tomb == NULL)
246 __gconv_close_transform (copy->towc, copy->towc_nsteps);
249 return copy->towc == NULL || copy->tomb == NULL ? 1 : 0;
252 void internal_function
253 _nl_cleanup_ctype (struct locale_data *locale)
255 const struct gconv_fcts *const data = locale->private.ctype;
256 if (data != NULL)
258 locale->private.ctype = NULL;
259 locale->private.cleanup = NULL;
261 /* Free the old conversions. */
262 __gconv_close_transform (data->tomb, data->tomb_nsteps);
263 __gconv_close_transform (data->towc, data->towc_nsteps);
264 free ((char *) data);