[英]TagLib - How to handle UTF-8 encoded file paths?
Problem: I want TagLib::FileRef to open a file with Unicode characters in its file name or path, but do not succeed. 问题:我希望TagLib :: FileRef打开文件名或路径中带有Unicode字符的文件,但不会成功。
I compiled TagLib 1.7 with MinGW (GNU Make 3.81, GCC 4.4.0) under Windows 7 (64-bit, but 32-bit compiler) after creating the Makefile with CMake 2.8.4. 在使用CMake 2.8.4创建Makefile之后,我在Windows 7(64位但32位编译器)下使用MinGW(GNU Make 3.81,GCC 4.4.0)编译了TagLib 1.7。 I use TagLib in conjunction with the Qt 4.7.2 framework.
我将TagLib与Qt 4.7.2框架结合使用。
For interchanging strings between the two libraries, TagLib provides: 为了在两个库之间交换字符串,TagLib提供了:
#define QStringToTString(s) TagLib::String(s.toUtf8().data(), TagLib::String::UTF8)
#define TStringToQString(s) QString::fromUtf8(s.toCString(true))
// toUtf8() is a fix, originally spelled utf8(), but that's not relevant here.
I constructed TagLib::FileRef as follows: 我按如下方式构造TagLib :: FileRef:
TagLib::FileRef fileRef( QStringToTString(filePath).toCString(true) );
// or:
TagLib::FileRef fileRef( TagLib::FileName( QStringToTString(filePath).toCString(true) ) );
As a result, files with paths containing Unicode characters or Latin-1 characters with a different code in the UTF-8 representation (f.ex. umlauts or chinese characters) fail to load (fileRef.isNull()). 结果,路径中包含Unicode字符或Latin-1字符的代码以UTF-8表示形式的不同代码(例如,变音符号或中文字符)无法加载(fileRef.isNull())。
If I pass false to the function toCString() above (declaration: see below), TagLib can handle umlauts (but not Unicode-only characters). 如果将false传递给上面的函数toCString()(声明:见下文),TagLib可以处理变音符(但不能处理仅Unicode字符)。 Therefore I guess I have compiled TagLib wrongly (TagLib::FileRef interpretes the given string as Latin-1), but I don't know how to check or even correct this.
因此,我想我错误地编译了TagLib(TagLib :: FileRef将给定的字符串解释为Latin-1),但是我不知道如何检查甚至更正此错误。 Note: Unicode strings in (ID3) tags are retrieved correctly with TagLib.
注意:(ID3)标记中的Unicode字符串可使用TagLib正确检索。
toCString(): toCString():
const char* TagLib::String::toCString( bool unicode = false ) const;
Doc: TagLib documentation Doc: TagLib文档
The FileRef
constructor accepts a FileName
object (not String
!), which can be either char*
or wchar_t*
string. FileRef
构造函数接受FileName
对象(不是String
!),该对象可以是char*
或wchar_t*
字符串。 On Windows you can assume that both wchar_t
and QChar
have 16-bits, so you can simply pass it filePath.constData()
. 在Windows上,您可以假设
wchar_t
和QChar
都具有16位,因此您可以简单地将其传递给filePath.constData()
。
Note that if you are planning to run the code on non-Windows platform, you need to have an #ifdef
check around the FileRef
object creation, because on other platforms it only accepts char*
strings and you should use QFile::encodeName(filePath).constData()
. 请注意,如果您打算在非Windows平台上运行代码,则需要对
FileRef
对象的创建进行一次#ifdef
检查,因为在其他平台上,它仅接受char*
字符串,因此您应该使用QFile::encodeName(filePath).constData()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.