OpenTTD
ini.cpp
Go to the documentation of this file.
1 /* $Id: ini.cpp 27389 2015-08-20 20:47:45Z frosch $ */
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 # include <unistd.h>
20 #endif
21 
22 #ifdef WIN32
23 # include <windows.h>
24 # include <shellapi.h>
25 # include "core/mem_func.hpp"
26 #endif
27 
28 #include "safeguards.h"
29 
34 IniFile::IniFile(const char * const *list_group_names) : IniLoadFile(list_group_names)
35 {
36 }
37 
43 bool IniFile::SaveToDisk(const char *filename)
44 {
45  /*
46  * First write the configuration to a (temporary) file and then rename
47  * that file. This to prevent that when OpenTTD crashes during the save
48  * you end up with a truncated configuration file.
49  */
50  char file_new[MAX_PATH];
51 
52  strecpy(file_new, filename, lastof(file_new));
53  strecat(file_new, ".new", lastof(file_new));
54  FILE *f = fopen(file_new, "w");
55  if (f == NULL) return false;
56 
57  for (const IniGroup *group = this->group; group != NULL; group = group->next) {
58  if (group->comment) fputs(group->comment, f);
59  fprintf(f, "[%s]\n", group->name);
60  for (const IniItem *item = group->item; item != NULL; item = item->next) {
61  if (item->comment != NULL) fputs(item->comment, f);
62 
63  /* protect item->name with quotes if needed */
64  if (strchr(item->name, ' ') != NULL ||
65  item->name[0] == '[') {
66  fprintf(f, "\"%s\"", item->name);
67  } else {
68  fprintf(f, "%s", item->name);
69  }
70 
71  fprintf(f, " = %s\n", item->value == NULL ? "" : item->value);
72  }
73  }
74  if (this->comment) fputs(this->comment, f);
75 
76 /*
77  * POSIX (and friends) do not guarantee that when a file is closed it is
78  * flushed to the disk. So we manually flush it do disk if we have the
79  * APIs to do so. We only need to flush the data as the metadata itself
80  * (modification date etc.) is not important to us; only the real data is.
81  */
82 #if defined(_POSIX_SYNCHRONIZED_IO) && _POSIX_SYNCHRONIZED_IO > 0
83  int ret = fdatasync(fileno(f));
84  fclose(f);
85  if (ret != 0) return false;
86 #else
87  fclose(f);
88 #endif
89 
90 #if defined(WIN32) || defined(WIN64)
91  /* _tcsncpy = strcpy is TCHAR is char, but isn't when TCHAR is wchar. */
92  #undef strncpy
93  /* Allocate space for one more \0 character. */
94  TCHAR tfilename[MAX_PATH + 1], tfile_new[MAX_PATH + 1];
95  _tcsncpy(tfilename, OTTD2FS(filename), MAX_PATH);
96  _tcsncpy(tfile_new, OTTD2FS(file_new), MAX_PATH);
97  /* SHFileOperation wants a double '\0' terminated string. */
98  tfilename[MAX_PATH - 1] = '\0';
99  tfile_new[MAX_PATH - 1] = '\0';
100  tfilename[_tcslen(tfilename) + 1] = '\0';
101  tfile_new[_tcslen(tfile_new) + 1] = '\0';
102 
103  /* Rename file without any user confirmation. */
104  SHFILEOPSTRUCT shfopt;
105  MemSetT(&shfopt, 0);
106  shfopt.wFunc = FO_MOVE;
107  shfopt.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI | FOF_SILENT;
108  shfopt.pFrom = tfile_new;
109  shfopt.pTo = tfilename;
110  SHFileOperation(&shfopt);
111 #else
112  if (rename(file_new, filename) < 0) {
113  DEBUG(misc, 0, "Renaming %s to %s failed; configuration not saved", file_new, filename);
114  }
115 #endif
116 
117  return true;
118 }
119 
120 /* virtual */ FILE *IniFile::OpenFile(const char *filename, Subdirectory subdir, size_t *size)
121 {
122  /* Open the text file in binary mode to prevent end-of-line translations
123  * done by ftell() and friends, as defined by K&R. */
124  return FioFOpenFile(filename, "rb", subdir, size);
125 }
126 
127 /* virtual */ void IniFile::ReportFileError(const char * const pre, const char * const buffer, const char * const post)
128 {
129  ShowInfoF("%s%s%s", pre, buffer, post);
130 }
A group within an ini file.
Definition: ini_type.h:38
static char * strecat(char *dst, const char *src, const char *last)
Appends characters from one string to another.
Definition: depend.cpp:99
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:110
char * comment
comment for group
Definition: ini_type.h:44
Functions related to debugging.
IniItem * item
the first item in the group
Definition: ini_type.h:41
Functions for Standard In/Out file operations.
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:50
IniItem * next
The next item in this group.
Definition: ini_type.h:26
char * comment
last comment in file
Definition: ini_type.h:57
A single "line" in an ini file.
Definition: ini_type.h:25
virtual FILE * OpenFile(const char *filename, Subdirectory subdir, size_t *size)
Open the INI file.
Definition: ini.cpp:120
void CDECL ShowInfoF(const char *str,...)
Shows some information on the console/a popup box depending on the OS.
Definition: openttd.cpp:130
Functions related to low-level strings.
IniGroup * group
the first group in the ini
Definition: ini_type.h:55
IniFile(const char *const *list_group_names=NULL)
Create a new ini file with given group names.
Definition: ini.cpp:34
bool SaveToDisk(const char *filename)
Save the Ini file&#39;s data to the disk.
Definition: ini.cpp:43
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:474
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:127
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:631
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:39
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: depend.cpp:68
IniGroup * next
the next group within this file
Definition: ini_type.h:39
char * name
name of group
Definition: ini_type.h:43
Ini file that only supports loading.
Definition: ini_type.h:54
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:51