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/>.
8 /** @file ini.cpp Definition of the IniItem class, related to reading/writing '*.ini' files. */
13 #include "string_func.h"
14 #include "fileio_func.h"
17 # include <emscripten.h>
20 #if (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 199309L) || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 500)
27 #include "safeguards.h"
30 * Create a new ini file with given group names.
31 * @param list_group_names A list with group names that should be loaded as lists instead of variables. @see IGT_LIST
33 IniFile::IniFile(const IniGroupNameList
&list_group_names
) : IniLoadFile(list_group_names
)
38 * Save the Ini file's data to the disk.
39 * @param filename the file to save to.
40 * @return true if saving succeeded.
42 bool IniFile::SaveToDisk(const std::string
&filename
)
45 * First write the configuration to a (temporary) file and then rename
46 * that file. This to prevent that when OpenTTD crashes during the save
47 * you end up with a truncated configuration file.
49 std::string file_new
{ filename
};
50 file_new
.append(".new");
52 std::ofstream
os(OTTD2FS(file_new
).c_str());
53 if (os
.fail()) return false;
55 for (const IniGroup
&group
: this->groups
) {
56 os
<< group
.comment
<< "[" << group
.name
<< "]\n";
57 for (const IniItem
&item
: group
.items
) {
60 /* protect item->name with quotes if needed */
61 if (item
.name
.find(' ') != std::string::npos
||
62 item
.name
[0] == '[') {
63 os
<< "\"" << item
.name
<< "\"";
68 os
<< " = " << item
.value
.value_or("") << "\n";
75 if (os
.fail()) return false;
78 * POSIX (and friends) do not guarantee that when a file is closed it is
79 * flushed to the disk. So we manually flush it do disk if we have the
80 * APIs to do so. We only need to flush the data as the metadata itself
81 * (modification date etc.) is not important to us; only the real data is.
83 #if defined(_POSIX_SYNCHRONIZED_IO) && _POSIX_SYNCHRONIZED_IO > 0
84 int f
= open(file_new
.c_str(), O_RDWR
);
85 int ret
= fdatasync(f
);
87 if (ret
!= 0) return false;
91 std::filesystem::rename(OTTD2FS(file_new
), OTTD2FS(filename
), ec
);
93 Debug(misc
, 0, "Renaming {} to {} failed; configuration not saved: {}", file_new
, filename
, ec
.message());
97 EM_ASM(if (window
["openttd_syncfs"]) openttd_syncfs());
103 /* virtual */ std::optional
<FileHandle
> IniFile::OpenFile(const std::string
&filename
, Subdirectory subdir
, size_t *size
)
105 /* Open the text file in binary mode to prevent end-of-line translations
106 * done by ftell() and friends, as defined by K&R. */
107 return FioFOpenFile(filename
, "rb", subdir
, size
);
110 /* virtual */ void IniFile::ReportFileError(const char * const pre
, const char * const buffer
, const char * const post
)
112 ShowInfo("{}{}{}", pre
, buffer
, post
);