Charset.cxx 3.32 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * 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 "config.h"
#include "Charset.hxx"
#include "Domain.hxx"
#include "Log.hxx"
24
#include "lib/icu/Converter.hxx"
25
#include "util/AllocatedString.hxx"
26

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

32
#include <algorithm>
33
#include <stdexcept>
34

35 36 37
#include <assert.h>
#include <string.h>

38
#ifdef HAVE_FS_CHARSET
39

40 41
static std::string fs_charset;

42
static IcuConverter *fs_converter;
43

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

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

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

57 58
#endif

59
void
60
DeinitFSCharset() noexcept
61
{
62 63 64 65
#ifdef HAVE_ICU_CONVERTER
	delete fs_converter;
	fs_converter = nullptr;
#endif
66 67
}

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

80 81
static inline PathTraitsUTF8::string &&
FixSeparators(PathTraitsUTF8::string &&s)
82 83 84 85
{
	// For whatever reason GCC can't convert constexpr to value reference.
	// This leads to link errors when passing separators directly.
	auto to = PathTraitsUTF8::SEPARATOR;
86
	decltype(to) from = PathTraitsFS::SEPARATOR;
87 88 89 90 91

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

92
	return std::move(s);
93 94
}

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

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

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

119
#if defined(HAVE_FS_CHARSET) || defined(_WIN32)
120

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

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

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

#endif