Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small refactors in utility/shared.h/cpp #2121

Merged
merged 3 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common/mapimg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion server/savegame/savemain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
42 changes: 8 additions & 34 deletions utility/shared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -657,25 +657,20 @@ QVector<QString> *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();
Expand All @@ -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;
}
Expand Down Expand Up @@ -731,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.
Expand Down Expand Up @@ -1185,28 +1181,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,
Expand Down
3 changes: 1 addition & 2 deletions utility/shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ const QStringList &get_scenario_dirs();
QVector<QString> *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();
Expand Down Expand Up @@ -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);

Expand Down
Loading