OpenTTD Source: src/ini.cpp Source File

OpenTTD Source 15.3

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
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
19
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
24
25#include <filesystem>
26
27#include "safeguards.h"
28
33IniFile::IniFile(const IniGroupNameList &list_group_names) : IniLoadFile(list_group_names)
34{
35}
36
42 bool IniFile::SaveToDisk(const std::string &filename)
43{
44 /*
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.
48 */
49 std::string file_new{ filename };
50 file_new.append(".new");
51
52 std::ofstream os(OTTD2FS(file_new).c_str());
53 if (os.fail()) return false;
54
55 for (const IniGroup &group : this->groups) {
56 os << group.comment << "[" << group.name << "]\n";
57 for (const IniItem &item : group.items) {
58 os << item.comment;
59
60 /* protect item->name with quotes if needed */
61 if (item.name.find(' ') != std::string::npos ||
62 item.name[0] == '[') {
63 os << "\"" << item.name << "\"";
64 } else {
65 os << item.name;
66 }
67
68 os << " = " << item.value.value_or("") << "\n";
69 }
70 }
71 os << this->comment;
72
73 os.flush();
74 os.close();
75 if (os.fail()) return false;
76
77/*
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.
82 */
83#if defined(_POSIX_SYNCHRONIZED_IO) && _POSIX_SYNCHRONIZED_IO > 0
84 int f = open(file_new.c_str(), O_RDWR);
85 if (f < 0) return false;
86 int ret = fdatasync(f);
87 close(f);
88 if (ret != 0) return false;
89#endif
90
91 std::error_code ec;
92 std::filesystem::rename(OTTD2FS(file_new), OTTD2FS(filename), ec);
93 if (ec) {
94 Debug(misc, 0, "Renaming {} to {} failed; configuration not saved: {}", file_new, filename, ec.message());
95 }
96
97#ifdef __EMSCRIPTEN__
98 EM_ASM(if (window["openttd_syncfs"]) openttd_syncfs());
99#endif
100
101 return true;
102}
103
104/* virtual */ std::optional<FileHandle> IniFile::OpenFile(std::string_view filename, Subdirectory subdir, size_t *size)
105{
106 /* Open the text file in binary mode to prevent end-of-line translations
107 * done by ftell() and friends, as defined by K&R. */
108 return FioFOpenFile(filename, "rb", subdir, size);
109}
110
111/* virtual */ void IniFile::ReportFileError(std::string_view message)
112{
113 ShowInfo("{}", message);
114}
debug.h
Functions related to debugging.
Debug
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
FioFOpenFile
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:242
fileio_func.h
Functions for Standard In/Out file operations.
Subdirectory
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition fileio_type.h:88
ini_type.h
Types related to reading/writing '*.ini' files.
safeguards.h
A number of safeguards to prevent using unsafe methods.
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
string_func.h
Functions related to low-level strings.
IniFile::SaveToDisk
bool SaveToDisk(const std::string &filename)
Save the Ini file's data to the disk.
Definition ini.cpp:42
IniFile::OpenFile
std::optional< FileHandle > OpenFile(std::string_view filename, Subdirectory subdir, size_t *size) override
Open the INI file.
Definition ini.cpp:104
IniFile::IniFile
IniFile(const IniGroupNameList &list_group_names={})
Create a new ini file with given group names.
Definition ini.cpp:33
IniFile::ReportFileError
void ReportFileError(std::string_view message) override
Report an error about the file contents.
Definition ini.cpp:111
IniGroup
A group within an ini file.
Definition ini_type.h:34
IniItem
A single "line" in an ini file.
Definition ini_type.h:23
IniLoadFile
Ini file that only supports loading.
Definition ini_type.h:50
IniLoadFile::groups
std::list< IniGroup > groups
all groups in the ini
Definition ini_type.h:53
IniLoadFile::comment
std::string comment
last comment in file
Definition ini_type.h:54
OTTD2FS
std::wstring OTTD2FS(std::string_view name)
Convert from OpenTTD's encoding to a wide string.
Definition win32.cpp:357