2008-08-24 17:29:57 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
/** @file ini.cpp Definition of the IniItem class, related to reading/writing '*.ini' files. */
|
2008-08-24 13:50:31 +00:00
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "core/alloc_func.hpp"
|
|
|
|
#include "core/math_func.hpp"
|
2009-03-14 00:26:03 +00:00
|
|
|
#include "core/mem_func.hpp"
|
2008-08-24 13:50:31 +00:00
|
|
|
#include "debug.h"
|
|
|
|
#include "ini_type.h"
|
|
|
|
#include "string_func.h"
|
2008-08-31 10:50:05 +00:00
|
|
|
#include "fileio_func.h"
|
2008-08-24 13:50:31 +00:00
|
|
|
|
2009-03-12 14:22:17 +00:00
|
|
|
#if (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 199309L) || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 500)
|
|
|
|
# define WITH_FDATASYNC
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2009-03-14 00:26:03 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
# include <shellapi.h>
|
|
|
|
#endif
|
2009-03-12 14:22:17 +00:00
|
|
|
|
2008-08-24 13:50:31 +00:00
|
|
|
IniItem::IniItem(IniGroup *parent, const char *name, size_t len) : next(NULL), value(NULL), comment(NULL)
|
|
|
|
{
|
|
|
|
if (len == 0) len = strlen(name);
|
|
|
|
|
|
|
|
this->name = strndup(name, len);
|
|
|
|
*parent->last_item = this;
|
|
|
|
parent->last_item = &this->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
IniItem::~IniItem()
|
|
|
|
{
|
|
|
|
free(this->name);
|
|
|
|
free(this->value);
|
|
|
|
free(this->comment);
|
|
|
|
|
|
|
|
delete this->next;
|
|
|
|
}
|
|
|
|
|
2008-08-25 15:15:41 +00:00
|
|
|
void IniItem::SetValue(const char *value)
|
|
|
|
{
|
|
|
|
free(this->value);
|
|
|
|
this->value = strdup(value);
|
|
|
|
}
|
|
|
|
|
2008-08-24 13:50:31 +00:00
|
|
|
IniGroup::IniGroup(IniFile *parent, const char *name, size_t len) : next(NULL), type(IGT_VARIABLES), item(NULL), comment(NULL)
|
|
|
|
{
|
|
|
|
if (len == 0) len = strlen(name);
|
|
|
|
|
|
|
|
this->name = strndup(name, len);
|
|
|
|
this->last_item = &this->item;
|
|
|
|
*parent->last_group = this;
|
|
|
|
parent->last_group = &this->next;
|
|
|
|
|
|
|
|
if (parent->list_group_names == NULL) return;
|
|
|
|
|
|
|
|
for (uint i = 0; parent->list_group_names[i] != NULL; i++) {
|
|
|
|
if (strcmp(this->name, parent->list_group_names[i]) == 0) {
|
|
|
|
this->type = IGT_LIST;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IniGroup::~IniGroup()
|
|
|
|
{
|
|
|
|
free(this->name);
|
|
|
|
free(this->comment);
|
|
|
|
|
|
|
|
delete this->item;
|
|
|
|
delete this->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
IniItem *IniGroup::GetItem(const char *name, bool create)
|
|
|
|
{
|
2009-03-12 11:28:03 +00:00
|
|
|
for (IniItem *item = this->item; item != NULL; item = item->next) {
|
2008-08-24 13:50:31 +00:00
|
|
|
if (strcmp(item->name, name) == 0) return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!create) return NULL;
|
|
|
|
|
|
|
|
/* otherwise make a new one */
|
2009-03-12 11:28:03 +00:00
|
|
|
return new IniItem(this, name, strlen(name));
|
2008-08-24 13:50:31 +00:00
|
|
|
}
|
|
|
|
|
2009-01-14 20:23:45 +00:00
|
|
|
void IniGroup::Clear()
|
|
|
|
{
|
|
|
|
delete this->item;
|
|
|
|
this->item = NULL;
|
|
|
|
this->last_item = &this->item;
|
|
|
|
}
|
|
|
|
|
2008-08-24 13:50:31 +00:00
|
|
|
IniFile::IniFile(const char **list_group_names) : group(NULL), comment(NULL), list_group_names(list_group_names)
|
|
|
|
{
|
|
|
|
this->last_group = &this->group;
|
|
|
|
}
|
|
|
|
|
|
|
|
IniFile::~IniFile()
|
|
|
|
{
|
|
|
|
free(this->comment);
|
|
|
|
delete this->group;
|
|
|
|
}
|
|
|
|
|
|
|
|
IniGroup *IniFile::GetGroup(const char *name, size_t len)
|
|
|
|
{
|
|
|
|
if (len == 0) len = strlen(name);
|
|
|
|
|
|
|
|
/* does it exist already? */
|
2009-03-12 11:28:03 +00:00
|
|
|
for (IniGroup *group = this->group; group != NULL; group = group->next) {
|
2008-08-24 13:50:31 +00:00
|
|
|
if (!memcmp(group->name, name, len) && group->name[len] == 0) {
|
|
|
|
return group;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* otherwise make a new one */
|
2009-03-12 11:28:03 +00:00
|
|
|
IniGroup *group = new IniGroup(this, name, len);
|
2008-08-24 13:50:31 +00:00
|
|
|
group->comment = strdup("\n");
|
|
|
|
return group;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IniFile::RemoveGroup(const char *name)
|
|
|
|
{
|
|
|
|
size_t len = strlen(name);
|
|
|
|
IniGroup *prev = NULL;
|
|
|
|
IniGroup *group;
|
|
|
|
|
|
|
|
/* does it exist already? */
|
|
|
|
for (group = this->group; group != NULL; prev = group, group = group->next) {
|
|
|
|
if (memcmp(group->name, name, len) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (group == NULL) return;
|
|
|
|
|
|
|
|
if (prev != NULL) {
|
|
|
|
prev->next = prev->next->next;
|
2008-09-07 08:51:26 +00:00
|
|
|
if (this->last_group == &group->next) this->last_group = &prev->next;
|
2008-08-24 13:50:31 +00:00
|
|
|
} else {
|
|
|
|
this->group = this->group->next;
|
2008-09-07 08:51:26 +00:00
|
|
|
if (this->last_group == &group->next) this->last_group = &this->group;
|
2008-08-24 13:50:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
group->next = NULL;
|
|
|
|
delete group;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IniFile::LoadFromDisk(const char *filename)
|
|
|
|
{
|
|
|
|
assert(this->last_group == &this->group);
|
|
|
|
|
2009-03-12 11:28:03 +00:00
|
|
|
char buffer[1024];
|
2008-08-24 13:50:31 +00:00
|
|
|
IniGroup *group = NULL;
|
|
|
|
|
|
|
|
char *comment = NULL;
|
|
|
|
uint comment_size = 0;
|
|
|
|
uint comment_alloc = 0;
|
|
|
|
|
2008-08-24 23:12:10 +00:00
|
|
|
size_t end;
|
2008-09-02 20:24:55 +00:00
|
|
|
/*
|
|
|
|
* Now we are going to open a file that contains no more than simple
|
|
|
|
* plain text. That would raise the question: "why open the file as
|
|
|
|
* if it is a binary file?". That's simple... Microsoft, in all
|
|
|
|
* their greatness and wisdom decided it would be useful if ftell
|
|
|
|
* is aware of '\r\n' and "sees" that as a single character. The
|
|
|
|
* easiest way to test for that situation is by searching for '\n'
|
|
|
|
* and decrease the value every time you encounter a '\n'. This will
|
|
|
|
* thus also make ftell "see" the '\r' when it is not there, so the
|
|
|
|
* result of ftell will be highly unreliable. So to work around this
|
|
|
|
* marvel of wisdom we have to open in as a binary file.
|
|
|
|
*/
|
|
|
|
FILE *in = FioFOpenFile(filename, "rb", DATA_DIR, &end);
|
2008-08-24 13:50:31 +00:00
|
|
|
if (in == NULL) return;
|
|
|
|
|
2008-08-24 23:12:10 +00:00
|
|
|
end += ftell(in);
|
2008-08-24 13:50:31 +00:00
|
|
|
|
2008-08-24 23:12:10 +00:00
|
|
|
/* for each line in the file */
|
|
|
|
while ((size_t)ftell(in) < end && fgets(buffer, sizeof(buffer), in)) {
|
2009-03-12 11:28:03 +00:00
|
|
|
char c, *s;
|
2008-08-24 13:50:31 +00:00
|
|
|
/* trim whitespace from the left side */
|
|
|
|
for (s = buffer; *s == ' ' || *s == '\t'; s++) {}
|
|
|
|
|
|
|
|
/* trim whitespace from right side. */
|
2009-03-12 11:28:03 +00:00
|
|
|
char *e = s + strlen(s);
|
2008-08-24 13:50:31 +00:00
|
|
|
while (e > s && ((c = e[-1]) == '\n' || c == '\r' || c == ' ' || c == '\t')) e--;
|
|
|
|
*e = '\0';
|
|
|
|
|
|
|
|
/* skip comments and empty lines */
|
|
|
|
if (*s == '#' || *s == ';' || *s == '\0') {
|
|
|
|
uint ns = comment_size + (e - s + 1);
|
|
|
|
uint a = comment_alloc;
|
|
|
|
/* add to comment */
|
|
|
|
if (ns > a) {
|
|
|
|
a = max(a, 128U);
|
|
|
|
do a *= 2; while (a < ns);
|
|
|
|
comment = ReallocT(comment, comment_alloc = a);
|
|
|
|
}
|
2009-03-12 11:28:03 +00:00
|
|
|
uint pos = comment_size;
|
2008-08-24 13:50:31 +00:00
|
|
|
comment_size += (e - s + 1);
|
|
|
|
comment[pos + e - s] = '\n'; // comment newline
|
|
|
|
memcpy(comment + pos, s, e - s); // copy comment contents
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* it's a group? */
|
|
|
|
if (s[0] == '[') {
|
|
|
|
if (e[-1] != ']') {
|
|
|
|
ShowInfoF("ini: invalid group name '%s'", buffer);
|
|
|
|
} else {
|
|
|
|
e--;
|
|
|
|
}
|
|
|
|
s++; // skip [
|
|
|
|
group = new IniGroup(this, s, e - s);
|
|
|
|
if (comment_size) {
|
|
|
|
group->comment = strndup(comment, comment_size);
|
|
|
|
comment_size = 0;
|
|
|
|
}
|
|
|
|
} else if (group) {
|
2009-03-12 11:28:03 +00:00
|
|
|
char *t;
|
2008-08-24 13:50:31 +00:00
|
|
|
/* find end of keyname */
|
|
|
|
if (*s == '\"') {
|
|
|
|
s++;
|
|
|
|
for (t = s; *t != '\0' && *t != '\"'; t++) {}
|
|
|
|
if (*t == '\"') *t = ' ';
|
|
|
|
} else {
|
|
|
|
for (t = s; *t != '\0' && *t != '=' && *t != '\t' && *t != ' '; t++) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* it's an item in an existing group */
|
2009-03-12 11:28:03 +00:00
|
|
|
IniItem *item = new IniItem(group, s, t - s);
|
2008-08-24 13:50:31 +00:00
|
|
|
if (comment_size) {
|
|
|
|
item->comment = strndup(comment, comment_size);
|
|
|
|
comment_size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find start of parameter */
|
|
|
|
while (*t == '=' || *t == ' ' || *t == '\t') t++;
|
|
|
|
|
2009-04-23 19:13:41 +00:00
|
|
|
bool quoted = (*t == '\"');
|
2008-08-24 13:50:31 +00:00
|
|
|
/* remove starting quotation marks */
|
|
|
|
if (*t == '\"') t++;
|
|
|
|
/* remove ending quotation marks */
|
|
|
|
e = t + strlen(t);
|
|
|
|
if (e > t && e[-1] == '\"') e--;
|
|
|
|
*e = '\0';
|
|
|
|
|
2009-04-23 19:13:41 +00:00
|
|
|
/* If the value was not quoted and empty, it must be NULL */
|
|
|
|
item->value = (!quoted && e == t) ? NULL : strndup(t, e - t);
|
2008-08-24 13:50:31 +00:00
|
|
|
} else {
|
|
|
|
/* it's an orphan item */
|
|
|
|
ShowInfoF("ini: '%s' outside of group", buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (comment_size > 0) {
|
|
|
|
this->comment = strndup(comment, comment_size);
|
|
|
|
comment_size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(comment);
|
|
|
|
fclose(in);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IniFile::SaveToDisk(const char *filename)
|
|
|
|
{
|
2009-03-12 14:22:17 +00:00
|
|
|
/*
|
|
|
|
* First write the configuration to a (temporary) file and then rename
|
|
|
|
* that file. This to prevent that when OpenTTD crashes during the save
|
|
|
|
* you end up with a truncated configuration file.
|
|
|
|
*/
|
|
|
|
char file_new[MAX_PATH];
|
|
|
|
|
|
|
|
strecpy(file_new, filename, lastof(file_new));
|
|
|
|
strecat(file_new, ".new", lastof(file_new));
|
|
|
|
FILE *f = fopen(file_new, "w");
|
2008-08-24 13:50:31 +00:00
|
|
|
if (f == NULL) return false;
|
|
|
|
|
2009-03-12 11:28:03 +00:00
|
|
|
for (const IniGroup *group = this->group; group != NULL; group = group->next) {
|
2008-08-24 13:50:31 +00:00
|
|
|
if (group->comment) fputs(group->comment, f);
|
|
|
|
fprintf(f, "[%s]\n", group->name);
|
2009-03-12 11:28:03 +00:00
|
|
|
for (const IniItem *item = group->item; item != NULL; item = item->next) {
|
2008-08-24 13:50:31 +00:00
|
|
|
if (item->comment != NULL) fputs(item->comment, f);
|
|
|
|
|
|
|
|
/* protect item->name with quotes if needed */
|
2009-04-10 17:16:46 +00:00
|
|
|
if (strchr(item->name, ' ') != NULL ||
|
|
|
|
item->name[0] == '[') {
|
2008-08-24 13:50:31 +00:00
|
|
|
fprintf(f, "\"%s\"", item->name);
|
|
|
|
} else {
|
|
|
|
fprintf(f, "%s", item->name);
|
|
|
|
}
|
|
|
|
|
2009-04-23 19:13:41 +00:00
|
|
|
fprintf(f, " = %s\n", item->value == NULL ? "" : item->value);
|
2008-08-24 13:50:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this->comment) fputs(this->comment, f);
|
|
|
|
|
2009-03-12 14:22:17 +00:00
|
|
|
/*
|
|
|
|
* POSIX (and friends) do not guarantee that when a file is closed it is
|
|
|
|
* flushed to the disk. So we manually flush it do disk if we have the
|
|
|
|
* APIs to do so. We only need to flush the data as the metadata itself
|
|
|
|
* (modification date etc.) is not important to us; only the real data is.
|
|
|
|
*/
|
|
|
|
#ifdef WITH_FDATASYNC
|
|
|
|
int ret = fdatasync(fileno(f));
|
|
|
|
fclose(f);
|
|
|
|
if (ret != 0) return false;
|
|
|
|
#else
|
2008-08-24 13:50:31 +00:00
|
|
|
fclose(f);
|
2009-03-12 14:22:17 +00:00
|
|
|
#endif
|
|
|
|
|
2009-03-14 00:26:03 +00:00
|
|
|
#if defined(WIN32) || defined(WIN64)
|
|
|
|
/* Allocate space for one more \0 character. */
|
|
|
|
TCHAR tfilename[MAX_PATH + 1], tfile_new[MAX_PATH + 1];
|
|
|
|
_tcsncpy(tfilename, OTTD2FS(filename), MAX_PATH);
|
|
|
|
_tcsncpy(tfile_new, OTTD2FS(file_new), MAX_PATH);
|
|
|
|
/* SHFileOperation wants a double '\0' terminated string. */
|
|
|
|
tfilename[MAX_PATH - 1] = '\0';
|
|
|
|
tfile_new[MAX_PATH - 1] = '\0';
|
|
|
|
tfilename[_tcslen(tfilename) + 1] = '\0';
|
|
|
|
tfile_new[_tcslen(tfile_new) + 1] = '\0';
|
|
|
|
|
|
|
|
/* Rename file without any user confirmation. */
|
|
|
|
SHFILEOPSTRUCT shfopt;
|
|
|
|
MemSetT(&shfopt, 0);
|
|
|
|
shfopt.wFunc = FO_MOVE;
|
|
|
|
shfopt.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI | FOF_SILENT;
|
|
|
|
shfopt.pFrom = tfile_new;
|
|
|
|
shfopt.pTo = tfilename;
|
|
|
|
SHFileOperation(&shfopt);
|
|
|
|
#else
|
2009-03-12 14:22:17 +00:00
|
|
|
rename(file_new, filename);
|
2009-03-14 00:26:03 +00:00
|
|
|
#endif
|
|
|
|
|
2008-08-24 13:50:31 +00:00
|
|
|
return true;
|
|
|
|
}
|