简体   繁体   English

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

[英]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_tQChar都具有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.

相关问题 如何检查文本文件是否以UTF-8编码? - How to check whether text file is encoded in UTF-8? 如何使用std :: ifstream读取UTF-8编码的文本文件? - How to read UTF-8 encoded text file using std::ifstream? 如何在Windows中用Windows编写UTF-8编码的字符串到文件中 - How do I write a UTF-8 encoded string to a file in windows, in C++ C++:如何将 std::string 的内容写入 UTF-8 编码文件? - C++: How do I write the contents of std::string to a UTF-8 encoded file? 如何使用QTextStream在Linux上创建ISO 8859-15(而不是默认的UTF-8)编码的文本文件? - How to create an ISO 8859-15 (instead of default UTF-8) encoded text file on Linux using QTextStream? 如何读取包含中文字符的UTF-8编码文件并在控制台上正确输出? - How to read an UTF-8 encoded file containing Chinese characters and output them correctly on console? C++17:检查文件是否编码为UTF-8 - C++17: Check if a file is encoded in UTF-8 将 shift-jis 编码文件转换为 c++ 中的 utf-8 - Converting shift-jis encoded file to to utf-8 in c++ 带有国家符号的C ++和文件路径(可能用utf8编码) - C++ and file paths with national symbols (encoded with utf8, maybe) 如何将以utf16编码的字符串转换为以UTF-8编码的字符串? - How to convert a string encoded in utf16 to a string encoded in UTF-8?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM