2007-01-15 12:09:25 -08:00
|
|
|
/*
|
|
|
|
This file is part of Warzone 2100.
|
|
|
|
Copyright (C) 1999-2004 Eidos Interactive
|
|
|
|
Copyright (C) 2005-2007 Warzone Resurrection Project
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
#include <physfs.h>
|
2006-11-06 06:40:07 -08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.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
|
|
|
|
|
|
|
//
|
|
|
|
// =======================================================================================================================
|
|
|
|
// =======================================================================================================================
|
|
|
|
//
|
|
|
|
void registry_clear( void )
|
|
|
|
{
|
|
|
|
//~~~~~~~~~~~~~~
|
|
|
|
unsigned int i;
|
|
|
|
//~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
for ( i = 0; i < REGISTRY_HASH_SIZE; ++i )
|
|
|
|
{
|
|
|
|
//~~~~~~~~~~~~~
|
|
|
|
regkey_t *j;
|
|
|
|
regkey_t *tmp;
|
|
|
|
//~~~~~~~~~~~~~
|
|
|
|
|
2007-06-14 12:21:52 -07:00
|
|
|
for (j = registry[i]; j != NULL; j = tmp)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
|
|
|
tmp = j->next;
|
2007-06-14 12:21:52 -07:00
|
|
|
free(j->key);
|
|
|
|
free(j->value);
|
|
|
|
free(j);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// =======================================================================================================================
|
|
|
|
// =======================================================================================================================
|
|
|
|
//
|
2006-09-19 11:45:48 -07:00
|
|
|
static unsigned int registry_hash( const char *s )
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
|
|
|
//~~~~~~~~~~~~~~~~~~
|
|
|
|
unsigned int i;
|
|
|
|
unsigned int h = 0;
|
|
|
|
//~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
if ( s != NULL )
|
|
|
|
{
|
|
|
|
for ( i = 0; s[i] != '\0'; ++i )
|
|
|
|
{
|
|
|
|
h += s[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return h % REGISTRY_HASH_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// =======================================================================================================================
|
|
|
|
// =======================================================================================================================
|
|
|
|
//
|
2006-09-19 11:45:48 -07:00
|
|
|
static regkey_t *registry_find_key( const char *k )
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
|
|
|
//~~~~~~~~~~~
|
|
|
|
regkey_t *i;
|
|
|
|
//~~~~~~~~~~~
|
|
|
|
|
|
|
|
for ( i = registry[registry_hash(k)]; i != NULL; i = i->next )
|
|
|
|
{
|
|
|
|
if ( !strcmp(k, i->key) )
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// =======================================================================================================================
|
|
|
|
// =======================================================================================================================
|
|
|
|
//
|
2006-09-19 11:45:48 -07:00
|
|
|
static char *registry_get_key( const char *k )
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
|
|
|
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
regkey_t *key = registry_find_key( k );
|
|
|
|
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
if ( key == NULL )
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// printf("registry_get_key(%s) -> key not found\n", k);
|
|
|
|
//
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// printf("registry_get_key(%s) -> %s\n", k, key->value);
|
|
|
|
//
|
|
|
|
return key->value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// =======================================================================================================================
|
|
|
|
// =======================================================================================================================
|
|
|
|
//
|
2006-09-19 11:45:48 -07:00
|
|
|
static void registry_set_key( const char *k, const char *v )
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
|
|
|
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
regkey_t *key = registry_find_key( k );
|
|
|
|
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
//
|
|
|
|
// printf("registry_set_key(%s, %s)\n", k, v);
|
|
|
|
//
|
|
|
|
if ( key == NULL )
|
|
|
|
{
|
|
|
|
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
unsigned int h = registry_hash( k );
|
|
|
|
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
key = (regkey_t *) malloc( sizeof(regkey_t) );
|
|
|
|
key->key = strdup( k );
|
|
|
|
key->next = registry[h];
|
|
|
|
registry[h] = key;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
free( key->value );
|
|
|
|
}
|
|
|
|
|
|
|
|
key->value = strdup( v );
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// =======================================================================================================================
|
|
|
|
// =======================================================================================================================
|
|
|
|
//
|
2006-09-19 11:45:48 -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;
|
|
|
|
|
|
|
|
debug(LOG_WZ, "Loading the registry from %s", filename);
|
|
|
|
if (PHYSFS_exists(filename)) {
|
2006-07-07 07:08:48 -07:00
|
|
|
if (!loadFile(filename, &bptr, &filesize)) {
|
2007-06-28 10:47:08 -07:00
|
|
|
return FALSE; // happens only in NDEBUG case
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Registry does not exist. Caller will write a new one.
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug(LOG_WZ, "Parsing the registry from %s", filename);
|
|
|
|
if (filesize == 0 || strlen(bptr) == 0) {
|
2007-03-18 14:15:32 -07:00
|
|
|
debug(LOG_WARNING, "Registry file %s is empty!", filename);
|
2007-06-28 10:47:08 -07:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
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);
|
2007-06-28 10:47:08 -07:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// =======================================================================================================================
|
|
|
|
// =======================================================================================================================
|
|
|
|
//
|
2006-09-19 11:45:48 -07:00
|
|
|
static BOOL registry_save( const char *filename )
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
|
|
|
char buffer[MAXLINESIZE * REGISTRY_HASH_SIZE];
|
|
|
|
unsigned int i;
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
debug(LOG_WZ, "Saving the registry to %s", filename);
|
|
|
|
for (i = 0; i < REGISTRY_HASH_SIZE; ++i) {
|
|
|
|
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);
|
|
|
|
assert(count < MAXLINESIZE * REGISTRY_HASH_SIZE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!saveFile(filename, buffer, count)) {
|
|
|
|
return FALSE; // only in NDEBUG case
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2007-06-14 12:21:52 -07:00
|
|
|
void setRegistryFilePath(const char* fileName)
|
|
|
|
{
|
2007-10-27 09:18:52 -07:00
|
|
|
strlcpy(RegFilePath, fileName, sizeof(RegFilePath));
|
2007-06-14 12:21:52 -07:00
|
|
|
}
|
|
|
|
|
2007-06-28 10:47:08 -07:00
|
|
|
///
|
|
|
|
// =======================================================================================================================
|
|
|
|
// =======================================================================================================================
|
|
|
|
//
|
|
|
|
BOOL openWarzoneKey( void )
|
|
|
|
{
|
|
|
|
//~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
static BOOL done = FALSE;
|
|
|
|
//~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
if ( done == FALSE )
|
|
|
|
{
|
|
|
|
registry_load( RegFilePath );
|
|
|
|
done = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
// =======================================================================================================================
|
|
|
|
// =======================================================================================================================
|
|
|
|
//
|
|
|
|
BOOL closeWarzoneKey( void )
|
|
|
|
{
|
|
|
|
registry_save( RegFilePath );
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2006-09-03 05:41:55 -07:00
|
|
|
/**
|
|
|
|
* Read config setting pName into val
|
|
|
|
* \param *pName Config setting
|
|
|
|
* \param *val Place where to store the setting
|
|
|
|
* \return Whether we succeed to find the setting
|
|
|
|
*/
|
2006-11-04 13:05:31 -08:00
|
|
|
BOOL getWarzoneKeyNumeric( const char *pName, SDWORD *val )
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
|
|
|
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
char *value = registry_get_key( pName );
|
|
|
|
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
if ( value == NULL || sscanf(value, "%i", val) != 1 )
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
// =======================================================================================================================
|
|
|
|
// =======================================================================================================================
|
|
|
|
//
|
2006-11-03 13:35:50 -08:00
|
|
|
BOOL getWarzoneKeyString( const char *pName, char *pString )
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
|
|
|
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
char *value = registry_get_key( pName );
|
|
|
|
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
if ( value == NULL )
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strcpy( pString, value );
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
// =======================================================================================================================
|
|
|
|
// =======================================================================================================================
|
|
|
|
//
|
2006-11-04 13:05:31 -08:00
|
|
|
BOOL setWarzoneKeyNumeric( const char *pName, SDWORD val )
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
|
|
|
//~~~~~~~~~~~~
|
|
|
|
char buf[32];
|
|
|
|
//~~~~~~~~~~~~
|
|
|
|
|
2007-10-24 14:11:29 -07:00
|
|
|
snprintf(buf, sizeof(buf), "%i", val);
|
|
|
|
// Guarantee to nul-terminate
|
|
|
|
buf[sizeof(buf) - 1] = '\0';
|
|
|
|
|
2007-06-28 10:47:08 -07:00
|
|
|
registry_set_key( pName, buf );
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
// =======================================================================================================================
|
|
|
|
// =======================================================================================================================
|
|
|
|
//
|
2006-11-03 13:35:50 -08:00
|
|
|
BOOL setWarzoneKeyString( const char *pName, const char *pString )
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
|
|
|
registry_set_key( pName, pString );
|
|
|
|
return TRUE;
|
|
|
|
}
|