@@ -73,10 +73,11 @@ class FileIo::Impl {
7373 // ! Mode of operation
7474 enum OpMode { opRead, opWrite, opSeek };
7575 // DATA
76- fs::path path_; // !< (Standard) path
77- std::string openMode_; // !< File open mode
78- FILE* fp_{}; // !< File stream pointer
79- OpMode opMode_{opSeek}; // !< File open mode
76+ fs::path path_; // !< (Standard) path
77+ std::string openMode_; // !< File open mode
78+ std::wstring wOpenMode_; // !< File open mode (wide)
79+ FILE* fp_{}; // !< File stream pointer
80+ OpMode opMode_{opSeek}; // !< File open mode
8081
8182#if defined _WIN32
8283 HANDLE hFile_{}; // !< Duplicated fd
@@ -118,6 +119,19 @@ int FileIo::Impl::switchMode(OpMode opMode) {
118119
119120 bool reopen = true ;
120121 switch (opMode) {
122+ #ifdef _WIN32
123+ case opRead:
124+ // Flush if current mode allows reading, else reopen (in mode "r+b"
125+ // as in this case we know that we can write to the file)
126+ if (wOpenMode_.front () == L' r' || wOpenMode_.at (1 ) == L' +' )
127+ reopen = false ;
128+ break ;
129+ case opWrite:
130+ // Flush if current mode allows writing, else reopen
131+ if (wOpenMode_.front () != L' r' || wOpenMode_.at (1 ) == L' +' )
132+ reopen = false ;
133+ break ;
134+ #else
121135 case opRead:
122136 // Flush if current mode allows reading, else reopen (in mode "r+b"
123137 // as in this case we know that we can write to the file)
@@ -129,6 +143,7 @@ int FileIo::Impl::switchMode(OpMode opMode) {
129143 if (openMode_.front () != ' r' || openMode_.at (1 ) == ' +' )
130144 reopen = false ;
131145 break ;
146+ #endif
132147 case opSeek:
133148 reopen = false ;
134149 break ;
@@ -481,26 +496,34 @@ size_t FileIo::size() const {
481496
482497int FileIo::open () {
483498 // Default open is in read-only binary mode
499+ #ifdef _WIN32
500+ return open (L" rb" );
501+ #else
484502 return open (" rb" );
503+ #endif
485504}
486505
487506int FileIo::open (const std::string& mode) {
488507 close ();
489508 p_->openMode_ = mode;
490509 p_->opMode_ = Impl::opSeek;
491- #ifdef _WIN32
492- wchar_t wmode[10 ];
493- MultiByteToWideChar (CP_UTF8, 0 , mode.c_str (), -1 , wmode, 10 );
494- if (_wfopen_s (&p_->fp_ , p_->path_ .c_str (), wmode))
495- return 1 ;
496- #else
497510 p_->fp_ = ::fopen (path ().c_str (), mode.c_str ());
498511 if (!p_->fp_ )
499512 return 1 ;
500- #endif
501513 return 0 ;
502514}
503515
516+ #ifdef _WIN32
517+ int FileIo::open (const std::wstring& mode) {
518+ close ();
519+ p_->wOpenMode_ = mode;
520+ p_->opMode_ = Impl::opSeek;
521+ if (_wfopen_s (&p_->fp_ , p_->path_ .c_str (), mode.c_str ()))
522+ return 1 ;
523+ return 0 ;
524+ }
525+ #endif
526+
504527bool FileIo::isopen () const {
505528 return p_->fp_ != nullptr ;
506529}
0 commit comments