CNewFileSelector & open file name with special characters

With CNewFileSelector I get the path to a file as a UTF8StringPtr.

But how to open this file when there are special characters like ä ö ü?

Actually it only works with standard characters.

This is highly dependent on the API you use to open the file. On unix, you can just use fopen as the path parameter is expected to be encoded in UTF-8. Which API and OS are you talking about?

Actually I am on Windows (7 any difference to Win10?).

I tried fopen, _wfopen, std::ifstream and std::wifstream, nothing finds the file.

CNewFileSelector outputs “Flöten.wav” instead of “Flöten.wav”.

What can I do?

remark: when I set a CTextlabel with “Flöten.wav” it displays correctly “Flöten.wav”

PS: on Mac no problem

PS: on Windows 10 it’s the same problem than in Windows 7, no opening of files with Umlaut (äöü).
It should work worldwide with any type of characterset.

In my SAM SPL 64 open source plugin, I can read any file with any character. Example: “/Volumes/Vault/tmp/abcΩdef/klmΨn/samplemΨo.wav” (I know this is a macOS path, but I run the same test on Windows and it works fine). But you have to be very careful in how you deal with file path like I did with my FilePath class…

As @Arne_Scheffler just pointed out the key is wchar_t on Windows like I have

/**
 * Implementation note: On Windows, file paths are wchar_t type and on macOS they are char (utf8 encoded).
 */
#if SMTG_OS_WINDOWS
using path_char_type = wchar_t;
#else
using path_char_type = char;
#endif

Windows likes to be different in this case and you have to convert the UTF-8 encoded string to a UCS2-Encoded string and use the APIs with wchar_t.

I do not see how to use it. Everything was so simple with VST2 and VSTGUI 3.65

You need to convert the UTF-8 encoded string with MultiByteToWideChar and then you can use the result with _wfopen. VST2 and VSTGUI 3.6 were not unicode capable. Thus opening files with non ansi characters were pure luck in most cases.

Hi Arne.

Thank you for your help. With that I could find a way to open all UTF8 files in Windows and Mac with following lines:

CNewFileSelector* selector = CNewFileSelector::create(getFrame(), CNewFileSelector::kSelectFile);
if (selector)
{
    selector->setAllowMultiFileSelection(false);
  //selector->addFileExtension(CFileExtension("AIFF", "aif", "audio/aiff"));
  //selector->addFileExtension(CFileExtension("AIFF", "aiff", "audio/aiff"));
    selector->addFileExtension(CFileExtension("WAVE", "wav", "audio/wav"));
    selector->setDefaultExtension(CFileExtension("WAVE", "wav"));
    selector->setTitle("Choose An Audio File");

    if (selector->runModal())
    {
        UTF8StringPtr filePathSelected = selector->getSelectedFile(0);

        char * TempByte;
        wchar_t myFilename[256];
        for (int i=0; i < 256; i++) myFilename[i]=0;
        FILE* fileHandle = NULL;

#if defined(WIN32) //Windows
MultiByteToWideChar(CP_UTF8, 0, filePathSelected, (int)strlen(filePathSelected), &myFilename[0], 256);
if ((fileHandle = _wfopen(myFilename, L"rb")) != NULL)
#else //Mac
if ((fileHandle = fopen(filePathSelected, “rb”)))
#endif
{
fseek(fileHandle, 0, SEEK_END);
int FileLength = ftell(fileHandle);
TempByte = new char[FileLength];
fseek(fileHandle, 0, SEEK_SET);
fread(TempByte, sizeof(char), FileLength, fileHandle);
fclose(fileHandle);
}

        //do something with the content of the char array
        // (...)
        if (TempByte) delete TempByte;
    }
    selector->forget();    
}

If you have a comment, please let me know.