Path.cxx 3.76 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2013 The Music Player Daemon Project
3
 * http://www.musicpd.org
Warren Dukes's avatar
Warren Dukes committed
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.
Warren Dukes's avatar
Warren Dukes committed
18 19
 */

20
#include "config.h"
21
#include "fs/Path.hxx"
Warren Dukes's avatar
Warren Dukes committed
22
#include "conf.h"
23
#include "mpd_error.h"
24
#include "gcc.h"
Warren Dukes's avatar
Warren Dukes committed
25

26 27
#include <glib.h>

28
#include <assert.h>
Max Kellermann's avatar
Max Kellermann committed
29 30
#include <string.h>

31 32 33 34 35
#ifdef G_OS_WIN32
#include <windows.h> // for GetACP()
#include <stdio.h> // for sprintf()
#endif

Max Kellermann's avatar
Max Kellermann committed
36 37 38
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "path"

39 40 41 42 43 44 45 46 47 48
/**
 * Maximal number of bytes required to represent path name in UTF-8
 * (including nul-terminator).
 * This value is a rought estimate of upper bound.
 * It's based on path name limit in bytes (MPD_PATH_MAX)
 * and assumption that some weird encoding could represent some UTF-8 4 byte
 * sequences with single byte.
 */
#define MPD_PATH_MAX_UTF8 ((MPD_PATH_MAX - 1) * 4 + 1)

49
std::string fs_charset;
Warren Dukes's avatar
Warren Dukes committed
50

51
std::string Path::ToUTF8(const_pointer path_fs)
52
{
53
	if (path_fs == nullptr)
54
		return std::string();
55

56
	GIConv conv = g_iconv_open("utf-8", fs_charset.c_str());
57
	if (conv == reinterpret_cast<GIConv>(-1))
58 59
		return std::string();

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
	// g_iconv() does not need nul-terminator,
	// std::string could be created without it too.
	char path_utf8[MPD_PATH_MAX_UTF8 - 1];
	char *in = const_cast<char *>(path_fs);
	char *out = path_utf8;
	size_t in_left = strlen(path_fs);
	size_t out_left = sizeof(path_utf8);

	size_t ret = g_iconv(conv, &in, &in_left, &out, &out_left);

	g_iconv_close(conv);

	if (ret == static_cast<size_t>(-1) || in_left > 0)
		return std::string();

	return std::string(path_utf8, sizeof(path_utf8) - out_left);
Warren Dukes's avatar
Warren Dukes committed
76 77
}

78
Path Path::FromUTF8(const char *path_utf8)
Avuton Olrich's avatar
Avuton Olrich committed
79
{
80 81
	gchar *p;

82
	p = g_convert(path_utf8, -1,
83
		      fs_charset.c_str(), "utf-8",
84
		      NULL, NULL, NULL);
85

86
	return Path(Donate(), p);
Warren Dukes's avatar
Warren Dukes committed
87 88
}

89 90 91 92 93 94 95 96 97 98 99 100 101
gcc_pure
static bool
IsSupportedCharset(const char *charset)
{
	/* convert a space to check if the charset is valid */
	char *test = g_convert(" ", 1, charset, "UTF-8", NULL, NULL, NULL);
	if (test == NULL)
		return false;

	g_free(test);
	return true;
}

102
static void
103
SetFSCharset(const char *charset)
Avuton Olrich's avatar
Avuton Olrich committed
104
{
105 106
	assert(charset != NULL);

107
	if (!IsSupportedCharset(charset))
108
		MPD_ERROR("invalid filesystem charset: %s", charset);
109

110
	fs_charset = charset;
Warren Dukes's avatar
Warren Dukes committed
111

112
	g_debug("SetFSCharset: fs charset is: %s", fs_charset.c_str());
Warren Dukes's avatar
Warren Dukes committed
113 114
}

115
const std::string &Path::GetFSCharset()
Avuton Olrich's avatar
Avuton Olrich committed
116
{
Max Kellermann's avatar
Max Kellermann committed
117
	return fs_charset;
Warren Dukes's avatar
Warren Dukes committed
118 119
}

120
void Path::GlobalInit()
Avuton Olrich's avatar
Avuton Olrich committed
121
{
122
	const char *charset = NULL;
123

124 125
	charset = config_get_string(CONF_FS_CHARSET, NULL);
	if (charset == NULL) {
126
#ifndef G_OS_WIN32
127 128 129 130
		const gchar **encodings;
		g_get_filename_charsets(&encodings);

		if (encodings[0] != NULL && *encodings[0] != '\0')
131
			charset = encodings[0];
132 133 134 135 136 137
#else /* G_OS_WIN32 */
		/* Glib claims that file system encoding is always utf-8
		 * on native Win32 (i.e. not Cygwin).
		 * However this is true only if <gstdio.h> helpers are used.
		 * MPD uses regular <stdio.h> functions.
		 * Those functions use encoding determined by GetACP(). */
138
		static char win_charset[13];
139 140 141
		sprintf(win_charset, "cp%u", GetACP());
		charset = win_charset;
#endif
142
	}
Warren Dukes's avatar
Warren Dukes committed
143

Avuton Olrich's avatar
Avuton Olrich committed
144
	if (charset) {
145
		SetFSCharset(charset);
Avuton Olrich's avatar
Avuton Olrich committed
146
	} else {
Max Kellermann's avatar
Max Kellermann committed
147
		g_message("setting filesystem charset to ISO-8859-1");
148
		SetFSCharset("ISO-8859-1");
Warren Dukes's avatar
Warren Dukes committed
149 150
	}
}