Charset.cxx 3.35 KB
Newer Older
1
/*
2
 * Copyright 2003-2019 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 <stdexcept>
35

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

39
#ifdef HAVE_FS_CHARSET
40

41 42
static std::string fs_charset;

43
static IcuConverter *fs_converter;
44

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

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

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

58 59
#endif

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

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

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

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

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

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

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

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

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

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

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

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

#endif