Path: convert fs_charset_to_utf8() to static method Path::ToUTF8()
This commit is contained in:
@@ -48,24 +48,31 @@
|
||||
|
||||
static char *fs_charset;
|
||||
|
||||
std::string Path::ToUTF8() const
|
||||
std::string Path::ToUTF8(const_pointer path_fs)
|
||||
{
|
||||
if (value == nullptr)
|
||||
if (path_fs == 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;
|
||||
}
|
||||
|
||||
char *
|
||||
fs_charset_to_utf8(const char *path_fs)
|
||||
{
|
||||
return g_convert(path_fs, -1,
|
||||
"utf-8", fs_charset,
|
||||
NULL, NULL, NULL);
|
||||
GIConv conv = g_iconv_open("utf-8", fs_charset);
|
||||
if (conv == reinterpret_cast<GIConv>(-1))
|
||||
return std::string();
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
char *
|
||||
|
@@ -48,13 +48,6 @@ void path_global_init();
|
||||
|
||||
void path_global_finish();
|
||||
|
||||
/**
|
||||
* Converts a file name in the filesystem charset to UTF-8. Returns
|
||||
* NULL on failure.
|
||||
*/
|
||||
char *
|
||||
fs_charset_to_utf8(const char *path_fs);
|
||||
|
||||
/**
|
||||
* Converts a file name in UTF-8 to the filesystem charset. Returns a
|
||||
* duplicate of the UTF-8 string on failure.
|
||||
@@ -173,6 +166,13 @@ public:
|
||||
return Path(Donate(), utf8_to_fs_charset(utf8));
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the path to UTF-8.
|
||||
* Returns empty string on error or if #path_fs is null pointer.
|
||||
*/
|
||||
gcc_pure
|
||||
static std::string ToUTF8(const_pointer path_fs);
|
||||
|
||||
/**
|
||||
* Copy a #Path object.
|
||||
*/
|
||||
@@ -257,7 +257,9 @@ public:
|
||||
* Returns empty string on error or if this instance is "nulled"
|
||||
* (#IsNull returns true).
|
||||
*/
|
||||
std::string ToUTF8() const;
|
||||
std::string ToUTF8() const {
|
||||
return ToUTF8(value);
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user