1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 // The BSDs have lots of *_l functions. This file provides reimplementations
10 // of those functions for non-BSD platforms.
11 //===----------------------------------------------------------------------===//
12
13 #ifndef _LIBCPP___BSD_LOCALE_FALLBACKS_H
14 #define _LIBCPP___BSD_LOCALE_FALLBACKS_H
15
16 #include <stdarg.h>
17 #include <stdlib.h>
18
19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20 # pragma GCC system_header
21 #endif
22
23 _LIBCPP_BEGIN_NAMESPACE_STD
24
25 inline _LIBCPP_INLINE_VISIBILITY
decltype(MB_CUR_MAX)26 decltype(MB_CUR_MAX) __libcpp_mb_cur_max_l(locale_t __l)
27 {
28 __libcpp_locale_guard __current(__l);
29 return MB_CUR_MAX;
30 }
31
32 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
33 inline _LIBCPP_INLINE_VISIBILITY
__libcpp_btowc_l(int __c,locale_t __l)34 wint_t __libcpp_btowc_l(int __c, locale_t __l)
35 {
36 __libcpp_locale_guard __current(__l);
37 return btowc(__c);
38 }
39
40 inline _LIBCPP_INLINE_VISIBILITY
__libcpp_wctob_l(wint_t __c,locale_t __l)41 int __libcpp_wctob_l(wint_t __c, locale_t __l)
42 {
43 __libcpp_locale_guard __current(__l);
44 return wctob(__c);
45 }
46
47 inline _LIBCPP_INLINE_VISIBILITY
__libcpp_wcsnrtombs_l(char * __dest,const wchar_t ** __src,size_t __nwc,size_t __len,mbstate_t * __ps,locale_t __l)48 size_t __libcpp_wcsnrtombs_l(char *__dest, const wchar_t **__src, size_t __nwc,
49 size_t __len, mbstate_t *__ps, locale_t __l)
50 {
51 __libcpp_locale_guard __current(__l);
52 return wcsnrtombs(__dest, __src, __nwc, __len, __ps);
53 }
54
55 inline _LIBCPP_INLINE_VISIBILITY
__libcpp_wcrtomb_l(char * __s,wchar_t __wc,mbstate_t * __ps,locale_t __l)56 size_t __libcpp_wcrtomb_l(char *__s, wchar_t __wc, mbstate_t *__ps, locale_t __l)
57 {
58 __libcpp_locale_guard __current(__l);
59 return wcrtomb(__s, __wc, __ps);
60 }
61
62 inline _LIBCPP_INLINE_VISIBILITY
__libcpp_mbsnrtowcs_l(wchar_t * __dest,const char ** __src,size_t __nms,size_t __len,mbstate_t * __ps,locale_t __l)63 size_t __libcpp_mbsnrtowcs_l(wchar_t * __dest, const char **__src, size_t __nms,
64 size_t __len, mbstate_t *__ps, locale_t __l)
65 {
66 __libcpp_locale_guard __current(__l);
67 return mbsnrtowcs(__dest, __src, __nms, __len, __ps);
68 }
69
70 inline _LIBCPP_INLINE_VISIBILITY
__libcpp_mbrtowc_l(wchar_t * __pwc,const char * __s,size_t __n,mbstate_t * __ps,locale_t __l)71 size_t __libcpp_mbrtowc_l(wchar_t *__pwc, const char *__s, size_t __n,
72 mbstate_t *__ps, locale_t __l)
73 {
74 __libcpp_locale_guard __current(__l);
75 return mbrtowc(__pwc, __s, __n, __ps);
76 }
77
78 inline _LIBCPP_INLINE_VISIBILITY
__libcpp_mbtowc_l(wchar_t * __pwc,const char * __pmb,size_t __max,locale_t __l)79 int __libcpp_mbtowc_l(wchar_t *__pwc, const char *__pmb, size_t __max, locale_t __l)
80 {
81 __libcpp_locale_guard __current(__l);
82 return mbtowc(__pwc, __pmb, __max);
83 }
84
85 inline _LIBCPP_INLINE_VISIBILITY
__libcpp_mbrlen_l(const char * __s,size_t __n,mbstate_t * __ps,locale_t __l)86 size_t __libcpp_mbrlen_l(const char *__s, size_t __n, mbstate_t *__ps, locale_t __l)
87 {
88 __libcpp_locale_guard __current(__l);
89 return mbrlen(__s, __n, __ps);
90 }
91 #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
92
93 inline _LIBCPP_INLINE_VISIBILITY
__libcpp_localeconv_l(locale_t __l)94 lconv *__libcpp_localeconv_l(locale_t __l)
95 {
96 __libcpp_locale_guard __current(__l);
97 return localeconv();
98 }
99
100 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
101 inline _LIBCPP_INLINE_VISIBILITY
__libcpp_mbsrtowcs_l(wchar_t * __dest,const char ** __src,size_t __len,mbstate_t * __ps,locale_t __l)102 size_t __libcpp_mbsrtowcs_l(wchar_t *__dest, const char **__src, size_t __len,
103 mbstate_t *__ps, locale_t __l)
104 {
105 __libcpp_locale_guard __current(__l);
106 return mbsrtowcs(__dest, __src, __len, __ps);
107 }
108 #endif
109
110 inline _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 4, 5)
__libcpp_snprintf_l(char * __s,size_t __n,locale_t __l,const char * __format,...)111 int __libcpp_snprintf_l(char *__s, size_t __n, locale_t __l, const char *__format, ...) {
112 va_list __va;
113 va_start(__va, __format);
114 __libcpp_locale_guard __current(__l);
115 int __res = vsnprintf(__s, __n, __format, __va);
116 va_end(__va);
117 return __res;
118 }
119
120 inline _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 3, 4)
__libcpp_asprintf_l(char ** __s,locale_t __l,const char * __format,...)121 int __libcpp_asprintf_l(char **__s, locale_t __l, const char *__format, ...) {
122 va_list __va;
123 va_start(__va, __format);
124 __libcpp_locale_guard __current(__l);
125 int __res = vasprintf(__s, __format, __va);
126 va_end(__va);
127 return __res;
128 }
129
130 inline _LIBCPP_ATTRIBUTE_FORMAT(__scanf__, 3, 4)
__libcpp_sscanf_l(const char * __s,locale_t __l,const char * __format,...)131 int __libcpp_sscanf_l(const char *__s, locale_t __l, const char *__format, ...) {
132 va_list __va;
133 va_start(__va, __format);
134 __libcpp_locale_guard __current(__l);
135 int __res = vsscanf(__s, __format, __va);
136 va_end(__va);
137 return __res;
138 }
139
140 _LIBCPP_END_NAMESPACE_STD
141
142 #endif // _LIBCPP___BSD_LOCALE_FALLBACKS_H
143