2002-08-08 09:20:00 +00:00
|
|
|
/*
|
|
|
|
#FILENAME#
|
|
|
|
|
|
|
|
#DESCRIPTION#
|
|
|
|
|
|
|
|
Copyright (C) 2002 #AUTHOR#
|
|
|
|
|
|
|
|
Author: #AUTHOR#
|
|
|
|
Date: #DATE#
|
|
|
|
|
|
|
|
This program 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.
|
|
|
|
|
|
|
|
This program 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 this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2003-01-14 20:18:29 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2002-08-08 09:20:00 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "QF/dstring.h"
|
2003-02-16 02:44:24 +00:00
|
|
|
#include "QF/va.h"
|
2002-08-08 09:20:00 +00:00
|
|
|
#include "QF/hash.h"
|
2003-02-16 02:44:24 +00:00
|
|
|
#include "QF/cvar.h"
|
2003-04-13 22:07:58 +00:00
|
|
|
|
|
|
|
#include "gib_parse.h"
|
|
|
|
#include "gib_vars.h"
|
2003-02-14 22:42:11 +00:00
|
|
|
|
|
|
|
hashtab_t *gib_globals = 0;
|
|
|
|
hashtab_t *gib_domains = 0;
|
2002-08-08 09:20:00 +00:00
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static gib_var_t *
|
2003-01-28 21:16:21 +00:00
|
|
|
GIB_Var_New (const char *key)
|
2002-08-08 09:20:00 +00:00
|
|
|
{
|
2003-02-14 22:42:11 +00:00
|
|
|
gib_var_t *new = calloc (1, sizeof (gib_var_t));
|
|
|
|
|
2018-09-07 11:00:57 +00:00
|
|
|
new->array = calloc (1, sizeof (struct gib_varray_s));
|
2003-01-28 21:16:21 +00:00
|
|
|
new->key = strdup (key);
|
2002-08-08 09:20:00 +00:00
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
static const char *
|
2012-07-18 13:34:37 +00:00
|
|
|
GIB_Var_Get_Key (const void *ele, void *ptr)
|
2002-08-08 09:20:00 +00:00
|
|
|
{
|
2003-02-14 22:42:11 +00:00
|
|
|
return ((gib_var_t *) ele)->key;
|
2002-08-08 09:20:00 +00:00
|
|
|
}
|
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
static void
|
2002-08-08 09:20:00 +00:00
|
|
|
GIB_Var_Free (void *ele, void *ptr)
|
|
|
|
{
|
2003-01-28 21:16:21 +00:00
|
|
|
unsigned int i;
|
2003-02-14 22:42:11 +00:00
|
|
|
gib_var_t *l = (gib_var_t *) ele;
|
|
|
|
|
2003-02-16 02:44:24 +00:00
|
|
|
for (i = 0; i < l->size; i++) {
|
|
|
|
if (l->array[i].value)
|
|
|
|
dstring_delete (l->array[i].value);
|
|
|
|
if (l->array[i].leaves)
|
|
|
|
Hash_DelTable (l->array[i].leaves);
|
|
|
|
}
|
|
|
|
free (l->array);
|
2003-02-14 22:42:11 +00:00
|
|
|
free ((void *) l->key);
|
|
|
|
free (l);
|
2002-08-08 09:20:00 +00:00
|
|
|
}
|
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
gib_var_t *
|
2003-02-14 22:42:11 +00:00
|
|
|
GIB_Var_Get (hashtab_t * first, hashtab_t * second, const char *key)
|
2002-08-08 09:20:00 +00:00
|
|
|
{
|
2003-02-14 22:42:11 +00:00
|
|
|
gib_var_t *var;
|
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
if (first && (var = Hash_Find (first, key)))
|
|
|
|
return var;
|
|
|
|
else if (second && (var = Hash_Find (second, key)))
|
|
|
|
return var;
|
|
|
|
else
|
2002-08-22 16:20:06 +00:00
|
|
|
return 0;
|
2002-08-08 09:20:00 +00:00
|
|
|
}
|
|
|
|
|
2003-02-16 19:46:34 +00:00
|
|
|
/* Alters key, but restores it */
|
2003-01-28 21:16:21 +00:00
|
|
|
gib_var_t *
|
2003-02-14 22:42:11 +00:00
|
|
|
GIB_Var_Get_Complex (hashtab_t ** first, hashtab_t ** second, char *key,
|
|
|
|
unsigned int *ind, qboolean create)
|
2002-08-08 09:20:00 +00:00
|
|
|
{
|
2003-02-16 02:44:24 +00:00
|
|
|
static hashtab_t *zero = 0;
|
2003-02-16 19:46:34 +00:00
|
|
|
unsigned int i, n, index = 0, len, start;
|
|
|
|
gib_var_t *var = 0;
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2003-02-16 02:44:24 +00:00
|
|
|
len = strlen(key);
|
|
|
|
for (start = i = 0; i <= len; i++) {
|
|
|
|
if (key[i] == '.' || key[i] == 0) {
|
|
|
|
index = 0;
|
|
|
|
key[i] = 0;
|
2003-02-16 19:46:34 +00:00
|
|
|
n = 0;
|
2003-02-16 02:44:24 +00:00
|
|
|
if (i && key[i - 1] == ']')
|
|
|
|
for (n = i - 1; n; n--)
|
|
|
|
if (key[n] == '[') {
|
|
|
|
index = atoi (key + n + 1);
|
|
|
|
key[n] = 0;
|
|
|
|
break;
|
|
|
|
}
|
2003-03-01 07:24:58 +00:00
|
|
|
if (!(var = GIB_Var_Get (*first, *second, key+start)) && create) {
|
|
|
|
var = GIB_Var_New (key+start);
|
|
|
|
if (!*first)
|
2020-03-25 06:43:16 +00:00
|
|
|
*first = Hash_NewTable (256, GIB_Var_Get_Key,
|
|
|
|
GIB_Var_Free, 0, 0);
|
2003-03-01 07:24:58 +00:00
|
|
|
Hash_Add (*first, var);
|
2003-02-16 02:44:24 +00:00
|
|
|
}
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2003-03-01 07:24:58 +00:00
|
|
|
// We are done looking up/creating var, fix up key
|
|
|
|
if (n)
|
|
|
|
key[n] = '[';
|
|
|
|
if (i < len)
|
|
|
|
key[i] = '.';
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2003-03-01 07:24:58 +00:00
|
|
|
// Give up
|
|
|
|
if (!var)
|
|
|
|
return 0;
|
|
|
|
else if (index >= var->size) {
|
2003-02-16 02:44:24 +00:00
|
|
|
if (create) {
|
2006-07-23 01:34:14 +00:00
|
|
|
var->array = realloc (var->array,
|
|
|
|
(index + 1) * sizeof (struct gib_varray_s));
|
2003-02-16 02:44:24 +00:00
|
|
|
memset (var->array + var->size, 0,
|
2003-02-16 19:46:34 +00:00
|
|
|
(index + 1 - var->size) * sizeof (struct gib_varray_s));
|
2003-02-16 02:44:24 +00:00
|
|
|
var->size = index + 1;
|
|
|
|
} else
|
|
|
|
return 0;
|
2003-01-28 21:16:21 +00:00
|
|
|
}
|
2003-02-16 02:44:24 +00:00
|
|
|
second = &zero;
|
|
|
|
first = &var->array[index].leaves;
|
|
|
|
start = i+1;
|
|
|
|
}
|
2002-08-08 09:20:00 +00:00
|
|
|
}
|
2003-02-16 02:44:24 +00:00
|
|
|
if (!var->array[index].value)
|
|
|
|
var->array[index].value = dstring_newstr ();
|
|
|
|
*ind = index;
|
|
|
|
return var;
|
|
|
|
}
|
|
|
|
|
2003-02-16 19:46:34 +00:00
|
|
|
/* Mangles the hell out of key */
|
2003-02-16 02:44:24 +00:00
|
|
|
gib_var_t *
|
|
|
|
GIB_Var_Get_Very_Complex (hashtab_t ** first, hashtab_t ** second, dstring_t *key, unsigned int start,
|
|
|
|
unsigned int *ind, qboolean create)
|
|
|
|
{
|
|
|
|
static hashtab_t *zero = 0;
|
|
|
|
hashtab_t *one = *first, *two = *second;
|
2003-03-02 05:11:22 +00:00
|
|
|
unsigned int i, index = 0, index2 = 0, n, protect, varstartskip;
|
2003-02-16 19:46:34 +00:00
|
|
|
gib_var_t *var = 0;
|
2003-02-16 02:44:24 +00:00
|
|
|
cvar_t *cvar;
|
2021-03-29 08:24:30 +00:00
|
|
|
char c;
|
|
|
|
const char *str;
|
2003-02-16 02:44:24 +00:00
|
|
|
qboolean done = false;
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2003-02-16 19:46:34 +00:00
|
|
|
for (i = start, protect = 0; !done; i++) {
|
2003-02-16 02:44:24 +00:00
|
|
|
if (key->str[i] == '.' || key->str[i] == 0) {
|
|
|
|
index = 0;
|
|
|
|
if (!key->str[i])
|
|
|
|
done = true;
|
|
|
|
key->str[i] = 0;
|
|
|
|
if (i && key->str[i - 1] == ']')
|
|
|
|
for (n = i-1; n; n--)
|
|
|
|
if (key->str[n] == '[') {
|
|
|
|
index = atoi (key->str + n + 1);
|
|
|
|
key->str[n] = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!(var = GIB_Var_Get (*first, *second, key->str+start))) {
|
|
|
|
if (create) {
|
|
|
|
var = GIB_Var_New (key->str+start);
|
|
|
|
if (!*first)
|
2020-03-25 06:43:16 +00:00
|
|
|
*first = Hash_NewTable (256, GIB_Var_Get_Key,
|
|
|
|
GIB_Var_Free, 0, 0);
|
2003-02-16 02:44:24 +00:00
|
|
|
Hash_Add (*first, var);
|
|
|
|
} else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (index >= var->size) {
|
|
|
|
if (create) {
|
2006-07-23 01:34:14 +00:00
|
|
|
var->array = realloc (var->array,
|
|
|
|
(index + 1) * sizeof (struct gib_varray_s));
|
2003-02-16 02:44:24 +00:00
|
|
|
memset (var->array + var->size, 0,
|
|
|
|
(index + 1 - var->size) * sizeof (struct gib_varray_s));
|
|
|
|
var->size = index + 1;
|
|
|
|
} else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
second = &zero;
|
|
|
|
first = &var->array[index].leaves;
|
|
|
|
start = i+1;
|
2003-02-16 19:46:34 +00:00
|
|
|
} else if (i >= protect && (key->str[i] == '$' || key->str[i] == '#')) {
|
2003-02-16 02:44:24 +00:00
|
|
|
n = i;
|
|
|
|
if (GIB_Parse_Match_Var (key->str, &i))
|
|
|
|
return 0;
|
|
|
|
c = key->str[i];
|
2003-03-02 05:11:22 +00:00
|
|
|
varstartskip = (c == '}');
|
|
|
|
key->str[i] = 0;
|
|
|
|
if ((var = GIB_Var_Get_Very_Complex (&one, &two, key, n+1+varstartskip, &index2, create))) {
|
2003-02-16 02:44:24 +00:00
|
|
|
if (key->str[n] == '#')
|
2021-01-31 07:01:20 +00:00
|
|
|
str = va (0, "%u", var->size);
|
2003-02-16 02:44:24 +00:00
|
|
|
else
|
|
|
|
str = var->array[index2].value->str;
|
2003-03-02 05:11:22 +00:00
|
|
|
key->str[i] = c;
|
|
|
|
dstring_replace (key, n, i-n+varstartskip, str, strlen (str));
|
|
|
|
protect = n+strlen(str);
|
|
|
|
} else if (key->str[n] == '#') {
|
|
|
|
key->str[i] = c;
|
|
|
|
dstring_replace (key, n, i-n+varstartskip, "0", 1);
|
|
|
|
protect = n+1;
|
|
|
|
} else if ((cvar = Cvar_FindVar (key->str+n+1+varstartskip))) {
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
const char *cvar_str = Cvar_VarString (cvar);
|
2003-03-02 05:11:22 +00:00
|
|
|
key->str[i] = c;
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
dstring_replace (key, n, i-n+varstartskip, cvar_str,
|
|
|
|
strlen (cvar_str));
|
|
|
|
protect = n+strlen(cvar_str);
|
2003-03-02 05:11:22 +00:00
|
|
|
} else {
|
|
|
|
key->str[i] = c;
|
|
|
|
dstring_snip (key, n, n-i+varstartskip);
|
|
|
|
protect = 0;
|
|
|
|
}
|
2003-02-16 19:46:34 +00:00
|
|
|
i = n;
|
2003-02-16 02:44:24 +00:00
|
|
|
}
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2002-11-19 04:15:36 +00:00
|
|
|
}
|
2003-02-16 02:44:24 +00:00
|
|
|
if (!var->array[index].value)
|
|
|
|
var->array[index].value = dstring_newstr ();
|
2003-01-28 21:16:21 +00:00
|
|
|
*ind = index;
|
|
|
|
return var;
|
2002-08-08 09:20:00 +00:00
|
|
|
}
|
|
|
|
|
2003-01-29 04:34:23 +00:00
|
|
|
void
|
2003-02-14 22:42:11 +00:00
|
|
|
GIB_Var_Assign (gib_var_t * var, unsigned int index, dstring_t ** values,
|
2003-05-14 21:13:41 +00:00
|
|
|
unsigned int numv, qboolean shrink)
|
2003-01-29 04:34:23 +00:00
|
|
|
{
|
|
|
|
unsigned int i, len;
|
|
|
|
|
|
|
|
// Now, expand the array to the correct size
|
|
|
|
len = numv + index;
|
|
|
|
if (len >= var->size) {
|
2003-02-16 02:44:24 +00:00
|
|
|
var->array = realloc (var->array, len * sizeof (struct gib_varray_s));
|
2003-02-14 22:42:11 +00:00
|
|
|
memset (var->array + var->size, 0,
|
2003-02-16 02:44:24 +00:00
|
|
|
(len - var->size) * sizeof (struct gib_varray_s));
|
2003-01-29 04:34:23 +00:00
|
|
|
var->size = len;
|
2003-05-14 21:13:41 +00:00
|
|
|
} else if (len < var->size && shrink) {
|
2003-02-16 02:44:24 +00:00
|
|
|
for (i = len; i < var->size; i++) {
|
|
|
|
if (var->array[i].value)
|
|
|
|
dstring_delete (var->array[i].value);
|
|
|
|
if (var->array[i].leaves)
|
|
|
|
Hash_DelTable (var->array[i].leaves);
|
|
|
|
}
|
|
|
|
var->array = realloc (var->array, len * sizeof (struct gib_varray_s));
|
2003-05-14 21:13:41 +00:00
|
|
|
var->size = len;
|
2003-01-29 04:34:23 +00:00
|
|
|
}
|
|
|
|
for (i = 0; i < numv; i++) {
|
2003-02-16 02:44:24 +00:00
|
|
|
if (var->array[i + index].value)
|
|
|
|
dstring_clearstr (var->array[i + index].value);
|
2003-01-29 04:34:23 +00:00
|
|
|
else
|
2003-02-16 02:44:24 +00:00
|
|
|
var->array[i + index].value = dstring_newstr ();
|
|
|
|
dstring_appendstr (var->array[i + index].value, values[i]->str);
|
2003-01-29 04:34:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
static const char *
|
2012-07-18 13:34:37 +00:00
|
|
|
GIB_Domain_Get_Key (const void *ele, void *ptr)
|
2002-08-24 05:14:46 +00:00
|
|
|
{
|
2003-02-14 22:42:11 +00:00
|
|
|
return ((gib_domain_t *) ele)->name;
|
2002-08-24 05:14:46 +00:00
|
|
|
}
|
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
static void
|
|
|
|
GIB_Domain_Free (void *ele, void *ptr)
|
2002-08-24 05:14:46 +00:00
|
|
|
{
|
2003-02-14 22:42:11 +00:00
|
|
|
gib_domain_t *l = (gib_domain_t *) ele;
|
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
Hash_DelTable (l->vars);
|
2003-02-14 22:42:11 +00:00
|
|
|
free ((void *) l->name);
|
2003-01-28 21:16:21 +00:00
|
|
|
free (l);
|
2002-08-24 05:14:46 +00:00
|
|
|
}
|
2002-10-23 03:56:57 +00:00
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
hashtab_t *
|
|
|
|
GIB_Domain_Get (const char *name)
|
2002-12-10 00:04:15 +00:00
|
|
|
{
|
2003-01-28 21:16:21 +00:00
|
|
|
gib_domain_t *d = Hash_Find (gib_domains, name);
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
if (!d) {
|
|
|
|
d = calloc (1, sizeof (gib_domain_t));
|
2003-02-14 22:42:11 +00:00
|
|
|
d->name = strdup (name);
|
2020-03-25 06:43:16 +00:00
|
|
|
d->vars = Hash_NewTable (1024, GIB_Var_Get_Key, GIB_Var_Free, 0, 0);
|
2003-01-29 04:34:23 +00:00
|
|
|
Hash_Add (gib_domains, d);
|
2003-01-28 21:16:21 +00:00
|
|
|
}
|
|
|
|
return d->vars;
|
2002-12-10 00:04:15 +00:00
|
|
|
}
|
|
|
|
|
2003-09-11 06:03:13 +00:00
|
|
|
hashtab_t *
|
|
|
|
GIB_Var_Hash_New (void)
|
|
|
|
{
|
2020-03-25 06:43:16 +00:00
|
|
|
return Hash_NewTable (1024, GIB_Var_Get_Key, GIB_Var_Free, 0, 0);
|
2003-09-11 06:03:13 +00:00
|
|
|
}
|
|
|
|
|
2002-10-23 03:56:57 +00:00
|
|
|
void
|
2003-01-28 21:16:21 +00:00
|
|
|
GIB_Var_Init (void)
|
2002-10-23 03:56:57 +00:00
|
|
|
{
|
2020-03-25 06:43:16 +00:00
|
|
|
gib_globals = Hash_NewTable (1024, GIB_Var_Get_Key, GIB_Var_Free, 0, 0);
|
|
|
|
gib_domains = Hash_NewTable (1024, GIB_Domain_Get_Key,
|
|
|
|
GIB_Domain_Free, 0, 0);
|
2002-10-23 03:56:57 +00:00
|
|
|
}
|