17 # include <emscripten.h>
20 #if (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 199309L) || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 500)
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;
56 os << group.comment <<
"[" << group.name <<
"]\n";
57 for (
const IniItem &item : group.items) {
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;
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;
93 Debug(misc, 0,
"Renaming {} to {} failed; configuration not saved: {}", file_new, filename, ec.message());
97 EM_ASM(
if (window[
"openttd_syncfs"]) openttd_syncfs());
112 ShowInfo(
"{}{}{}", pre, buffer, post);
Functions related to debugging.
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
std::optional< FileHandle > FioFOpenFile(const std::string &filename, const char *mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
Functions for Standard In/Out file operations.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Types related to reading/writing '*.ini' files.
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
Functions related to low-level strings.
std::optional< FileHandle > OpenFile(const std::string &filename, Subdirectory subdir, size_t *size) override
Open the INI file.
bool SaveToDisk(const std::string &filename)
Save the Ini file's data to the disk.
IniFile(const IniGroupNameList &list_group_names={})
Create a new ini file with given group names.
void ReportFileError(const char *const pre, const char *const buffer, const char *const post) override
Report an error about the file contents.
A group within an ini file.
A single "line" in an ini file.
std::optional< std::string > value
The value of this item.
std::string name
The name of this item.
std::string comment
The comment associated with this item.
Ini file that only supports loading.
std::list< IniGroup > groups
all groups in the ini
std::string comment
last comment in file
std::wstring OTTD2FS(const std::string &name)
Convert from OpenTTD's encoding to a wide string.