diff options
Diffstat (limited to 'src/fs/Charset.cxx')
-rw-r--r-- | src/fs/Charset.cxx | 51 |
1 files changed, 44 insertions, 7 deletions
diff --git a/src/fs/Charset.cxx b/src/fs/Charset.cxx index dad5779f9..abf4d8287 100644 --- a/src/fs/Charset.cxx +++ b/src/fs/Charset.cxx @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2013 The Music Player Daemon Project + * Copyright (C) 2003-2014 The Music Player Daemon Project * http://www.musicpd.org * * This program is free software; you can redistribute it and/or modify @@ -22,15 +22,20 @@ #include "Domain.hxx" #include "Limits.hxx" #include "system/FatalError.hxx" -#include "util/Error.hxx" -#include "util/Domain.hxx" #include "Log.hxx" +#include "Traits.hxx" +#ifdef HAVE_GLIB #include <glib.h> +#endif + +#include <algorithm> #include <assert.h> #include <string.h> +#ifdef HAVE_GLIB + /** * Maximal number of bytes required to represent path name in UTF-8 * (including nul-terminator). @@ -70,10 +75,29 @@ SetFSCharset(const char *charset) "SetFSCharset: fs charset is: %s", fs_charset.c_str()); } +#endif + const char * GetFSCharset() { - return fs_charset.empty() ? "utf-8" : fs_charset.c_str(); +#ifdef HAVE_GLIB + return fs_charset.empty() ? "UTF-8" : fs_charset.c_str(); +#else + return "UTF-8"; +#endif +} + +static inline void FixSeparators(std::string &s) +{ +#ifdef WIN32 + // For whatever reason GCC can't convert constexpr to value reference. + // This leads to link errors when passing separators directly. + auto from = PathTraitsFS::SEPARATOR; + auto to = PathTraitsUTF8::SEPARATOR; + std::replace(s.begin(), s.end(), from, to); +#else + (void)s; +#endif } std::string @@ -81,8 +105,14 @@ PathToUTF8(const char *path_fs) { assert(path_fs != nullptr); - if (fs_charset.empty()) - return std::string(path_fs); +#ifdef HAVE_GLIB + if (fs_charset.empty()) { +#endif + auto result = std::string(path_fs); + FixSeparators(result); + return result; +#ifdef HAVE_GLIB + } GIConv conv = g_iconv_open("utf-8", fs_charset.c_str()); if (conv == reinterpret_cast<GIConv>(-1)) @@ -103,9 +133,14 @@ PathToUTF8(const char *path_fs) if (ret == static_cast<size_t>(-1) || in_left > 0) return std::string(); - return std::string(path_utf8, sizeof(path_utf8) - out_left); + auto result_path = std::string(path_utf8, sizeof(path_utf8) - out_left); + FixSeparators(result_path); + return result_path; +#endif } +#ifdef HAVE_GLIB + char * PathFromUTF8(const char *path_utf8) { @@ -118,3 +153,5 @@ PathFromUTF8(const char *path_utf8) fs_charset.c_str(), "utf-8", nullptr, nullptr, nullptr); } + +#endif |