From 18d1df606f475c49bf7bfc6cdbbbdb9208e0e7df Mon Sep 17 00:00:00 2001 From: RSDuck Date: Tue, 12 Mar 2024 08:35:56 +0100 Subject: fix #1959 Use QT again for opening file so that we don't depend on locale --- src/frontend/qt_sdl/Platform.cpp | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/frontend/qt_sdl/Platform.cpp b/src/frontend/qt_sdl/Platform.cpp index 0cd4f61..7ffabfd 100644 --- a/src/frontend/qt_sdl/Platform.cpp +++ b/src/frontend/qt_sdl/Platform.cpp @@ -216,9 +216,30 @@ std::string InstanceFileSuffix() return suffix; } -constexpr char AccessMode(FileMode mode, bool file_exists) +static QIODevice::OpenMode GetQMode(FileMode mode) { + QIODevice::OpenMode qmode = 0; + if (mode & FileMode::Read) + qmode |= QIODevice::OpenModeFlag::ReadOnly; + if (mode & FileMode::Write) + qmode |= QIODevice::OpenModeFlag::WriteOnly; + if (mode & FileMode::Append) + qmode |= QIODevice::OpenModeFlag::Append; + + if ((mode & FileMode::Write) && !(mode & FileMode::Preserve)) + qmode |= QIODevice::OpenModeFlag::Truncate; + + if (mode & FileMode::NoCreate) + qmode |= QIODevice::OpenModeFlag::ExistingOnly; + + if (mode & FileMode::Text) + qmode |= QIODevice::OpenModeFlag::Text; + return qmode; +} + +constexpr char AccessMode(FileMode mode, bool file_exists) +{ if (mode & FileMode::Append) return 'a'; @@ -266,10 +287,15 @@ FileHandle* OpenFile(const std::string& path, FileMode mode) return nullptr; } - bool file_exists = QFile::exists(QString::fromStdString(path)); - std::string modeString = GetModeString(mode, file_exists); + QString qpath{QString::fromStdString(path)}; + + std::string modeString = GetModeString(mode, QFile::exists(qpath)); + QIODevice::OpenMode qmode = GetQMode(mode); + QFile qfile{qpath}; + qfile.open(qmode); + FILE* file = fdopen(dup(qfile.handle()), modeString.c_str()); + qfile.close(); - FILE* file = fopen(path.c_str(), modeString.c_str()); if (file) { Log(LogLevel::Debug, "Opened \"%s\" with FileMode 0x%x (effective mode \"%s\")\n", path.c_str(), mode, modeString.c_str()); -- cgit v1.2.3