4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file ini.cpp Definition of the IniItem class, related to reading/writing '*.ini' files. */
15 #include "string_func.h"
16 #include "fileio_func.h"
18 #if (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 199309L) || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 500)
19 # define WITH_FDATASYNC
25 # include <shellapi.h>
26 # include "core/mem_func.hpp"
30 * Create a new ini file with given group names.
31 * @param list_group_names A \c NULL terminated list with group names that should be loaded as lists instead of variables. @see IGT_LIST
33 IniFile::IniFile(const char * const *list_group_names
) : IniLoadFile(list_group_names
)
38 * Save the Ini file's data to the disk.
39 * @param filename the file to save to.
40 * @return true if saving succeeded.
42 bool IniFile::SaveToDisk(const char *filename
)
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.
49 char file_new
[MAX_PATH
];
51 strecpy(file_new
, filename
, lastof(file_new
));
52 strecat(file_new
, ".new", lastof(file_new
));
53 FILE *f
= fopen(file_new
, "w");
54 if (f
== NULL
) return false;
56 for (const IniGroup
*group
= this->group
; group
!= NULL
; group
= group
->next
) {
57 if (group
->comment
) fputs(group
->comment
, f
);
58 fprintf(f
, "[%s]\n", group
->name
);
59 for (const IniItem
*item
= group
->item
; item
!= NULL
; item
= item
->next
) {
60 if (item
->comment
!= NULL
) fputs(item
->comment
, f
);
62 /* protect item->name with quotes if needed */
63 if (strchr(item
->name
, ' ') != NULL
||
64 item
->name
[0] == '[') {
65 fprintf(f
, "\"%s\"", item
->name
);
67 fprintf(f
, "%s", item
->name
);
70 fprintf(f
, " = %s\n", item
->value
== NULL
? "" : item
->value
);
73 if (this->comment
) fputs(this->comment
, f
);
76 * POSIX (and friends) do not guarantee that when a file is closed it is
77 * flushed to the disk. So we manually flush it do disk if we have the
78 * APIs to do so. We only need to flush the data as the metadata itself
79 * (modification date etc.) is not important to us; only the real data is.
82 int ret
= fdatasync(fileno(f
));
84 if (ret
!= 0) return false;
89 #if defined(WIN32) || defined(WIN64)
90 /* Allocate space for one more \0 character. */
91 TCHAR tfilename
[MAX_PATH
+ 1], tfile_new
[MAX_PATH
+ 1];
92 _tcsncpy(tfilename
, OTTD2FS(filename
), MAX_PATH
);
93 _tcsncpy(tfile_new
, OTTD2FS(file_new
), MAX_PATH
);
94 /* SHFileOperation wants a double '\0' terminated string. */
95 tfilename
[MAX_PATH
- 1] = '\0';
96 tfile_new
[MAX_PATH
- 1] = '\0';
97 tfilename
[_tcslen(tfilename
) + 1] = '\0';
98 tfile_new
[_tcslen(tfile_new
) + 1] = '\0';
100 /* Rename file without any user confirmation. */
101 SHFILEOPSTRUCT shfopt
;
103 shfopt
.wFunc
= FO_MOVE
;
104 shfopt
.fFlags
= FOF_NOCONFIRMATION
| FOF_NOCONFIRMMKDIR
| FOF_NOERRORUI
| FOF_SILENT
;
105 shfopt
.pFrom
= tfile_new
;
106 shfopt
.pTo
= tfilename
;
107 SHFileOperation(&shfopt
);
109 if (rename(file_new
, filename
) < 0) {
110 DEBUG(misc
, 0, "Renaming %s to %s failed; configuration not saved", file_new
, filename
);
117 /* virtual */ FILE *IniFile::OpenFile(const char *filename
, Subdirectory subdir
, size_t *size
)
119 /* Open the text file in binary mode to prevent end-of-line translations
120 * done by ftell() and friends, as defined by K&R. */
121 return FioFOpenFile(filename
, "rb", subdir
, size
);
124 /* virtual */ void IniFile::ReportFileError(const char * const pre
, const char * const buffer
, const char * const post
)
126 ShowInfoF("%s%s%s", pre
, buffer
, post
);