Merge remote-tracking branch 'upstream/master'

This commit is contained in:
pelya
2021-01-25 00:50:42 +02:00
1076 changed files with 25433 additions and 61762 deletions

View File

@@ -12,9 +12,14 @@
#include "ini_type.h"
#include "string_func.h"
#include "fileio_func.h"
#include <fstream>
#ifdef __EMSCRIPTEN__
# include <emscripten.h>
#endif
#if (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 199309L) || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 500) || defined(__ANDROID__)
# include <unistd.h>
# include <fcntl.h>
#endif
#ifdef _WIN32
@@ -38,38 +43,40 @@ IniFile::IniFile(const char * const *list_group_names) : IniLoadFile(list_group_
* @param filename the file to save to.
* @return true if saving succeeded.
*/
bool IniFile::SaveToDisk(const char *filename)
bool IniFile::SaveToDisk(const std::string &filename)
{
/*
* First write the configuration to a (temporary) file and then rename
* that file. This to prevent that when OpenTTD crashes during the save
* you end up with a truncated configuration file.
*/
char file_new[MAX_PATH];
std::string file_new{ filename };
file_new.append(".new");
strecpy(file_new, filename, lastof(file_new));
strecat(file_new, ".new", lastof(file_new));
FILE *f = fopen(file_new, "w");
if (f == nullptr) return false;
std::ofstream os(OTTD2FS(file_new.c_str()));
if (os.fail()) return false;
for (const IniGroup *group = this->group; group != nullptr; group = group->next) {
if (group->comment) fputs(group->comment, f);
fprintf(f, "[%s]\n", group->name);
os << group->comment << "[" << group->name << "]\n";
for (const IniItem *item = group->item; item != nullptr; item = item->next) {
if (item->comment != nullptr) fputs(item->comment, f);
os << item->comment;
/* protect item->name with quotes if needed */
if (strchr(item->name, ' ') != nullptr ||
item->name[0] == '[') {
fprintf(f, "\"%s\"", item->name);
if (item->name.find(' ') != std::string::npos ||
item->name[0] == '[') {
os << "\"" << item->name << "\"";
} else {
fprintf(f, "%s", item->name);
os << item->name;
}
fprintf(f, " = %s\n", item->value == nullptr ? "" : item->value);
os << " = " << item->value.value_or("") << "\n";
}
}
if (this->comment) fputs(this->comment, f);
os << this->comment;
os.flush();
os.close();
if (os.fail()) return false;
/*
* POSIX (and friends) do not guarantee that when a file is closed it is
@@ -78,20 +85,19 @@ bool IniFile::SaveToDisk(const char *filename)
* (modification date etc.) is not important to us; only the real data is.
*/
#if defined(_POSIX_SYNCHRONIZED_IO) && _POSIX_SYNCHRONIZED_IO > 0
int ret = fdatasync(fileno(f));
fclose(f);
int f = open(file_new.c_str(), O_RDWR);
int ret = fdatasync(f);
close(f);
if (ret != 0) return false;
#else
fclose(f);
#endif
#if defined(_WIN32)
/* _tcsncpy = strcpy is TCHAR is char, but isn't when TCHAR is wchar. */
#undef strncpy
# undef strncpy
/* Allocate space for one more \0 character. */
TCHAR tfilename[MAX_PATH + 1], tfile_new[MAX_PATH + 1];
_tcsncpy(tfilename, OTTD2FS(filename), MAX_PATH);
_tcsncpy(tfile_new, OTTD2FS(file_new), MAX_PATH);
_tcsncpy(tfilename, OTTD2FS(filename.c_str()), MAX_PATH);
_tcsncpy(tfile_new, OTTD2FS(file_new.c_str()), MAX_PATH);
/* SHFileOperation wants a double '\0' terminated string. */
tfilename[MAX_PATH - 1] = '\0';
tfile_new[MAX_PATH - 1] = '\0';
@@ -107,15 +113,19 @@ bool IniFile::SaveToDisk(const char *filename)
shfopt.pTo = tfilename;
SHFileOperation(&shfopt);
#else
if (rename(file_new, filename) < 0) {
DEBUG(misc, 0, "Renaming %s to %s failed; configuration not saved", file_new, filename);
if (rename(file_new.c_str(), filename.c_str()) < 0) {
DEBUG(misc, 0, "Renaming %s to %s failed; configuration not saved", file_new.c_str(), filename.c_str());
}
#endif
#ifdef __EMSCRIPTEN__
EM_ASM(if (window["openttd_syncfs"]) openttd_syncfs());
#endif
return true;
}
/* virtual */ FILE *IniFile::OpenFile(const char *filename, Subdirectory subdir, size_t *size)
/* virtual */ FILE *IniFile::OpenFile(const std::string &filename, Subdirectory subdir, size_t *size)
{
/* Open the text file in binary mode to prevent end-of-line translations
* done by ftell() and friends, as defined by K&R. */