Skip to content

Commit

Permalink
Remove fc_stat usage
Browse files Browse the repository at this point in the history
Use QFileInfo. This removes the need to encode the file name in the system
locale by hand.

See longturn#565.
  • Loading branch information
lmoureaux committed Jan 7, 2024
1 parent a09faf5 commit ecc5951
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 33 deletions.
7 changes: 3 additions & 4 deletions client/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3788,7 +3788,6 @@ static const char *get_last_option_file_name(bool *allow_digital_boolean)
fc_strlcpy(name_buffer, OPTION_FILE_NAME, sizeof(name_buffer));
#else
int major, minor;
struct stat buf;

name = freeciv_storage_dir();
if (name.isEmpty()) {
Expand All @@ -3807,7 +3806,7 @@ static const char *get_last_option_file_name(bool *allow_digital_boolean)
fc_snprintf(name_buffer, sizeof(name_buffer),
"%s/freeciv-client-rc-%d.%d", qUtf8Printable(name),
major, minor);
if (0 == fc_stat(name_buffer, &buf)) {
if (QFileInfo::exists(name_buffer)) {
if (MAJOR_NEW_OPTION_FILE_NAME != major
|| MINOR_NEW_OPTION_FILE_NAME != minor) {
qInfo(_("Didn't find '%s' option file, "
Expand All @@ -3831,7 +3830,7 @@ static const char *get_last_option_file_name(bool *allow_digital_boolean)
fc_snprintf(name_buffer, sizeof(name_buffer),
"%s/.freeciv-client-rc-%d.%d",
qUtf8Printable(QDir::homePath()), major, minor);
if (0 == fc_stat(name_buffer, &buf)) {
if (QFileInfo::exists(name_buffer)) {
qInfo(_("Didn't find '%s' option file, "
"loading from '%s' instead."),
get_current_option_file_name()
Expand All @@ -3848,7 +3847,7 @@ static const char *get_last_option_file_name(bool *allow_digital_boolean)
// Try with the old one.
fc_snprintf(name_buffer, sizeof(name_buffer), "%s/%s",
qUtf8Printable(name), OLD_OPTION_FILE_NAME);
if (0 == fc_stat(name_buffer, &buf)) {
if (QFileInfo::exists(name_buffer)) {
qInfo(_("Didn't find '%s' option file, "
"loading from '%s' instead."),
get_current_option_file_name(), OLD_OPTION_FILE_NAME);
Expand Down
9 changes: 4 additions & 5 deletions server/scripting/script_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,18 @@ bool script_server_unsafe_do_string(struct connection *caller,
bool script_server_load_file(const char *filename, char **buf)
{
FILE *ffile;
struct stat stats;
char *buffer;

fc_stat(filename, &stats);
QFileInfo stats(filename);
ffile = fc_fopen(filename, "r");

if (ffile != nullptr) {
int len;

buffer = new char[stats.st_size + 1];
len = fread(buffer, 1, stats.st_size, ffile);
buffer = new char[stats.size() + 1];
len = fread(buffer, 1, stats.size(), ffile);

if (len == stats.st_size) {
if (len == stats.size()) {
buffer[len] = '\0';

*buf = buffer;
Expand Down
5 changes: 2 additions & 3 deletions tools/fcmp/modinst.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ void load_install_info_lists(struct fcmp_params *fcmp)
{
char main_db_filename[500];
char scenario_db_filename[500];
struct stat buf;

fc_snprintf(main_db_filename, sizeof(main_db_filename),
"%s/" DATASUBDIR "/" FCMP_CONTROLD "/mp.db",
Expand All @@ -56,14 +55,14 @@ void load_install_info_lists(struct fcmp_params *fcmp)
"%s/scenarios/" FCMP_CONTROLD "/modpacks.db",
qUtf8Printable(fcmp->inst_prefix));

if (fc_stat(main_db_filename, &buf)) {
if (QFileInfo::exists(main_db_filename)) {
create_mpdb(main_db_filename, false);
load_install_info_list(main_ii_filename);
} else {
open_mpdb(main_db_filename, false);
}

if (fc_stat(scenario_db_filename, &buf)) {
if (QFileInfo::exists(scenario_db_filename)) {
create_mpdb(scenario_db_filename, true);
load_install_info_list(scenario_ii_filename);
} else {
Expand Down
2 changes: 1 addition & 1 deletion utility/shared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ QVector<QString> *fileinfolist(const QStringList &dirs, const char *suffix)
dir.setNameFilters({QStringLiteral("*") + QString::fromUtf8(suffix)});
for (auto name : dir.entryList()) {
name.truncate(name.length() - qstrlen(suffix));
files->append(name.toUtf8().data());
files->append(name);
}
}
std::sort(files->begin(), files->end());
Expand Down
19 changes: 0 additions & 19 deletions utility/support.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,25 +286,6 @@ int fc_remove(const char *filename)
#endif // FREECIV_MSWINDOWS
}

/**
Wrapper function for stat() with filename conversion to local
encoding on Windows.
*/
int fc_stat(const char *filename, struct stat *buf)
{
#ifdef FREECIV_MSWINDOWS
int result;
char *filename_in_local_encoding =
internal_to_local_string_malloc(filename);

result = stat(filename_in_local_encoding, buf);
free(filename_in_local_encoding);
return result;
#else // FREECIV_MSWINDOWS
return stat(filename, buf);
#endif // FREECIV_MSWINDOWS
}

/**
Returns last error code.
*/
Expand Down
1 change: 0 additions & 1 deletion utility/support.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ int fc_stricoll(const char *str0, const char *str1);

FILE *fc_fopen(const char *filename, const char *opentype);
int fc_remove(const char *filename);
int fc_stat(const char *filename, struct stat *buf);

fc_errno fc_get_errno();
const char *fc_strerror(fc_errno err);
Expand Down

0 comments on commit ecc5951

Please sign in to comment.