OpenTTD
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 
16 #if (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 199309L) || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 500)
17 # include <unistd.h>
18 #endif
19 
20 #ifdef _WIN32
21 # include <windows.h>
22 # include <shellapi.h>
23 # include "core/mem_func.hpp"
24 #endif
25 
26 #include "safeguards.h"
27 
32 IniFile::IniFile(const char * const *list_group_names) : IniLoadFile(list_group_names)
33 {
34 }
35 
41 bool IniFile::SaveToDisk(const char *filename)
42 {
43  /*
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.
47  */
48  char file_new[MAX_PATH];
49 
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;
54 
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);
60 
61  /* protect item->name with quotes if needed */
62  if (strchr(item->name, ' ') != nullptr ||
63  item->name[0] == '[') {
64  fprintf(f, "\"%s\"", item->name);
65  } else {
66  fprintf(f, "%s", item->name);
67  }
68 
69  fprintf(f, " = %s\n", item->value == nullptr ? "" : item->value);
70  }
71  }
72  if (this->comment) fputs(this->comment, f);
73 
74 /*
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.
79  */
80 #if defined(_POSIX_SYNCHRONIZED_IO) && _POSIX_SYNCHRONIZED_IO > 0
81  int ret = fdatasync(fileno(f));
82  fclose(f);
83  if (ret != 0) return false;
84 #else
85  fclose(f);
86 #endif
87 
88 #if defined(_WIN32)
89  /* _tcsncpy = strcpy is TCHAR is char, but isn't when TCHAR is wchar. */
90  #undef strncpy
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';
100 
101  /* Rename file without any user confirmation. */
102  SHFILEOPSTRUCT shfopt;
103  MemSetT(&shfopt, 0);
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);
109 #else
110  if (rename(file_new, filename) < 0) {
111  DEBUG(misc, 0, "Renaming %s to %s failed; configuration not saved", file_new, filename);
112  }
113 #endif
114 
115  return true;
116 }
117 
118 /* virtual */ FILE *IniFile::OpenFile(const char *filename, Subdirectory subdir, size_t *size)
119 {
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);
123 }
124 
125 /* virtual */ void IniFile::ReportFileError(const char * const pre, const char * const buffer, const char * const post)
126 {
127  ShowInfoF("%s%s%s", pre, buffer, post);
128 }
A group within an ini file.
Definition: ini_type.h:36
static char * strecat(char *dst, const char *src, const char *last)
Appends characters from one string to another.
Definition: depend.cpp:97
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:108
char * comment
comment for group
Definition: ini_type.h:42
Functions related to debugging.
IniItem * item
the first item in the group
Definition: ini_type.h:39
Functions for Standard In/Out file operations.
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:48
IniItem * next
The next item in this group.
Definition: ini_type.h:24
char * comment
last comment in file
Definition: ini_type.h:55
A single "line" in an ini file.
Definition: ini_type.h:23
virtual FILE * OpenFile(const char *filename, Subdirectory subdir, size_t *size)
Open the INI file.
Definition: ini.cpp:118
void CDECL ShowInfoF(const char *str,...)
Shows some information on the console/a popup box depending on the OS.
Definition: openttd.cpp:134
Functions related to low-level strings.
IniGroup * group
the first group in the ini
Definition: ini_type.h:53
IniFile(const char *const *list_group_names=nullptr)
Create a new ini file with given group names.
Definition: ini.cpp:32
bool SaveToDisk(const char *filename)
Save the Ini file&#39;s data to the disk.
Definition: ini.cpp:41
FILE * FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
Definition: fileio.cpp:463
Definition of base types and functions in a cross-platform compatible way.
A number of safeguards to prevent using unsafe methods.
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:125
Types related to reading/writing &#39;*.ini&#39; files.
const TCHAR * OTTD2FS(const char *name, bool console_cp)
Convert from OpenTTD&#39;s encoding to that of the local environment.
Definition: win32.cpp:576
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: depend.cpp:66
IniGroup * next
the next group within this file
Definition: ini_type.h:37
char * name
name of group
Definition: ini_type.h:41
Ini file that only supports loading.
Definition: ini_type.h:52
Functions related to memory operations.
static void MemSetT(T *ptr, byte value, size_t num=1)
Type-safe version of memset().
Definition: mem_func.hpp:49