OpenTTD Source  12.0-beta2
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 #ifdef _WIN32
26 # include <windows.h>
27 # include <shellapi.h>
28 # include "core/mem_func.hpp"
29 #endif
30 
31 #include "safeguards.h"
32 
37 IniFile::IniFile(const char * const *list_group_names) : IniLoadFile(list_group_names)
38 {
39 }
40 
46 bool IniFile::SaveToDisk(const std::string &filename)
47 {
48  /*
49  * First write the configuration to a (temporary) file and then rename
50  * that file. This to prevent that when OpenTTD crashes during the save
51  * you end up with a truncated configuration file.
52  */
53  std::string file_new{ filename };
54  file_new.append(".new");
55 
56  std::ofstream os(OTTD2FS(file_new).c_str());
57  if (os.fail()) return false;
58 
59  for (const IniGroup *group = this->group; group != nullptr; group = group->next) {
60  os << group->comment << "[" << group->name << "]\n";
61  for (const IniItem *item = group->item; item != nullptr; item = item->next) {
62  os << item->comment;
63 
64  /* protect item->name with quotes if needed */
65  if (item->name.find(' ') != std::string::npos ||
66  item->name[0] == '[') {
67  os << "\"" << item->name << "\"";
68  } else {
69  os << item->name;
70  }
71 
72  os << " = " << item->value.value_or("") << "\n";
73  }
74  }
75  os << this->comment;
76 
77  os.flush();
78  os.close();
79  if (os.fail()) return false;
80 
81 /*
82  * POSIX (and friends) do not guarantee that when a file is closed it is
83  * flushed to the disk. So we manually flush it do disk if we have the
84  * APIs to do so. We only need to flush the data as the metadata itself
85  * (modification date etc.) is not important to us; only the real data is.
86  */
87 #if defined(_POSIX_SYNCHRONIZED_IO) && _POSIX_SYNCHRONIZED_IO > 0
88  int f = open(file_new.c_str(), O_RDWR);
89  int ret = fdatasync(f);
90  close(f);
91  if (ret != 0) return false;
92 #endif
93 
94 #if defined(_WIN32)
95  /* Allocate space for one more \0 character. */
96  wchar_t tfilename[MAX_PATH + 1], tfile_new[MAX_PATH + 1];
97  wcsncpy(tfilename, OTTD2FS(filename).c_str(), MAX_PATH);
98  wcsncpy(tfile_new, OTTD2FS(file_new).c_str(), MAX_PATH);
99  /* SHFileOperation wants a double '\0' terminated string. */
100  tfilename[MAX_PATH - 1] = '\0';
101  tfile_new[MAX_PATH - 1] = '\0';
102  tfilename[wcslen(tfilename) + 1] = '\0';
103  tfile_new[wcslen(tfile_new) + 1] = '\0';
104 
105  /* Rename file without any user confirmation. */
106  SHFILEOPSTRUCT shfopt;
107  MemSetT(&shfopt, 0);
108  shfopt.wFunc = FO_MOVE;
109  shfopt.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI | FOF_SILENT;
110  shfopt.pFrom = tfile_new;
111  shfopt.pTo = tfilename;
112  SHFileOperation(&shfopt);
113 #else
114  if (rename(file_new.c_str(), filename.c_str()) < 0) {
115  Debug(misc, 0, "Renaming {} to {} failed; configuration not saved", file_new, filename);
116  }
117 #endif
118 
119 #ifdef __EMSCRIPTEN__
120  EM_ASM(if (window["openttd_syncfs"]) openttd_syncfs());
121 #endif
122 
123  return true;
124 }
125 
126 /* virtual */ FILE *IniFile::OpenFile(const std::string &filename, Subdirectory subdir, size_t *size)
127 {
128  /* Open the text file in binary mode to prevent end-of-line translations
129  * done by ftell() and friends, as defined by K&R. */
130  return FioFOpenFile(filename, "rb", subdir, size);
131 }
132 
133 /* virtual */ void IniFile::ReportFileError(const char * const pre, const char * const buffer, const char * const post)
134 {
135  ShowInfoF("%s%s%s", pre, buffer, post);
136 }
IniLoadFile::comment
std::string comment
last comment in file
Definition: ini_type.h:58
IniItem::next
IniItem * next
The next item in this group.
Definition: ini_type.h:26
mem_func.hpp
IniItem
A single "line" in an ini file.
Definition: ini_type.h:25
IniGroup
A group within an ini file.
Definition: ini_type.h:38
fileio_func.h
IniLoadFile
Ini file that only supports loading.
Definition: ini_type.h:55
IniFile::IniFile
IniFile(const char *const *list_group_names=nullptr)
Create a new ini file with given group names.
Definition: ini.cpp:37
FioFOpenFile
FILE * 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:245
IniFile::SaveToDisk
bool SaveToDisk(const std::string &filename)
Save the Ini file's data to the disk.
Definition: ini.cpp:46
safeguards.h
stdafx.h
IniFile::ReportFileError
virtual void ReportFileError(const char *const pre, const char *const buffer, const char *const post)
Report an error about the file contents.
Definition: ini.cpp:133
string_func.h
IniLoadFile::group
IniGroup * group
the first group in the ini
Definition: ini_type.h:56
IniGroup::comment
std::string comment
comment for group
Definition: ini_type.h:44
IniGroup::name
std::string name
name of group
Definition: ini_type.h:43
IniFile::OpenFile
virtual FILE * OpenFile(const std::string &filename, Subdirectory subdir, size_t *size)
Open the INI file.
Definition: ini.cpp:126
ShowInfoF
void CDECL ShowInfoF(const char *str,...)
Shows some information on the console/a popup box depending on the OS.
Definition: openttd.cpp:154
Subdirectory
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:108
Debug
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
IniGroup::item
IniItem * item
the first item in the group
Definition: ini_type.h:41
MemSetT
static void MemSetT(T *ptr, byte value, size_t num=1)
Type-safe version of memset().
Definition: mem_func.hpp:49
ini_type.h
OTTD2FS
std::wstring OTTD2FS(const std::string &name)
Convert from OpenTTD's encoding to a wide string.
Definition: win32.cpp:560
debug.h
IniGroup::next
IniGroup * next
the next group within this file
Definition: ini_type.h:39