c++windowsqtunicodetaglib

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.

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


Solution

  • 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().