Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "debug.h"
00014 #include "ini_type.h"
00015 #include "string_func.h"
00016 #include "fileio_func.h"
00017
00018 #if (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 199309L) || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 500)
00019 # define WITH_FDATASYNC
00020 # include <unistd.h>
00021 #endif
00022
00023 #ifdef WIN32
00024 # include <windows.h>
00025 # include <shellapi.h>
00026 # include "core/mem_func.hpp"
00027 #endif
00028
00033 IniFile::IniFile(const char * const *list_group_names) : IniLoadFile(list_group_names)
00034 {
00035 }
00036
00042 bool IniFile::SaveToDisk(const char *filename)
00043 {
00044
00045
00046
00047
00048
00049 char file_new[MAX_PATH];
00050
00051 strecpy(file_new, filename, lastof(file_new));
00052 strecat(file_new, ".new", lastof(file_new));
00053 FILE *f = fopen(file_new, "w");
00054 if (f == NULL) return false;
00055
00056 for (const IniGroup *group = this->group; group != NULL; group = group->next) {
00057 if (group->comment) fputs(group->comment, f);
00058 fprintf(f, "[%s]\n", group->name);
00059 for (const IniItem *item = group->item; item != NULL; item = item->next) {
00060 if (item->comment != NULL) fputs(item->comment, f);
00061
00062
00063 if (strchr(item->name, ' ') != NULL ||
00064 item->name[0] == '[') {
00065 fprintf(f, "\"%s\"", item->name);
00066 } else {
00067 fprintf(f, "%s", item->name);
00068 }
00069
00070 fprintf(f, " = %s\n", item->value == NULL ? "" : item->value);
00071 }
00072 }
00073 if (this->comment) fputs(this->comment, f);
00074
00075
00076
00077
00078
00079
00080
00081 #ifdef WITH_FDATASYNC
00082 int ret = fdatasync(fileno(f));
00083 fclose(f);
00084 if (ret != 0) return false;
00085 #else
00086 fclose(f);
00087 #endif
00088
00089 #if defined(WIN32) || defined(WIN64)
00090
00091 TCHAR tfilename[MAX_PATH + 1], tfile_new[MAX_PATH + 1];
00092 _tcsncpy(tfilename, OTTD2FS(filename), MAX_PATH);
00093 _tcsncpy(tfile_new, OTTD2FS(file_new), MAX_PATH);
00094
00095 tfilename[MAX_PATH - 1] = '\0';
00096 tfile_new[MAX_PATH - 1] = '\0';
00097 tfilename[_tcslen(tfilename) + 1] = '\0';
00098 tfile_new[_tcslen(tfile_new) + 1] = '\0';
00099
00100
00101 SHFILEOPSTRUCT shfopt;
00102 MemSetT(&shfopt, 0);
00103 shfopt.wFunc = FO_MOVE;
00104 shfopt.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI | FOF_SILENT;
00105 shfopt.pFrom = tfile_new;
00106 shfopt.pTo = tfilename;
00107 SHFileOperation(&shfopt);
00108 #else
00109 if (rename(file_new, filename) < 0) {
00110 DEBUG(misc, 0, "Renaming %s to %s failed; configuration not saved", file_new, filename);
00111 }
00112 #endif
00113
00114 return true;
00115 }
00116
00117 FILE *IniFile::OpenFile(const char *filename, Subdirectory subdir, size_t *size)
00118 {
00119
00120
00121 return FioFOpenFile(filename, "rb", subdir, size);
00122 }
00123
00124 void IniFile::ReportFileError(const char * const pre, const char * const buffer, const char * const post)
00125 {
00126 ShowInfoF("%s%s%s", pre, buffer, post);
00127 }