Charset.cxx 3 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "Charset.hxx"
21
#include "Features.hxx"
22 23
#include "Domain.hxx"
#include "Log.hxx"
24
#include "lib/icu/Converter.hxx"
25
#include "util/AllocatedString.hxx"
26
#include "config.h"
27

28
#ifdef _WIN32
29
#include "lib/icu/Win32.hxx"
30 31 32
#include <windows.h>
#endif

33
#include <algorithm>
34
#include <cassert>
35

36
#ifdef HAVE_FS_CHARSET
37

38 39
static std::string fs_charset;

40
static std::unique_ptr<IcuConverter> fs_converter;
41

42 43
void
SetFSCharset(const char *charset)
44
{
45
	assert(charset != nullptr);
46
	assert(fs_converter == nullptr);
47

48 49
	fs_converter = IcuConverter::Create(charset);
	assert(fs_converter != nullptr);
50 51 52 53 54

	FormatDebug(path_domain,
		    "SetFSCharset: fs charset is: %s", fs_charset.c_str());
}

55 56
#endif

57
void
58
DeinitFSCharset() noexcept
59
{
60
#ifdef HAVE_ICU_CONVERTER
61
	fs_converter.reset();
62
#endif
63 64
}

65
const char *
66
GetFSCharset() noexcept
67
{
68
#ifdef HAVE_FS_CHARSET
69
	return fs_charset.empty() ? "UTF-8" : fs_charset.c_str();
70
#elif defined(_WIN32)
71
	return "ACP";
72
#else
73
	return "UTF-8";
74
#endif
75 76
}

77 78
static inline PathTraitsUTF8::string
FixSeparators(const PathTraitsUTF8::string_view _s)
79 80 81 82
{
	// For whatever reason GCC can't convert constexpr to value reference.
	// This leads to link errors when passing separators directly.
	auto to = PathTraitsUTF8::SEPARATOR;
83
	decltype(to) from = PathTraitsFS::SEPARATOR;
84

85 86
	PathTraitsUTF8::string s(_s);

87 88 89 90
	if (from != to)
		/* convert backslash to slash on WIN32 */
		std::replace(s.begin(), s.end(), from, to);

91
	return s;
92 93
}

94
PathTraitsUTF8::string
95
PathToUTF8(PathTraitsFS::string_view path_fs)
96
{
97
#ifdef _WIN32
98
	const auto buffer = WideCharToMultiByte(CP_UTF8, path_fs);
99
	return FixSeparators(buffer);
100
#else
101
#ifdef HAVE_FS_CHARSET
102
	if (fs_converter == nullptr)
103
#endif
104
		return FixSeparators(path_fs);
105
#ifdef HAVE_FS_CHARSET
106

107
	const auto buffer = fs_converter->ToUTF8(path_fs);
108
	return FixSeparators(buffer);
109
#endif
110
#endif
111 112
}

113
#if defined(HAVE_FS_CHARSET) || defined(_WIN32)
114

115
PathTraitsFS::string
116
PathFromUTF8(PathTraitsUTF8::string_view path_utf8)
117
{
118
#ifdef _WIN32
119
	const auto buffer = MultiByteToWideChar(CP_UTF8, path_utf8);
120
	return PathTraitsFS::string(buffer);
121
#else
122
	if (fs_converter == nullptr)
123
		return PathTraitsFS::string(path_utf8);
124

125
	const auto buffer = fs_converter->FromUTF8(path_utf8);
126
	return PathTraitsFS::string(buffer);
127
#endif
128
}
129 130

#endif