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));
|
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
new->array = calloc (1, sizeof (dstring_t *));
|
|
|
|
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)
|
|
|
|
*first = Hash_NewTable (256, GIB_Var_Get_Key, GIB_Var_Free, 0);
|
|
|
|
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;
|
|
|
|
char c, *str;
|
|
|
|
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)
|
|
|
|
*first = Hash_NewTable (256, GIB_Var_Get_Key, GIB_Var_Free, 0);
|
|
|
|
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] == '#')
|
|
|
|
str = va("%u", var->size);
|
|
|
|
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))) {
|
|
|
|
key->str[i] = c;
|
|
|
|
dstring_replace (key, n, i-n+varstartskip, cvar->string, strlen (cvar->string));
|
|
|
|
protect = n+strlen(cvar->string);
|
|
|
|
} 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);
|
2003-01-28 21:16:21 +00:00
|
|
|
d->vars = Hash_NewTable (1024, GIB_Var_Get_Key, GIB_Var_Free, 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)
|
|
|
|
{
|
|
|
|
return Hash_NewTable (1024, GIB_Var_Get_Key, GIB_Var_Free, 0);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2003-01-28 21:16:21 +00:00
|
|
|
gib_globals = Hash_NewTable (1024, GIB_Var_Get_Key, GIB_Var_Free, 0);
|
|
|
|
gib_domains = Hash_NewTable (1024, GIB_Domain_Get_Key, GIB_Domain_Free, 0);
|
2002-10-23 03:56:57 +00:00
|
|
|
}
|