OpenTTD Source  20241124-master-g9399a92a4f
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 <http://www.gnu.org/licenses/>.
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 
33 IniFile::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  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(misc, 0, "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(const std::string &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(const char * const pre, const char * const buffer, const char * const post)
111 {
112  ShowInfo("{}{}{}", pre, buffer, post);
113 }
Functions related to debugging.
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
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.
Definition: fileio.cpp:242
Functions for Standard In/Out file operations.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:115
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.
Definition: ini.cpp:103
bool SaveToDisk(const std::string &filename)
Save the Ini file's data to the disk.
Definition: ini.cpp:42
IniFile(const IniGroupNameList &list_group_names={})
Create a new ini file with given group names.
Definition: ini.cpp:33
void ReportFileError(const char *const pre, const char *const buffer, const char *const post) 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
Ini file that only supports loading.
Definition: ini_type.h:50
std::list< IniGroup > groups
all groups in the ini
Definition: ini_type.h:53
std::string comment
last comment in file
Definition: ini_type.h:54
std::wstring OTTD2FS(const std::string &name)
Convert from OpenTTD's encoding to a wide string.
Definition: win32.cpp:354