2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2008-08-24 17:29:57 +00:00
|
|
|
/** @file ini.cpp Definition of the IniItem class, related to reading/writing '*.ini' files. */
|
2008-08-24 13:50:31 +00:00
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "ini_type.h"
|
|
|
|
#include "string_func.h"
|
2008-08-31 10:50:05 +00:00
|
|
|
#include "fileio_func.h"
|
2020-05-17 21:32:03 +00:00
|
|
|
#include <fstream>
|
2020-12-05 20:57:47 +00:00
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
# include <emscripten.h>
|
|
|
|
#endif
|
2008-08-24 13:50:31 +00:00
|
|
|
|
2009-03-12 14:22:17 +00:00
|
|
|
#if (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 199309L) || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 500)
|
|
|
|
# include <unistd.h>
|
2020-05-17 21:32:03 +00:00
|
|
|
# include <fcntl.h>
|
2009-03-12 14:22:17 +00:00
|
|
|
#endif
|
|
|
|
|
2018-12-09 01:28:14 +00:00
|
|
|
#ifdef _WIN32
|
2012-07-19 19:21:47 +00:00
|
|
|
# include <windows.h>
|
2009-03-14 00:26:03 +00:00
|
|
|
# include <shellapi.h>
|
2011-03-03 23:34:14 +00:00
|
|
|
# include "core/mem_func.hpp"
|
2009-03-14 00:26:03 +00:00
|
|
|
#endif
|
2009-03-12 14:22:17 +00:00
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2011-05-02 17:42:12 +00:00
|
|
|
/**
|
|
|
|
* Create a new ini file with given group names.
|
2019-04-10 21:07:06 +00:00
|
|
|
* @param list_group_names A \c nullptr terminated list with group names that should be loaded as lists instead of variables. @see IGT_LIST
|
2011-05-02 17:42:12 +00:00
|
|
|
*/
|
2011-03-03 20:50:24 +00:00
|
|
|
IniFile::IniFile(const char * const *list_group_names) : IniLoadFile(list_group_names)
|
2008-08-24 13:50:31 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-01-18 22:31:06 +00:00
|
|
|
/**
|
|
|
|
* Save the Ini file's data to the disk.
|
|
|
|
* @param filename the file to save to.
|
|
|
|
* @return true if saving succeeded.
|
|
|
|
*/
|
2020-12-06 20:11:42 +00:00
|
|
|
bool IniFile::SaveToDisk(const std::string &filename)
|
2008-08-24 13:50:31 +00:00
|
|
|
{
|
2009-03-12 14:22:17 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2020-05-17 21:32:03 +00:00
|
|
|
std::string file_new{ filename };
|
|
|
|
file_new.append(".new");
|
2009-03-12 14:22:17 +00:00
|
|
|
|
2021-04-10 09:15:47 +00:00
|
|
|
std::ofstream os(OTTD2FS(file_new).c_str());
|
2020-05-17 21:32:03 +00:00
|
|
|
if (os.fail()) return false;
|
2008-08-24 13:50:31 +00:00
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
for (const IniGroup *group = this->group; group != nullptr; group = group->next) {
|
2020-05-17 21:32:03 +00:00
|
|
|
os << group->comment << "[" << group->name << "]\n";
|
2019-04-10 21:07:06 +00:00
|
|
|
for (const IniItem *item = group->item; item != nullptr; item = item->next) {
|
2020-05-17 21:32:03 +00:00
|
|
|
os << item->comment;
|
2008-08-24 13:50:31 +00:00
|
|
|
|
|
|
|
/* protect item->name with quotes if needed */
|
2020-05-17 21:32:03 +00:00
|
|
|
if (item->name.find(' ') != std::string::npos ||
|
|
|
|
item->name[0] == '[') {
|
|
|
|
os << "\"" << item->name << "\"";
|
2008-08-24 13:50:31 +00:00
|
|
|
} else {
|
2020-05-17 21:32:03 +00:00
|
|
|
os << item->name;
|
2008-08-24 13:50:31 +00:00
|
|
|
}
|
|
|
|
|
2020-05-17 21:32:03 +00:00
|
|
|
os << " = " << item->value.value_or("") << "\n";
|
2008-08-24 13:50:31 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-17 21:32:03 +00:00
|
|
|
os << this->comment;
|
|
|
|
|
|
|
|
os.flush();
|
|
|
|
os.close();
|
|
|
|
if (os.fail()) return false;
|
2008-08-24 13:50:31 +00:00
|
|
|
|
2009-03-12 14:22:17 +00:00
|
|
|
/*
|
|
|
|
* POSIX (and friends) do not guarantee that when a file is closed it is
|
|
|
|
* flushed to the disk. So we manually flush it do disk if we have the
|
|
|
|
* APIs to do so. We only need to flush the data as the metadata itself
|
|
|
|
* (modification date etc.) is not important to us; only the real data is.
|
|
|
|
*/
|
2015-08-20 20:47:45 +00:00
|
|
|
#if defined(_POSIX_SYNCHRONIZED_IO) && _POSIX_SYNCHRONIZED_IO > 0
|
2020-05-17 21:32:03 +00:00
|
|
|
int f = open(file_new.c_str(), O_RDWR);
|
|
|
|
int ret = fdatasync(f);
|
|
|
|
close(f);
|
2009-03-12 14:22:17 +00:00
|
|
|
if (ret != 0) return false;
|
|
|
|
#endif
|
|
|
|
|
2018-12-09 01:28:14 +00:00
|
|
|
#if defined(_WIN32)
|
2009-03-14 00:26:03 +00:00
|
|
|
/* Allocate space for one more \0 character. */
|
2021-02-21 19:48:21 +00:00
|
|
|
wchar_t tfilename[MAX_PATH + 1], tfile_new[MAX_PATH + 1];
|
2021-02-21 16:03:19 +00:00
|
|
|
wcsncpy(tfilename, OTTD2FS(filename).c_str(), MAX_PATH);
|
|
|
|
wcsncpy(tfile_new, OTTD2FS(file_new).c_str(), MAX_PATH);
|
2009-03-14 00:26:03 +00:00
|
|
|
/* SHFileOperation wants a double '\0' terminated string. */
|
|
|
|
tfilename[MAX_PATH - 1] = '\0';
|
|
|
|
tfile_new[MAX_PATH - 1] = '\0';
|
2021-02-21 19:48:21 +00:00
|
|
|
tfilename[wcslen(tfilename) + 1] = '\0';
|
|
|
|
tfile_new[wcslen(tfile_new) + 1] = '\0';
|
2009-03-14 00:26:03 +00:00
|
|
|
|
|
|
|
/* Rename file without any user confirmation. */
|
|
|
|
SHFILEOPSTRUCT shfopt;
|
|
|
|
MemSetT(&shfopt, 0);
|
|
|
|
shfopt.wFunc = FO_MOVE;
|
|
|
|
shfopt.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI | FOF_SILENT;
|
|
|
|
shfopt.pFrom = tfile_new;
|
|
|
|
shfopt.pTo = tfilename;
|
|
|
|
SHFileOperation(&shfopt);
|
|
|
|
#else
|
2020-12-06 20:11:42 +00:00
|
|
|
if (rename(file_new.c_str(), filename.c_str()) < 0) {
|
2021-06-12 07:10:17 +00:00
|
|
|
Debug(misc, 0, "Renaming {} to {} failed; configuration not saved", file_new, filename);
|
2013-11-23 13:15:07 +00:00
|
|
|
}
|
2009-03-14 00:26:03 +00:00
|
|
|
#endif
|
|
|
|
|
2020-12-05 20:57:47 +00:00
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
EM_ASM(if (window["openttd_syncfs"]) openttd_syncfs());
|
|
|
|
#endif
|
|
|
|
|
2008-08-24 13:50:31 +00:00
|
|
|
return true;
|
|
|
|
}
|
2011-03-03 20:53:09 +00:00
|
|
|
|
2020-12-06 20:11:49 +00:00
|
|
|
/* virtual */ FILE *IniFile::OpenFile(const std::string &filename, Subdirectory subdir, size_t *size)
|
2011-03-03 20:53:09 +00:00
|
|
|
{
|
|
|
|
/* Open the text file in binary mode to prevent end-of-line translations
|
|
|
|
* done by ftell() and friends, as defined by K&R. */
|
2011-08-24 13:38:26 +00:00
|
|
|
return FioFOpenFile(filename, "rb", subdir, size);
|
2011-03-03 20:53:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */ void IniFile::ReportFileError(const char * const pre, const char * const buffer, const char * const post)
|
|
|
|
{
|
|
|
|
ShowInfoF("%s%s%s", pre, buffer, post);
|
|
|
|
}
|