xref: /llvm-project/libcxx/include/__locale_dir/locale_base_api/bsd_locale_fallbacks.h (revision 34e0f9cd36e9d4eb7fd153f536c811ec668be458)
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_DIR_LOCALE_BASE_API_BSD_LOCALE_FALLBACKS_H
14 #define _LIBCPP___LOCALE_DIR_LOCALE_BASE_API_BSD_LOCALE_FALLBACKS_H
15 
16 #include <locale.h>
17 #include <stdarg.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 
21 #if _LIBCPP_HAS_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 struct __locale_guard {
32   _LIBCPP_HIDE_FROM_ABI __locale_guard(locale_t& __loc) : __old_loc_(::uselocale(__loc)) {}
33 
34   _LIBCPP_HIDE_FROM_ABI ~__locale_guard() {
35     if (__old_loc_)
36       ::uselocale(__old_loc_);
37   }
38 
39   locale_t __old_loc_;
40 
41   __locale_guard(__locale_guard const&)            = delete;
42   __locale_guard& operator=(__locale_guard const&) = delete;
43 };
44 
45 inline _LIBCPP_HIDE_FROM_ABI decltype(MB_CUR_MAX) __libcpp_mb_cur_max_l(locale_t __l) {
46   __locale_guard __current(__l);
47   return MB_CUR_MAX;
48 }
49 
50 #if _LIBCPP_HAS_WIDE_CHARACTERS
51 inline _LIBCPP_HIDE_FROM_ABI wint_t __libcpp_btowc_l(int __c, locale_t __l) {
52   __locale_guard __current(__l);
53   return btowc(__c);
54 }
55 
56 inline _LIBCPP_HIDE_FROM_ABI int __libcpp_wctob_l(wint_t __c, locale_t __l) {
57   __locale_guard __current(__l);
58   return wctob(__c);
59 }
60 
61 inline _LIBCPP_HIDE_FROM_ABI size_t
62 __libcpp_wcsnrtombs_l(char* __dest, const wchar_t** __src, size_t __nwc, size_t __len, mbstate_t* __ps, locale_t __l) {
63   __locale_guard __current(__l);
64   return wcsnrtombs(__dest, __src, __nwc, __len, __ps);
65 }
66 
67 inline _LIBCPP_HIDE_FROM_ABI size_t __libcpp_wcrtomb_l(char* __s, wchar_t __wc, mbstate_t* __ps, locale_t __l) {
68   __locale_guard __current(__l);
69   return wcrtomb(__s, __wc, __ps);
70 }
71 
72 inline _LIBCPP_HIDE_FROM_ABI size_t
73 __libcpp_mbsnrtowcs_l(wchar_t* __dest, const char** __src, size_t __nms, size_t __len, mbstate_t* __ps, locale_t __l) {
74   __locale_guard __current(__l);
75   return mbsnrtowcs(__dest, __src, __nms, __len, __ps);
76 }
77 
78 inline _LIBCPP_HIDE_FROM_ABI size_t
79 __libcpp_mbrtowc_l(wchar_t* __pwc, const char* __s, size_t __n, mbstate_t* __ps, locale_t __l) {
80   __locale_guard __current(__l);
81   return mbrtowc(__pwc, __s, __n, __ps);
82 }
83 
84 inline _LIBCPP_HIDE_FROM_ABI int __libcpp_mbtowc_l(wchar_t* __pwc, const char* __pmb, size_t __max, locale_t __l) {
85   __locale_guard __current(__l);
86   return mbtowc(__pwc, __pmb, __max);
87 }
88 
89 inline _LIBCPP_HIDE_FROM_ABI size_t __libcpp_mbrlen_l(const char* __s, size_t __n, mbstate_t* __ps, locale_t __l) {
90   __locale_guard __current(__l);
91   return mbrlen(__s, __n, __ps);
92 }
93 #endif // _LIBCPP_HAS_WIDE_CHARACTERS
94 
95 inline _LIBCPP_HIDE_FROM_ABI lconv* __libcpp_localeconv_l(locale_t& __l) {
96   __locale_guard __current(__l);
97   return localeconv();
98 }
99 
100 #if _LIBCPP_HAS_WIDE_CHARACTERS
101 inline _LIBCPP_HIDE_FROM_ABI size_t
102 __libcpp_mbsrtowcs_l(wchar_t* __dest, const char** __src, size_t __len, mbstate_t* __ps, locale_t __l) {
103   __locale_guard __current(__l);
104   return mbsrtowcs(__dest, __src, __len, __ps);
105 }
106 #endif
107 
108 inline _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 4, 5) int __libcpp_snprintf_l(
109     char* __s, size_t __n, locale_t __l, const char* __format, ...) {
110   va_list __va;
111   va_start(__va, __format);
112   __locale_guard __current(__l);
113   int __res = vsnprintf(__s, __n, __format, __va);
114   va_end(__va);
115   return __res;
116 }
117 
118 inline _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 3, 4) int __libcpp_asprintf_l(
119     char** __s, locale_t __l, const char* __format, ...) {
120   va_list __va;
121   va_start(__va, __format);
122   __locale_guard __current(__l);
123   int __res = vasprintf(__s, __format, __va);
124   va_end(__va);
125   return __res;
126 }
127 
128 inline _LIBCPP_ATTRIBUTE_FORMAT(__scanf__, 3, 4) int __libcpp_sscanf_l(
129     const char* __s, locale_t __l, const char* __format, ...) {
130   va_list __va;
131   va_start(__va, __format);
132   __locale_guard __current(__l);
133   int __res = vsscanf(__s, __format, __va);
134   va_end(__va);
135   return __res;
136 }
137 
138 _LIBCPP_END_NAMESPACE_STD
139 
140 #endif // _LIBCPP___LOCALE_DIR_LOCALE_BASE_API_BSD_LOCALE_FALLBACKS_H
141