2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2013-01-17 00:43:27 +01:00
|
|
|
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2004-02-24 00:41:20 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2004-02-24 00:41:20 +01:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-01-21 19:14:37 +01:00
|
|
|
#include "fs/Path.hxx"
|
2004-02-24 00:41:20 +01:00
|
|
|
#include "conf.h"
|
2010-09-25 15:00:43 +02:00
|
|
|
#include "mpd_error.h"
|
2013-01-10 18:06:21 +01:00
|
|
|
#include "gcc.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-10-15 19:36:30 +02:00
|
|
|
#include <glib.h>
|
|
|
|
|
2009-02-20 12:31:00 +01:00
|
|
|
#include <assert.h>
|
2008-12-29 17:28:32 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2011-10-21 20:57:30 +02:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
#include <windows.h> // for GetACP()
|
|
|
|
#include <stdio.h> // for sprintf()
|
|
|
|
#endif
|
|
|
|
|
2008-12-29 17:32:53 +01:00
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#define G_LOG_DOMAIN "path"
|
|
|
|
|
2013-01-23 19:56:46 +01:00
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
|
2008-11-05 18:21:52 +01:00
|
|
|
static char *fs_charset;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2013-01-23 19:48:14 +01:00
|
|
|
std::string Path::ToUTF8() const
|
|
|
|
{
|
|
|
|
if (value == nullptr)
|
|
|
|
return std::string();
|
|
|
|
char *path_utf8 = fs_charset_to_utf8(value);
|
|
|
|
if (path_utf8 == nullptr)
|
|
|
|
return std::string();
|
|
|
|
std::string result = value;
|
|
|
|
g_free(path_utf8);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2009-01-08 21:20:46 +01:00
|
|
|
char *
|
|
|
|
fs_charset_to_utf8(const char *path_fs)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-01-08 21:20:46 +01:00
|
|
|
return g_convert(path_fs, -1,
|
|
|
|
"utf-8", fs_charset,
|
|
|
|
NULL, NULL, NULL);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2009-01-08 21:20:46 +01:00
|
|
|
char *
|
|
|
|
utf8_to_fs_charset(const char *path_utf8)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-10-15 19:36:30 +02:00
|
|
|
gchar *p;
|
|
|
|
|
2009-01-08 21:20:46 +01:00
|
|
|
p = g_convert(path_utf8, -1,
|
2008-11-20 17:48:11 +01:00
|
|
|
fs_charset, "utf-8",
|
2009-01-04 19:50:22 +01:00
|
|
|
NULL, NULL, NULL);
|
|
|
|
if (p == NULL)
|
2008-10-15 19:36:30 +02:00
|
|
|
/* fall back to UTF-8 */
|
2009-01-08 21:20:46 +01:00
|
|
|
p = g_strdup(path_utf8);
|
2008-10-15 19:36:30 +02:00
|
|
|
|
2009-01-08 21:20:46 +01:00
|
|
|
return p;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2013-01-10 18:06:21 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-02-20 14:49:57 +01:00
|
|
|
static void
|
|
|
|
path_set_fs_charset(const char *charset)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-02-20 12:31:00 +01:00
|
|
|
assert(charset != NULL);
|
|
|
|
|
2013-01-10 18:06:21 +01:00
|
|
|
if (!IsSupportedCharset(charset))
|
2010-09-25 15:00:43 +02:00
|
|
|
MPD_ERROR("invalid filesystem charset: %s", charset);
|
2009-02-20 12:31:00 +01:00
|
|
|
|
2008-11-05 18:21:52 +01:00
|
|
|
g_free(fs_charset);
|
|
|
|
fs_charset = g_strdup(charset);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-12-29 17:32:53 +01:00
|
|
|
g_debug("path_set_fs_charset: fs charset is: %s", fs_charset);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-11-05 18:21:52 +01:00
|
|
|
const char *path_get_fs_charset(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-11-05 18:21:52 +01:00
|
|
|
return fs_charset;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-11-05 18:21:52 +01:00
|
|
|
void path_global_init(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-11-05 18:26:45 +01:00
|
|
|
const char *charset = NULL;
|
2005-03-06 20:00:58 +01:00
|
|
|
|
2009-01-25 16:00:51 +01:00
|
|
|
charset = config_get_string(CONF_FS_CHARSET, NULL);
|
|
|
|
if (charset == NULL) {
|
2011-10-21 20:57:30 +02:00
|
|
|
#ifndef G_OS_WIN32
|
2008-11-05 18:25:57 +01:00
|
|
|
const gchar **encodings;
|
|
|
|
g_get_filename_charsets(&encodings);
|
|
|
|
|
|
|
|
if (encodings[0] != NULL && *encodings[0] != '\0')
|
2008-11-05 18:26:45 +01:00
|
|
|
charset = encodings[0];
|
2011-10-21 20:57:30 +02:00
|
|
|
#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(). */
|
2013-01-10 17:54:11 +01:00
|
|
|
static char win_charset[13];
|
2011-10-21 20:57:30 +02:00
|
|
|
sprintf(win_charset, "cp%u", GetACP());
|
|
|
|
charset = win_charset;
|
|
|
|
#endif
|
2007-02-18 01:42:22 +01:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (charset) {
|
2008-11-05 18:21:52 +01:00
|
|
|
path_set_fs_charset(charset);
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
2008-12-29 17:32:53 +01:00
|
|
|
g_message("setting filesystem charset to ISO-8859-1");
|
2008-11-05 18:21:52 +01:00
|
|
|
path_set_fs_charset("ISO-8859-1");
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-05 18:21:52 +01:00
|
|
|
void path_global_finish(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-11-05 18:21:52 +01:00
|
|
|
g_free(fs_charset);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|