hash.[ch]:

add Hash_Free to ease freeing of hash table entries.
quakefs.c:
	beginnings of proper variable subtitution in qfs. not yet working but
	it compiles.
This commit is contained in:
Bill Currie 2003-01-27 18:16:15 +00:00
parent 55bec57b18
commit fd5695eb03
3 changed files with 107 additions and 0 deletions

View file

@ -118,6 +118,9 @@ void **Hash_FindElementList (hashtab_t *tab, void *ele);
void *Hash_Del (hashtab_t *tab, const char *key); void *Hash_Del (hashtab_t *tab, const char *key);
void *Hash_DelElement (hashtab_t *tab, void *ele); void *Hash_DelElement (hashtab_t *tab, void *ele);
/* Hash_Free (tab, Hash_Del (tab, key)); */
void Hash_Free (hashtab_t *tab, void *ele);
/* /*
returh the hash value of a string. this is the same function as used returh the hash value of a string. this is the same function as used
internally. internally.

View file

@ -380,6 +380,13 @@ Hash_DelElement (hashtab_t *tab, void *ele)
return 0; return 0;
} }
void
Hash_Free (hashtab_t *tab, void *ele)
{
if (ele && tab->free_ele)
tab->free_ele (ele, tab->user_data);
}
void ** void **
Hash_GetList (hashtab_t *tab) Hash_GetList (hashtab_t *tab)
{ {

View file

@ -127,6 +127,11 @@ searchpath_t *com_searchpaths;
//QFS //QFS
typedef struct qfs_var_s {
char *var;
char *val;
} qfs_var_t;
static void COM_AddGameDirectory (const char *dir); //FIXME static void COM_AddGameDirectory (const char *dir); //FIXME
gamedir_t *qfs_gamedir; gamedir_t *qfs_gamedir;
@ -154,6 +159,98 @@ static const char *qfs_default_dirconf =
" };" " };"
"}"; "}";
static const char *
qfs_var_get_key (void *_v, void *unused)
{
return ((qfs_var_t *)_v)->var;
}
static void
qfs_var_free (void *_v, void *unused)
{
qfs_var_t *v = (qfs_var_t *) _v;
free (v->var);
free (v->val);
free (v);
}
static __attribute__ ((unused)) hashtab_t *
qfs_new_vars (void)
{
return Hash_NewTable (61, qfs_var_get_key, qfs_var_free, 0);
}
static __attribute__ ((unused)) void
qfs_set_var (hashtab_t *vars, const char *var, const char *val)
{
qfs_var_t *v = Hash_Find (vars, var);
if (!v) {
v = malloc (sizeof (qfs_var_t));
v->var = strdup (var);
v->val = 0;
Hash_Add (vars, v);
}
if (v->val)
free (v->val);
v->val = strdup (val);
}
static __attribute__ ((unused)) char *
qfs_var_subst (const char *string, hashtab_t *vars)
{
dstring_t *new = dstring_newstr ();
const char *s = string;
const char *e = s;
const char *var;
char *t;
qfs_var_t *sub;
while (1) {
while (*e && *e != '$')
e++;
dstring_appendsubstr (new, s, (e - s));
if (!*e++)
break;
if (*e == '$') {
dstring_appendstr (new, "$");
s = ++e;
} else if (*e == '{') {
s = e;
while (*e && *e != '}')
e++;
if (!*e) {
dstring_appendsubstr (new, s, (e - s));
break;
}
var = va ("%*s", (e - s) - 1, s + 1);
sub = Hash_Find (vars, var);
if (sub)
dstring_appendstr (new, sub->val);
else
dstring_appendsubstr (new, s - 1, (e - s) + 2);
s = ++e;
} else if (isalnum ((byte) *e) || *e == '_') {
s = e;
while (isalnum ((byte) *e) || *e == '_')
e++;
var = va ("%*s", e - s, s);
sub = Hash_Find (vars, var);
if (sub)
dstring_appendstr (new, sub->val);
else
dstring_appendsubstr (new, s - 1, (e - s) + 1);
s = e;
} else {
dstring_appendstr (new, "$");
s = e;
}
}
t = new->str;
free (new);
return t;
}
static void static void
qfs_get_gd_params (plitem_t *gdpl, gamedir_t *gamedir, dstring_t *path) qfs_get_gd_params (plitem_t *gdpl, gamedir_t *gamedir, dstring_t *path)
{ {