Skip to content

Commit 7b424f8

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

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
@@ -67,19 +67,13 @@ void BasicIo::seekOrThrow(int64_t offset, Position pos, ErrorCode err) {
6767
class FileIo::Impl {
6868
public:
6969
//! Constructor
70-
explicit Impl(std::string path);
71-
#ifdef _WIN32
72-
explicit Impl(std::wstring path);
73-
#endif
70+
explicit Impl(fs::path path);
7471
~Impl() = default;
7572
// Enumerations
7673
//! Mode of operation
7774
enum OpMode { opRead, opWrite, opSeek };
7875
// DATA
79-
std::string path_; //!< (Standard) path
80-
#ifdef _WIN32
81-
std::wstring wpath_; //!< UCS2 path
82-
#endif
76+
fs::path path_; //!< (Standard) path
8377
std::string openMode_; //!< File open mode
8478
FILE* fp_{}; //!< File stream pointer
8579
OpMode opMode_{opSeek}; //!< File open mode
@@ -113,21 +107,8 @@ class FileIo::Impl {
113107
Impl& operator=(const Impl&) = delete; //!< Assignment
114108
};
115109

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

132113
int FileIo::Impl::switchMode(OpMode opMode) {
133114
if (opMode_ == opMode)
@@ -177,7 +158,7 @@ int FileIo::Impl::switchMode(OpMode opMode) {
177158
openMode_ = "r+b";
178159
opMode_ = opSeek;
179160
#ifdef _WIN32
180-
if (_wfopen_s(&fp_, wpath_.c_str(), L"r+b"))
161+
if (_wfopen_s(&fp_, path_.c_str(), L"r+b"))
181162
return 1;
182163
return _fseeki64(fp_, offset, SEEK_SET);
183164
#else
@@ -189,11 +170,7 @@ int FileIo::Impl::switchMode(OpMode opMode) {
189170
} // FileIo::Impl::switchMode
190171

191172
int FileIo::Impl::stat(StructStat& buf) const {
192-
#ifdef _WIN32
193-
const auto& file = wpath_;
194-
#else
195173
const auto& file = path_;
196-
#endif
197174
try {
198175
buf.st_size = fs::file_size(file);
199176
buf.st_mode = fs::status(file).permissions();
@@ -320,21 +297,12 @@ byte* FileIo::mmap(bool isWriteable) {
320297
void FileIo::setPath(const std::string& path) {
321298
close();
322299
p_->path_ = path;
323-
#ifdef _WIN32
324-
wchar_t t[512];
325-
const auto nw = MultiByteToWideChar(CP_UTF8, 0, p_->path_.data(), static_cast<int>(p_->path_.size()), t, 512);
326-
p_->wpath_.assign(t, nw);
327-
#endif
328300
}
329301

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

@@ -523,7 +491,7 @@ int FileIo::open(const std::string& mode) {
523491
#ifdef _WIN32
524492
wchar_t wmode[10];
525493
MultiByteToWideChar(CP_UTF8, 0, mode.c_str(), -1, wmode, 10);
526-
if (_wfopen_s(&p_->fp_, p_->wpath_.c_str(), wmode))
494+
if (_wfopen_s(&p_->fp_, p_->path_.c_str(), wmode))
527495
return 1;
528496
#else
529497
p_->fp_ = ::fopen(path().c_str(), mode.c_str());
@@ -583,7 +551,9 @@ bool FileIo::eof() const {
583551
}
584552

585553
const std::string& FileIo::path() const noexcept {
586-
return p_->path_;
554+
static thread_local std::string p;
555+
p = p_->path_.string();
556+
return p;
587557
}
588558

589559
void FileIo::populateFakeData() {
@@ -870,7 +840,7 @@ bool MemIo::eof() const {
870840
}
871841

872842
const std::string& MemIo::path() const noexcept {
873-
static std::string _path{"MemIo"};
843+
static const std::string _path{"MemIo"};
874844
return _path;
875845
}
876846

0 commit comments

Comments
 (0)