OpenTTD: src/ini.cpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

ini.cpp
Go to the documentation of this file.
1 /* $Id: ini.cpp 26517 2014-04-26 07:30:15Z rubidium $ */
2
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 */
9
12 #include "stdafx.h"
13 #include "debug.h"
14 #include "ini_type.h"
15 #include "string_func.h"
16 #include "fileio_func.h"
17
18 #if (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 199309L) || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 500)
19 # define WITH_FDATASYNC
20 # include <unistd.h>
21 #endif
22
23 #ifdef WIN32
24 # include <windows.h>
25 # include <shellapi.h>
26 # include "core/mem_func.hpp"
27 #endif
28
29 #include "safeguards.h"
30
35 IniFile::IniFile(const char * const *list_group_names) : IniLoadFile(list_group_names)
36 {
37 }
38
44 bool IniFile::SaveToDisk(const char *filename)
45 {
46 /*
47 * First write the configuration to a (temporary) file and then rename
48 * that file. This to prevent that when OpenTTD crashes during the save
49 * you end up with a truncated configuration file.
50 */
51 char file_new[MAX_PATH];
52
53 strecpy(file_new, filename, lastof(file_new));
54 strecat(file_new, ".new", lastof(file_new));
55 FILE *f = fopen(file_new, "w");
56 if (f == NULL) return false;
57
58 for (const IniGroup *group = this->group; group != NULL; group = group->next) {
59 if (group->comment) fputs(group->comment, f);
60 fprintf(f, "[%s]\n", group->name);
61 for (const IniItem *item = group->item; item != NULL; item = item->next) {
62 if (item->comment != NULL) fputs(item->comment, f);
63
64 /* protect item->name with quotes if needed */
65 if (strchr(item->name, ' ') != NULL ||
66 item->name[0] == '[') {
67 fprintf(f, "\"%s\"", item->name);
68 } else {
69 fprintf(f, "%s", item->name);
70 }
71
72 fprintf(f, " = %s\n", item->value == NULL ? "" : item->value);
73 }
74 }
75 if (this->comment) fputs(this->comment, f);
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 #ifdef WITH_FDATASYNC
84 int ret = fdatasync(fileno(f));
85 fclose(f);
86 if (ret != 0) return false;
87 #else
88 fclose(f);
89 #endif
90
91 #if defined(WIN32) || defined(WIN64)
92 /* _tcsncpy = strcpy is TCHAR is char, but isn't when TCHAR is wchar. */
93 #undef strncpy
94 /* Allocate space for one more \0 character. */
95 TCHAR tfilename[MAX_PATH + 1], tfile_new[MAX_PATH + 1];
96 _tcsncpy(tfilename, OTTD2FS(filename), MAX_PATH);
97 _tcsncpy(tfile_new, OTTD2FS(file_new), MAX_PATH);
98 /* SHFileOperation wants a double '\0' terminated string. */
99 tfilename[MAX_PATH - 1] = '\0';
100 tfile_new[MAX_PATH - 1] = '\0';
101 tfilename[_tcslen(tfilename) + 1] = '\0';
102 tfile_new[_tcslen(tfile_new) + 1] = '\0';
103
104 /* Rename file without any user confirmation. */
105 SHFILEOPSTRUCT shfopt;
106 MemSetT(&shfopt, 0);
107 shfopt.wFunc = FO_MOVE;
108 shfopt.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI | FOF_SILENT;
109 shfopt.pFrom = tfile_new;
110 shfopt.pTo = tfilename;
111 SHFileOperation(&shfopt);
112 #else
113 if (rename(file_new, filename) < 0) {
114 DEBUG(misc, 0, "Renaming %s to %s failed; configuration not saved", file_new, filename);
115 }
116 #endif
117
118 return true;
119 }
120
121 /* virtual */ FILE *IniFile::OpenFile(const char *filename, Subdirectory subdir, size_t *size)
122 {
123 /* Open the text file in binary mode to prevent end-of-line translations
124 * done by ftell() and friends, as defined by K&R. */
125 return FioFOpenFile(filename, "rb", subdir, size);
126 }
127
128 /* virtual */ void IniFile::ReportFileError(const char * const pre, const char * const buffer, const char * const post)
129 {
130 ShowInfoF("%s%s%s", pre, buffer, post);
131 }