Fix old map array tunnel head conversion
[openttd/fttd.git] / src / ini.cpp
blob5c391a10c5683dd551d990d7905337561b2fb66f
1 /* $Id$ */
3 /*
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/>.
8 */
10 /** @file ini.cpp Definition of the IniItem class, related to reading/writing '*.ini' files. */
12 #include "stdafx.h"
13 #include "debug.h"
14 #include "ini_type.h"
15 #include "string.h"
16 #include "fileio_func.h"
18 #if (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 199309L) || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 500)
19 # include <unistd.h>
20 #endif
22 #ifdef WIN32
23 # include <windows.h>
24 # include <shellapi.h>
25 # include "core/mem_func.hpp"
26 #endif
28 /**
29 * Create a new ini file with given group names.
30 * @param list_group_names A \c NULL terminated list with group names that should be loaded as lists instead of variables. @see IGT_LIST
32 IniFile::IniFile (const char *filename, Subdirectory subdir, const char * const *list_group_names)
33 : IniLoadFile (list_group_names)
35 /* Open the text file in binary mode to prevent end-of-line
36 * translations done by ftell() and friends, as defined by K&R. */
37 size_t end;
38 FILE *in = FioFOpenFile (filename, "rb", subdir, &end);
39 if (in != NULL) {
40 end += ftell (in);
41 this->load (in, end);
45 /**
46 * Save the Ini file's data to the disk.
47 * @param filename the file to save to.
48 * @return true if saving succeeded.
50 bool IniFile::SaveToDisk(const char *filename)
53 * First write the configuration to a (temporary) file and then rename
54 * that file. This to prevent that when OpenTTD crashes during the save
55 * you end up with a truncated configuration file.
57 char file_new[MAX_PATH];
58 bstrfmt (file_new, "%s.new", filename);
60 FILE *f = fopen(file_new, "w");
61 if (f == NULL) return false;
63 for (IniGroup::const_iterator group = this->cbegin(); group != this->cend(); group++) {
64 if (group->comment) fputs(group->comment, f);
65 fprintf(f, "[%s]\n", group->get_name());
66 for (IniItem::const_iterator item = group->cbegin(); item != group->cend(); item++) {
67 if (item->comment != NULL) fputs(item->comment, f);
69 /* protect item->name with quotes if needed */
70 if (strchr(item->get_name(), ' ') != NULL ||
71 item->get_name()[0] == '[') {
72 fprintf(f, "\"%s\"", item->get_name());
73 } else {
74 fprintf(f, "%s", item->get_name());
77 fprintf(f, " = %s\n", item->value == NULL ? "" : item->value);
80 if (this->comment) fputs(this->comment, f);
83 * POSIX (and friends) do not guarantee that when a file is closed it is
84 * flushed to the disk. So we manually flush it do disk if we have the
85 * APIs to do so. We only need to flush the data as the metadata itself
86 * (modification date etc.) is not important to us; only the real data is.
88 #if defined(_POSIX_SYNCHRONIZED_IO) && _POSIX_SYNCHRONIZED_IO > 0
89 int ret = fdatasync(fileno(f));
90 fclose(f);
91 if (ret != 0) return false;
92 #else
93 fclose(f);
94 #endif
96 #if defined(WIN32) || defined(WIN64)
97 /* Allocate space for one more \0 character. */
98 TCHAR tfilename[MAX_PATH + 1], tfile_new[MAX_PATH + 1];
99 _tcsncpy(tfilename, OTTD2FS(filename), MAX_PATH);
100 _tcsncpy(tfile_new, OTTD2FS(file_new), MAX_PATH);
101 /* SHFileOperation wants a double '\0' terminated string. */
102 tfilename[MAX_PATH - 1] = '\0';
103 tfile_new[MAX_PATH - 1] = '\0';
104 tfilename[_tcslen(tfilename) + 1] = '\0';
105 tfile_new[_tcslen(tfile_new) + 1] = '\0';
107 /* Rename file without any user confirmation. */
108 SHFILEOPSTRUCT shfopt;
109 MemSetT(&shfopt, 0);
110 shfopt.wFunc = FO_MOVE;
111 shfopt.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI | FOF_SILENT;
112 shfopt.pFrom = tfile_new;
113 shfopt.pTo = tfilename;
114 SHFileOperation(&shfopt);
115 #else
116 if (rename(file_new, filename) < 0) {
117 DEBUG(misc, 0, "Renaming %s to %s failed; configuration not saved", file_new, filename);
119 #endif
121 return true;
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);