Charset.cxx 3.78 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

28 29 30 31
#ifdef WIN32
#include <windows.h>
#endif

32 33
#include <algorithm>

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

37
#ifdef HAVE_FS_CHARSET
38

39 40
static std::string fs_charset;

41
static IcuConverter *fs_converter;
42

43 44
bool
SetFSCharset(const char *charset, Error &error)
45
{
46
	assert(charset != nullptr);
47
	assert(fs_converter == nullptr);
48

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

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

58 59
#endif

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

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

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

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

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

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

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
#ifdef WIN32
	int length = WideCharToMultiByte(CP_UTF8, 0, path_fs, -1, nullptr, 0,
					 nullptr, nullptr);
	if (length <= 0)
		return PathTraitsUTF8::string();

	char *buffer = new char[length];
	length = WideCharToMultiByte(CP_UTF8, 0, path_fs, -1, buffer, length,
				     nullptr, nullptr);
	if (length <= 0) {
		delete[] buffer;
		return PathTraitsUTF8::string();
	}

	PathTraitsUTF8::string result(buffer);
	delete[] buffer;
	return FixSeparators(std::move(result));
#else
120
#ifdef HAVE_FS_CHARSET
121
	if (fs_converter == nullptr)
122
#endif
123
		return FixSeparators(path_fs);
124
#ifdef HAVE_FS_CHARSET
125

126
	return FixSeparators(fs_converter->ToUTF8(path_fs));
127
#endif
128
#endif
129 130
}

131
#if defined(HAVE_FS_CHARSET) || defined(WIN32)
132

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

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
#ifdef WIN32
	int length = MultiByteToWideChar(CP_UTF8, 0, path_utf8, -1,
					 nullptr, 0);
	if (length <= 0)
		return PathTraitsFS::string();

	wchar_t *buffer = new wchar_t[length];
	length = MultiByteToWideChar(CP_UTF8, 0, path_utf8, -1,
				     buffer, length);
	if (length <= 0) {
		delete[] buffer;
		return PathTraitsFS::string();
	}

	PathTraitsFS::string result(buffer);
	delete[] buffer;
	return std::move(result);
#else
159
	if (fs_converter == nullptr)
160
		return path_utf8;
161

162
	return fs_converter->FromUTF8(path_utf8);
163
#endif
164
}
165 166

#endif