refactor: move FS's toStdString to StringUtils

This is so that anyone can use it :)

Signed-off-by: flow <flowlnlnln@gmail.com>
This commit is contained in:
flow 2022-11-03 16:44:23 -03:00
parent 353b51f11e
commit ab6c7244fc
No known key found for this signature in database
GPG Key ID: 8D0F221F0A59F469
2 changed files with 19 additions and 20 deletions

View File

@ -44,7 +44,9 @@
#include <QStandardPaths>
#include <QTextStream>
#include <QUrl>
#include "DesktopServices.h"
#include "StringUtils.h"
#if defined Q_OS_WIN32
#include <objbase.h>
@ -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);

View File

@ -3,5 +3,18 @@
#include <QString>
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