Update.
[glibc.git] / wcsmbs / wcsmbsload.c
blobb16aa6c500b7ef0d53dfa10eb2d44db044d942fd
1 /* Copyright (C) 1998, 1999 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. */
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//",
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//",
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;
90 if (__gconv_find_transform (to, from, &result, &nsteps) != GCONV_OK)
91 /* Loading the conversion step is not possible. */
92 return NULL;
94 /* We must only have one step in this conversion. */
95 if (nsteps != 1)
96 return NULL;
98 return result;
102 /* Extract from the given locale name the character set portion. Since
103 only the XPG form of the name includes this information we don't have
104 to take care for the CEN form. */
105 #define extract_charset_name(str) \
106 ({ \
107 const char *cp = str; \
108 char *result = NULL; \
110 cp += strcspn (cp, "@.+,"); \
111 if (*cp == '.') \
113 const char *endp = ++cp; \
114 while (*endp != '\0' && *endp != '@') \
115 ++endp; \
116 if (endp != cp) \
117 result = strndupa (cp, endp - cp); \
119 result; \
123 /* The gconv functions expects the name to be complete, including the
124 trailing shashes if necessary. */
125 #define norm_add_slashes(str) \
126 ({ \
127 const char *cp = str; \
128 char *result; \
129 char *tmp; \
130 size_t cnt = 0; \
132 while (*cp != '\0') \
133 if (*cp++ == '/') \
134 ++cnt; \
136 tmp = result = alloca (cp - str + 3); \
137 cp = str; \
138 while (*cp != '\0') \
139 *tmp++ = _toupper (*cp++); \
140 if (cnt < 2) \
142 *tmp++ = '/'; \
143 if (cnt < 1) \
144 *tmp++ = '/'; \
146 *tmp = '\0'; \
147 result; \
151 /* Load conversion functions for the currently selected locale. */
152 void
153 internal_function
154 __wcsmbs_load_conv (const struct locale_data *new_category)
156 /* We must modify global data. */
157 __libc_lock_define_initialized (static, lock)
159 /* Acquire the lock. */
160 __libc_lock_lock (lock);
162 /* We should repest the test since while we waited some other thread
163 might have run this function. */
164 if (__wcsmbs_last_locale != new_category)
166 if (new_category->name == _nl_C_name) /* Yes, pointer comparison. */
168 failed:
169 __wcsmbs_gconv_fcts.towc = &to_wc;
170 __wcsmbs_gconv_fcts.tomb = &to_mb;
172 else
174 /* We must find the real functions. */
175 const char *charset_name;
176 const char *complete_name;
178 /* Get name of charset of the locale. We first examine
179 whether we have a character set mentioned in the locale
180 name. If this isn't the case we use the information from
181 the locale files. */
182 charset_name = extract_charset_name (setlocale (LC_CTYPE, NULL));
183 if (charset_name == NULL)
184 charset_name =
185 new_category->values[_NL_ITEM_INDEX(CODESET)].string;
187 /* Normalize the name and add the slashes necessary for a
188 complete lookup. */
189 complete_name = norm_add_slashes (charset_name);
191 __wcsmbs_gconv_fcts.tomb = getfct (complete_name, "INTERNAL");
192 __wcsmbs_gconv_fcts.towc = getfct ("INTERNAL", complete_name);
194 /* If any of the conversion functions is not available we don't
195 use any since this would mean we cannot convert back and
196 forth.*/
197 if (__wcsmbs_gconv_fcts.towc == NULL
198 || __wcsmbs_gconv_fcts.tomb == NULL)
199 goto failed;
202 /* Set last-used variable for current locale. */
203 __wcsmbs_last_locale = new_category;
206 __libc_lock_unlock (lock);