Skip to content

Commit 5a1c3b1

Browse files
committed
basicio: use fs::path
Allows removing manual UTF8 handling. Signed-off-by: Rosen Penev <[email protected]>
1 parent 51dfe90 commit 5a1c3b1

File tree

1 file changed

+10
-40
lines changed

1 file changed

+10
-40
lines changed

src/basicio.cpp

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,13 @@ void BasicIo::seekOrThrow(int64_t offset, Position pos, ErrorCode err) {
6868
class FileIo::Impl {
6969
public:
7070
//! Constructor
71-
explicit Impl(std::string path);
72-
#ifdef _WIN32
73-
explicit Impl(std::wstring path);
74-
#endif
71+
explicit Impl(fs::path path);
7572
~Impl() = default;
7673
// Enumerations
7774
//! Mode of operation
7875
enum OpMode { opRead, opWrite, opSeek };
7976
// DATA
80-
std::string path_; //!< (Standard) path
81-
#ifdef _WIN32
82-
std::wstring wpath_; //!< UCS2 path
83-
#endif
77+
fs::path path_; //!< (Standard) path
8478
std::string openMode_; //!< File open mode
8579
FILE* fp_{}; //!< File stream pointer
8680
OpMode opMode_{opSeek}; //!< File open mode
@@ -114,21 +108,8 @@ class FileIo::Impl {
114108
Impl& operator=(const Impl&) = delete; //!< Assignment
115109
};
116110

117-
FileIo::Impl::Impl(std::string path) : path_(std::move(path)) {
118-
#ifdef _WIN32
119-
wchar_t t[512];
120-
const auto nw = MultiByteToWideChar(CP_UTF8, 0, path_.data(), static_cast<int>(path_.size()), t, 512);
121-
wpath_.assign(t, nw);
122-
#endif
123-
}
124-
#ifdef _WIN32
125-
FileIo::Impl::Impl(std::wstring path) : wpath_(std::move(path)) {
126-
char t[1024];
127-
const auto nc =
128-
WideCharToMultiByte(CP_UTF8, 0, wpath_.data(), static_cast<int>(wpath_.size()), t, 1024, nullptr, nullptr);
129-
path_.assign(t, nc);
111+
FileIo::Impl::Impl(fs::path path) : path_(std::move(path)) {
130112
}
131-
#endif
132113

133114
int FileIo::Impl::switchMode(OpMode opMode) {
134115
if (opMode_ == opMode)
@@ -178,7 +159,7 @@ int FileIo::Impl::switchMode(OpMode opMode) {
178159
openMode_ = "r+b";
179160
opMode_ = opSeek;
180161
#ifdef _WIN32
181-
if (_wfopen_s(&fp_, wpath_.c_str(), L"r+b"))
162+
if (_wfopen_s(&fp_, path_.c_str(), L"r+b"))
182163
return 1;
183164
return _fseeki64(fp_, offset, SEEK_SET);
184165
#else
@@ -190,11 +171,7 @@ int FileIo::Impl::switchMode(OpMode opMode) {
190171
} // FileIo::Impl::switchMode
191172

192173
int FileIo::Impl::stat(StructStat& buf) const {
193-
#ifdef _WIN32
194-
const auto& file = wpath_;
195-
#else
196174
const auto& file = path_;
197-
#endif
198175
try {
199176
buf.st_size = fs::file_size(file);
200177
buf.st_mode = fs::status(file).permissions();
@@ -321,21 +298,12 @@ byte* FileIo::mmap(bool isWriteable) {
321298
void FileIo::setPath(const std::string& path) {
322299
close();
323300
p_->path_ = path;
324-
#ifdef _WIN32
325-
wchar_t t[512];
326-
const auto nw = MultiByteToWideChar(CP_UTF8, 0, p_->path_.data(), static_cast<int>(p_->path_.size()), t, 512);
327-
p_->wpath_.assign(t, nw);
328-
#endif
329301
}
330302

331303
#ifdef _WIN32
332304
void FileIo::setPath(const std::wstring& path) {
333305
close();
334-
p_->wpath_ = path;
335-
char t[1024];
336-
const auto nc = WideCharToMultiByte(CP_UTF8, 0, p_->wpath_.data(), static_cast<int>(p_->wpath_.size()), t, 1024,
337-
nullptr, nullptr);
338-
p_->path_.assign(t, nc);
306+
p_->path_ = path;
339307
}
340308
#endif
341309

@@ -524,7 +492,7 @@ int FileIo::open(const std::string& mode) {
524492
#ifdef _WIN32
525493
wchar_t wmode[10];
526494
MultiByteToWideChar(CP_UTF8, 0, mode.c_str(), -1, wmode, 10);
527-
if (_wfopen_s(&p_->fp_, p_->wpath_.c_str(), wmode))
495+
if (_wfopen_s(&p_->fp_, p_->path_.c_str(), wmode))
528496
return 1;
529497
#else
530498
p_->fp_ = ::fopen(path().c_str(), mode.c_str());
@@ -584,7 +552,9 @@ bool FileIo::eof() const {
584552
}
585553

586554
const std::string& FileIo::path() const noexcept {
587-
return p_->path_;
555+
static thread_local std::string p;
556+
p = p_->path_.string();
557+
return p;
588558
}
589559

590560
void FileIo::populateFakeData() {
@@ -871,7 +841,7 @@ bool MemIo::eof() const {
871841
}
872842

873843
const std::string& MemIo::path() const noexcept {
874-
static std::string _path{"MemIo"};
844+
static const std::string _path{"MemIo"};
875845
return _path;
876846
}
877847

0 commit comments

Comments
 (0)