1 // -*- C++ -*-
2 //===-------------------- support/win32/locale_win32.cpp ------------------===//
3 //
4 // The LLVM Compiler Infrastructure
5 //
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #include <locale>
12 #include <cstdarg> // va_start, va_end
13
14 // FIXME: base currently unused. Needs manual work to construct the new locale
newlocale(int mask,const char * locale,locale_t)15 locale_t newlocale( int mask, const char * locale, locale_t /*base*/ )
16 {
17 return _create_locale( mask, locale );
18 }
uselocale(locale_t newloc)19 locale_t uselocale( locale_t newloc )
20 {
21 locale_t old_locale = _get_current_locale();
22 if ( newloc == NULL )
23 return old_locale;
24 // uselocale sets the thread's locale by definition, so unconditionally use thread-local locale
25 _configthreadlocale( _ENABLE_PER_THREAD_LOCALE );
26 // uselocale sets all categories
27 setlocale( LC_ALL, newloc->locinfo->lc_category[LC_ALL].locale );
28 // uselocale returns the old locale_t
29 return old_locale;
30 }
localeconv_l(locale_t loc)31 lconv *localeconv_l( locale_t loc )
32 {
33 __locale_raii __current( uselocale(loc), uselocale );
34 return localeconv();
35 }
mbrlen_l(const char * __restrict s,size_t n,mbstate_t * __restrict ps,locale_t loc)36 size_t mbrlen_l( const char *__restrict s, size_t n,
37 mbstate_t *__restrict ps, locale_t loc )
38 {
39 __locale_raii __current( uselocale(loc), uselocale );
40 return mbrlen( s, n, ps );
41 }
mbsrtowcs_l(wchar_t * __restrict dst,const char ** __restrict src,size_t len,mbstate_t * __restrict ps,locale_t loc)42 size_t mbsrtowcs_l( wchar_t *__restrict dst, const char **__restrict src,
43 size_t len, mbstate_t *__restrict ps, locale_t loc )
44 {
45 __locale_raii __current( uselocale(loc), uselocale );
46 return mbsrtowcs( dst, src, len, ps );
47 }
wcrtomb_l(char * __restrict s,wchar_t wc,mbstate_t * __restrict ps,locale_t loc)48 size_t wcrtomb_l( char *__restrict s, wchar_t wc, mbstate_t *__restrict ps,
49 locale_t loc )
50 {
51 __locale_raii __current( uselocale(loc), uselocale );
52 return wcrtomb( s, wc, ps );
53 }
mbrtowc_l(wchar_t * __restrict pwc,const char * __restrict s,size_t n,mbstate_t * __restrict ps,locale_t loc)54 size_t mbrtowc_l( wchar_t *__restrict pwc, const char *__restrict s,
55 size_t n, mbstate_t *__restrict ps, locale_t loc )
56 {
57 __locale_raii __current( uselocale(loc), uselocale );
58 return mbrtowc( pwc, s, n, ps );
59 }
mbsnrtowcs_l(wchar_t * __restrict dst,const char ** __restrict src,size_t nms,size_t len,mbstate_t * __restrict ps,locale_t loc)60 size_t mbsnrtowcs_l( wchar_t *__restrict dst, const char **__restrict src,
61 size_t nms, size_t len, mbstate_t *__restrict ps, locale_t loc )
62 {
63 __locale_raii __current( uselocale(loc), uselocale );
64 return mbsnrtowcs( dst, src, nms, len, ps );
65 }
wcsnrtombs_l(char * __restrict dst,const wchar_t ** __restrict src,size_t nwc,size_t len,mbstate_t * __restrict ps,locale_t loc)66 size_t wcsnrtombs_l( char *__restrict dst, const wchar_t **__restrict src,
67 size_t nwc, size_t len, mbstate_t *__restrict ps, locale_t loc )
68 {
69 __locale_raii __current( uselocale(loc), uselocale );
70 return wcsnrtombs( dst, src, nwc, len, ps );
71 }
btowc_l(int c,locale_t loc)72 wint_t btowc_l( int c, locale_t loc )
73 {
74 __locale_raii __current( uselocale(loc), uselocale );
75 return btowc( c );
76 }
wctob_l(wint_t c,locale_t loc)77 int wctob_l( wint_t c, locale_t loc )
78 {
79 __locale_raii __current( uselocale(loc), uselocale );
80 return wctob( c );
81 }
82
snprintf_l(char * ret,size_t n,locale_t loc,const char * format,...)83 int snprintf_l(char *ret, size_t n, locale_t loc, const char *format, ...)
84 {
85 __locale_raii __current( uselocale(loc), uselocale );
86 va_list ap;
87 va_start( ap, format );
88 int result = vsnprintf( ret, n, format, ap );
89 va_end(ap);
90 return result;
91 }
92
asprintf_l(char ** ret,locale_t loc,const char * format,...)93 int asprintf_l( char **ret, locale_t loc, const char *format, ... )
94 {
95 va_list ap;
96 va_start( ap, format );
97 int result = vasprintf_l( ret, loc, format, ap );
98 va_end(ap);
99 return result;
100 }
vasprintf_l(char ** ret,locale_t loc,const char * format,va_list ap)101 int vasprintf_l( char **ret, locale_t loc, const char *format, va_list ap )
102 {
103 __locale_raii __current( uselocale(loc), uselocale );
104 return vasprintf( ret, format, ap );
105 }
106