1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2020 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef CHARSET_H
21 #define CHARSET_H
22 
23 #include "config.h"
24 #include "util/Compiler.h"
25 
26 #include <string>
27 
28 #include <stddef.h>
29 
30 #ifdef HAVE_ICONV
31 void
32 charset_init() noexcept;
33 #endif
34 
35 char *
36 CopyUtf8ToLocale(char *dest, size_t dest_size, const char *src) noexcept;
37 
38 char *
39 CopyUtf8ToLocale(char *dest, size_t dest_size,
40 		 const char *src, size_t src_length) noexcept;
41 
42 gcc_pure
43 const char *
44 utf8_to_locale(const char *src, char *buffer, size_t size) noexcept;
45 
46 /**
47  * Convert an UTF-8 string to the locale charset.  The source string
48  * must remain valid while this object is used.  If no conversion is
49  * necessary, then this class is a no-op.
50  */
51 class Utf8ToLocale {
52 #ifdef HAVE_ICONV
53 	const std::string value;
54 #else
55 	const char *const value;
56 #endif
57 
58 public:
59 #ifdef HAVE_ICONV
60 	explicit Utf8ToLocale(const char *src) noexcept;
61 	Utf8ToLocale(const char *src, size_t length) noexcept;
62 
63 	Utf8ToLocale(const Utf8ToLocale &) = delete;
64 	Utf8ToLocale &operator=(const Utf8ToLocale &) = delete;
65 #else
66 	explicit Utf8ToLocale(const char *src) noexcept
67 		:value(src) {}
68 #endif
69 
c_str() const70 	const char *c_str() const noexcept {
71 #ifdef HAVE_ICONV
72 		return value.c_str();
73 #else
74 		return value;
75 #endif
76 	}
77 };
78 
79 /**
80  * Convert an locale charset string to UTF-8.  The source string must
81  * remain valid while this object is used.  If no conversion is
82  * necessary, then this class is a no-op.
83  */
84 class LocaleToUtf8 {
85 #ifdef HAVE_ICONV
86 	const std::string value;
87 #else
88 	const char *const value;
89 #endif
90 
91 public:
92 #ifdef HAVE_ICONV
93 	explicit LocaleToUtf8(const char *src) noexcept;
94 
95 	LocaleToUtf8(const LocaleToUtf8 &) = delete;
96 	LocaleToUtf8 &operator=(const LocaleToUtf8 &) = delete;
97 #else
98 	explicit LocaleToUtf8(const char *src) noexcept
99 		:value(src) {}
100 #endif
101 
c_str() const102 	const char *c_str() const noexcept {
103 #ifdef HAVE_ICONV
104 		return value.c_str();
105 #else
106 		return value;
107 #endif
108 	}
109 };
110 
111 #endif
112