1 /* Copyright (C) 1998 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. */
25 #include <locale/localeinfo.h>
26 #include <wcsmbsload.h>
27 #include <bits/libc-lock.h>
28 #include <iconv/gconv_int.h>
31 /* Last loaded locale for LC_CTYPE. We initialize for the C locale
32 which is enabled at startup. */
33 extern const struct locale_data _nl_C_LC_CTYPE
;
34 const struct locale_data
*__wcsmbs_last_locale
= &_nl_C_LC_CTYPE
;
37 /* These are the descriptions for the default conversion functions. */
38 static struct gconv_step to_wc
=
43 from_name
: "ANSI_X3.4-1968//",
45 fct
: __gconv_transform_ascii_internal
,
51 static struct gconv_step to_mb
=
56 from_name
: "INTERNAL",
57 to_name
: "ANSI_X3.4-1968//",
58 fct
: __gconv_transform_internal_ascii
,
65 /* For the default locale we only have to handle ANSI_X3.4-1968. */
66 struct gconv_fcts __wcsmbs_gconv_fcts
=
73 static inline struct gconv_step
*
74 getfct (const char *to
, const char *from
)
77 struct gconv_step
*result
;
79 if (__gconv_find_transform (to
, from
, &result
, &nsteps
) != GCONV_OK
)
80 /* Loading the conversion step is not possible. */
83 /* We must only have one step in this conversion. */
91 /* Extract from the given locale name the character set portion. Since
92 only the XPG form of the name includes this information we don't have
93 to take care for the CEN form. */
94 #define extract_charset_name(str) \
96 const char *cp = str; \
97 char *result = NULL; \
99 cp += strcspn (cp, "@.+,"); \
102 const char *endp = ++cp; \
103 while (*endp != '\0' && *endp != '@') \
106 result = strndupa (cp, endp - cp); \
112 /* The gconv functions expects the name to be complete, including the
113 trailing shashes if necessary. */
114 #define add_slashes(str) \
116 const char *cp = str; \
121 while (*cp != '\0') \
125 result = alloca (cp - str + 3); \
126 tmp = __mempcpy (result, str, cp - str); \
138 /* Load conversion functions for the currently selected locale. */
141 __wcsmbs_load_conv (const struct locale_data
*new_category
)
143 /* We must modify global data. */
144 __libc_lock_define_initialized (static, lock
)
146 /* Acquire the lock. */
147 __libc_lock_lock (lock
);
149 /* We should repest the test since while we waited some other thread
150 might have run this function. */
151 if (__wcsmbs_last_locale
!= new_category
)
153 if (new_category
->name
== _nl_C_name
) /* Yes, pointer comparison. */
156 __wcsmbs_gconv_fcts
.towc
= &to_wc
;
157 __wcsmbs_gconv_fcts
.tomb
= &to_mb
;
161 /* We must find the real functions. */
162 const char *charset_name
;
163 const char *complete_name
;
165 /* Get name of charset of the locale. We first examine
166 whether we have a character set mentioned in the locale
167 name. If this isn't the case we use the information from
169 charset_name
= extract_charset_name (setlocale (LC_CTYPE
, NULL
));
170 if (charset_name
== NULL
)
172 new_category
->values
[_NL_ITEM_INDEX(CODESET
)].string
;
174 /* Add the slashes necessary for a complete lookup. */
175 complete_name
= add_slashes (charset_name
);
177 __wcsmbs_gconv_fcts
.tomb
= getfct (complete_name
, "INTERNAL");
178 __wcsmbs_gconv_fcts
.towc
= getfct ("INTERNAL", complete_name
);
180 /* If any of the conversion functions is not available we don't
181 use any since this would mean we cannot convert back and
183 if (__wcsmbs_gconv_fcts
.towc
== NULL
184 || __wcsmbs_gconv_fcts
.tomb
== NULL
)
188 /* Set last-used variable for current locale. */
189 __wcsmbs_last_locale
= new_category
;
192 __libc_lock_unlock (lock
);