From a82b918320ca6c8fa8bec739597246d6a14aacd7 Mon Sep 17 00:00:00 2001 From: Louis Moureaux Date: Tue, 2 Jan 2024 00:34:56 +0100 Subject: [PATCH 1/3] Remove path_is_absolute --- common/mapimg.cpp | 2 +- server/savegame/savemain.cpp | 2 +- utility/shared.cpp | 22 ---------------------- utility/shared.h | 1 - 4 files changed, 2 insertions(+), 25 deletions(-) diff --git a/common/mapimg.cpp b/common/mapimg.cpp index 0c38a02ec8..d3c99a0606 100644 --- a/common/mapimg.cpp +++ b/common/mapimg.cpp @@ -1989,7 +1989,7 @@ static bool img_save(const struct img *pimg, const char *mapimgfile, return false; } - if (!path_is_absolute(mapimgfile) && path != nullptr) { + if (!QFileInfo(mapimgfile).isAbsolute() && path != nullptr) { make_dir(path); sz_strlcpy(tmpname, path); diff --git a/server/savegame/savemain.cpp b/server/savegame/savemain.cpp index 66d8bd2a36..fb38297b16 100644 --- a/server/savegame/savemain.cpp +++ b/server/savegame/savemain.cpp @@ -243,7 +243,7 @@ void save_game(const char *orig_filename, const char *save_reason, } } - if (!path_is_absolute(stdata->filepath)) { + if (!QFileInfo(stdata->filepath).isAbsolute()) { QString tmpname; if (!scenario) { diff --git a/utility/shared.cpp b/utility/shared.cpp index e32bdb25e2..8da607db63 100644 --- a/utility/shared.cpp +++ b/utility/shared.cpp @@ -1185,28 +1185,6 @@ bool make_dir(const char *pathname) return r; } -/** - Returns TRUE if the filename's path is absolute. - */ -bool path_is_absolute(const char *filename) -{ - if (!filename) { - return false; - } - -#ifdef FREECIV_MSWINDOWS - if (strchr(filename, ':')) { - return true; - } -#else // FREECIV_MSWINDOWS - if (filename[0] == '/') { - return true; - } -#endif // FREECIV_MSWINDOWS - - return false; -} - /** Scan in a word or set of words from start to but not including any of the given delimiters. The buf pointer will point past delimiter, diff --git a/utility/shared.h b/utility/shared.h index bf1f3045f4..78f94ef41e 100644 --- a/utility/shared.h +++ b/utility/shared.h @@ -183,7 +183,6 @@ void interpret_tilde(char *buf, size_t buf_size, const QString &filename); char *interpret_tilde_alloc(const char *filename); bool make_dir(const char *pathname); -bool path_is_absolute(const char *filename); char scanin(char **buf, char *delimiters, char *dest, int size); From 20869f164a18b8911aed0a3f6b902f38a17359fa Mon Sep 17 00:00:00 2001 From: Louis Moureaux Date: Tue, 2 Jan 2024 00:56:02 +0100 Subject: [PATCH 2/3] Have fileinfoname take a QString parameter --- utility/shared.cpp | 18 +++++++----------- utility/shared.h | 2 +- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/utility/shared.cpp b/utility/shared.cpp index 8da607db63..bbfee720df 100644 --- a/utility/shared.cpp +++ b/utility/shared.cpp @@ -657,25 +657,20 @@ QVector *fileinfolist(const QStringList &dirs, const char *suffix) Returns a filename to access the specified file from a directory by searching all specified directories for the file. - If the specified 'filename' is nullptr, the returned string contains + If the specified 'filename' is empty, the returned string contains the effective path. (But this should probably only be used for debug output.) - Returns nullptr if the specified filename cannot be found in any of the - data directories. (A file is considered "found" if it can be - read-opened.) The returned pointer points to static memory, so this - function can only supply one filename at a time. Don't free that - pointer. - - TODO: Make this re-entrant + Returns an empty string if the specified filename cannot be found + in any of the data directories. */ -QString fileinfoname(const QStringList &dirs, const char *filename) +QString fileinfoname(const QStringList &dirs, const QString &filename) { if (dirs.isEmpty()) { return QString(); } - if (!filename) { + if (filename.isEmpty()) { bool first = true; realfile->clear(); @@ -700,7 +695,8 @@ QString fileinfoname(const QStringList &dirs, const char *filename) } } - qDebug("Could not find readable file \"%s\" in data path.", filename); + qDebug("Could not find readable file \"%s\" in data path.", + qUtf8Printable(filename)); return nullptr; } diff --git a/utility/shared.h b/utility/shared.h index 78f94ef41e..4aca5202b2 100644 --- a/utility/shared.h +++ b/utility/shared.h @@ -134,7 +134,7 @@ const QStringList &get_scenario_dirs(); QVector *fileinfolist(const QStringList &dirs, const char *suffix); QFileInfoList find_files_in_path(const QStringList &path, const QString &pattern, bool nodups); -QString fileinfoname(const QStringList &dirs, const char *filename); +QString fileinfoname(const QStringList &dirs, const QString &filename); void init_nls(); void free_nls(); From d319a00ef3be7240decbff7fb541445d7dee0d53 Mon Sep 17 00:00:00 2001 From: Louis Moureaux Date: Tue, 2 Jan 2024 01:23:36 +0100 Subject: [PATCH 3/3] Fix removing duplicates in find_files_in_path --- utility/shared.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utility/shared.cpp b/utility/shared.cpp index bbfee720df..3be886ab0b 100644 --- a/utility/shared.cpp +++ b/utility/shared.cpp @@ -727,7 +727,7 @@ QFileInfoList find_files_in_path(const QStringList &path, [](const auto &lhs, const auto &rhs) { return lhs.absoluteFilePath() < rhs.absoluteFilePath(); }); - std::unique(files.begin(), files.end()); + files.erase(std::unique(files.begin(), files.end()), files.end()); } // Sort the list by last modification time.