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
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
2003-01-15 15:31:36 +00:00
|
|
|
|
|
|
|
static __attribute__ ((unused)) const char rcsid[] =
|
|
|
|
"$Id$";
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
#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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-05 01:45:52 +00:00
|
|
|
int
|
|
|
|
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;
|
2003-02-19 17:39:21 +00:00
|
|
|
byte *s, *d;
|
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");
|
2003-08-05 01:45:52 +00:00
|
|
|
return 0;
|
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");
|
2003-08-05 01:45:52 +00:00
|
|
|
return 0;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
2003-08-05 01:45:52 +00:00
|
|
|
return 0;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
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-05 02:08:20 +00:00
|
|
|
if (info->maxsize &&
|
|
|
|
cursize + strlen (key) + 1 + strlen (value) + 1 > info->maxsize) {
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("Info string length exceeded\n");
|
2003-08-05 01:45:52 +00:00
|
|
|
return 0;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
2001-11-04 07:50:39 +00:00
|
|
|
if (k) {
|
|
|
|
if (strequal (k->value, value))
|
2003-08-05 01:45:52 +00:00
|
|
|
return 0;
|
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))))
|
2002-05-14 06:12:29 +00:00
|
|
|
Sys_Error ("Info_SetValueForStarKey: out of memory");
|
2001-11-04 07:50:39 +00:00
|
|
|
if (!(k->key = strdup (key)))
|
2002-05-14 06:12:29 +00:00
|
|
|
Sys_Error ("Info_SetValueForStarKey: out of memory");
|
2001-11-04 07:50:39 +00:00
|
|
|
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)))
|
2002-05-14 06:12:29 +00:00
|
|
|
Sys_Error ("Info_SetValueForStarKey: out of memory");
|
2003-02-19 17:39:21 +00:00
|
|
|
for (d = s = str; *s; s++) {
|
|
|
|
if (flags & 1) {
|
|
|
|
*s &= 127;
|
|
|
|
if (*s < 32)
|
|
|
|
continue;
|
|
|
|
}
|
2001-11-04 22:00:32 +00:00
|
|
|
if (flags & 2)
|
2003-02-19 17:39:21 +00:00
|
|
|
*s = tolower (*s);
|
|
|
|
if (*s > 13)
|
|
|
|
*d++ = *s;
|
2001-11-04 22:00:32 +00:00
|
|
|
}
|
2003-07-08 17:26:50 +00:00
|
|
|
*d = 0;
|
2001-11-04 22:00:32 +00:00
|
|
|
info->cursize += strlen (str) + 1;
|
|
|
|
k->value = str;
|
2003-08-05 01:45:52 +00:00
|
|
|
return 1;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
2003-08-05 01:45:52 +00:00
|
|
|
int
|
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");
|
2003-08-05 01:45:52 +00:00
|
|
|
return 0;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
2003-08-05 01:45:52 +00:00
|
|
|
return 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 *
|
2001-11-05 03:21:42 +00:00
|
|
|
info_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 *
|
2003-02-19 17:39:21 +00:00
|
|
|
Info_ParseString (const char *s, int maxsize, int flags)
|
2001-11-04 07:50:39 +00:00
|
|
|
{
|
|
|
|
info_t *info;
|
|
|
|
char *string = Hunk_TempAlloc (strlen (s) + 1);
|
|
|
|
char *key, *value, *end;
|
|
|
|
|
|
|
|
info = malloc (sizeof (info_t));
|
2001-11-05 03:21:42 +00:00
|
|
|
info->tab = Hash_NewTable (61, info_get_key, free_key, 0);
|
2001-11-04 07:50:39 +00:00
|
|
|
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 {
|
2003-01-06 18:28:13 +00:00
|
|
|
value = end = (char *)"";
|
2001-11-04 07:50:39 +00:00
|
|
|
}
|
2003-02-19 17:39:21 +00:00
|
|
|
Info_SetValueForStarKey (info, key, value, flags);
|
2001-11-04 07:50:39 +00:00
|
|
|
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++) {
|
2001-11-05 01:29:45 +00:00
|
|
|
if (!*(*key)->value)
|
|
|
|
continue;
|
2001-11-04 07:50:39 +00:00
|
|
|
if (filter && filter ((*key)->key))
|
|
|
|
continue;
|
|
|
|
*d++ = '\\';
|
|
|
|
for (s = (*key)->key; *s; s++)
|
|
|
|
*d++ = *s;
|
|
|
|
*d++ = '\\';
|
|
|
|
for (s = (*key)->value; *s; s++)
|
|
|
|
*d++ = *s;
|
|
|
|
}
|
2001-12-03 21:24:48 +00:00
|
|
|
*d = 0;
|
2001-11-04 07:50:39 +00:00
|
|
|
free (key_list);
|
|
|
|
return string;
|
2001-05-22 12:00:45 +00:00
|
|
|
}
|
2003-08-05 01:45:52 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
Info_AddKeys (info_t *info, info_t *keys)
|
|
|
|
{
|
|
|
|
info_key_t **key_list;
|
|
|
|
info_key_t **key;
|
|
|
|
|
|
|
|
key_list = (info_key_t **)Hash_GetList (keys->tab);
|
|
|
|
for (key = key_list; *key; key++) {
|
|
|
|
Info_SetValueForKey (info, (*key)->key, (*key)->value, 0);
|
|
|
|
}
|
|
|
|
free (key_list);
|
|
|
|
}
|