Search code examples
c++encodingwxwidgetsdefault

.NET Encoding.Default alternative in wxWIdgets?


I need to read files with different encodings. Unicode files are correctly read using

wxFileInputStream fileInputStream(dialog->GetPath());
wxTextInputStream textInputStream(fileInputStream);

If I need to read, say, Cyrillic (cp1251) files, I use:

wxFileInputStream fileInputStream(dialog->GetPath());
wxTextInputStream textInputStream(fileInputStream, " \n", wxCSConv(wxFONTENCODING_CP1251));

But neither of these ways works with both kinds of files. In .NET we can just use:

new StreamReader(file, Encoding.Default)

So what's the alternative of Encoding.Default in wxWidgets or in C++ in general?

Thank you


Solution

  • The problem was solved by using wxConvAuto(wxFONTENCODING_SYSTEM) instead of wxCSConv(wxFONTENCODING_SYSTEM). The wxConvAuto function first tries to read the file as a Unicode document, and then if it fails, it uses system's encoding to read the ANSI file. It works great!