PART ONE
I clearly have a lack of knowledge regarding character encoding and the more I look into it, the more I find how deep the rabbit hole goes and I’m hoping to find the secret of the ancients in here.
Long story short, I’m working on a C++ app that has several functions passing filenames that uses const char*
as arguments. Basically, operations like opening a file needs to carry const char*
in multiples loops. That said, changing argument from const char*
to let say const wchar*
is logistically speaking, quite challenging and it end up in a deadlock several times.
Most documentation I found about character encoding conversion always assume what the input would be known. But in this case, the app takes YouTube video titles as filenames and it never knows what it is going to get… or does it?
Simply put, I got something like this:
wchar_t *ZStr::UTF8ToWChar(const char *in_char)
{
std::wstring_convert<std::codecvt_utf8<wchar_t>> l_converter;
std::wstring l_path = l_converter.from_bytes(in_char);
Int64 l_size = l_path.length() + 1;
wchar_t *l_buffer = new wchar_t[l_size];
wcsncpy(l_buffer, l_path.c_str(), l_size);
return l_buffer;
}
bool ZStream::Open(const char *in_path, const char *in_mode)
{
return m_File = _wfopen(ZStr::UTF8ToWChar(in_path), ZStr::UTF8ToWChar(in_mode));
}
It seems to work so far with filenames such as "360° Eiffel Tower.mp4" or "Tropical Break.mp4" but I’m not quite sure I’m doing it right.
My question is, is there a way to cover all possible OS filenames and keep const char*
arguments throughout my app?
Note: My development environment is Windows 10 but I’d really like to find a solution that is portable. In the worst case, I’d go for a OS dependent one. (I don’t mind whatever overhead it might require).
PART TWO
Thank you all for your comments, it is very appreciated. To complicate things a little more, I use QT open file dialog that returns a QString which seems to be a (wchar_t*) to open files. QString offers a great deal of conversion right out the box but I’m still banging my head to find the one solution that works for all filenames. So far, Tropical Break.mp4 returns invalid by _waccess:
Bool ZSystem::FileAccess(const Char *in_path, Int in_val)
{
return (_waccess(ZStr::UTF8ToWChar(in_path).c_str(), in_val) == 0);
}
bool QT_Editor::Open(const QString &in_path)
{
if (ZSystem::FileAccess(in_path.toUtf8().constData(), 0)) /// constData() = const char*
{
///...
}
}
I try several combinations without success. I realized that it is not very efficient but I’m stuck with choices I’ve made a long time ago. I’m not even sure what type is but its integrity seems to be lost in the loops. Any insights would be greatly welcome.