Skip to content

Commit

Permalink
Fix fileinfoname for non-ASCII characters
Browse files Browse the repository at this point in the history
There was an encoding issue in fileinfoname() that prevented finding files with
special characters in the path (fc_stat would fail). Rewrite it using Qt APIs
only.

Closes #565.
  • Loading branch information
lmoureaux authored and jwrober committed Jan 8, 2024
1 parent 9ef87d4 commit 6689c54
Showing 1 changed file with 4 additions and 32 deletions.
36 changes: 4 additions & 32 deletions utility/shared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ static QStringList scenario_dir_names = {};

static char *mc_group = nullptr;

Q_GLOBAL_STATIC(QString, realfile);

/**
An AND function for fc_tristate.
*/
Expand Down Expand Up @@ -657,48 +655,22 @@ 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 empty, the returned string contains
the effective path. (But this should probably only be used for
debug output.)
Returns an empty string if the specified filename cannot be found
in any of the data directories.
*/
QString fileinfoname(const QStringList &dirs, const QString &filename)
{
if (dirs.isEmpty()) {
return QString();
}

if (filename.isEmpty()) {
bool first = true;

realfile->clear();
for (const auto &dirname : dirs) {
if (first) {
*realfile += QStringLiteral("/%1").arg(dirname);
first = false;
} else {
*realfile += QStringLiteral("%1").arg(dirname);
}
}

return *realfile;
}

for (const auto &dirname : dirs) {
struct stat buf; // see if we can open the file or directory

*realfile = QStringLiteral("%1/%2").arg(dirname, filename);
if (fc_stat(qUtf8Printable(*realfile), &buf) == 0) {
return *realfile;
QString path = dirname + QLatin1String("/") + filename;
if (QFileInfo::exists(path)) {
return path;
}
}

qDebug("Could not find readable file \"%s\" in data path.",
qUtf8Printable(filename));

return nullptr;
return QString();
}

/**
Expand Down

0 comments on commit 6689c54

Please sign in to comment.