2

I want to refactor this code in C++17. Can I use std::filesystem in this case?

 #if defined(BOOSTER_WIN_NATIVE)
 bool open(std::string const &file_name,std::string const &encoding)
 {
     close();

     //
     // Under Windows, we have to use "_wfopen" to get
     // access to paths with Unicode in them.
     //
     // As not all standard C++ libraries support nonstandard std::istream::open(wchar_t const *)
     // we would use good old stdio and _wfopen CRTL functions.
     //

     std::wstring wfile_name = conv::to_utf<wchar_t>(file_name,encoding);
     file = _wfopen(wfile_name.c_str(),L"rb");
     return file!=0;
 }

 #else // POSIX systems do not have all this Wide API crap, as native codepages are UTF-8
       // We do not use encoding as we use native file name encoding
 bool open(std::string const &file_name,std::string const &/* encoding */)
 {
      close();
      file = fopen(file_name.c_str(),"rb");
      return file!=0;
 }
 #endif

I've already changed utf8 utf16 converting, but it seems there is no value in this anymore.

 std::wstring wfile_name;
 typedef std::codecvt_utf8_utf16<wchar_t> codec;
 std::wstring_convert<codec> converter;
 wfile_name = converter.from_bytes(file_name);
 file = _wfopen(wfile_name.c_str(), L"rb");
 return file != 0;
donjuedo
  • 2,475
  • 18
  • 28
  • 1
    If you target Win10 1903 or later you can set UTF-8 as the default codepage in the manifest, see [1](https://learn.microsoft.com/en-us/windows/uwp/design/globalizing/use-utf8-code-page), [2](https://www.reddit.com/r/cpp/comments/g7tkd7/state_of_utf8_support_on_windows/fore4z3/?utm_source=reddit&utm_medium=web2x&context=3). – dxiv Mar 08 '21 at 20:21
  • 1
    The motivation to resort to `stdio` is very unclear. Windows has `std::fstream` that can deal with wide string filenames. Other systems have `std::fstream` that does not need wide string filenames. Why use `_fwopen` on Windows and `open` elsewhere, and not `std::fstream` on Windows and `std::fstream` elsewhere? – n. m. could be an AI Mar 08 '21 at 20:57
  • Why use _fwopen on Windows and open elsewhere, and not std::fstream on Windows and std::fstream elsewhere?... That is legacy code. I just learn how does it works. – Sergey Inozemcev Apr 06 '21 at 15:15

0 Answers0