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#ifndef _LIBCPP___LOCALE_DIR_MESSAGES_H
10#define _LIBCPP___LOCALE_DIR_MESSAGES_H
11
12#include <__config>
13#include <__iterator/back_insert_iterator.h>
14#include <__locale>
15#include <string>
16
17#if _LIBCPP_HAS_LOCALIZATION
18
19# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20# pragma GCC system_header
21# endif
22
23# if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
24// Most unix variants have catopen. These are the specific ones that don't.
25# if !defined(__BIONIC__) && !_LIBCPP_LIBC_NEWLIB && !defined(__EMSCRIPTEN__)
26# define _LIBCPP_HAS_CATOPEN 1
27# include <nl_types.h>
28# else
29# define _LIBCPP_HAS_CATOPEN 0
30# endif
31# else
32# define _LIBCPP_HAS_CATOPEN 0
33# endif
34
35_LIBCPP_BEGIN_NAMESPACE_STD
36_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
37
38class _LIBCPP_EXPORTED_FROM_ABI messages_base {
39public:
40 typedef intptr_t catalog;
41
42 _LIBCPP_HIDE_FROM_ABI messages_base() {}
43};
44
45template <class _CharT>
46class messages : public locale::facet, public messages_base {
47public:
48 typedef _CharT char_type;
49 typedef basic_string<_CharT> string_type;
50
51 _LIBCPP_HIDE_FROM_ABI explicit messages(size_t __refs = 0) : locale::facet(__refs) {}
52
53 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI catalog open(const basic_string<char>& __nm, const locale& __loc) const {
54 return do_open(__nm, __loc);
55 }
56
57 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI string_type
58 get(catalog __c, int __set, int __msgid, const string_type& __dflt) const {
59 return do_get(__c, __set, __msgid, __dflt);
60 }
61
62 _LIBCPP_HIDE_FROM_ABI void close(catalog __c) const { do_close(__c); }
63
64 static locale::id id;
65
66protected:
67 _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~messages() override {}
68
69 virtual catalog do_open(const basic_string<char>&, const locale&) const;
70 virtual string_type do_get(catalog, int __set, int __msgid, const string_type& __dflt) const;
71 virtual void do_close(catalog) const;
72};
73
74template <class _CharT>
75locale::id messages<_CharT>::id;
76
77template <class _CharT>
78typename messages<_CharT>::catalog messages<_CharT>::do_open(const basic_string<char>& __nm, const locale&) const {
79# if _LIBCPP_HAS_CATOPEN
80 return (catalog)catopen(cat_name: __nm.c_str(), NL_CAT_LOCALE);
81# else // !_LIBCPP_HAS_CATOPEN
82 (void)__nm;
83 return -1;
84# endif // _LIBCPP_HAS_CATOPEN
85}
86
87template <class _CharT>
88typename messages<_CharT>::string_type
89messages<_CharT>::do_get(catalog __c, int __set, int __msgid, const string_type& __dflt) const {
90# if _LIBCPP_HAS_CATOPEN
91 string __ndflt;
92 __narrow_to_utf8<sizeof(char_type) * __CHAR_BIT__>()(
93 std::back_inserter(x&: __ndflt), __dflt.c_str(), __dflt.c_str() + __dflt.size());
94 nl_catd __cat = (nl_catd)__c;
95 static_assert(sizeof(catalog) >= sizeof(nl_catd), "Unexpected nl_catd type");
96 char* __n = catgets(catalog: __cat, __set, number: __msgid, string: __ndflt.c_str());
97 string_type __w;
98 __widen_from_utf8<sizeof(char_type) * __CHAR_BIT__>()(std::back_inserter(__w), __n, __n + std::strlen(s: __n));
99 return __w;
100# else // !_LIBCPP_HAS_CATOPEN
101 (void)__c;
102 (void)__set;
103 (void)__msgid;
104 return __dflt;
105# endif // _LIBCPP_HAS_CATOPEN
106}
107
108template <class _CharT>
109void messages<_CharT>::do_close(catalog __c) const {
110# if _LIBCPP_HAS_CATOPEN
111 catclose(catalog: (nl_catd)__c);
112# else // !_LIBCPP_HAS_CATOPEN
113 (void)__c;
114# endif // _LIBCPP_HAS_CATOPEN
115}
116
117extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages<char>;
118# if _LIBCPP_HAS_WIDE_CHARACTERS
119extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages<wchar_t>;
120# endif
121
122template <class _CharT>
123class messages_byname : public messages<_CharT> {
124public:
125 typedef messages_base::catalog catalog;
126 typedef basic_string<_CharT> string_type;
127
128 _LIBCPP_HIDE_FROM_ABI explicit messages_byname(const char*, size_t __refs = 0) : messages<_CharT>(__refs) {}
129
130 _LIBCPP_HIDE_FROM_ABI explicit messages_byname(const string&, size_t __refs = 0) : messages<_CharT>(__refs) {}
131
132protected:
133 _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~messages_byname() override {}
134};
135
136extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages_byname<char>;
137# if _LIBCPP_HAS_WIDE_CHARACTERS
138extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages_byname<wchar_t>;
139# endif
140
141_LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
142_LIBCPP_END_NAMESPACE_STD
143
144#endif // _LIBCPP_HAS_LOCALIZATION
145
146#endif // _LIBCPP___LOCALE_DIR_MESSAGES_H
147