Use QT again for opening file so that we don't depend on locale
This commit is contained in:
RSDuck 2024-03-12 08:35:56 +01:00
parent b117bb8f58
commit 18d1df606f
1 changed files with 30 additions and 4 deletions

View File

@ -216,9 +216,30 @@ std::string InstanceFileSuffix()
return suffix;
}
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());