2007-01-15 12:09:25 -08:00
|
|
|
/*
|
|
|
|
This file is part of Warzone 2100.
|
|
|
|
Copyright (C) 1999-2004 Eidos Interactive
|
2009-02-10 10:01:48 -08:00
|
|
|
Copyright (C) 2005-2009 Warzone Resurrection Project
|
2007-01-15 12:09:25 -08:00
|
|
|
|
|
|
|
Warzone 2100 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; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Warzone 2100 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. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Warzone 2100; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2007-06-28 10:47:08 -07:00
|
|
|
// Config.c saves your favourite options to the Registry.
|
|
|
|
#include "frame.h"
|
|
|
|
#include "configfile.h"
|
|
|
|
|
2006-11-06 06:40:07 -08:00
|
|
|
#include <string.h>
|
2009-02-10 09:23:09 -08:00
|
|
|
#include "string_ext.h"
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2008-03-16 05:39:08 -07:00
|
|
|
#include "file.h"
|
|
|
|
|
2007-06-28 10:47:08 -07:00
|
|
|
#define REGISTRY_HASH_SIZE 32
|
|
|
|
#define MAXLINESIZE 255
|
|
|
|
|
|
|
|
typedef struct regkey_t
|
|
|
|
{
|
|
|
|
char *key;
|
|
|
|
char *value;
|
|
|
|
struct regkey_t *next;
|
|
|
|
} regkey_t;
|
2007-06-14 12:21:52 -07:00
|
|
|
static regkey_t* registry[REGISTRY_HASH_SIZE] = { NULL };
|
2007-08-21 05:59:05 -07:00
|
|
|
static char RegFilePath[PATH_MAX];
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2009-04-19 05:56:54 -07:00
|
|
|
void registry_clear()
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
2009-04-19 05:56:54 -07:00
|
|
|
for (i = 0; i < ARRAY_SIZE(registry); ++i)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2009-04-19 05:56:54 -07:00
|
|
|
regkey_t* j = registry[i];
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2009-04-19 05:56:54 -07:00
|
|
|
while (j)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2009-04-19 05:56:54 -07:00
|
|
|
regkey_t* const toDelete = j;
|
|
|
|
j = j->next;
|
|
|
|
|
|
|
|
free(toDelete->key);
|
|
|
|
free(toDelete->value);
|
|
|
|
free(toDelete);
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2007-06-14 12:21:52 -07:00
|
|
|
registry[i] = NULL;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-19 05:56:54 -07:00
|
|
|
static unsigned int registry_hash(const char* s)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2009-04-19 05:56:54 -07:00
|
|
|
unsigned int h = 0;
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2009-04-19 05:56:54 -07:00
|
|
|
while (s && *s)
|
|
|
|
h += *s++;
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2009-04-19 05:56:54 -07:00
|
|
|
return h % ARRAY_SIZE(registry);
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2009-04-19 05:56:54 -07:00
|
|
|
static regkey_t* registry_find_key(const char* key)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2009-04-19 05:56:54 -07:00
|
|
|
regkey_t* i;
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2009-04-19 05:56:54 -07:00
|
|
|
for (i = registry[registry_hash(key)]; i != NULL; i = i->next)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2009-04-19 05:56:54 -07:00
|
|
|
if (!strcmp(key, i->key))
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-04-19 05:56:54 -07:00
|
|
|
static char* registry_get_by_key(const char* key)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2009-04-19 05:56:54 -07:00
|
|
|
regkey_t* regkey = registry_find_key(key);
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2009-04-19 05:56:54 -07:00
|
|
|
if (regkey == NULL)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2009-04-19 05:56:54 -07:00
|
|
|
#if 0
|
|
|
|
debug(LOG_NEVER, "Key \"%s\" not found", key);
|
|
|
|
#endif
|
2007-06-28 10:47:08 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-04-19 05:56:54 -07:00
|
|
|
#if 0
|
|
|
|
debug(LOG_NEVER, "Key \"%s\" has value \"%s\"", key, regkey->value);
|
|
|
|
#endif
|
|
|
|
return regkey->value;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-19 05:56:54 -07:00
|
|
|
static void registry_set_key(const char* key, const char* value)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2009-04-19 05:56:54 -07:00
|
|
|
regkey_t* regkey = registry_find_key(key);
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
debug(LOG_NEVER, "Set key \"%s\" to value \"%s\"", key, value);
|
|
|
|
#endif
|
|
|
|
if (regkey == NULL)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2009-04-19 05:56:54 -07:00
|
|
|
// Add a new bucked to the hashtable for the given key
|
|
|
|
const unsigned int h = registry_hash(key);
|
|
|
|
|
|
|
|
regkey = (regkey_t *)malloc(sizeof(*regkey));
|
|
|
|
regkey->key = strdup(key);
|
|
|
|
regkey->next = registry[h];
|
|
|
|
registry[h] = regkey;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-04-19 05:56:54 -07:00
|
|
|
free(regkey->value);
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2009-04-19 05:56:54 -07:00
|
|
|
regkey->value = strdup(value);
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2009-04-19 05:56:54 -07:00
|
|
|
static bool registry_load(const char* filename)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2006-07-07 07:08:48 -07:00
|
|
|
char buffer[MAXLINESIZE];
|
2006-08-12 09:52:37 -07:00
|
|
|
char *bptr = NULL, *bufstart = NULL;
|
2007-06-28 10:47:08 -07:00
|
|
|
char key[32];
|
2007-10-16 14:07:10 -07:00
|
|
|
int l; // sscanf expects an int to receive %n, not an unsigned int
|
2007-06-28 10:47:08 -07:00
|
|
|
UDWORD filesize;
|
|
|
|
|
2008-09-07 13:00:54 -07:00
|
|
|
debug(LOG_WZ, "Loading the registry from [directory: %s] %s", PHYSFS_getRealDir(filename), filename);
|
2007-06-28 10:47:08 -07:00
|
|
|
if (PHYSFS_exists(filename)) {
|
2006-07-07 07:08:48 -07:00
|
|
|
if (!loadFile(filename, &bptr, &filesize)) {
|
2008-03-24 09:51:17 -07:00
|
|
|
return false; // happens only in NDEBUG case
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Registry does not exist. Caller will write a new one.
|
2008-03-24 09:51:17 -07:00
|
|
|
return false;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2008-09-07 13:00:54 -07:00
|
|
|
debug(LOG_WZ, "Parsing the registry from [directory: %s] %s", PHYSFS_getRealDir(filename) , filename);
|
2007-06-28 10:47:08 -07:00
|
|
|
if (filesize == 0 || strlen(bptr) == 0) {
|
2007-03-18 14:15:32 -07:00
|
|
|
debug(LOG_WARNING, "Registry file %s is empty!", filename);
|
2008-03-24 09:51:17 -07:00
|
|
|
return false;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
bufstart = bptr;
|
|
|
|
bptr[filesize - 1] = '\0'; // make sure it is terminated
|
|
|
|
while (*bptr != '\0') {
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
/* Put a line into buffer */
|
|
|
|
while (*bptr != '\0' && *bptr != '\n' && count < MAXLINESIZE) {
|
|
|
|
buffer[count] = *bptr;
|
|
|
|
bptr++;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
if (*bptr != '\0') {
|
|
|
|
bptr++; // skip EOL
|
|
|
|
}
|
|
|
|
buffer[count] = '\0';
|
2007-10-16 13:51:34 -07:00
|
|
|
if (sscanf(buffer, " %[^=] = %n", key, &l) == 1) {
|
2007-06-28 10:47:08 -07:00
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = l;; ++i) {
|
|
|
|
if (buffer[i] == '\0') {
|
|
|
|
break;
|
|
|
|
} else if (buffer[i] < ' ') {
|
|
|
|
buffer[i] = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
registry_set_key(key, buffer + l);
|
|
|
|
}
|
|
|
|
}
|
2007-04-15 03:43:05 -07:00
|
|
|
free(bufstart);
|
2008-03-24 09:51:17 -07:00
|
|
|
return true;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2009-04-19 05:56:54 -07:00
|
|
|
static bool registry_save(const char* filename)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2009-04-19 05:56:54 -07:00
|
|
|
char buffer[MAXLINESIZE * ARRAY_SIZE(registry)];
|
2007-06-28 10:47:08 -07:00
|
|
|
unsigned int i;
|
|
|
|
int count = 0;
|
|
|
|
|
2008-09-07 13:00:54 -07:00
|
|
|
debug(LOG_WZ, "Saving the registry to [directory: %s] %s", PHYSFS_getRealDir(filename), filename);
|
2009-04-19 05:56:54 -07:00
|
|
|
for (i = 0; i < ARRAY_SIZE(registry); ++i)
|
|
|
|
{
|
2007-06-28 10:47:08 -07:00
|
|
|
regkey_t *j;
|
|
|
|
|
|
|
|
for (j = registry[i]; j != NULL; j = j->next) {
|
|
|
|
char linebuf[MAXLINESIZE];
|
|
|
|
|
|
|
|
snprintf(linebuf, sizeof(linebuf), "%s=%s\n", j->key, j->value);
|
|
|
|
assert(strlen(linebuf) > 0 && strlen(linebuf) < MAXLINESIZE);
|
|
|
|
memcpy(buffer + count, linebuf, strlen(linebuf));
|
|
|
|
count += strlen(linebuf);
|
2009-04-19 05:56:54 -07:00
|
|
|
assert(count < MAXLINESIZE * ARRAY_SIZE(registry));
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-19 05:56:54 -07:00
|
|
|
return saveFile(filename, buffer, count);
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2007-06-14 12:21:52 -07:00
|
|
|
void setRegistryFilePath(const char* fileName)
|
|
|
|
{
|
2008-05-25 06:46:49 -07:00
|
|
|
sstrcpy(RegFilePath, fileName);
|
2007-06-14 12:21:52 -07:00
|
|
|
}
|
|
|
|
|
2009-04-19 05:56:54 -07:00
|
|
|
bool openWarzoneKey()
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2009-04-19 05:56:54 -07:00
|
|
|
static bool done = false;
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2009-04-19 05:56:54 -07:00
|
|
|
if (done == false)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2009-04-19 05:56:54 -07:00
|
|
|
done = registry_load(RegFilePath);
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2008-03-24 09:51:17 -07:00
|
|
|
return true;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2009-04-19 05:56:54 -07:00
|
|
|
bool closeWarzoneKey()
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2009-04-19 05:56:54 -07:00
|
|
|
return registry_save(RegFilePath);
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2006-09-03 05:41:55 -07:00
|
|
|
/**
|
2009-04-19 05:56:54 -07:00
|
|
|
* Read config setting \c key into val
|
|
|
|
* \param *key Config setting
|
2006-09-03 05:41:55 -07:00
|
|
|
* \param *val Place where to store the setting
|
|
|
|
* \return Whether we succeed to find the setting
|
|
|
|
*/
|
2009-04-19 05:56:54 -07:00
|
|
|
bool getWarzoneKeyNumeric(const char* key, int* val)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2009-04-19 05:56:54 -07:00
|
|
|
const char* const value = registry_get_by_key(key);
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2009-04-19 05:56:54 -07:00
|
|
|
return (value != NULL
|
|
|
|
&& sscanf(value, "%i", val) == 1);
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2009-04-19 05:56:54 -07:00
|
|
|
bool getWarzoneKeyString(const char* key, char* val)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2009-04-19 05:56:54 -07:00
|
|
|
const char* const value = registry_get_by_key(key);
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2009-04-19 05:56:54 -07:00
|
|
|
if (value == NULL)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2008-03-24 09:51:17 -07:00
|
|
|
return false;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2009-04-19 05:56:54 -07:00
|
|
|
strcpy(val, value);
|
2008-03-24 09:51:17 -07:00
|
|
|
return true;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2009-04-19 05:56:54 -07:00
|
|
|
bool setWarzoneKeyNumeric(const char* key, int val)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2009-04-19 05:56:54 -07:00
|
|
|
char* numBuf;
|
|
|
|
sasprintf(&numBuf, "%i", val);
|
2007-10-24 14:11:29 -07:00
|
|
|
|
2009-04-19 05:56:54 -07:00
|
|
|
registry_set_key(key, numBuf);
|
2008-03-24 09:51:17 -07:00
|
|
|
return true;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2009-04-19 05:56:54 -07:00
|
|
|
bool setWarzoneKeyString(const char* key, const char* val)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2009-04-19 05:56:54 -07:00
|
|
|
registry_set_key(key, val);
|
2008-03-24 09:51:17 -07:00
|
|
|
return true;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|