1//===----------------------------------------------------------------------===// 2// 3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4// See https://llvm.org/LICENSE.txt for license information. 5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6// 7//===----------------------------------------------------------------------===// 8 9#include <locale> 10#include <cstdarg> // va_start, va_end 11#include <memory> 12#include <type_traits> 13 14int __libcpp_vasprintf(char **sptr, const char *__restrict fmt, va_list ap); 15 16using std::__libcpp_locale_guard; 17 18// FIXME: base currently unused. Needs manual work to construct the new locale 19locale_t newlocale( int mask, const char * locale, locale_t /*base*/ ) 20{ 21 return {_create_locale( LC_ALL, locale ), locale}; 22} 23 24decltype(MB_CUR_MAX) MB_CUR_MAX_L( locale_t __l ) 25{ 26#if defined(_LIBCPP_MSVCRT) 27 return ___mb_cur_max_l_func(__l); 28#else 29 __libcpp_locale_guard __current(__l); 30 return MB_CUR_MAX; 31#endif 32} 33 34lconv *localeconv_l( locale_t &loc ) 35{ 36 __libcpp_locale_guard __current(loc); 37 lconv *lc = localeconv(); 38 if (!lc) 39 return lc; 40 return loc.__store_lconv(lc); 41} 42size_t mbrlen_l( const char *__restrict s, size_t n, 43 mbstate_t *__restrict ps, locale_t loc ) 44{ 45 __libcpp_locale_guard __current(loc); 46 return mbrlen( s, n, ps ); 47} 48size_t mbsrtowcs_l( wchar_t *__restrict dst, const char **__restrict src, 49 size_t len, mbstate_t *__restrict ps, locale_t loc ) 50{ 51 __libcpp_locale_guard __current(loc); 52 return mbsrtowcs( dst, src, len, ps ); 53} 54size_t wcrtomb_l( char *__restrict s, wchar_t wc, mbstate_t *__restrict ps, 55 locale_t loc ) 56{ 57 __libcpp_locale_guard __current(loc); 58 return wcrtomb( s, wc, ps ); 59} 60size_t mbrtowc_l( wchar_t *__restrict pwc, const char *__restrict s, 61 size_t n, mbstate_t *__restrict ps, locale_t loc ) 62{ 63 __libcpp_locale_guard __current(loc); 64 return mbrtowc( pwc, s, n, ps ); 65} 66size_t mbsnrtowcs_l( wchar_t *__restrict dst, const char **__restrict src, 67 size_t nms, size_t len, mbstate_t *__restrict ps, locale_t loc ) 68{ 69 __libcpp_locale_guard __current(loc); 70 return mbsnrtowcs( dst, src, nms, len, ps ); 71} 72size_t wcsnrtombs_l( char *__restrict dst, const wchar_t **__restrict src, 73 size_t nwc, size_t len, mbstate_t *__restrict ps, locale_t loc ) 74{ 75 __libcpp_locale_guard __current(loc); 76 return wcsnrtombs( dst, src, nwc, len, ps ); 77} 78wint_t btowc_l( int c, locale_t loc ) 79{ 80 __libcpp_locale_guard __current(loc); 81 return btowc( c ); 82} 83int wctob_l( wint_t c, locale_t loc ) 84{ 85 __libcpp_locale_guard __current(loc); 86 return wctob( c ); 87} 88 89int snprintf_l(char *ret, size_t n, locale_t loc, const char *format, ...) 90{ 91 va_list ap; 92 va_start( ap, format ); 93#if defined(_LIBCPP_MSVCRT) 94 // FIXME: Remove usage of internal CRT function and globals. 95 int result = __stdio_common_vsprintf( 96 _CRT_INTERNAL_LOCAL_PRINTF_OPTIONS | _CRT_INTERNAL_PRINTF_STANDARD_SNPRINTF_BEHAVIOR, 97 ret, n, format, loc, ap); 98#else 99 __libcpp_locale_guard __current(loc); 100 _LIBCPP_DIAGNOSTIC_PUSH 101 _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wformat-nonliteral") 102 int result = vsnprintf( ret, n, format, ap ); 103 _LIBCPP_DIAGNOSTIC_POP 104#endif 105 va_end(ap); 106 return result; 107} 108 109int asprintf_l( char **ret, locale_t loc, const char *format, ... ) 110{ 111 va_list ap; 112 va_start( ap, format ); 113 int result = vasprintf_l( ret, loc, format, ap ); 114 va_end(ap); 115 return result; 116} 117int vasprintf_l( char **ret, locale_t loc, const char *format, va_list ap ) 118{ 119 __libcpp_locale_guard __current(loc); 120 return __libcpp_vasprintf( ret, format, ap ); 121} 122 123#if !defined(_LIBCPP_MSVCRT) 124float strtof_l(const char* nptr, char** endptr, locale_t loc) { 125 __libcpp_locale_guard __current(loc); 126 return strtof(nptr, endptr); 127} 128 129long double strtold_l(const char* nptr, char** endptr, locale_t loc) { 130 __libcpp_locale_guard __current(loc); 131 return strtold(nptr, endptr); 132} 133#endif 134 135#if defined(__MINGW32__) && __MSVCRT_VERSION__ < 0x0800 136size_t strftime_l(char *ret, size_t n, const char *format, const struct tm *tm, 137 locale_t loc) { 138 __libcpp_locale_guard __current(loc); 139 return strftime(ret, n, format, tm); 140} 141#endif 142