Charset.cxx 3.59 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 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 24
 * 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 "Limits.hxx"
#include "Log.hxx"
25
#include "lib/icu/Converter.hxx"
26
#include "util/Error.hxx"
27
#include "util/AllocatedString.hxx"
28

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

34 35
#include <algorithm>

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
bool
SetFSCharset(const char *charset, Error &error)
47
{
48
	assert(charset != nullptr);
49
	assert(fs_converter == nullptr);
50

51 52
	fs_converter = IcuConverter::Create(charset, error);
	if (fs_converter == nullptr)
53
		return false;
54 55 56

	FormatDebug(path_domain,
		    "SetFSCharset: fs charset is: %s", fs_charset.c_str());
57
	return true;
58 59
}

60 61
#endif

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

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

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

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

95
	return std::move(s);
96 97
}

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

106
#ifdef WIN32
107 108
	const auto buffer = WideCharToMultiByte(CP_UTF8, path_fs);
	if (buffer.IsNull())
109 110
		return PathTraitsUTF8::string();

111
	return FixSeparators(PathTraitsUTF8::string(buffer.c_str()));
112
#else
113
#ifdef HAVE_FS_CHARSET
114
	if (fs_converter == nullptr)
115
#endif
116
		return FixSeparators(path_fs);
117
#ifdef HAVE_FS_CHARSET
118

119 120 121 122 123
	const auto buffer = fs_converter->ToUTF8(path_fs);
	if (buffer.IsNull())
		return PathTraitsUTF8::string();

	return FixSeparators(PathTraitsUTF8::string(buffer.c_str()));
124
#endif
125
#endif
126 127
}

128
#if defined(HAVE_FS_CHARSET) || defined(WIN32)
129

130 131
PathTraitsFS::string
PathFromUTF8(PathTraitsUTF8::const_pointer path_utf8)
132
{
133 134
#if !CLANG_CHECK_VERSION(3,6)
	/* disabled on clang due to -Wtautological-pointer-compare */
135
	assert(path_utf8 != nullptr);
136
#endif
137

138
#ifdef WIN32
139 140
	const auto buffer = MultiByteToWideChar(CP_UTF8, path_utf8);
	if (buffer.IsNull())
141 142
		return PathTraitsFS::string();

143
	return PathTraitsFS::string(buffer.c_str());
144
#else
145
	if (fs_converter == nullptr)
146
		return path_utf8;
147

148 149 150 151 152
	const auto buffer = fs_converter->FromUTF8(path_utf8);
	if (buffer.IsNull())
		return PathTraitsFS::string();

	return PathTraitsFS::string(buffer.c_str());
153
#endif
154
}
155 156

#endif