TagLib - 如何处理 UTF-8 编码的文件路径?

发布于 2024-10-31 14:36:07 字数 1260 浏览 1 评论 0原文

问题:我希望 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦醒灬来后我 2024-11-07 14:36:07

FileRef 构造函数接受一个 FileName 对象(不是 String!),该对象可以是 char*wchar_t* 字符串。在 Windows 上,您可以假设 wchar_tQChar 都有 16 位,因此您只需传递 filePath.constData() 即可。

请注意,如果您计划在非 Windows 平台上运行代码,则需要围绕 FileRef 对象创建进行 #ifdef 检查,因为在其他平台上它只接受 char* 字符串,您应该使用 QFile::encodeName(filePath).constData()

The FileRef constructor accepts a FileName object (not String!), which can be either char* or wchar_t* string. On Windows you can assume that both wchar_t and QChar have 16-bits, so you can simply pass it 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().

山人契 2024-11-07 14:36:07

另一种方法是使用 toStdString() 后跟 c_str()

QString path(...);
std::string stdString = path.toStdString();
const char *cPath = stdString.c_str();
TagLib::FileRef fr(cPath);

确保将 stdString 存储为变量,否则它将在行尾被销毁。

至少在我的例子中,QFile::encodeName(filePath)QString::toLocal8Bit()都失败了 - 它用替换了ąęóżłćĄĘŁ >aeózlcAEL (看起来 ó 很幸运地幸存下来),这导致我的应用程序崩溃。不幸的是,我无法回答为什么 Qt 方法在我的案例中不起作用。

Another way to is to use toStdString() followed by c_str().

QString path(...);
std::string stdString = path.toStdString();
const char *cPath = stdString.c_str();
TagLib::FileRef fr(cPath);

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) and QString::toLocal8Bit() failed - it replaced ąęóżłćĄĘŁ with aeó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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文