2001-02-19 21:15:25 +00:00
|
|
|
/*
|
|
|
|
info.c
|
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
Info strings.
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
*/
|
2001-09-28 06:26:31 +00:00
|
|
|
static const char rcsid[] =
|
|
|
|
"$Id$";
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
2001-11-04 07:50:39 +00:00
|
|
|
#include "QF/hash.h"
|
2001-03-27 20:33:07 +00:00
|
|
|
#include "QF/info.h"
|
2001-09-21 04:22:46 +00:00
|
|
|
#include "QF/sys.h"
|
2001-11-04 07:50:39 +00:00
|
|
|
#include "QF/zone.h"
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
#include "compat.h"
|
|
|
|
|
2001-09-11 03:10:29 +00:00
|
|
|
/*
|
|
|
|
Info_FilterForKey
|
|
|
|
|
|
|
|
Searches for key in the "client-needed" info string list
|
|
|
|
*/
|
|
|
|
qboolean
|
|
|
|
Info_FilterForKey (const char *key, const char **filter_list)
|
|
|
|
{
|
|
|
|
const char **s;
|
|
|
|
|
|
|
|
for (s = filter_list; *s; s++) {
|
|
|
|
if (strequal (*s, key)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Info_ValueForKey
|
|
|
|
|
|
|
|
Searches the string for the given
|
|
|
|
key and returns the associated value, or an empty string.
|
|
|
|
*/
|
2001-08-15 22:38:11 +00:00
|
|
|
const char *
|
2001-11-04 07:50:39 +00:00
|
|
|
Info_ValueForKey (info_t *info, const char *key)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-11-04 07:50:39 +00:00
|
|
|
info_key_t *k = Hash_Find (info->tab, key);
|
|
|
|
if (k)
|
|
|
|
return k->value;
|
|
|
|
return "";
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-11-04 07:50:39 +00:00
|
|
|
Info_RemoveKey (info_t *info, const char *key)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-11-04 07:50:39 +00:00
|
|
|
info_key_t *k = Hash_Del (info->tab, key);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-11-04 07:50:39 +00:00
|
|
|
if (k) {
|
|
|
|
free ((char*)k->key);
|
|
|
|
free ((char*)k->value);
|
|
|
|
free (k);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-11-04 07:50:39 +00:00
|
|
|
Info_SetValueForStarKey (info_t *info, const char *key, const char *value, int flags)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-11-04 07:50:39 +00:00
|
|
|
info_key_t *k;
|
|
|
|
int cursize;
|
2001-11-04 22:00:32 +00:00
|
|
|
char *str;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
if (strstr (key, "\\") || strstr (value, "\\")) {
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("Can't use keys or values with a \\\n");
|
2001-02-19 21:15:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strstr (key, "\"") || strstr (value, "\"")) {
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("Can't use keys or values with a \"\n");
|
2001-02-19 21:15:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen (key) > 63 || strlen (value) > 63) {
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("Keys and values must be < 64 characters.\n");
|
2001-02-19 21:15:25 +00:00
|
|
|
return;
|
|
|
|
}
|
2001-11-04 07:50:39 +00:00
|
|
|
k = Hash_Find (info->tab, key);
|
|
|
|
cursize = info->cursize;
|
|
|
|
if (k) {
|
|
|
|
cursize -= strlen (k->key) + 1;
|
|
|
|
cursize -= strlen (k->value) + 1;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
2001-11-04 07:50:39 +00:00
|
|
|
if (cursize + strlen (key) + 1 + strlen (value) + 1 > info->maxsize) {
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("Info string length exceeded\n");
|
2001-02-19 21:15:25 +00:00
|
|
|
return;
|
|
|
|
}
|
2001-11-04 07:50:39 +00:00
|
|
|
if (k) {
|
|
|
|
if (strequal (k->value, value))
|
|
|
|
return;
|
2001-11-04 08:18:54 +00:00
|
|
|
info->cursize -= strlen (k->value) + 1;
|
2001-11-04 07:50:39 +00:00
|
|
|
free ((char*)k->value);
|
|
|
|
} else {
|
|
|
|
if (!(k = malloc (sizeof (info_key_t))))
|
|
|
|
Sys_Error ("Info_SetValueForStarKey: out of memory\n");
|
|
|
|
if (!(k->key = strdup (key)))
|
|
|
|
Sys_Error ("Info_SetValueForStarKey: out of memory\n");
|
|
|
|
info->cursize += strlen (key) + 1;
|
|
|
|
Hash_Add (info->tab, k);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
2001-11-04 22:00:32 +00:00
|
|
|
if (!(str = strdup (value)))
|
2001-11-04 07:50:39 +00:00
|
|
|
Sys_Error ("Info_SetValueForStarKey: out of memory\n");
|
2001-11-04 22:00:32 +00:00
|
|
|
if (flags & 1) {
|
|
|
|
byte *s, *d;
|
|
|
|
for (d = s = str; *s; s++)
|
|
|
|
if ((*s & 127) >= 32 && (*s & 127) <=127)
|
|
|
|
*d++ = *s & 127;
|
|
|
|
if (flags & 2)
|
|
|
|
for (d = s = str; *s; s++)
|
|
|
|
*d++ = tolower (*s);
|
|
|
|
} else {
|
|
|
|
byte *s, *d;
|
|
|
|
for (d = s = str; *s; s++)
|
|
|
|
if (*s >= 13)
|
|
|
|
*d++ = *s;
|
|
|
|
}
|
|
|
|
info->cursize += strlen (str) + 1;
|
|
|
|
k->value = str;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-11-04 07:50:39 +00:00
|
|
|
Info_SetValueForKey (info_t *info, const char *key, const char *value,
|
|
|
|
int flags)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
if (key[0] == '*') {
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("Can't set * keys\n");
|
2001-02-19 21:15:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-11-04 07:50:39 +00:00
|
|
|
Info_SetValueForStarKey (info, key, value, flags);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-11-04 07:50:39 +00:00
|
|
|
Info_Print (info_t *info)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-11-04 07:50:39 +00:00
|
|
|
info_key_t **key_list;
|
|
|
|
info_key_t **key;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-11-04 07:50:39 +00:00
|
|
|
key_list = (info_key_t **)Hash_GetList (info->tab);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-11-04 07:50:39 +00:00
|
|
|
for (key = key_list; *key; key++) {
|
2001-11-04 20:48:12 +00:00
|
|
|
Sys_Printf ("%-15s %s\n", (*key)->key, (*key)->value);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
2001-11-04 07:50:39 +00:00
|
|
|
free (key_list);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
2001-05-22 12:00:45 +00:00
|
|
|
|
2001-11-04 07:50:39 +00:00
|
|
|
static const char *
|
|
|
|
get_key (void *k, void *unused)
|
2001-05-22 12:00:45 +00:00
|
|
|
{
|
2001-11-04 07:50:39 +00:00
|
|
|
return ((info_key_t *)k)->key;
|
|
|
|
}
|
2001-05-22 12:00:45 +00:00
|
|
|
|
2001-11-04 07:50:39 +00:00
|
|
|
static void
|
|
|
|
free_key (void *_k, void *unused)
|
|
|
|
{
|
|
|
|
info_key_t *k = (info_key_t *)_k;
|
|
|
|
free ((char*)k->key);
|
|
|
|
free ((char*)k->value);
|
|
|
|
free (k);
|
|
|
|
}
|
2001-05-22 12:00:45 +00:00
|
|
|
|
2001-11-04 07:50:39 +00:00
|
|
|
info_t *
|
|
|
|
Info_ParseString (const char *s, int maxsize)
|
|
|
|
{
|
|
|
|
info_t *info;
|
|
|
|
char *string = Hunk_TempAlloc (strlen (s) + 1);
|
|
|
|
char *key, *value, *end;
|
|
|
|
|
|
|
|
info = malloc (sizeof (info_t));
|
|
|
|
info->tab = Hash_NewTable (61, get_key, free_key, 0);
|
|
|
|
info->maxsize = maxsize;
|
|
|
|
info->cursize = 0;
|
|
|
|
|
|
|
|
strcpy (string, s);
|
|
|
|
key = string;
|
|
|
|
if (*key == '\\')
|
|
|
|
key++;
|
|
|
|
while (*key) {
|
|
|
|
value = key;
|
|
|
|
while (*value && *value != '\\')
|
|
|
|
value++;
|
|
|
|
if (*value) {
|
|
|
|
*value++ = 0;
|
|
|
|
for (end = value; *end && *end != '\\'; end++)
|
|
|
|
;
|
|
|
|
if (*end)
|
|
|
|
*end++ = 0;
|
|
|
|
} else {
|
|
|
|
value = end = "";
|
|
|
|
}
|
|
|
|
Info_SetValueForStarKey (info, key, value, 0);
|
|
|
|
key = end;
|
|
|
|
}
|
|
|
|
return info;
|
|
|
|
}
|
2001-05-22 12:00:45 +00:00
|
|
|
|
2001-11-04 07:50:39 +00:00
|
|
|
void
|
|
|
|
Info_Destroy (info_t *info)
|
|
|
|
{
|
|
|
|
Hash_DelTable (info->tab);
|
|
|
|
free (info);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
Info_MakeString (info_t *info, int (*filter)(const char *))
|
|
|
|
{
|
2001-11-04 15:42:26 +00:00
|
|
|
char *string;
|
2001-11-04 07:50:39 +00:00
|
|
|
const char *s;
|
2001-11-04 15:42:26 +00:00
|
|
|
char *d;
|
2001-11-04 07:50:39 +00:00
|
|
|
info_key_t **key_list;
|
|
|
|
info_key_t **key;
|
|
|
|
|
2001-11-04 15:42:26 +00:00
|
|
|
d = string = Hunk_TempAlloc (info->cursize + 1);
|
2001-11-04 07:50:39 +00:00
|
|
|
key_list = (info_key_t **)Hash_GetList (info->tab);
|
|
|
|
|
|
|
|
for (key = key_list; *key; key++) {
|
|
|
|
if (filter && filter ((*key)->key))
|
|
|
|
continue;
|
|
|
|
*d++ = '\\';
|
|
|
|
for (s = (*key)->key; *s; s++)
|
|
|
|
*d++ = *s;
|
|
|
|
*d++ = '\\';
|
|
|
|
for (s = (*key)->value; *s; s++)
|
|
|
|
*d++ = *s;
|
|
|
|
}
|
|
|
|
free (key_list);
|
|
|
|
return string;
|
2001-05-22 12:00:45 +00:00
|
|
|
}
|