TagLib - 如何处理 UTF-8 编码的文件路径?
问题:我希望 TagLib::FileRef 打开文件名或路径中包含 Unicode 字符的文件,但没有成功。
在使用 CMake 2.8.4 创建 Makefile 后,我在 Windows 7(64 位,但 32 位编译器)下使用 MinGW(GNU Make 3.81、GCC 4.4.0)编译了 TagLib 1.7。我将 TagLib 与 Qt 4.7.2 框架结合使用。
为了在两个库之间交换字符串,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.
我构建了 TagLib::FileRef,如下所示:
TagLib::FileRef fileRef( QStringToTString(filePath).toCString(true) );
// or:
TagLib::FileRef fileRef( TagLib::FileName( QStringToTString(filePath).toCString(true) ) );
结果,路径包含 Unicode 字符或 Latin-1 字符的文件在 UTF-8 表示形式中具有不同的代码(例如:元音变音或中文字符)无法加载(fileRef.isNull())。
如果我将 false 传递给上面的函数 toCString() (声明:见下文),TagLib 可以处理变音符号(但不能处理仅限 Unicode 的字符)。因此我猜我错误地编译了 TagLib (TagLib::FileRef 将给定的字符串解释为 Latin-1),但我不知道如何检查甚至纠正它。注意:使用 TagLib 可以正确检索 (ID3) 标记中的 Unicode 字符串。
toCString():
const char* TagLib::String::toCString( bool unicode = false ) const;
文档:TagLib 文档
Problem: I want TagLib::FileRef to open a file with Unicode characters in its file name or path, but do not succeed.
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. I use TagLib in conjunction with the Qt 4.7.2 framework.
For interchanging strings between the two libraries, TagLib provides:
#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 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()).
If I pass false to the function toCString() above (declaration: see below), TagLib can handle umlauts (but not Unicode-only characters). 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. Note: Unicode strings in (ID3) tags are retrieved correctly with TagLib.
toCString():
const char* TagLib::String::toCString( bool unicode = false ) const;
Doc: TagLib documentation
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
FileRef
构造函数接受一个FileName
对象(不是String
!),该对象可以是char*
或wchar_t*
字符串。在 Windows 上,您可以假设wchar_t
和QChar
都有 16 位,因此您只需传递filePath.constData()
即可。请注意,如果您计划在非 Windows 平台上运行代码,则需要围绕
FileRef
对象创建进行#ifdef
检查,因为在其他平台上它只接受char*
字符串,您应该使用QFile::encodeName(filePath).constData()
。The
FileRef
constructor accepts aFileName
object (notString
!), which can be eitherchar*
orwchar_t*
string. On Windows you can assume that bothwchar_t
andQChar
have 16-bits, so you can simply pass itfilePath.constData()
.Note that if you are planning to run the code on non-Windows platform, you need to have an
#ifdef
check around theFileRef
object creation, because on other platforms it only acceptschar*
strings and you should useQFile::encodeName(filePath).constData()
.另一种方法是使用
toStdString()
后跟c_str()
。确保将 stdString 存储为变量,否则它将在行尾被销毁。
至少在我的例子中,
QFile::encodeName(filePath)
和QString::toLocal8Bit()
都失败了 - 它用替换了
(看起来ąęóżłćĄĘŁ
>aeózlcAELó
很幸运地幸存下来),这导致我的应用程序崩溃。不幸的是,我无法回答为什么 Qt 方法在我的案例中不起作用。Another way to is to use
toStdString()
followed byc_str()
.Make sure to store the stdString as a variable, as without that it will be destroyed by the end of the line.
At least in my case, both
QFile::encodeName(filePath)
andQString::toLocal8Bit()
failed - it replacedąęóżłćĄĘŁ
withaeózlcAEL
(looks likeó
was lucky enough to survive that), which caused my application to crash. Unfortunately I am unable to answer why the Qt methods didn't work in my case.