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. */
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
=
44 from_name
: "ANSI_X3.4-1968//",
46 fct
: __gconv_transform_ascii_internal
,
57 static struct gconv_step to_mb
=
62 from_name
: "INTERNAL",
63 to_name
: "ANSI_X3.4-1968//",
64 fct
: __gconv_transform_internal_ascii
,
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
;
90 if (__gconv_find_transform (to
, from
, &result
, &nsteps
) != GCONV_OK
)
91 /* Loading the conversion step is not possible. */
94 /* We must only have one step in this conversion. */
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) \
107 const char *cp = str; \
108 char *result = NULL; \
110 cp += strcspn (cp, "@.+,"); \
113 const char *endp = ++cp; \
114 while (*endp != '\0' && *endp != '@') \
117 result = strndupa (cp, endp - cp); \
123 /* The gconv functions expects the name to be complete, including the
124 trailing shashes if necessary. */
125 #define norm_add_slashes(str) \
127 const char *cp = str; \
132 while (*cp != '\0') \
136 tmp = result = alloca (cp - str + 3); \
138 while (*cp != '\0') \
139 *tmp++ = _toupper (*cp++); \
151 /* Load conversion functions for the currently selected locale. */
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. */
169 __wcsmbs_gconv_fcts
.towc
= &to_wc
;
170 __wcsmbs_gconv_fcts
.tomb
= &to_mb
;
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
182 charset_name
= extract_charset_name (setlocale (LC_CTYPE
, NULL
));
183 if (charset_name
== NULL
)
185 new_category
->values
[_NL_ITEM_INDEX(CODESET
)].string
;
187 /* Normalize the name and add the slashes necessary for a
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
197 if (__wcsmbs_gconv_fcts
.towc
== NULL
198 || __wcsmbs_gconv_fcts
.tomb
== NULL
)
202 /* Set last-used variable for current locale. */
203 __wcsmbs_last_locale
= new_category
;
206 __libc_lock_unlock (lock
);