1// -*- C++ -*-
2//===---------------------- __bsd_locale_fallbacks.h ----------------------===//
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// The BSDs have lots of *_l functions.  This file provides reimplementations
11// of those functions for non-BSD platforms.
12//===----------------------------------------------------------------------===//
13
14#ifndef _LIBCPP_BSD_LOCALE_FALLBACKS_DEFAULTS_H
15#define _LIBCPP_BSD_LOCALE_FALLBACKS_DEFAULTS_H
16
17#include <stdlib.h>
18#include <memory>
19
20_LIBCPP_BEGIN_NAMESPACE_STD
21
22typedef _VSTD::remove_pointer<locale_t>::type __use_locale_struct;
23typedef _VSTD::unique_ptr<__use_locale_struct, decltype(&uselocale)> __locale_raii;
24
25inline _LIBCPP_ALWAYS_INLINE
26decltype(MB_CUR_MAX) __libcpp_mb_cur_max_l(locale_t __l)
27{
28    __locale_raii __current( uselocale(__l), uselocale );
29    return MB_CUR_MAX;
30}
31
32inline _LIBCPP_ALWAYS_INLINE
33wint_t __libcpp_btowc_l(int __c, locale_t __l)
34{
35    __locale_raii __current( uselocale(__l), uselocale );
36    return btowc(__c);
37}
38
39inline _LIBCPP_ALWAYS_INLINE
40int __libcpp_wctob_l(wint_t __c, locale_t __l)
41{
42    __locale_raii __current( uselocale(__l), uselocale );
43    return wctob(__c);
44}
45
46inline _LIBCPP_ALWAYS_INLINE
47size_t __libcpp_wcsnrtombs_l(char *__dest, const wchar_t **__src, size_t __nwc,
48                         size_t __len, mbstate_t *__ps, locale_t __l)
49{
50    __locale_raii __current( uselocale(__l), uselocale );
51    return wcsnrtombs(__dest, __src, __nwc, __len, __ps);
52}
53
54inline _LIBCPP_ALWAYS_INLINE
55size_t __libcpp_wcrtomb_l(char *__s, wchar_t __wc, mbstate_t *__ps, locale_t __l)
56{
57    __locale_raii __current( uselocale(__l), uselocale );
58    return wcrtomb(__s, __wc, __ps);
59}
60
61inline _LIBCPP_ALWAYS_INLINE
62size_t __libcpp_mbsnrtowcs_l(wchar_t * __dest, const char **__src, size_t __nms,
63                      size_t __len, mbstate_t *__ps, locale_t __l)
64{
65    __locale_raii __current( uselocale(__l), uselocale );
66    return mbsnrtowcs(__dest, __src, __nms, __len, __ps);
67}
68
69inline _LIBCPP_ALWAYS_INLINE
70size_t __libcpp_mbrtowc_l(wchar_t *__pwc, const char *__s, size_t __n,
71                   mbstate_t *__ps, locale_t __l)
72{
73    __locale_raii __current( uselocale(__l), uselocale );
74    return mbrtowc(__pwc, __s, __n, __ps);
75}
76
77inline _LIBCPP_ALWAYS_INLINE
78int __libcpp_mbtowc_l(wchar_t *__pwc, const char *__pmb, size_t __max, locale_t __l)
79{
80    __locale_raii __current( uselocale(__l), uselocale );
81    return mbtowc(__pwc, __pmb, __max);
82}
83
84inline _LIBCPP_ALWAYS_INLINE
85size_t __libcpp_mbrlen_l(const char *__s, size_t __n, mbstate_t *__ps, locale_t __l)
86{
87    __locale_raii __current( uselocale(__l), uselocale );
88    return mbrlen(__s, __n, __ps);
89}
90
91inline _LIBCPP_ALWAYS_INLINE
92lconv *__libcpp_localeconv_l(locale_t __l)
93{
94    __locale_raii __current( uselocale(__l), uselocale );
95    return localeconv();
96}
97
98inline _LIBCPP_ALWAYS_INLINE
99size_t __libcpp_mbsrtowcs_l(wchar_t *__dest, const char **__src, size_t __len,
100                     mbstate_t *__ps, locale_t __l)
101{
102    __locale_raii __current( uselocale(__l), uselocale );
103    return mbsrtowcs(__dest, __src, __len, __ps);
104}
105
106inline
107int __libcpp_snprintf_l(char *__s, size_t __n, locale_t __l, const char *__format, ...) {
108    va_list __va;
109    va_start(__va, __format);
110    __locale_raii __current( uselocale(__l), uselocale );
111    int __res = vsnprintf(__s, __n, __format, __va);
112    va_end(__va);
113    return __res;
114}
115
116inline
117int __libcpp_asprintf_l(char **__s, locale_t __l, const char *__format, ...) {
118    va_list __va;
119    va_start(__va, __format);
120    __locale_raii __current( uselocale(__l), uselocale );
121    int __res = vasprintf(__s, __format, __va);
122    va_end(__va);
123    return __res;
124}
125
126inline
127int __libcpp_sscanf_l(const char *__s, locale_t __l, const char *__format, ...) {
128    va_list __va;
129    va_start(__va, __format);
130    __locale_raii __current( uselocale(__l), uselocale );
131    int __res = vsscanf(__s, __format, __va);
132    va_end(__va);
133    return __res;
134}
135
136_LIBCPP_END_NAMESPACE_STD
137
138#endif // _LIBCPP_BSD_LOCALE_FALLBACKS_DEFAULTS_H
139