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. */
140 __wcsmbs_load_conv (const struct locale_data
*new_category
)
142 /* We must modify global data. */
143 __libc_lock_define_initialized (static, lock
)
145 /* Acquire the lock. */
146 __libc_lock_lock (lock
);
148 /* We should repest the test since while we waited some other thread
149 might have run this function. */
150 if (__wcsmbs_last_locale
!= new_category
)
152 if (new_category
->name
== _nl_C_name
) /* Yes, pointer comparison. */
155 __wcsmbs_gconv_fcts
.towc
= &to_wc
;
156 __wcsmbs_gconv_fcts
.tomb
= &to_mb
;
160 /* We must find the real functions. */
161 const char *charset_name
;
162 const char *complete_name
;
164 /* Get name of charset of the locale. We first examine
165 whether we have a character set mentioned in the locale
166 name. If this isn't the case we use the information from
168 charset_name
= extract_charset_name (setlocale (LC_CTYPE
, NULL
));
169 if (charset_name
== NULL
)
171 new_category
->values
[_NL_ITEM_INDEX(CODESET
)].string
;
173 /* Add the slashes necessary for a complete lookup. */
174 complete_name
= add_slashes (charset_name
);
176 __wcsmbs_gconv_fcts
.tomb
= getfct (complete_name
, "INTERNAL");
177 __wcsmbs_gconv_fcts
.towc
= getfct ("INTERNAL", complete_name
);
179 /* If any of the conversion functions is not available we don't
180 use any since this would mean we cannot convert back and
182 if (__wcsmbs_gconv_fcts
.towc
== NULL
183 || __wcsmbs_gconv_fcts
.tomb
== NULL
)
187 /* Set last-used variable for current locale. */
188 __wcsmbs_last_locale
= new_category
;
191 __libc_lock_unlock (lock
);