Handle UTF-8 in Qt streams in the Qt6-compatible way

7220-lua-add-a-general-purpose-lexical-parser
Andrei Kortunov 2 years ago
parent 6a9af5ccca
commit bfcbc2350d

@ -7,13 +7,13 @@
#include <QDebug> #include <QDebug>
#include <QDir> #include <QDir>
#include <QMessageBox> #include <QMessageBox>
#include <QTextCodec>
#include <QTime> #include <QTime>
#include <boost/program_options/options_description.hpp> #include <boost/program_options/options_description.hpp>
#include <boost/program_options/variables_map.hpp> #include <boost/program_options/variables_map.hpp>
#include <components/files/conversion.hpp> #include <components/files/conversion.hpp>
#include <components/files/qtconversion.hpp> #include <components/files/qtconversion.hpp>
#include <components/misc/utf8qtextstream.hpp>
#include "advancedpage.hpp" #include "advancedpage.hpp"
#include "datafilespage.hpp" #include "datafilespage.hpp"
@ -323,7 +323,7 @@ bool Launcher::MainDialog::setupLauncherSettings()
return false; return false;
} }
QTextStream stream(&file); QTextStream stream(&file);
stream.setCodec(QTextCodec::codecForName("UTF-8")); ensureUtf8Encoding(stream);
mLauncherSettings.readFile(stream); mLauncherSettings.readFile(stream);
} }
@ -359,7 +359,7 @@ bool Launcher::MainDialog::setupGameSettings()
return {}; return {};
} }
QTextStream stream(&file); QTextStream stream(&file);
stream.setCodec(QTextCodec::codecForName("UTF-8")); ensureUtf8Encoding(stream);
(mGameSettings.*reader)(stream, ignoreContent); (mGameSettings.*reader)(stream, ignoreContent);
file.close(); file.close();
@ -559,7 +559,7 @@ bool Launcher::MainDialog::writeSettings()
QTextStream stream(&file); QTextStream stream(&file);
stream.setDevice(&file); stream.setDevice(&file);
stream.setCodec(QTextCodec::codecForName("UTF-8")); ensureUtf8Encoding(stream);
mLauncherSettings.writeFile(stream); mLauncherSettings.writeFile(stream);
file.close(); file.close();

@ -164,7 +164,11 @@ bool Wizard::IniSettings::writeFile(const QString& path, QTextStream& stream)
if (file.open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text)) if (file.open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text))
{ {
QTextStream in(&file); QTextStream in(&file);
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
in.setCodec(stream.codec()); in.setCodec(stream.codec());
#else
in.setEncoding(stream.encoding());
#endif
// Write the updated buffer to an empty file // Write the updated buffer to an empty file
in << buffer; in << buffer;

@ -5,9 +5,9 @@
#include <QDir> #include <QDir>
#include <QMessageBox> #include <QMessageBox>
#include <QProcess> #include <QProcess>
#include <QTextCodec>
#include <components/files/qtconversion.hpp> #include <components/files/qtconversion.hpp>
#include <components/misc/utf8qtextstream.hpp>
#include <components/process/processinvoker.hpp> #include <components/process/processinvoker.hpp>
#include "componentselectionpage.hpp" #include "componentselectionpage.hpp"
@ -165,7 +165,7 @@ void Wizard::MainWizard::setupGameSettings()
return; return;
} }
QTextStream stream(&file); QTextStream stream(&file);
stream.setCodec(QTextCodec::codecForName("UTF-8")); ensureUtf8Encoding(stream);
mGameSettings.readUserFile(stream); mGameSettings.readUserFile(stream);
} }
@ -197,7 +197,7 @@ void Wizard::MainWizard::setupGameSettings()
return; return;
} }
QTextStream stream(&file); QTextStream stream(&file);
stream.setCodec(QTextCodec::codecForName("UTF-8")); ensureUtf8Encoding(stream);
mGameSettings.readFile(stream); mGameSettings.readFile(stream);
} }
@ -233,7 +233,7 @@ void Wizard::MainWizard::setupLauncherSettings()
return; return;
} }
QTextStream stream(&file); QTextStream stream(&file);
stream.setCodec(QTextCodec::codecForName("UTF-8")); ensureUtf8Encoding(stream);
mLauncherSettings.readFile(stream); mLauncherSettings.readFile(stream);
} }
@ -460,7 +460,7 @@ void Wizard::MainWizard::writeSettings()
} }
QTextStream stream(&file); QTextStream stream(&file);
stream.setCodec(QTextCodec::codecForName("UTF-8")); ensureUtf8Encoding(stream);
mGameSettings.writeFile(stream); mGameSettings.writeFile(stream);
file.close(); file.close();
@ -486,7 +486,7 @@ void Wizard::MainWizard::writeSettings()
} }
stream.setDevice(&file); stream.setDevice(&file);
stream.setCodec(QTextCodec::codecForName("UTF-8")); ensureUtf8Encoding(stream);
mLauncherSettings.writeFile(stream); mLauncherSettings.writeFile(stream);
file.close(); file.close();

@ -197,7 +197,7 @@ add_component_dir (esm4
) )
add_component_dir (misc add_component_dir (misc
constants utf8stream resourcehelpers rng messageformatparser weakcache thread constants utf8stream utf8qtextstream resourcehelpers rng messageformatparser weakcache thread
compression osguservalues color tuplemeta tuplehelpers compression osguservalues color tuplemeta tuplehelpers
) )

@ -3,10 +3,10 @@
#include <QDir> #include <QDir>
#include <QRegularExpression> #include <QRegularExpression>
#include <QTextCodec>
#include <components/files/configurationmanager.hpp> #include <components/files/configurationmanager.hpp>
#include <components/files/qtconversion.hpp> #include <components/files/qtconversion.hpp>
#include <components/misc/utf8qtextstream.hpp>
const char Config::GameSettings::sArchiveKey[] = "fallback-archive"; const char Config::GameSettings::sArchiveKey[] = "fallback-archive";
const char Config::GameSettings::sContentKey[] = "content"; const char Config::GameSettings::sContentKey[] = "content";
@ -232,7 +232,7 @@ bool Config::GameSettings::isOrderedLine(const QString& line)
bool Config::GameSettings::writeFileWithComments(QFile& file) bool Config::GameSettings::writeFileWithComments(QFile& file)
{ {
QTextStream stream(&file); QTextStream stream(&file);
stream.setCodec(QTextCodec::codecForName("UTF-8")); ensureUtf8Encoding(stream);
// slurp // slurp
std::vector<QString> fileCopy; std::vector<QString> fileCopy;

@ -0,0 +1,20 @@
#ifndef MISC_UTF8QTEXTSTREAM_HPP
#define MISC_UTF8QTEXTSTREAM_HPP
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include <QTextCodec>
#endif
#include <QTextStream>
namespace
{
void ensureUtf8Encoding(QTextStream& stream)
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
stream.setCodec(QTextCodec::codecForName("UTF-8"));
#else
stream.setEncoding(QStringConverter::Utf8);
#endif
}
}
#endif
Loading…
Cancel
Save