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/>.
8 /** @file ini.cpp Definition of the IniItem class, related to reading/writing '*.ini' files. */
13 #include "string_func.h"
14 #include "fileio_func.h"
16 #if (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 199309L) || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 500)
22 # include <shellapi.h>
23 # include "core/mem_func.hpp"
26 #include "safeguards.h"
29 * Create a new ini file with given group names.
30 * @param list_group_names A \c nullptr terminated list with group names that should be loaded as lists instead of variables. @see IGT_LIST
32 IniFile::IniFile(const char * const *list_group_names
) : IniLoadFile(list_group_names
)
37 * Save the Ini file's data to the disk.
38 * @param filename the file to save to.
39 * @return true if saving succeeded.
41 bool IniFile::SaveToDisk(const char *filename
)
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.
48 char file_new
[MAX_PATH
];
50 strecpy(file_new
, filename
, lastof(file_new
));
51 strecat(file_new
, ".new", lastof(file_new
));
52 FILE *f
= fopen(file_new
, "w");
53 if (f
== nullptr) return false;
55 for (const IniGroup
*group
= this->group
; group
!= nullptr; group
= group
->next
) {
56 if (group
->comment
) fputs(group
->comment
, f
);
57 fprintf(f
, "[%s]\n", group
->name
);
58 for (const IniItem
*item
= group
->item
; item
!= nullptr; item
= item
->next
) {
59 if (item
->comment
!= nullptr) fputs(item
->comment
, f
);
61 /* protect item->name with quotes if needed */
62 if (strchr(item
->name
, ' ') != nullptr ||
63 item
->name
[0] == '[') {
64 fprintf(f
, "\"%s\"", item
->name
);
66 fprintf(f
, "%s", item
->name
);
69 fprintf(f
, " = %s\n", item
->value
== nullptr ? "" : item
->value
);
72 if (this->comment
) fputs(this->comment
, f
);
75 * POSIX (and friends) do not guarantee that when a file is closed it is
76 * flushed to the disk. So we manually flush it do disk if we have the
77 * APIs to do so. We only need to flush the data as the metadata itself
78 * (modification date etc.) is not important to us; only the real data is.
80 #if defined(_POSIX_SYNCHRONIZED_IO) && _POSIX_SYNCHRONIZED_IO > 0
81 int ret
= fdatasync(fileno(f
));
83 if (ret
!= 0) return false;
89 /* _tcsncpy = strcpy is TCHAR is char, but isn't when TCHAR is wchar. */
91 /* Allocate space for one more \0 character. */
92 TCHAR tfilename
[MAX_PATH
+ 1], tfile_new
[MAX_PATH
+ 1];
93 _tcsncpy(tfilename
, OTTD2FS(filename
), MAX_PATH
);
94 _tcsncpy(tfile_new
, OTTD2FS(file_new
), MAX_PATH
);
95 /* SHFileOperation wants a double '\0' terminated string. */
96 tfilename
[MAX_PATH
- 1] = '\0';
97 tfile_new
[MAX_PATH
- 1] = '\0';
98 tfilename
[_tcslen(tfilename
) + 1] = '\0';
99 tfile_new
[_tcslen(tfile_new
) + 1] = '\0';
101 /* Rename file without any user confirmation. */
102 SHFILEOPSTRUCT shfopt
;
104 shfopt
.wFunc
= FO_MOVE
;
105 shfopt
.fFlags
= FOF_NOCONFIRMATION
| FOF_NOCONFIRMMKDIR
| FOF_NOERRORUI
| FOF_SILENT
;
106 shfopt
.pFrom
= tfile_new
;
107 shfopt
.pTo
= tfilename
;
108 SHFileOperation(&shfopt
);
110 if (rename(file_new
, filename
) < 0) {
111 DEBUG(misc
, 0, "Renaming %s to %s failed; configuration not saved", file_new
, filename
);
118 /* virtual */ FILE *IniFile::OpenFile(const char *filename
, Subdirectory subdir
, size_t *size
)
120 /* Open the text file in binary mode to prevent end-of-line translations
121 * done by ftell() and friends, as defined by K&R. */
122 return FioFOpenFile(filename
, "rb", subdir
, size
);
125 /* virtual */ void IniFile::ReportFileError(const char * const pre
, const char * const buffer
, const char * const post
)
127 ShowInfoF("%s%s%s", pre
, buffer
, post
);