Path: blob/main/contrib/llvm-project/libcxx/include/__locale_dir/messages.h
213766 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef _LIBCPP___LOCALE_DIR_MESSAGES_H9#define _LIBCPP___LOCALE_DIR_MESSAGES_H1011#include <__config>12#include <__iterator/back_insert_iterator.h>13#include <__locale>14#include <string>1516#if _LIBCPP_HAS_LOCALIZATION1718# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)19# pragma GCC system_header20# endif2122# if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))23// Most unix variants have catopen. These are the specific ones that don't.24# if !defined(__BIONIC__) && !defined(_NEWLIB_VERSION) && !defined(__EMSCRIPTEN__)25# define _LIBCPP_HAS_CATOPEN 126# include <nl_types.h>27# else28# define _LIBCPP_HAS_CATOPEN 029# endif30# else31# define _LIBCPP_HAS_CATOPEN 032# endif3334_LIBCPP_BEGIN_NAMESPACE_STD3536class _LIBCPP_EXPORTED_FROM_ABI messages_base {37public:38typedef intptr_t catalog;3940_LIBCPP_HIDE_FROM_ABI messages_base() {}41};4243template <class _CharT>44class messages : public locale::facet, public messages_base {45public:46typedef _CharT char_type;47typedef basic_string<_CharT> string_type;4849_LIBCPP_HIDE_FROM_ABI explicit messages(size_t __refs = 0) : locale::facet(__refs) {}5051_LIBCPP_HIDE_FROM_ABI catalog open(const basic_string<char>& __nm, const locale& __loc) const {52return do_open(__nm, __loc);53}5455_LIBCPP_HIDE_FROM_ABI string_type get(catalog __c, int __set, int __msgid, const string_type& __dflt) const {56return do_get(__c, __set, __msgid, __dflt);57}5859_LIBCPP_HIDE_FROM_ABI void close(catalog __c) const { do_close(__c); }6061static locale::id id;6263protected:64_LIBCPP_HIDE_FROM_ABI_VIRTUAL ~messages() override {}6566virtual catalog do_open(const basic_string<char>&, const locale&) const;67virtual string_type do_get(catalog, int __set, int __msgid, const string_type& __dflt) const;68virtual void do_close(catalog) const;69};7071template <class _CharT>72locale::id messages<_CharT>::id;7374template <class _CharT>75typename messages<_CharT>::catalog messages<_CharT>::do_open(const basic_string<char>& __nm, const locale&) const {76# if _LIBCPP_HAS_CATOPEN77return (catalog)catopen(__nm.c_str(), NL_CAT_LOCALE);78# else // !_LIBCPP_HAS_CATOPEN79(void)__nm;80return -1;81# endif // _LIBCPP_HAS_CATOPEN82}8384template <class _CharT>85typename messages<_CharT>::string_type86messages<_CharT>::do_get(catalog __c, int __set, int __msgid, const string_type& __dflt) const {87# if _LIBCPP_HAS_CATOPEN88string __ndflt;89__narrow_to_utf8<sizeof(char_type) * __CHAR_BIT__>()(90std::back_inserter(__ndflt), __dflt.c_str(), __dflt.c_str() + __dflt.size());91nl_catd __cat = (nl_catd)__c;92static_assert(sizeof(catalog) >= sizeof(nl_catd), "Unexpected nl_catd type");93char* __n = catgets(__cat, __set, __msgid, __ndflt.c_str());94string_type __w;95__widen_from_utf8<sizeof(char_type) * __CHAR_BIT__>()(std::back_inserter(__w), __n, __n + std::strlen(__n));96return __w;97# else // !_LIBCPP_HAS_CATOPEN98(void)__c;99(void)__set;100(void)__msgid;101return __dflt;102# endif // _LIBCPP_HAS_CATOPEN103}104105template <class _CharT>106void messages<_CharT>::do_close(catalog __c) const {107# if _LIBCPP_HAS_CATOPEN108catclose((nl_catd)__c);109# else // !_LIBCPP_HAS_CATOPEN110(void)__c;111# endif // _LIBCPP_HAS_CATOPEN112}113114extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages<char>;115# if _LIBCPP_HAS_WIDE_CHARACTERS116extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages<wchar_t>;117# endif118119template <class _CharT>120class messages_byname : public messages<_CharT> {121public:122typedef messages_base::catalog catalog;123typedef basic_string<_CharT> string_type;124125_LIBCPP_HIDE_FROM_ABI explicit messages_byname(const char*, size_t __refs = 0) : messages<_CharT>(__refs) {}126127_LIBCPP_HIDE_FROM_ABI explicit messages_byname(const string&, size_t __refs = 0) : messages<_CharT>(__refs) {}128129protected:130_LIBCPP_HIDE_FROM_ABI_VIRTUAL ~messages_byname() override {}131};132133extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages_byname<char>;134# if _LIBCPP_HAS_WIDE_CHARACTERS135extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages_byname<wchar_t>;136# endif137138_LIBCPP_END_NAMESPACE_STD139140#endif // _LIBCPP_HAS_LOCALIZATION141142#endif // _LIBCPP___LOCALE_DIR_MESSAGES_H143144145