Charset.cxx 3.29 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 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(PathTraitsUTF8::string &&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 87 88

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

89
	return std::move(s);
90 91
}

92
PathTraitsUTF8::string
93
PathToUTF8(PathTraitsFS::const_pointer path_fs)
94
{
95 96
#if !CLANG_CHECK_VERSION(3,6)
	/* disabled on clang due to -Wtautological-pointer-compare */
97
	assert(path_fs != nullptr);
98
#endif
99

100
#ifdef _WIN32
101 102
	const auto buffer = WideCharToMultiByte(CP_UTF8, path_fs);
	return FixSeparators(PathTraitsUTF8::string(buffer.c_str()));
103
#else
104
#ifdef HAVE_FS_CHARSET
105
	if (fs_converter == nullptr)
106
#endif
107
		return FixSeparators(path_fs);
108
#ifdef HAVE_FS_CHARSET
109

110 111
	const auto buffer = fs_converter->ToUTF8(path_fs);
	return FixSeparators(PathTraitsUTF8::string(buffer.c_str()));
112
#endif
113
#endif
114 115
}

116
#if defined(HAVE_FS_CHARSET) || defined(_WIN32)
117

118
PathTraitsFS::string
119
PathFromUTF8(PathTraitsUTF8::const_pointer path_utf8)
120
{
121 122
#if !CLANG_CHECK_VERSION(3,6)
	/* disabled on clang due to -Wtautological-pointer-compare */
123
	assert(path_utf8 != nullptr);
124
#endif
125

126
#ifdef _WIN32
127 128
	const auto buffer = MultiByteToWideChar(CP_UTF8, path_utf8);
	return PathTraitsFS::string(buffer.c_str());
129
#else
130
	if (fs_converter == nullptr)
131
		return path_utf8;
132

133 134
	const auto buffer = fs_converter->FromUTF8(path_utf8);
	return PathTraitsFS::string(buffer.c_str());
135
#endif
136
}
137 138

#endif