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___LOCALE_LOCALE_BASE_API_BSD_LOCALE_FALLBACKS_H
14 #define _LIBCPP___LOCALE_LOCALE_BASE_API_BSD_LOCALE_FALLBACKS_H
15 
16 #include <__locale_dir/locale_base_api/locale_guard.h>
17 #include <cstdio>
18 #include <stdarg.h>
19 #include <stdlib.h>
20 
21 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
22 #  include <cwchar>
23 #endif
24 
25 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
26 #  pragma GCC system_header
27 #endif
28 
29 _LIBCPP_BEGIN_NAMESPACE_STD
30 
31 inline _LIBCPP_INLINE_VISIBILITY
32 decltype(MB_CUR_MAX) __libcpp_mb_cur_max_l(locale_t __l)
33 {
34     __libcpp_locale_guard __current(__l);
35     return MB_CUR_MAX;
36 }
37 
38 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
39 inline _LIBCPP_INLINE_VISIBILITY
40 wint_t __libcpp_btowc_l(int __c, locale_t __l)
41 {
42     __libcpp_locale_guard __current(__l);
43     return btowc(__c);
44 }
45 
46 inline _LIBCPP_INLINE_VISIBILITY
47 int __libcpp_wctob_l(wint_t __c, locale_t __l)
48 {
49     __libcpp_locale_guard __current(__l);
50     return wctob(__c);
51 }
52 
53 inline _LIBCPP_INLINE_VISIBILITY
54 size_t __libcpp_wcsnrtombs_l(char *__dest, const wchar_t **__src, size_t __nwc,
55                          size_t __len, mbstate_t *__ps, locale_t __l)
56 {
57     __libcpp_locale_guard __current(__l);
58     return wcsnrtombs(__dest, __src, __nwc, __len, __ps);
59 }
60 
61 inline _LIBCPP_INLINE_VISIBILITY
62 size_t __libcpp_wcrtomb_l(char *__s, wchar_t __wc, mbstate_t *__ps, locale_t __l)
63 {
64     __libcpp_locale_guard __current(__l);
65     return wcrtomb(__s, __wc, __ps);
66 }
67 
68 inline _LIBCPP_INLINE_VISIBILITY
69 size_t __libcpp_mbsnrtowcs_l(wchar_t * __dest, const char **__src, size_t __nms,
70                       size_t __len, mbstate_t *__ps, locale_t __l)
71 {
72     __libcpp_locale_guard __current(__l);
73     return mbsnrtowcs(__dest, __src, __nms, __len, __ps);
74 }
75 
76 inline _LIBCPP_INLINE_VISIBILITY
77 size_t __libcpp_mbrtowc_l(wchar_t *__pwc, const char *__s, size_t __n,
78                    mbstate_t *__ps, locale_t __l)
79 {
80     __libcpp_locale_guard __current(__l);
81     return mbrtowc(__pwc, __s, __n, __ps);
82 }
83 
84 inline _LIBCPP_INLINE_VISIBILITY
85 int __libcpp_mbtowc_l(wchar_t *__pwc, const char *__pmb, size_t __max, locale_t __l)
86 {
87     __libcpp_locale_guard __current(__l);
88     return mbtowc(__pwc, __pmb, __max);
89 }
90 
91 inline _LIBCPP_INLINE_VISIBILITY
92 size_t __libcpp_mbrlen_l(const char *__s, size_t __n, mbstate_t *__ps, locale_t __l)
93 {
94     __libcpp_locale_guard __current(__l);
95     return mbrlen(__s, __n, __ps);
96 }
97 #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
98 
99 inline _LIBCPP_INLINE_VISIBILITY
100 lconv *__libcpp_localeconv_l(locale_t __l)
101 {
102     __libcpp_locale_guard __current(__l);
103     return localeconv();
104 }
105 
106 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
107 inline _LIBCPP_INLINE_VISIBILITY
108 size_t __libcpp_mbsrtowcs_l(wchar_t *__dest, const char **__src, size_t __len,
109                      mbstate_t *__ps, locale_t __l)
110 {
111     __libcpp_locale_guard __current(__l);
112     return mbsrtowcs(__dest, __src, __len, __ps);
113 }
114 #endif
115 
116 inline _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 4, 5)
117 int __libcpp_snprintf_l(char *__s, size_t __n, locale_t __l, const char *__format, ...) {
118     va_list __va;
119     va_start(__va, __format);
120     __libcpp_locale_guard __current(__l);
121     int __res = vsnprintf(__s, __n, __format, __va);
122     va_end(__va);
123     return __res;
124 }
125 
126 inline _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 3, 4)
127 int __libcpp_asprintf_l(char **__s, locale_t __l, const char *__format, ...) {
128     va_list __va;
129     va_start(__va, __format);
130     __libcpp_locale_guard __current(__l);
131     int __res = vasprintf(__s, __format, __va);
132     va_end(__va);
133     return __res;
134 }
135 
136 inline _LIBCPP_ATTRIBUTE_FORMAT(__scanf__, 3, 4)
137 int __libcpp_sscanf_l(const char *__s, locale_t __l, const char *__format, ...) {
138     va_list __va;
139     va_start(__va, __format);
140     __libcpp_locale_guard __current(__l);
141     int __res = vsscanf(__s, __format, __va);
142     va_end(__va);
143     return __res;
144 }
145 
146 _LIBCPP_END_NAMESPACE_STD
147 
148 #endif // _LIBCPP___LOCALE_LOCALE_BASE_API_BSD_LOCALE_FALLBACKS_H
149