From ab6c7244fc472de0bed761cf29700a96dd89e8ad Mon Sep 17 00:00:00 2001 From: flow Date: Thu, 3 Nov 2022 16:44:23 -0300 Subject: [PATCH] refactor: move FS's toStdString to StringUtils This is so that anyone can use it :) Signed-off-by: flow --- launcher/FileSystem.cpp | 26 ++++++-------------------- launcher/StringUtils.h | 13 +++++++++++++ 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/launcher/FileSystem.cpp b/launcher/FileSystem.cpp index 4026d6c1..1cbb538c 100644 --- a/launcher/FileSystem.cpp +++ b/launcher/FileSystem.cpp @@ -44,7 +44,9 @@ #include #include #include + #include "DesktopServices.h" +#include "StringUtils.h" #if defined Q_OS_WIN32 #include @@ -79,22 +81,6 @@ namespace fs = std::filesystem; namespace fs = ghc::filesystem; #endif -#if defined Q_OS_WIN32 - -std::wstring toStdString(QString s) -{ - return s.toStdWString(); -} - -#else - -std::string toStdString(QString s) -{ - return s.toStdString(); -} - -#endif - namespace FS { void ensureExists(const QDir& dir) @@ -191,7 +177,7 @@ bool copy::operator()(const QString& offset) auto dst_path = PathCombine(dst, relative_dst_path); ensureFilePathExists(dst_path); - fs::copy(toStdString(src_path), toStdString(dst_path), opt, err); + fs::copy(StringUtils::toStdString(src_path), StringUtils::toStdString(dst_path), opt, err); if (err) { qWarning() << "Failed to copy files:" << QString::fromStdString(err.message()); qDebug() << "Source file:" << src_path; @@ -213,7 +199,7 @@ bool copy::operator()(const QString& offset) } // If the root src is not a directory, the previous iterator won't run. - if (!fs::is_directory(toStdString(src))) + if (!fs::is_directory(StringUtils::toStdString(src))) copy_file(src, ""); return err.value() == 0; @@ -223,7 +209,7 @@ bool deletePath(QString path) { std::error_code err; - fs::remove_all(toStdString(path), err); + fs::remove_all(StringUtils::toStdString(path), err); if (err) { qWarning() << "Failed to remove files:" << QString::fromStdString(err.message()); @@ -414,7 +400,7 @@ bool overrideFolder(QString overwritten_path, QString override_path) fs::copy_options opt = copy_opts::recursive | copy_opts::overwrite_existing; // FIXME: hello traveller! Apparently std::copy does NOT overwrite existing files on GNU libstdc++ on Windows? - fs::copy(toStdString(override_path), toStdString(overwritten_path), opt, err); + fs::copy(StringUtils::toStdString(override_path), StringUtils::toStdString(overwritten_path), opt, err); if (err) { qCritical() << QString("Failed to apply override from %1 to %2").arg(override_path, overwritten_path); diff --git a/launcher/StringUtils.h b/launcher/StringUtils.h index fbe72117..d7706b0f 100644 --- a/launcher/StringUtils.h +++ b/launcher/StringUtils.h @@ -3,5 +3,18 @@ #include namespace StringUtils { + +#if defined Q_OS_WIN32 +inline std::wstring toStdString(QString s) +{ + return s.toStdWString(); +} +#else +inline std::string toStdString(QString s) +{ + return s.toStdString(); +} +#endif + int naturalCompare(const QString& s1, const QString& s2, Qt::CaseSensitivity cs); } // namespace StringUtils