OpenTTD Source 20261112-master-gbcbec5e63f
ini.cpp
Go to the documentation of this file.
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "stdafx.h"
11#include "debug.h"
12#include "ini_type.h"
13#include "fileio_func.h"
14#include <fstream>
15#ifdef __EMSCRIPTEN__
16# include <emscripten.h>
17#endif
18
19#if (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 199309L) || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 500) || defined(__HAIKU__) || defined(__ANDROID__)
20# include <unistd.h>
21# include <fcntl.h>
22#endif
23
24#include <filesystem>
25
26#include "safeguards.h"
27
35
41bool IniFile::SaveToDisk(const std::string &filename)
42{
43 /*
44 * First write the configuration to a (temporary) file and then rename
45 * that file. This to prevent that when OpenTTD crashes during the save
46 * you end up with a truncated configuration file.
47 */
48 std::string file_new{ filename };
49 file_new.append(".new");
50
51 std::ofstream os(OTTD2FS(file_new).c_str());
52 if (os.fail()) return false;
53
54 for (const IniGroup &group : this->groups) {
55 os << group.comment << "[" << group.name << "]\n";
56 for (const IniItem &item : group.items) {
57 os << item.comment;
58
59 /* protect item->name with quotes if needed */
60 if (item.name.find(' ') != std::string::npos ||
61 item.name[0] == '[') {
62 os << "\"" << item.name << "\"";
63 } else {
64 os << item.name;
65 }
66
67 os << " = " << item.value.value_or("") << "\n";
68 }
69 }
70 os << this->comment;
71
72 os.flush();
73 os.close();
74 if (os.fail()) return false;
75
76/*
77 * POSIX (and friends) do not guarantee that when a file is closed it is
78 * flushed to the disk. So we manually flush it do disk if we have the
79 * APIs to do so. We only need to flush the data as the metadata itself
80 * (modification date etc.) is not important to us; only the real data is.
81 */
82#if defined(_POSIX_SYNCHRONIZED_IO) && _POSIX_SYNCHRONIZED_IO > 0
83 int f = open(file_new.c_str(), O_RDWR);
84 if (f < 0) return false;
85 int ret = fdatasync(f);
86 close(f);
87 if (ret != 0) return false;
88#endif
89
90 std::error_code ec;
91 std::filesystem::rename(OTTD2FS(file_new), OTTD2FS(filename), ec);
92 if (ec) {
93 Debug(Facility::Misc, Severity::Critical, "Renaming {} to {} failed; configuration not saved: {}", file_new, filename, ec.message());
94 }
95
96#ifdef __EMSCRIPTEN__
97 EM_ASM(if (window["openttd_syncfs"]) openttd_syncfs());
98#endif
99
100 return true;
101}
102
103/* virtual */ std::optional<FileHandle> IniFile::OpenFile(std::string_view filename, Subdirectory subdir, size_t *size)
104{
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);
108}
109
110/* virtual */ void IniFile::ReportFileError(std::string_view message)
111{
112 ShowInfo("{}", message);
113}
Functions related to debugging.
#define Debug(facility, severity, format_string,...)
Output a line of debugging information.
Definition debug.h:36
@ Misc
Misc message facility.
Definition debug_type.h:32
@ Critical
Critical, user should know about this.
Definition debug_type.h:15
std::optional< FileHandle > FioFOpenFile(std::string_view filename, std::string_view mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
Definition fileio.cpp:248
Functions for standard in/out file operations.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition fileio_type.h:87
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.
bool SaveToDisk(const std::string &filename)
Save the Ini file's data to the disk.
Definition ini.cpp:41
std::optional< FileHandle > OpenFile(std::string_view filename, Subdirectory subdir, size_t *size) override
Open the INI file.
Definition ini.cpp:103
IniFile(const IniGroupNameList &list_group_names={})
Create a new ini file with given group names.
Definition ini.cpp:32
void ReportFileError(std::string_view message) override
Report an error about the file contents.
Definition ini.cpp:110
A group within an ini file.
Definition ini_type.h:34
A single "line" in an ini file.
Definition ini_type.h:23
std::optional< std::string > value
The value of this item.
Definition ini_type.h:25
std::string name
The name of this item.
Definition ini_type.h:24
std::string comment
The comment associated with this item.
Definition ini_type.h:26
std::list< IniGroup > groups
all groups in the ini
Definition ini_type.h:53
IniLoadFile(const IniGroupNameList &list_group_names={}, const IniGroupNameList &seq_group_names={})
Construct a new in-memory Ini file representation.
Definition ini_load.cpp:106
std::string comment
last comment in file
Definition ini_type.h:54
const IniGroupNameList list_group_names
list of group names that are lists
Definition ini_type.h:55
std::wstring OTTD2FS(std::string_view name)
Convert from OpenTTD's encoding to a wide string.
Definition win32.cpp:389