wchar for file paths?

Hey all, I’m a bit out of my depth with regards to file path formats and have a question.

I started using std::filesystem recently, and noticed that on windows it uses wchar. Up until now, I’ve been using char* and std::string for file paths, and I see that IMG_LoadTexture() and TTF_OpenFont() both use regular char*. I started looking into wchar though, and now I’m very confused.

My question is, do I need to worry about it? Are there scenarios where the IMG and TTF functions will break? (with foreign file names, perhaps?) Or will I be fine just converting filesystem’s output to char*? (or just not using std::filesystem)

I started using std::filesystem recently, and noticed that on windows it uses wchar.

OMG, what horrible API design :disappointed:

Anyway, to answer your question, SDL uses UTF-8 for paths, so you’ll have to convert between wchar (on Windows UTF-16 AFAIK?) and UTF-8 - as it’s just different encodings of Unicode, the conversion will be lossless, so you shouldn’t have to worry about foreign file names etc.
As this apparently only affects windows, I’d suggest using MultiByteToWideChar() and its counterpart WideCharToMultiByte().

Example:

const char* path = ..; // in UTF-8, from SDL
WCHAR wpath[2048] = {0};
MultiByteToWideChar(CP_UTF8, 0, path, -1, wpath, 2048);

and

wchar_t* wpath = ...; // from std::filesystem
char utfbuf[4096];
WideCharToMultiByte(CP_UTF8, 0, wpath, -1, utfbuf, sizeof(utfbuf), NULL, NULL);
1 Like