Francesco149 / oppai-ng

difficulty and pp calculator for osu!. better, smaller, faster rewrite of https://github.com/Francesco149/oppai
The Unlicense
156 stars 36 forks source link

Support for non-English characters as input path #62

Open l3lackShark opened 4 years ago

l3lackShark commented 4 years ago

I'm using oppai as a library for my project and if the user's Windows username contains non-English characters, the fopen() function will result in an I/O error. This might be what I'm looking for, but I'm not very well experienced with C... Is this something that I have to deal with on my own or can it be fixed within the scope of this project? image

omkelderman commented 4 years ago

ok so I did some random testing, conciser this extremely simple C program:

#include <stdio.h>
int main(int argc, char* argv[])
{
    for (int i = 0; i < argc; ++i)
    {
        printf("%i -> %s\n", i, argv[i]);
    }
}

that resulted in:

then I changed the code to this:

#include <stdio.h>

int wmain(int argc, wchar_t* argv[])
{
    for(int i=0; i<argc; ++i)
    {
        wprintf(L"%i -> %ws\n", i, argv[i]);
    }
}

got literally the same result.

But, once you start debugging, there is a difference! the first program:

the second program:

notice how with the first thing, there are literal question mark characters in the place of the non-english things, with the second program that is not the case and the string-data is fine there, there it is just the output that gets fucked (which results in the same apparent result).

As for opening a file, if you have a wchar_t string, you can do so with _wfopen (which takes a `wchar_tstring instead of acharstring), which returns aFILEso all the read and close code should be the same. So putting the fopen in a platform macro is ez, but the problem here is that also the main function needs to be changed to support this (ofc only on windows), and then all the commandline data is suddenly awchar_t*` string which makes everything else probably slightly fucked

so uuh. yeeee, windows sucks lol

Francesco149 commented 4 years ago

yea this looks like a windows specific issue, might be solvable by explicitly setting locale or something. will test eventually

Francesco149 commented 4 years ago

doing setlocale(LC_CTYPE, ".UTF-8") might work if you wanna test on your own.

Francesco149 commented 4 years ago

ehh this turns out to be a bit more complicated and messy. need to use _wmain instead of main and convert between utf-8 and utf-16 i guess

Wieku commented 4 years ago

I had similar problem in my program, but with BASS library. Windows only support UTF-16 paths. I resolved it by adding a simple converter that launches only under Windows: https://github.com/Wieku/danser-go/blob/master/framework/bass/bass_util.c

l3lackShark commented 3 years ago

Was able to fix this on my own. My implementation is ugly, so if someone picks up the solution and makes a PR out of it that would be highly appreciated. https://github.com/l3lackShark/gosumemory/commit/0630d5d5da58930daa60cd65ade05ea13d8afb39#diff-788c0375f94352a09cb71054e8f4e45b4897d0a49eac28e85c799f9f1549a9a5