3
0
Fork 0
forked from suyu/suyu

game_list_worker: Use QFile over our own IOFile instance or std streams

Stays consistent in our code with using Qt's provided mechanisms, and
also properly handles Unicode paths (which file streams on Windows don't
do very well).
This commit is contained in:
Lioncash 2019-05-30 22:13:38 -04:00
parent de2533d389
commit d0d97de1e4

View file

@ -8,6 +8,7 @@
#include <vector> #include <vector>
#include <QDir> #include <QDir>
#include <QFile>
#include <QFileInfo> #include <QFileInfo>
#include <QSettings> #include <QSettings>
@ -46,20 +47,17 @@ QString GetGameListCachedObject(const std::string& filename, const std::string&
if (!FileUtil::Exists(path)) { if (!FileUtil::Exists(path)) {
const auto str = generator(); const auto str = generator();
std::ofstream stream(path); QFile file{QString::fromStdString(path)};
if (stream) { if (file.open(QFile::WriteOnly)) {
stream << str.toStdString(); file.write(str.toUtf8());
} }
return str; return str;
} }
std::ifstream stream(path); QFile file{QString::fromStdString(path)};
if (file.open(QFile::ReadOnly)) {
if (stream) { return QString::fromUtf8(file.readAll());
const std::string out(std::istreambuf_iterator<char>{stream},
std::istreambuf_iterator<char>{});
return QString::fromStdString(out);
} }
return generator(); return generator();
@ -82,53 +80,51 @@ std::pair<std::vector<u8>, std::string> GetGameListCachedObject(
if (!FileUtil::Exists(path1) || !FileUtil::Exists(path2)) { if (!FileUtil::Exists(path1) || !FileUtil::Exists(path2)) {
const auto [icon, nacp] = generator(); const auto [icon, nacp] = generator();
FileUtil::IOFile file1(path1, "wb"); QFile file1{QString::fromStdString(path1)};
if (!file1.IsOpen()) { if (!file1.open(QFile::WriteOnly)) {
LOG_ERROR(Frontend, "Failed to open cache file."); LOG_ERROR(Frontend, "Failed to open cache file.");
return generator(); return generator();
} }
if (!file1.Resize(icon.size())) { if (!file1.resize(icon.size())) {
LOG_ERROR(Frontend, "Failed to resize cache file to necessary size."); LOG_ERROR(Frontend, "Failed to resize cache file to necessary size.");
return generator(); return generator();
} }
if (file1.WriteBytes(icon.data(), icon.size()) != icon.size()) { if (file1.write(reinterpret_cast<const char*>(icon.data()), icon.size()) != icon.size()) {
LOG_ERROR(Frontend, "Failed to write data to cache file."); LOG_ERROR(Frontend, "Failed to write data to cache file.");
return generator(); return generator();
} }
std::ofstream stream2(path2, std::ios::out); QFile file2{QString::fromStdString(path2)};
if (stream2) { if (file2.open(QFile::WriteOnly)) {
stream2 << nacp; file2.write(nacp.data(), nacp.size());
} }
return std::make_pair(icon, nacp); return std::make_pair(icon, nacp);
} }
FileUtil::IOFile file1(path1, "rb"); QFile file1(QString::fromStdString(path1));
std::ifstream stream2(path2); QFile file2(QString::fromStdString(path2));
if (!file1.IsOpen()) { if (!file1.open(QFile::ReadOnly)) {
LOG_ERROR(Frontend, "Failed to open cache file for reading."); LOG_ERROR(Frontend, "Failed to open cache file for reading.");
return generator(); return generator();
} }
if (!stream2) { if (!file2.open(QFile::ReadOnly)) {
LOG_ERROR(Frontend, "Failed to open cache file for reading."); LOG_ERROR(Frontend, "Failed to open cache file for reading.");
return generator(); return generator();
} }
std::vector<u8> vec(file1.GetSize()); std::vector<u8> vec(file1.size());
file1.ReadBytes(vec.data(), vec.size()); if (file1.read(reinterpret_cast<char*>(vec.data()), vec.size()) !=
static_cast<s64>(vec.size())) {
if (stream2 && !vec.empty()) { return generator();
const std::string out(std::istreambuf_iterator<char>{stream2},
std::istreambuf_iterator<char>{});
return std::make_pair(vec, out);
} }
return generator(); const auto data = file2.readAll();
return std::make_pair(vec, data.toStdString());
} }
void GetMetadataFromControlNCA(const FileSys::PatchManager& patch_manager, const FileSys::NCA& nca, void GetMetadataFromControlNCA(const FileSys::PatchManager& patch_manager, const FileSys::NCA& nca,