Fix #9521: Don't load at just removed docks that were part of a multi-dock station...
[openttd-github.git] / src / ini.cpp
blobfe6cbaa083c975978808f7863b53632c5a8717cd
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
8 /** @file ini.cpp Definition of the IniItem class, related to reading/writing '*.ini' files. */
10 #include "stdafx.h"
11 #include "debug.h"
12 #include "ini_type.h"
13 #include "string_func.h"
14 #include "fileio_func.h"
15 #include <fstream>
16 #ifdef __EMSCRIPTEN__
17 # include <emscripten.h>
18 #endif
20 #if (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 199309L) || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 500)
21 # include <unistd.h>
22 # include <fcntl.h>
23 #endif
25 #ifdef _WIN32
26 # include <windows.h>
27 # include <shellapi.h>
28 # include "core/mem_func.hpp"
29 #endif
31 #include "safeguards.h"
33 /**
34 * Create a new ini file with given group names.
35 * @param list_group_names A \c nullptr terminated list with group names that should be loaded as lists instead of variables. @see IGT_LIST
37 IniFile::IniFile(const char * const *list_group_names) : IniLoadFile(list_group_names)
41 /**
42 * Save the Ini file's data to the disk.
43 * @param filename the file to save to.
44 * @return true if saving succeeded.
46 bool IniFile::SaveToDisk(const std::string &filename)
49 * First write the configuration to a (temporary) file and then rename
50 * that file. This to prevent that when OpenTTD crashes during the save
51 * you end up with a truncated configuration file.
53 std::string file_new{ filename };
54 file_new.append(".new");
56 std::ofstream os(OTTD2FS(file_new).c_str());
57 if (os.fail()) return false;
59 for (const IniGroup *group = this->group; group != nullptr; group = group->next) {
60 os << group->comment << "[" << group->name << "]\n";
61 for (const IniItem *item = group->item; item != nullptr; item = item->next) {
62 os << item->comment;
64 /* protect item->name with quotes if needed */
65 if (item->name.find(' ') != std::string::npos ||
66 item->name[0] == '[') {
67 os << "\"" << item->name << "\"";
68 } else {
69 os << item->name;
72 os << " = " << item->value.value_or("") << "\n";
75 os << this->comment;
77 os.flush();
78 os.close();
79 if (os.fail()) return false;
82 * POSIX (and friends) do not guarantee that when a file is closed it is
83 * flushed to the disk. So we manually flush it do disk if we have the
84 * APIs to do so. We only need to flush the data as the metadata itself
85 * (modification date etc.) is not important to us; only the real data is.
87 #if defined(_POSIX_SYNCHRONIZED_IO) && _POSIX_SYNCHRONIZED_IO > 0
88 int f = open(file_new.c_str(), O_RDWR);
89 int ret = fdatasync(f);
90 close(f);
91 if (ret != 0) return false;
92 #endif
94 #if defined(_WIN32)
95 /* Allocate space for one more \0 character. */
96 wchar_t tfilename[MAX_PATH + 1], tfile_new[MAX_PATH + 1];
97 wcsncpy(tfilename, OTTD2FS(filename).c_str(), MAX_PATH);
98 wcsncpy(tfile_new, OTTD2FS(file_new).c_str(), MAX_PATH);
99 /* SHFileOperation wants a double '\0' terminated string. */
100 tfilename[MAX_PATH - 1] = '\0';
101 tfile_new[MAX_PATH - 1] = '\0';
102 tfilename[wcslen(tfilename) + 1] = '\0';
103 tfile_new[wcslen(tfile_new) + 1] = '\0';
105 /* Rename file without any user confirmation. */
106 SHFILEOPSTRUCT shfopt;
107 MemSetT(&shfopt, 0);
108 shfopt.wFunc = FO_MOVE;
109 shfopt.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI | FOF_SILENT;
110 shfopt.pFrom = tfile_new;
111 shfopt.pTo = tfilename;
112 SHFileOperation(&shfopt);
113 #else
114 if (rename(file_new.c_str(), filename.c_str()) < 0) {
115 Debug(misc, 0, "Renaming {} to {} failed; configuration not saved", file_new, filename);
117 #endif
119 #ifdef __EMSCRIPTEN__
120 EM_ASM(if (window["openttd_syncfs"]) openttd_syncfs());
121 #endif
123 return true;
126 /* virtual */ FILE *IniFile::OpenFile(const std::string &filename, Subdirectory subdir, size_t *size)
128 /* Open the text file in binary mode to prevent end-of-line translations
129 * done by ftell() and friends, as defined by K&R. */
130 return FioFOpenFile(filename, "rb", subdir, size);
133 /* virtual */ void IniFile::ReportFileError(const char * const pre, const char * const buffer, const char * const post)
135 ShowInfoF("%s%s%s", pre, buffer, post);