2001-02-19 21:15:25 +00:00
|
|
|
/*
|
|
|
|
qfplist.c
|
|
|
|
|
|
|
|
Property list management
|
|
|
|
|
2001-06-19 02:10:47 +00:00
|
|
|
Copyright (C) 2000 Jeff Teunissen <deek@d2dc.net>
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2011-08-25 13:35:20 +00:00
|
|
|
#if defined(_WIN32) && defined(HAVE_MALLOC_H)
|
2007-03-22 23:20:57 +00:00
|
|
|
#include <malloc.h>
|
|
|
|
#endif
|
|
|
|
|
2013-01-22 05:09:41 +00:00
|
|
|
#include "qfalloca.h"
|
|
|
|
|
2020-07-02 05:20:46 +00:00
|
|
|
#include "QF/darray.h"
|
2004-11-11 11:18:00 +00:00
|
|
|
#include "QF/dstring.h"
|
2001-06-19 02:10:47 +00:00
|
|
|
#include "QF/hash.h"
|
2001-03-27 20:33:07 +00:00
|
|
|
#include "QF/qfplist.h"
|
|
|
|
#include "QF/qtypes.h"
|
2001-08-01 07:05:28 +00:00
|
|
|
#include "QF/sys.h"
|
2020-07-02 05:20:46 +00:00
|
|
|
#include "QF/va.h"
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2010-08-20 04:10:37 +00:00
|
|
|
/*
|
|
|
|
Generic property list item.
|
|
|
|
*/
|
|
|
|
struct plitem_s {
|
|
|
|
pltype_t type;
|
2020-07-01 12:47:09 +00:00
|
|
|
int line;
|
2010-08-20 04:10:37 +00:00
|
|
|
void *data;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Dictionaries
|
|
|
|
*/
|
|
|
|
struct dictkey_s {
|
|
|
|
char *key;
|
|
|
|
plitem_t *value;
|
|
|
|
};
|
|
|
|
typedef struct dictkey_s dictkey_t;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Arrays
|
|
|
|
*/
|
|
|
|
struct plarray_s {
|
|
|
|
int numvals; ///< Number of items in array
|
|
|
|
int maxvals; ///< Number of items that can be stored
|
|
|
|
///< before a realloc is necesary.
|
2011-08-20 07:30:43 +00:00
|
|
|
struct plitem_s **values; ///< Array data
|
2010-08-20 04:10:37 +00:00
|
|
|
};
|
|
|
|
typedef struct plarray_s plarray_t;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Typeless, unformatted binary data
|
|
|
|
*/
|
|
|
|
struct plbinary_s {
|
|
|
|
size_t size;
|
|
|
|
void *data;
|
|
|
|
};
|
|
|
|
typedef struct plbinary_s plbinary_t;
|
|
|
|
|
|
|
|
typedef struct pldata_s { // Unparsed property list string
|
2021-01-23 09:56:15 +00:00
|
|
|
const char *ptr;
|
|
|
|
unsigned end;
|
|
|
|
unsigned pos;
|
|
|
|
unsigned line;
|
|
|
|
plitem_t *error;
|
2010-08-20 04:10:37 +00:00
|
|
|
} pldata_t;
|
|
|
|
|
2004-11-11 11:18:00 +00:00
|
|
|
// Ugly defines for fast checking and conversion from char to number
|
|
|
|
#define inrange(ch,min,max) ((ch) >= (min) && (ch) <= (max))
|
|
|
|
#define char2num(ch) \
|
2011-08-20 07:30:43 +00:00
|
|
|
(inrange((ch), '0', '9') ? ((ch) - '0') \
|
|
|
|
: 10 + (inrange((ch), 'a', 'f') ? ((ch) - 'a') \
|
|
|
|
: ((ch) - 'A')))
|
2004-11-11 11:18:00 +00:00
|
|
|
|
2020-07-02 05:20:46 +00:00
|
|
|
static const char *pl_types[] = {
|
|
|
|
"dictionary",
|
|
|
|
"array",
|
|
|
|
"biinary",
|
|
|
|
"string",
|
|
|
|
};
|
|
|
|
|
2004-11-11 11:18:00 +00:00
|
|
|
static byte quotable_bitmap[32];
|
|
|
|
static inline int
|
|
|
|
is_quotable (byte x)
|
|
|
|
{
|
|
|
|
return quotable_bitmap[x / 8] & (1 << (x % 8));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
init_quotables (void)
|
|
|
|
{
|
|
|
|
const char *unquotables = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
"abcdefghijklmnopqrstuvwxyz!#$%&*+-./:?@|~_^";
|
2005-04-11 20:59:03 +00:00
|
|
|
const byte *c;
|
2004-11-11 11:18:00 +00:00
|
|
|
memset (quotable_bitmap, ~0, sizeof (quotable_bitmap));
|
2005-06-08 06:35:48 +00:00
|
|
|
for (c = (byte *) unquotables; *c; c++)
|
2004-11-11 11:18:00 +00:00
|
|
|
quotable_bitmap[*c / 8] &= ~(1 << (*c % 8));
|
|
|
|
}
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
static plitem_t *PL_ParsePropertyListItem (pldata_t *);
|
|
|
|
static qboolean PL_SkipSpace (pldata_t *);
|
|
|
|
static char *PL_ParseQuotedString (pldata_t *);
|
|
|
|
static char *PL_ParseUnquotedString (pldata_t *);
|
2004-11-11 11:18:00 +00:00
|
|
|
static char *PL_ParseData (pldata_t *, int *);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2003-03-18 19:15:31 +00:00
|
|
|
static const char *
|
2012-07-18 13:34:37 +00:00
|
|
|
dict_get_key (const void *i, void *unused)
|
2003-03-18 19:15:31 +00:00
|
|
|
{
|
|
|
|
dictkey_t *item = (dictkey_t *) i;
|
|
|
|
return item->key;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dict_free (void *i, void *unused)
|
|
|
|
{
|
|
|
|
dictkey_t *item = (dictkey_t *) i;
|
|
|
|
free (item->key);
|
2006-12-09 06:00:36 +00:00
|
|
|
if (item->value) // Make descended stuff get freed
|
|
|
|
PL_Free (item->value);
|
2003-03-18 19:15:31 +00:00
|
|
|
free (item);
|
|
|
|
}
|
|
|
|
|
|
|
|
static plitem_t *
|
|
|
|
PL_NewItem (pltype_t type)
|
|
|
|
{
|
|
|
|
plitem_t *item = calloc (1, sizeof (plitem_t));
|
|
|
|
item->type = type;
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE plitem_t *
|
2003-03-18 19:15:31 +00:00
|
|
|
PL_NewDictionary (void)
|
|
|
|
{
|
|
|
|
plitem_t *item = PL_NewItem (QFDictionary);
|
2020-03-25 06:43:16 +00:00
|
|
|
//FIXME need a per-thread hashlink freelist for plist to be thread-safe
|
|
|
|
hashtab_t *dict = Hash_NewTable (1021, dict_get_key, dict_free, NULL, 0);
|
2003-03-18 19:15:31 +00:00
|
|
|
item->data = dict;
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE plitem_t *
|
2003-03-18 19:15:31 +00:00
|
|
|
PL_NewArray (void)
|
|
|
|
{
|
|
|
|
plitem_t *item = PL_NewItem (QFArray);
|
|
|
|
plarray_t *array = calloc (1, sizeof (plarray_t));
|
|
|
|
item->data = array;
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE plitem_t *
|
2010-08-20 04:21:22 +00:00
|
|
|
PL_NewData (void *data, size_t size)
|
2003-03-18 19:15:31 +00:00
|
|
|
{
|
|
|
|
plitem_t *item = PL_NewItem (QFBinary);
|
|
|
|
plbinary_t *bin = malloc (sizeof (plbinary_t));
|
|
|
|
item->data = bin;
|
2010-08-20 04:21:22 +00:00
|
|
|
bin->data = data;
|
2003-03-18 19:15:31 +00:00
|
|
|
bin->size = size;
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2003-11-21 21:34:53 +00:00
|
|
|
static plitem_t *
|
2020-07-01 12:47:09 +00:00
|
|
|
new_string (char *str, int line)
|
2003-03-18 19:15:31 +00:00
|
|
|
{
|
|
|
|
plitem_t *item = PL_NewItem (QFString);
|
|
|
|
item->data = str;
|
2020-07-01 12:47:09 +00:00
|
|
|
item->line = line;
|
2003-03-18 19:15:31 +00:00
|
|
|
return item;
|
|
|
|
}
|
2001-08-22 22:03:16 +00:00
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE plitem_t *
|
2003-11-21 21:34:53 +00:00
|
|
|
PL_NewString (const char *str)
|
|
|
|
{
|
2020-07-01 12:47:09 +00:00
|
|
|
return new_string (strdup (str), 0);
|
2003-11-21 21:34:53 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2004-01-06 11:09:40 +00:00
|
|
|
PL_Free (plitem_t *item)
|
2001-06-19 02:10:47 +00:00
|
|
|
{
|
|
|
|
switch (item->type) {
|
|
|
|
case QFDictionary:
|
|
|
|
Hash_DelTable (item->data);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QFArray: {
|
|
|
|
int i = ((plarray_t *) item->data)->numvals;
|
|
|
|
|
2002-12-18 20:53:49 +00:00
|
|
|
while (i-- > 0) {
|
2004-01-06 11:09:40 +00:00
|
|
|
PL_Free (((plarray_t *) item->data)->values[i]);
|
2001-06-19 02:10:47 +00:00
|
|
|
}
|
2003-04-07 19:29:52 +00:00
|
|
|
free (((plarray_t *) item->data)->values);
|
2001-06-19 02:10:47 +00:00
|
|
|
free (item->data);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QFBinary:
|
|
|
|
free (((plbinary_t *) item->data)->data);
|
|
|
|
free (item->data);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QFString:
|
|
|
|
free (item->data);
|
|
|
|
break;
|
2021-01-02 05:02:33 +00:00
|
|
|
case QFMultiType:
|
|
|
|
break;
|
2001-06-19 02:10:47 +00:00
|
|
|
}
|
|
|
|
free (item);
|
|
|
|
}
|
|
|
|
|
2020-12-23 05:07:30 +00:00
|
|
|
VISIBLE size_t
|
|
|
|
PL_BinarySize (const plitem_t *binary)
|
|
|
|
{
|
|
|
|
plbinary_t *bin = (plbinary_t *) binary->data;
|
|
|
|
|
|
|
|
if (binary->type != QFBinary)
|
|
|
|
return 0;
|
|
|
|
return bin->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
VISIBLE const void *
|
|
|
|
PL_BinaryData (const plitem_t *binary)
|
|
|
|
{
|
|
|
|
plbinary_t *bin = (plbinary_t *) binary->data;
|
|
|
|
|
|
|
|
if (binary->type != QFBinary)
|
|
|
|
return 0;
|
|
|
|
return bin->data;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE const char *
|
2020-07-16 13:02:26 +00:00
|
|
|
PL_String (const plitem_t *string)
|
2004-11-11 11:18:00 +00:00
|
|
|
{
|
|
|
|
if (string->type != QFString)
|
|
|
|
return NULL;
|
|
|
|
return string->data;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE plitem_t *
|
2004-01-07 20:06:15 +00:00
|
|
|
PL_ObjectForKey (plitem_t *dict, const char *key)
|
2001-06-19 02:10:47 +00:00
|
|
|
{
|
2004-01-07 20:06:15 +00:00
|
|
|
hashtab_t *table = (hashtab_t *) dict->data;
|
2003-03-18 19:15:31 +00:00
|
|
|
dictkey_t *k;
|
|
|
|
|
2004-01-07 20:06:15 +00:00
|
|
|
if (dict->type != QFDictionary)
|
2003-03-18 19:15:31 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
k = (dictkey_t *) Hash_Find (table, key);
|
|
|
|
return k ? k->value : NULL;
|
2001-06-19 02:10:47 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE plitem_t *
|
2006-12-09 06:00:36 +00:00
|
|
|
PL_RemoveObjectForKey (plitem_t *dict, const char *key)
|
|
|
|
{
|
|
|
|
hashtab_t *table = (hashtab_t *) dict->data;
|
|
|
|
dictkey_t *k;
|
|
|
|
plitem_t *value;
|
|
|
|
|
|
|
|
if (dict->type != QFDictionary)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
k = (dictkey_t *) Hash_Del (table, key);
|
|
|
|
if (!k)
|
|
|
|
return NULL;
|
|
|
|
value = k->value;
|
|
|
|
k->value = 0;
|
|
|
|
dict_free (k, 0);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE plitem_t *
|
2004-01-06 11:09:40 +00:00
|
|
|
PL_D_AllKeys (plitem_t *dict)
|
|
|
|
{
|
2004-11-11 11:18:00 +00:00
|
|
|
void **list, **l;
|
2004-01-06 11:09:40 +00:00
|
|
|
dictkey_t *current;
|
|
|
|
plitem_t *array;
|
|
|
|
|
|
|
|
if (dict->type != QFDictionary)
|
|
|
|
return NULL;
|
|
|
|
|
2004-11-11 11:18:00 +00:00
|
|
|
if (!(l = list = Hash_GetList ((hashtab_t *) dict->data)))
|
2004-01-06 11:09:40 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!(array = PL_NewArray ()))
|
|
|
|
return NULL;
|
|
|
|
|
2004-11-11 11:18:00 +00:00
|
|
|
while ((current = (dictkey_t *) *l++)) {
|
2004-01-06 11:09:40 +00:00
|
|
|
PL_A_AddObject (array, PL_NewString (current->key));
|
|
|
|
}
|
2004-11-11 11:18:00 +00:00
|
|
|
free (list);
|
2004-01-06 11:09:40 +00:00
|
|
|
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE int
|
2004-02-07 07:47:23 +00:00
|
|
|
PL_D_NumKeys (plitem_t *dict)
|
|
|
|
{
|
|
|
|
if (dict->type != QFDictionary)
|
|
|
|
return 0;
|
|
|
|
return Hash_NumElements ((hashtab_t *) dict->data);
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE plitem_t *
|
2004-01-10 06:11:41 +00:00
|
|
|
PL_ObjectAtIndex (plitem_t *array, int index)
|
2001-06-19 02:10:47 +00:00
|
|
|
{
|
2004-01-10 06:11:41 +00:00
|
|
|
plarray_t *arr = (plarray_t *) array->data;
|
2003-03-18 19:15:31 +00:00
|
|
|
|
2004-01-10 06:11:41 +00:00
|
|
|
if (array->type != QFArray)
|
2003-03-18 19:15:31 +00:00
|
|
|
return NULL;
|
|
|
|
|
2004-01-10 06:11:41 +00:00
|
|
|
return index >= 0 && index < arr->numvals ? arr->values[index] : NULL;
|
2001-06-19 02:10:47 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE qboolean
|
2006-12-09 06:00:36 +00:00
|
|
|
PL_D_AddObject (plitem_t *dict, const char *key, plitem_t *value)
|
2001-06-19 02:10:47 +00:00
|
|
|
{
|
2003-03-18 19:15:31 +00:00
|
|
|
dictkey_t *k;
|
2002-12-18 16:09:47 +00:00
|
|
|
|
2003-03-18 19:15:31 +00:00
|
|
|
if (dict->type != QFDictionary)
|
2004-01-07 07:01:31 +00:00
|
|
|
return false;
|
2002-12-18 16:09:47 +00:00
|
|
|
|
2006-12-09 06:00:36 +00:00
|
|
|
if ((k = Hash_Find ((hashtab_t *)dict->data, key))) {
|
2004-01-06 11:09:40 +00:00
|
|
|
PL_Free ((plitem_t *) k->value);
|
2004-01-07 05:35:06 +00:00
|
|
|
k->value = value;
|
2004-01-06 11:09:40 +00:00
|
|
|
} else {
|
|
|
|
k = malloc (sizeof (dictkey_t));
|
2003-03-18 19:15:31 +00:00
|
|
|
|
2004-01-06 11:09:40 +00:00
|
|
|
if (!k)
|
2004-01-07 07:01:31 +00:00
|
|
|
return false;
|
2003-03-18 19:15:31 +00:00
|
|
|
|
2006-12-09 06:00:36 +00:00
|
|
|
k->key = strdup (key);
|
2004-01-07 05:35:06 +00:00
|
|
|
k->value = value;
|
2003-03-18 19:15:31 +00:00
|
|
|
|
2004-01-06 11:09:40 +00:00
|
|
|
Hash_Add ((hashtab_t *)dict->data, k);
|
|
|
|
}
|
2004-01-07 07:01:31 +00:00
|
|
|
return true;
|
2003-03-18 19:15:31 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE qboolean
|
2004-01-07 20:06:15 +00:00
|
|
|
PL_A_InsertObjectAtIndex (plitem_t *array, plitem_t *item, int index)
|
2003-03-18 19:15:31 +00:00
|
|
|
{
|
2004-01-07 20:06:15 +00:00
|
|
|
plarray_t *arr;
|
2003-03-18 19:15:31 +00:00
|
|
|
|
2004-01-07 20:06:15 +00:00
|
|
|
if (array->type != QFArray)
|
2004-01-07 07:01:31 +00:00
|
|
|
return false;
|
2003-03-18 19:15:31 +00:00
|
|
|
|
2004-01-07 20:06:15 +00:00
|
|
|
arr = (plarray_t *)array->data;
|
2003-03-18 19:15:31 +00:00
|
|
|
|
2004-01-07 20:06:15 +00:00
|
|
|
if (arr->numvals == arr->maxvals) {
|
|
|
|
int size = (arr->maxvals + 128) * sizeof (plitem_t *);
|
|
|
|
plitem_t **tmp = realloc (arr->values, size);
|
2003-04-08 02:56:36 +00:00
|
|
|
|
2003-04-07 19:29:52 +00:00
|
|
|
if (!tmp)
|
2004-01-07 07:01:31 +00:00
|
|
|
return false;
|
|
|
|
|
2004-01-07 20:06:15 +00:00
|
|
|
arr->maxvals += 128;
|
|
|
|
arr->values = tmp;
|
|
|
|
memset (arr->values + arr->numvals, 0,
|
|
|
|
(arr->maxvals - arr->numvals) * sizeof (plitem_t *));
|
2003-04-07 19:29:52 +00:00
|
|
|
}
|
2003-03-18 19:15:31 +00:00
|
|
|
|
|
|
|
if (index == -1)
|
2004-01-07 20:06:15 +00:00
|
|
|
index = arr->numvals;
|
2003-03-18 19:15:31 +00:00
|
|
|
|
2004-01-07 20:06:15 +00:00
|
|
|
if (index < 0 || index > arr->numvals)
|
2004-01-07 07:01:31 +00:00
|
|
|
return false;
|
2003-03-18 19:15:31 +00:00
|
|
|
|
2004-01-07 20:06:15 +00:00
|
|
|
memmove (arr->values + index + 1, arr->values + index,
|
|
|
|
(arr->numvals - index) * sizeof (plitem_t *));
|
|
|
|
arr->values[index] = item;
|
|
|
|
arr->numvals++;
|
2004-01-07 07:01:31 +00:00
|
|
|
return true;
|
2003-03-18 19:15:31 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE qboolean
|
2004-01-07 20:06:15 +00:00
|
|
|
PL_A_AddObject (plitem_t *array, plitem_t *item)
|
2003-03-18 19:15:31 +00:00
|
|
|
{
|
2004-01-07 20:06:15 +00:00
|
|
|
return PL_A_InsertObjectAtIndex (array, item, -1);
|
2001-06-19 02:10:47 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE int
|
2004-02-07 07:47:23 +00:00
|
|
|
PL_A_NumObjects (plitem_t *array)
|
|
|
|
{
|
|
|
|
if (array->type != QFArray)
|
|
|
|
return 0;
|
|
|
|
return ((plarray_t *) array->data)->numvals;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE plitem_t *
|
2006-12-09 02:33:08 +00:00
|
|
|
PL_RemoveObjectAtIndex (plitem_t *array, int index)
|
|
|
|
{
|
|
|
|
plarray_t *arr;
|
|
|
|
plitem_t *item;
|
|
|
|
|
|
|
|
if (array->type != QFArray)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
arr = (plarray_t *)array->data;
|
|
|
|
|
|
|
|
if (index < 0 || index >= arr->numvals)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
item = arr->values[index];
|
|
|
|
arr->numvals--;
|
|
|
|
while (index < arr->numvals) {
|
|
|
|
arr->values[index] = arr->values[index + 1];
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
static qboolean
|
|
|
|
PL_SkipSpace (pldata_t *pl)
|
|
|
|
{
|
|
|
|
while (pl->pos < pl->end) {
|
|
|
|
char c = pl->ptr[pl->pos];
|
2001-06-19 02:26:19 +00:00
|
|
|
|
2002-03-03 06:03:51 +00:00
|
|
|
if (!isspace ((byte) c)) {
|
2001-02-19 21:15:25 +00:00
|
|
|
if (c == '/' && pl->pos < pl->end - 1) { // check for comments
|
|
|
|
if (pl->ptr[pl->pos + 1] == '/') {
|
|
|
|
pl->pos += 2;
|
|
|
|
|
|
|
|
while (pl->pos < pl->end) {
|
|
|
|
c = pl->ptr[pl->pos];
|
|
|
|
|
|
|
|
if (c == '\n')
|
|
|
|
break;
|
|
|
|
pl->pos++;
|
|
|
|
}
|
|
|
|
if (pl->pos >= pl->end) {
|
2021-01-23 09:56:15 +00:00
|
|
|
pl->error = PL_NewString ("Reached end of string in comment");
|
2001-02-19 21:15:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (pl->ptr[pl->pos + 1] == '*') { // "/*" comments
|
|
|
|
pl->pos += 2;
|
|
|
|
|
|
|
|
while (pl->pos < pl->end) {
|
|
|
|
c = pl->ptr[pl->pos];
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
if (c == '\n') {
|
|
|
|
pl->line++;
|
|
|
|
} else if (c == '*' && pl->pos < pl->end - 1
|
|
|
|
&& pl->ptr[pl->pos+1] == '/') {
|
|
|
|
pl->pos++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pl->pos++;
|
|
|
|
}
|
|
|
|
if (pl->pos >= pl->end) {
|
2021-01-23 09:56:15 +00:00
|
|
|
pl->error = PL_NewString ("Reached end of string in comment");
|
2001-02-19 21:15:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (c == '\n') {
|
|
|
|
pl->line++;
|
|
|
|
}
|
|
|
|
pl->pos++;
|
|
|
|
}
|
2021-01-23 09:56:15 +00:00
|
|
|
pl->error = PL_NewString ("Reached end of string");
|
2001-02-19 21:15:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-11-11 11:18:00 +00:00
|
|
|
static inline byte
|
2007-05-13 03:27:54 +00:00
|
|
|
from_hex (byte a)
|
2004-11-11 11:18:00 +00:00
|
|
|
{
|
|
|
|
if (a >= '0' && a <= '9')
|
|
|
|
return a - '0';
|
|
|
|
if (a >= 'A' && a <= 'F')
|
|
|
|
return a - 'A' + 10;
|
|
|
|
return a - 'a' + 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline byte
|
|
|
|
make_byte (byte h, byte l)
|
|
|
|
{
|
2007-05-13 03:27:54 +00:00
|
|
|
return (from_hex (h) << 4) | from_hex (l);
|
2004-11-11 11:18:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
PL_ParseData (pldata_t *pl, int *len)
|
|
|
|
{
|
2012-04-17 12:26:33 +00:00
|
|
|
unsigned start = ++pl->pos;
|
|
|
|
int nibbles = 0, i;
|
|
|
|
char *str, c;
|
2004-11-11 11:18:00 +00:00
|
|
|
|
|
|
|
while (pl->pos < pl->end) {
|
2012-04-17 12:26:33 +00:00
|
|
|
c = pl->ptr[pl->pos++];
|
2012-08-18 02:02:52 +00:00
|
|
|
if (isxdigit ((byte) c)) {
|
2004-11-11 11:18:00 +00:00
|
|
|
nibbles++;
|
|
|
|
continue;
|
|
|
|
}
|
2012-04-17 12:26:33 +00:00
|
|
|
if (c == '>') {
|
2004-11-11 11:18:00 +00:00
|
|
|
if (nibbles & 1) {
|
2021-01-23 09:56:15 +00:00
|
|
|
pl->error = PL_NewString ("invalid data, missing nibble");
|
2004-11-11 11:18:00 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
*len = nibbles / 2;
|
|
|
|
str = malloc (*len);
|
|
|
|
for (i = 0; i < *len; i++)
|
|
|
|
str[i] = make_byte (pl->ptr[start + i * 2],
|
|
|
|
pl->ptr[start + i * 2 + 1]);
|
|
|
|
return str;
|
|
|
|
}
|
2021-01-23 09:56:15 +00:00
|
|
|
pl->error = PL_NewString (va ("invalid character in data: %02x", c));
|
2004-11-11 11:18:00 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2021-01-23 09:56:15 +00:00
|
|
|
pl->error = PL_NewString ("Reached end of string while parsing data");
|
2004-11-11 11:18:00 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
static char *
|
|
|
|
PL_ParseQuotedString (pldata_t *pl)
|
|
|
|
{
|
|
|
|
unsigned int start = ++pl->pos;
|
|
|
|
unsigned int escaped = 0;
|
|
|
|
unsigned int shrink = 0;
|
|
|
|
qboolean hex = false;
|
2007-10-13 07:55:58 +00:00
|
|
|
qboolean long_string = false;
|
2001-02-19 21:15:25 +00:00
|
|
|
char *str;
|
|
|
|
|
2007-10-13 07:55:58 +00:00
|
|
|
if (pl->ptr[pl->pos] == '"' &&
|
|
|
|
pl->ptr[pl->pos + 1] == '"') {
|
|
|
|
long_string = true;
|
|
|
|
start += 2;
|
|
|
|
pl->pos += 2;
|
|
|
|
}
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
while (pl->pos < pl->end) {
|
|
|
|
|
|
|
|
char c = pl->ptr[pl->pos];
|
|
|
|
|
|
|
|
if (escaped) {
|
2013-01-08 07:54:23 +00:00
|
|
|
if (escaped == 1 && inrange (c, '0', '7')) {
|
2001-02-19 21:15:25 +00:00
|
|
|
escaped++;
|
|
|
|
hex = false;
|
|
|
|
} else if (escaped > 1) {
|
|
|
|
if (escaped == 2 && c == 'x') {
|
|
|
|
hex = true;
|
|
|
|
shrink++;
|
|
|
|
escaped++;
|
2013-01-08 07:54:23 +00:00
|
|
|
} else if (hex && inrange (escaped, 3, 4)
|
|
|
|
&& isxdigit ((byte) c)) {
|
2001-02-19 21:15:25 +00:00
|
|
|
shrink++;
|
|
|
|
escaped++;
|
2013-01-08 07:54:23 +00:00
|
|
|
} else if (inrange (escaped, 1, 3) && inrange (c, '0', '7')) {
|
2001-02-19 21:15:25 +00:00
|
|
|
shrink++;
|
|
|
|
escaped++;
|
|
|
|
} else {
|
|
|
|
pl->pos--;
|
|
|
|
escaped = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
escaped = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (c == '\\') {
|
|
|
|
escaped = 1;
|
|
|
|
shrink++;
|
2007-10-13 07:55:58 +00:00
|
|
|
} else if (c == '"'
|
|
|
|
&& (!long_string || (pl->ptr[pl->pos + 1] == '"'
|
2011-08-20 07:30:43 +00:00
|
|
|
&& pl->ptr[pl->pos + 2] == '"'))) {
|
2001-02-19 21:15:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c == '\n') {
|
|
|
|
pl->line++;
|
|
|
|
}
|
|
|
|
|
|
|
|
pl->pos++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pl->pos >= pl->end) {
|
2021-01-23 09:56:15 +00:00
|
|
|
pl->error = PL_NewString ("Reached end of string while parsing quoted string");
|
2001-02-19 21:15:25 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2001-06-19 02:26:19 +00:00
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
if (pl->pos - start - shrink == 0) {
|
2003-01-06 18:28:13 +00:00
|
|
|
str = strdup ("");
|
2001-02-19 21:15:25 +00:00
|
|
|
} else {
|
2007-03-22 23:20:57 +00:00
|
|
|
char *chars = alloca(pl->pos - start - shrink);
|
2001-02-19 21:15:25 +00:00
|
|
|
unsigned int j;
|
|
|
|
unsigned int k;
|
|
|
|
|
|
|
|
escaped = 0;
|
|
|
|
hex = false;
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
for (j = start, k = 0; j < pl->pos; j++) {
|
|
|
|
|
|
|
|
char c = pl->ptr[j];
|
|
|
|
|
|
|
|
if (escaped) {
|
2013-01-08 07:54:23 +00:00
|
|
|
if (escaped == 1 && inrange (c, '0', '7')) {
|
|
|
|
chars[k] = c - '0';
|
2001-02-19 21:15:25 +00:00
|
|
|
hex = false;
|
|
|
|
escaped++;
|
|
|
|
} else if (escaped > 1) {
|
|
|
|
if (escaped == 2 && c == 'x') {
|
|
|
|
hex = true;
|
|
|
|
escaped++;
|
2013-01-08 07:54:23 +00:00
|
|
|
} else if (hex && inrange (escaped, 3, 4)
|
|
|
|
&& isxdigit ((byte) c)) {
|
2001-02-19 21:15:25 +00:00
|
|
|
chars[k] <<= 4;
|
|
|
|
chars[k] |= char2num (c);
|
|
|
|
escaped++;
|
2013-01-08 07:54:23 +00:00
|
|
|
} else if (inrange (escaped, 1, 3)
|
|
|
|
&& inrange (c, '0', '7')) {
|
2001-02-19 21:15:25 +00:00
|
|
|
chars[k] <<= 3;
|
|
|
|
chars[k] |= (c - '0');
|
|
|
|
escaped++;
|
|
|
|
} else {
|
|
|
|
escaped = 0;
|
|
|
|
j--;
|
|
|
|
k++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
escaped = 0;
|
|
|
|
switch (c) {
|
|
|
|
case 'a':
|
|
|
|
chars[k] = '\a';
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
chars[k] = '\b';
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
chars[k] = '\t';
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
chars[k] = '\r';
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
chars[k] = '\n';
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
chars[k] = '\v';
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
chars[k] = '\f';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
chars[k] = c;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
k++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
chars[k] = c;
|
|
|
|
if (c == '\\') {
|
|
|
|
escaped = 1;
|
|
|
|
} else {
|
|
|
|
k++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-08-22 22:03:16 +00:00
|
|
|
str = strncat (calloc ((pl->pos - start - shrink) + 1, 1), chars,
|
|
|
|
pl->pos - start - shrink);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
2007-10-13 07:55:58 +00:00
|
|
|
if (long_string)
|
|
|
|
pl->pos += 2;
|
2001-02-19 21:15:25 +00:00
|
|
|
pl->pos++;
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
PL_ParseUnquotedString (pldata_t *pl)
|
|
|
|
{
|
|
|
|
unsigned int start = pl->pos;
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
while (pl->pos < pl->end) {
|
2004-11-11 11:18:00 +00:00
|
|
|
if (is_quotable (pl->ptr[pl->pos]))
|
2001-02-19 21:15:25 +00:00
|
|
|
break;
|
|
|
|
pl->pos++;
|
|
|
|
}
|
2001-08-22 22:03:16 +00:00
|
|
|
str = strncat (calloc ((pl->pos - start) + 1, 1), &pl->ptr[start],
|
|
|
|
pl->pos - start);
|
2001-02-19 21:15:25 +00:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
static plitem_t *
|
|
|
|
PL_ParsePropertyListItem (pldata_t *pl)
|
|
|
|
{
|
|
|
|
plitem_t *item = NULL;
|
|
|
|
|
|
|
|
if (!PL_SkipSpace (pl))
|
|
|
|
return NULL;
|
2001-06-19 02:26:19 +00:00
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
switch (pl->ptr[pl->pos]) {
|
2001-08-22 22:03:16 +00:00
|
|
|
case '{':
|
|
|
|
{
|
2003-03-18 19:15:31 +00:00
|
|
|
item = PL_NewDictionary ();
|
2020-07-01 12:47:09 +00:00
|
|
|
item->line = pl->line;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
pl->pos++;
|
2001-06-19 02:26:19 +00:00
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
while (PL_SkipSpace (pl) && pl->ptr[pl->pos] != '}') {
|
|
|
|
plitem_t *key;
|
|
|
|
plitem_t *value;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2003-03-18 19:15:31 +00:00
|
|
|
if (!(key = PL_ParsePropertyListItem (pl))) {
|
2004-01-06 11:09:40 +00:00
|
|
|
PL_Free (item);
|
2001-08-22 22:03:16 +00:00
|
|
|
return NULL;
|
2003-03-18 19:15:31 +00:00
|
|
|
}
|
2001-06-19 02:10:47 +00:00
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
if (!(PL_SkipSpace (pl))) {
|
2004-01-06 11:09:40 +00:00
|
|
|
PL_Free (key);
|
|
|
|
PL_Free (item);
|
2001-08-22 22:03:16 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2001-06-19 02:10:47 +00:00
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
if (key->type != QFString) {
|
2021-01-23 09:56:15 +00:00
|
|
|
pl->error = PL_NewString ("Key is not a string");
|
2004-01-06 11:09:40 +00:00
|
|
|
PL_Free (key);
|
|
|
|
PL_Free (item);
|
2001-08-22 22:03:16 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2001-06-19 02:10:47 +00:00
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
if (pl->ptr[pl->pos] != '=') {
|
2021-01-23 09:56:15 +00:00
|
|
|
pl->error = PL_NewString (va ("Unexpected character %c (expected '=')", pl->ptr[pl->pos]));
|
2004-01-06 11:09:40 +00:00
|
|
|
PL_Free (key);
|
|
|
|
PL_Free (item);
|
2001-08-22 22:03:16 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
pl->pos++;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2012-05-21 23:23:22 +00:00
|
|
|
// If there is no value, lose the key
|
2001-08-22 22:03:16 +00:00
|
|
|
if (!(value = PL_ParsePropertyListItem (pl))) {
|
2004-01-06 11:09:40 +00:00
|
|
|
PL_Free (key);
|
|
|
|
PL_Free (item);
|
2001-08-22 22:03:16 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2001-06-19 02:10:47 +00:00
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
if (!(PL_SkipSpace (pl))) {
|
2004-01-06 11:09:40 +00:00
|
|
|
PL_Free (key);
|
|
|
|
PL_Free (value);
|
|
|
|
PL_Free (item);
|
2001-08-22 22:03:16 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2001-06-19 02:10:47 +00:00
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
if (pl->ptr[pl->pos] == ';') {
|
|
|
|
pl->pos++;
|
|
|
|
} else if (pl->ptr[pl->pos] != '}') {
|
2021-01-23 09:56:15 +00:00
|
|
|
pl->error = PL_NewString (va ("Unexpected character %c (wanted ';' or '}')", pl->ptr[pl->pos]));
|
2004-01-06 11:09:40 +00:00
|
|
|
PL_Free (key);
|
|
|
|
PL_Free (value);
|
|
|
|
PL_Free (item);
|
2001-08-22 22:03:16 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2003-03-18 19:15:31 +00:00
|
|
|
// Add the key/value pair to the dictionary
|
2006-12-09 06:00:36 +00:00
|
|
|
if (!PL_D_AddObject (item, PL_String (key), value)) {
|
2004-01-06 11:09:40 +00:00
|
|
|
PL_Free (key);
|
|
|
|
PL_Free (value);
|
|
|
|
PL_Free (item);
|
2003-03-18 19:15:31 +00:00
|
|
|
return NULL;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
2004-01-06 11:09:40 +00:00
|
|
|
PL_Free (key);
|
2001-08-22 22:03:16 +00:00
|
|
|
}
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
if (pl->pos >= pl->end) { // Catch the error
|
2021-01-23 09:56:15 +00:00
|
|
|
pl->error = PL_NewString ("Unexpected end of string when parsing dictionary");
|
2004-01-06 11:09:40 +00:00
|
|
|
PL_Free (item);
|
2001-08-22 22:03:16 +00:00
|
|
|
return NULL;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
2001-08-22 22:03:16 +00:00
|
|
|
pl->pos++;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
return item;
|
|
|
|
}
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
case '(': {
|
2003-03-18 19:15:31 +00:00
|
|
|
item = PL_NewArray ();
|
2020-07-01 12:47:09 +00:00
|
|
|
item->line = pl->line;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
pl->pos++;
|
2001-06-19 02:26:19 +00:00
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
while (PL_SkipSpace (pl) && pl->ptr[pl->pos] != ')') {
|
|
|
|
plitem_t *value;
|
2001-06-19 02:26:19 +00:00
|
|
|
|
2003-03-18 19:15:31 +00:00
|
|
|
if (!(value = PL_ParsePropertyListItem (pl))) {
|
2004-01-06 11:09:40 +00:00
|
|
|
PL_Free (item);
|
2001-08-22 22:03:16 +00:00
|
|
|
return NULL;
|
2003-03-18 19:15:31 +00:00
|
|
|
}
|
2001-06-19 02:26:19 +00:00
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
if (!(PL_SkipSpace (pl))) {
|
2004-01-06 11:09:40 +00:00
|
|
|
PL_Free (value);
|
|
|
|
PL_Free (item);
|
2001-08-22 22:03:16 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2001-06-19 02:26:19 +00:00
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
if (pl->ptr[pl->pos] == ',') {
|
|
|
|
pl->pos++;
|
|
|
|
} else if (pl->ptr[pl->pos] != ')') {
|
2021-01-23 09:56:15 +00:00
|
|
|
pl->error = PL_NewString (va ("Unexpected character %c (wanted ',' or ')')", pl->ptr[pl->pos]));
|
2004-01-06 11:09:40 +00:00
|
|
|
PL_Free (value);
|
|
|
|
PL_Free (item);
|
2001-08-22 22:03:16 +00:00
|
|
|
return NULL;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
2001-06-19 02:26:19 +00:00
|
|
|
|
2003-03-18 19:15:31 +00:00
|
|
|
if (!PL_A_AddObject (item, value)) {
|
2021-01-23 09:56:15 +00:00
|
|
|
pl->error = PL_NewString ("Unexpected character (too many items in array)");
|
2004-01-06 11:09:40 +00:00
|
|
|
PL_Free (value);
|
|
|
|
PL_Free (item);
|
2001-08-22 22:03:16 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
2001-08-22 22:03:16 +00:00
|
|
|
pl->pos++;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2004-11-11 11:18:00 +00:00
|
|
|
case '<': {
|
|
|
|
int len;
|
|
|
|
char *str = PL_ParseData (pl, &len);
|
|
|
|
|
|
|
|
if (!str) {
|
|
|
|
return NULL;
|
|
|
|
} else {
|
2020-07-01 12:47:09 +00:00
|
|
|
item = PL_NewData (str, len);
|
|
|
|
item->line = pl->line;
|
|
|
|
return item;
|
2004-11-11 11:18:00 +00:00
|
|
|
}
|
|
|
|
}
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
case '"': {
|
2020-07-01 12:47:09 +00:00
|
|
|
int line = pl->line;
|
2001-08-22 22:03:16 +00:00
|
|
|
char *str = PL_ParseQuotedString (pl);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
if (!str) {
|
|
|
|
return NULL;
|
|
|
|
} else {
|
2020-07-01 12:47:09 +00:00
|
|
|
return new_string (str, line);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
2001-08-22 22:03:16 +00:00
|
|
|
}
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
default: {
|
2020-07-01 12:47:09 +00:00
|
|
|
int line = pl->line;
|
2001-08-22 22:03:16 +00:00
|
|
|
char *str = PL_ParseUnquotedString (pl);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
if (!str) {
|
|
|
|
return NULL;
|
|
|
|
} else {
|
2020-07-01 12:47:09 +00:00
|
|
|
return new_string (str, line);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
2001-08-22 22:03:16 +00:00
|
|
|
}
|
2001-02-19 21:15:25 +00:00
|
|
|
} // switch
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE plitem_t *
|
2001-02-19 21:15:25 +00:00
|
|
|
PL_GetPropertyList (const char *string)
|
|
|
|
{
|
|
|
|
pldata_t *pl = calloc (1, sizeof (pldata_t));
|
2001-08-01 07:03:46 +00:00
|
|
|
plitem_t *newpl = NULL;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2004-11-11 11:18:00 +00:00
|
|
|
if (!quotable_bitmap[0])
|
|
|
|
init_quotables ();
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
pl->ptr = string;
|
|
|
|
pl->pos = 0;
|
|
|
|
pl->end = strlen (string);
|
|
|
|
pl->error = NULL;
|
|
|
|
pl->line = 1;
|
|
|
|
|
2001-08-01 07:05:28 +00:00
|
|
|
if ((newpl = PL_ParsePropertyListItem (pl))) {
|
2001-08-01 07:03:46 +00:00
|
|
|
free (pl);
|
|
|
|
return newpl;
|
|
|
|
} else {
|
2021-01-23 09:56:15 +00:00
|
|
|
if (pl && pl->error) {
|
|
|
|
const char *error = PL_String (pl->error);
|
|
|
|
if (error[0]) {
|
|
|
|
Sys_Printf ("plist: %d,%d: %s", pl->line, pl->pos, error);
|
|
|
|
}
|
|
|
|
PL_Free (pl->error);
|
|
|
|
}
|
2001-08-01 07:03:46 +00:00
|
|
|
free (pl);
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
2004-11-11 11:18:00 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
write_tabs (dstring_t *dstr, int num)
|
|
|
|
{
|
2007-05-13 03:13:01 +00:00
|
|
|
char *tabs = dstring_reservestr (dstr, num);
|
2004-11-11 11:18:00 +00:00
|
|
|
|
2007-05-13 03:13:01 +00:00
|
|
|
memset (tabs, '\t', num);
|
|
|
|
tabs[num] = 0;
|
2004-11-11 11:18:00 +00:00
|
|
|
}
|
|
|
|
|
2007-05-13 03:27:54 +00:00
|
|
|
static void
|
|
|
|
write_string_len (dstring_t *dstr, const char *str, int len)
|
|
|
|
{
|
|
|
|
char *dst = dstring_reservestr (dstr, len);
|
|
|
|
memcpy (dst, str, len);
|
|
|
|
dst[len] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char
|
|
|
|
to_hex (byte b)
|
|
|
|
{
|
|
|
|
char c = (b & 0xf) + '0';
|
|
|
|
if (c > '9')
|
|
|
|
c = c - '0' + 'A';
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
write_binary (dstring_t *dstr, byte *binary, int len)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char *dst = dstring_reservestr (dstr, len * 2);
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
*dst++ = to_hex (binary[i] >> 4);
|
|
|
|
*dst++ = to_hex (binary[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-11-11 11:18:00 +00:00
|
|
|
static void
|
|
|
|
write_string (dstring_t *dstr, const char *str)
|
|
|
|
{
|
|
|
|
const char *s;
|
2007-05-13 03:13:01 +00:00
|
|
|
int len = 0;
|
|
|
|
char *dst;
|
|
|
|
int quoted = 0;
|
|
|
|
|
|
|
|
for (s = str; *s; s++) {
|
|
|
|
if (is_quotable (*s))
|
|
|
|
quoted = 1;
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
if (!quoted) {
|
|
|
|
dst = dstring_reservestr (dstr, len);
|
|
|
|
strcpy (dst, str);
|
2004-11-11 11:18:00 +00:00
|
|
|
return;
|
|
|
|
}
|
2007-05-13 03:13:01 +00:00
|
|
|
// assume worst case of all octal chars plus two quotes.
|
2013-01-08 09:10:27 +00:00
|
|
|
dst = dstring_reservestr (dstr, len * 4 + 2);
|
2007-05-13 03:13:01 +00:00
|
|
|
*dst++= '\"';
|
2004-11-11 11:18:00 +00:00
|
|
|
while (*str) {
|
2007-05-13 03:13:01 +00:00
|
|
|
if (*str && isascii ((byte) *str) && isprint ((byte) *str)
|
|
|
|
&& *str != '\\' && *str != '\"') {
|
|
|
|
*dst++ = *str++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*str) {
|
2007-10-13 07:14:47 +00:00
|
|
|
*dst++ = '\\';
|
2007-05-13 03:13:01 +00:00
|
|
|
switch (*str) {
|
2004-11-11 11:18:00 +00:00
|
|
|
case '\"':
|
|
|
|
case '\\':
|
2007-05-13 03:13:01 +00:00
|
|
|
*dst++ = *str;
|
2004-11-11 11:18:00 +00:00
|
|
|
break;
|
|
|
|
case '\n':
|
2007-05-13 03:13:01 +00:00
|
|
|
*dst++ = 'n';
|
2004-11-11 11:18:00 +00:00
|
|
|
break;
|
|
|
|
case '\a':
|
2007-05-13 03:13:01 +00:00
|
|
|
*dst++ = 'a';
|
2004-11-11 11:18:00 +00:00
|
|
|
break;
|
|
|
|
case '\b':
|
2007-05-13 03:13:01 +00:00
|
|
|
*dst++ = 'b';
|
2004-11-11 11:18:00 +00:00
|
|
|
break;
|
|
|
|
case '\f':
|
2007-05-13 03:13:01 +00:00
|
|
|
*dst++ = 'f';
|
2004-11-11 11:18:00 +00:00
|
|
|
break;
|
|
|
|
case '\r':
|
2007-05-13 03:13:01 +00:00
|
|
|
*dst++ = 'r';
|
2004-11-11 11:18:00 +00:00
|
|
|
break;
|
|
|
|
case '\t':
|
2007-05-13 03:13:01 +00:00
|
|
|
*dst++ = 't';
|
2004-11-11 11:18:00 +00:00
|
|
|
break;
|
|
|
|
case '\v':
|
2007-05-13 03:13:01 +00:00
|
|
|
*dst++ = 'v';
|
2004-11-11 11:18:00 +00:00
|
|
|
break;
|
|
|
|
default:
|
2007-05-13 03:13:01 +00:00
|
|
|
*dst++ = '0' + ((((byte) *str) >> 6) & 3);
|
|
|
|
*dst++ = '0' + ((((byte) *str) >> 3) & 7);
|
|
|
|
*dst++ = '0' + (((byte) *str) & 7);
|
2004-11-11 11:18:00 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-05-13 03:13:01 +00:00
|
|
|
str++;
|
2004-11-11 11:18:00 +00:00
|
|
|
}
|
|
|
|
}
|
2007-05-13 03:13:01 +00:00
|
|
|
*dst++ = '\"';
|
|
|
|
*dst++ = 0;
|
|
|
|
dstr->size = dst - dstr->str;
|
2004-11-11 11:18:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
write_item (dstring_t *dstr, plitem_t *item, int level)
|
|
|
|
{
|
|
|
|
void **list, **l;
|
|
|
|
dictkey_t *current;
|
|
|
|
plarray_t *array;
|
|
|
|
plbinary_t *binary;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
switch (item->type) {
|
|
|
|
case QFDictionary:
|
2007-05-13 03:27:54 +00:00
|
|
|
write_string_len (dstr, "{\n", 2);
|
2004-11-11 11:18:00 +00:00
|
|
|
l = list = Hash_GetList ((hashtab_t *) item->data);
|
|
|
|
while ((current = (dictkey_t *) *l++)) {
|
|
|
|
write_tabs (dstr, level + 1);
|
|
|
|
write_string (dstr, current->key);
|
2007-05-13 03:27:54 +00:00
|
|
|
write_string_len (dstr, " = ", 3);
|
2004-11-11 11:18:00 +00:00
|
|
|
write_item (dstr, current->value, level + 1);
|
2007-05-13 03:27:54 +00:00
|
|
|
write_string_len (dstr, ";\n", 2);
|
2004-11-11 11:18:00 +00:00
|
|
|
}
|
|
|
|
free (list);
|
|
|
|
write_tabs (dstr, level);
|
2007-05-13 03:27:54 +00:00
|
|
|
write_string_len (dstr, "}", 1);
|
2004-11-11 11:18:00 +00:00
|
|
|
break;
|
|
|
|
case QFArray:
|
2007-05-13 03:27:54 +00:00
|
|
|
write_string_len (dstr, "(\n", 2);
|
2004-11-11 11:18:00 +00:00
|
|
|
array = (plarray_t *) item->data;
|
|
|
|
for (i = 0; i < array->numvals; i++) {
|
|
|
|
write_tabs (dstr, level + 1);
|
|
|
|
write_item (dstr, array->values[i], level + 1);
|
|
|
|
if (i < array->numvals - 1)
|
2007-05-13 03:27:54 +00:00
|
|
|
write_string_len (dstr, ",\n", 2);
|
2004-11-11 11:18:00 +00:00
|
|
|
}
|
2007-05-13 03:27:54 +00:00
|
|
|
write_string_len (dstr, "\n", 1);
|
2004-11-11 11:18:00 +00:00
|
|
|
write_tabs (dstr, level);
|
2007-05-13 03:27:54 +00:00
|
|
|
write_string_len (dstr, ")", 1);
|
2004-11-11 11:18:00 +00:00
|
|
|
break;
|
|
|
|
case QFBinary:
|
2007-05-13 03:27:54 +00:00
|
|
|
write_string_len (dstr, "<", 1);
|
2004-11-11 11:18:00 +00:00
|
|
|
binary = (plbinary_t *) item->data;
|
2007-05-13 03:27:54 +00:00
|
|
|
write_binary (dstr, binary->data, binary->size);
|
|
|
|
write_string_len (dstr, ">", 1);
|
2004-11-11 11:18:00 +00:00
|
|
|
break;
|
|
|
|
case QFString:
|
|
|
|
write_string (dstr, item->data);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE char *
|
2004-11-11 11:18:00 +00:00
|
|
|
PL_WritePropertyList (plitem_t *pl)
|
|
|
|
{
|
|
|
|
dstring_t *dstr = dstring_newstr ();
|
|
|
|
|
|
|
|
if (!quotable_bitmap[0])
|
|
|
|
init_quotables ();
|
|
|
|
write_item (dstr, pl, 0);
|
2007-05-13 03:27:54 +00:00
|
|
|
write_string_len (dstr, "\n", 1);
|
2004-11-11 11:18:00 +00:00
|
|
|
return dstring_freeze (dstr);
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE pltype_t
|
2020-07-16 15:22:47 +00:00
|
|
|
PL_Type (const plitem_t *item)
|
2004-11-11 11:18:00 +00:00
|
|
|
{
|
|
|
|
return item->type;
|
|
|
|
}
|
2020-07-01 12:47:09 +00:00
|
|
|
|
|
|
|
VISIBLE int
|
2020-07-16 15:22:47 +00:00
|
|
|
PL_Line (const plitem_t *item)
|
2020-07-01 12:47:09 +00:00
|
|
|
{
|
|
|
|
return item->line;
|
|
|
|
}
|
2020-07-02 05:20:46 +00:00
|
|
|
|
2020-07-16 13:04:33 +00:00
|
|
|
VISIBLE void
|
|
|
|
PL_Message (plitem_t *messages, const plitem_t *item, const char *fmt, ...)
|
2020-07-02 05:20:46 +00:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
dstring_t *string;
|
|
|
|
|
|
|
|
string = dstring_new ();
|
|
|
|
|
|
|
|
va_start (args, fmt);
|
|
|
|
dvsprintf (string, fmt, args);
|
|
|
|
va_end (args);
|
|
|
|
|
|
|
|
if (item) {
|
|
|
|
PL_A_AddObject (messages,
|
|
|
|
PL_NewString (va ("%d: %s", item->line, string->str)));
|
|
|
|
} else {
|
|
|
|
PL_A_AddObject (messages,
|
|
|
|
PL_NewString (va ("internal: %s", string->str)));
|
|
|
|
}
|
|
|
|
dstring_delete (string);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
pl_default_parser (const plfield_t *field, const plitem_t *item, void *data,
|
2020-12-21 04:55:54 +00:00
|
|
|
plitem_t *messages, void *context)
|
2020-07-02 05:20:46 +00:00
|
|
|
{
|
|
|
|
switch (field->type) {
|
|
|
|
case QFDictionary:
|
|
|
|
{
|
|
|
|
*(hashtab_t **)data = (hashtab_t *)item->data;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
case QFArray:
|
|
|
|
{
|
|
|
|
plarray_t *array = (plarray_t *)item->data;
|
|
|
|
typedef struct DARRAY_TYPE (plitem_t *) arraydata_t;
|
|
|
|
arraydata_t *arraydata = DARRAY_ALLOCFIXED(arraydata_t,
|
|
|
|
array->numvals,
|
|
|
|
malloc);
|
|
|
|
memcpy (arraydata->a, array->values,
|
|
|
|
array->numvals * sizeof (arraydata->a[0]));
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
case QFBinary:
|
|
|
|
{
|
|
|
|
plbinary_t *binary = (plbinary_t *)item->data;
|
|
|
|
typedef struct DARRAY_TYPE (byte) bindata_t;
|
|
|
|
bindata_t *bindata = DARRAY_ALLOCFIXED(bindata_t,
|
|
|
|
binary->size, malloc);
|
|
|
|
memcpy (bindata->a, binary->data, binary->size);
|
|
|
|
*(bindata_t **)data = bindata;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
case QFString:
|
|
|
|
*(char **)data = (char *)item->data;
|
|
|
|
return 1;
|
2021-01-02 05:02:33 +00:00
|
|
|
case QFMultiType:
|
|
|
|
break;
|
2020-07-02 05:20:46 +00:00
|
|
|
}
|
2020-07-16 13:04:33 +00:00
|
|
|
PL_Message (messages, 0, "invalid item type: %d", field->type);
|
2020-07-02 05:20:46 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-01-05 14:39:52 +00:00
|
|
|
VISIBLE int
|
|
|
|
PL_CheckType (pltype_t field_type, pltype_t item_type)
|
2021-01-02 05:02:33 +00:00
|
|
|
{
|
|
|
|
if (field_type & QFMultiType) {
|
|
|
|
// field_type is a mask of allowed types
|
|
|
|
return field_type & (1 << item_type);
|
|
|
|
} else {
|
|
|
|
// exact match
|
|
|
|
return field_type == item_type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-05 14:39:52 +00:00
|
|
|
VISIBLE void
|
|
|
|
PL_TypeMismatch (plitem_t *messages, const plitem_t *item, const char *name,
|
|
|
|
pltype_t field_type, pltype_t item_type)
|
2021-01-02 05:02:33 +00:00
|
|
|
{
|
|
|
|
const int num_types = sizeof (pl_types) / sizeof (pl_types[0]);
|
|
|
|
if (field_type & QFMultiType) {
|
|
|
|
PL_Message (messages, item,
|
|
|
|
"error: %s is the wrong type. Got %s, expected on of:",
|
|
|
|
name, pl_types[item_type]);
|
|
|
|
field_type &= ~QFMultiType;
|
|
|
|
for (int type = 0; field_type && type < num_types;
|
|
|
|
type++, field_type >>= 1) {
|
|
|
|
if (field_type & 1) {
|
|
|
|
PL_Message (messages, item, " %s", pl_types[type]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
PL_Message (messages, item,
|
|
|
|
"error: %s is the wrong type. Got %s, expected %s", name,
|
|
|
|
pl_types[item_type], pl_types[field_type]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-02 05:20:46 +00:00
|
|
|
VISIBLE int
|
2020-12-24 04:33:37 +00:00
|
|
|
PL_ParseStruct (const plfield_t *fields, const plitem_t *dict, void *data,
|
|
|
|
plitem_t *messages, void *context)
|
2020-07-02 05:20:46 +00:00
|
|
|
{
|
|
|
|
void **list, **l;
|
|
|
|
dictkey_t *current;
|
2020-07-16 15:23:09 +00:00
|
|
|
int result = 1;
|
2020-07-14 03:05:34 +00:00
|
|
|
plparser_t parser;
|
2020-07-02 05:20:46 +00:00
|
|
|
|
|
|
|
if (dict->type != QFDictionary) {
|
2020-07-16 13:04:33 +00:00
|
|
|
PL_Message (messages, dict, "error: not a dictionary object");
|
2020-07-02 05:20:46 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(l = list = Hash_GetList ((hashtab_t *) dict->data))) {
|
|
|
|
// empty struct: leave as default
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((current = (dictkey_t *) *l++)) {
|
|
|
|
const plfield_t *f;
|
|
|
|
for (f = fields; f->name; f++) {
|
|
|
|
if (strcmp (f->name, current->key) == 0) {
|
|
|
|
plitem_t *item = current->value;
|
|
|
|
void *flddata = (byte *)data + f->offset;
|
|
|
|
|
|
|
|
if (f->parser) {
|
|
|
|
parser = f->parser;
|
|
|
|
} else {
|
|
|
|
parser = pl_default_parser;
|
|
|
|
}
|
2021-01-05 14:39:52 +00:00
|
|
|
if (!PL_CheckType (f->type, item->type)) {
|
|
|
|
PL_TypeMismatch (messages, item, current->key,
|
|
|
|
f->type, item->type);
|
2020-07-02 05:20:46 +00:00
|
|
|
result = 0;
|
|
|
|
} else {
|
2020-12-21 04:55:54 +00:00
|
|
|
if (!parser (f, item, flddata, messages, context)) {
|
2020-07-02 05:20:46 +00:00
|
|
|
result = 0;
|
|
|
|
}
|
|
|
|
}
|
2020-07-16 12:12:05 +00:00
|
|
|
break;
|
2020-07-02 05:20:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!f->name) {
|
2020-07-16 13:04:33 +00:00
|
|
|
PL_Message (messages, dict, "error: unknown field %s",
|
2020-07-02 05:20:46 +00:00
|
|
|
current->key);
|
|
|
|
result = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free (list);
|
|
|
|
return result;
|
|
|
|
}
|
2020-07-14 03:05:34 +00:00
|
|
|
|
|
|
|
VISIBLE int
|
|
|
|
PL_ParseArray (const plfield_t *field, const plitem_t *array, void *data,
|
2020-12-21 04:55:54 +00:00
|
|
|
plitem_t *messages, void *context)
|
2020-07-14 03:05:34 +00:00
|
|
|
{
|
2020-07-16 15:23:09 +00:00
|
|
|
int result = 1;
|
2020-07-14 03:05:34 +00:00
|
|
|
plparser_t parser;
|
|
|
|
plarray_t *plarray = (plarray_t *) array->data;
|
|
|
|
plelement_t *element = (plelement_t *) field->data;
|
|
|
|
typedef struct arr_s DARRAY_TYPE(byte) arr_t;
|
|
|
|
arr_t *arr;
|
|
|
|
plfield_t f = { 0, 0, element->type, element->parser, element->data };
|
|
|
|
|
|
|
|
if (array->type != QFArray) {
|
2020-07-16 13:04:33 +00:00
|
|
|
PL_Message (messages, array, "error: not an array object");
|
2020-07-14 03:05:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2020-07-16 12:12:05 +00:00
|
|
|
if (f.parser) {
|
|
|
|
parser = f.parser;
|
2020-07-14 03:05:34 +00:00
|
|
|
} else {
|
|
|
|
parser = pl_default_parser;
|
|
|
|
}
|
|
|
|
|
|
|
|
arr = DARRAY_ALLOCFIXED (arr_t, plarray->numvals * element->stride,
|
|
|
|
element->alloc);
|
2020-07-16 13:05:15 +00:00
|
|
|
memset (arr->a, 0, arr->size);
|
|
|
|
// the array is allocated using bytes, but need the actual number of
|
|
|
|
// elements in the array
|
|
|
|
arr->size = arr->maxSize = plarray->numvals;
|
2020-07-14 03:05:34 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < plarray->numvals; i++) {
|
|
|
|
plitem_t *item = plarray->values[i];
|
|
|
|
void *eledata = &arr->a[i * element->stride];
|
|
|
|
|
2021-01-05 14:39:52 +00:00
|
|
|
if (!PL_CheckType (element->type, item->type)) {
|
2021-01-02 05:02:33 +00:00
|
|
|
char index[16];
|
|
|
|
snprintf (index, sizeof(index) - 1, "%d", i);
|
|
|
|
index[15] = 0;
|
2021-01-05 14:39:52 +00:00
|
|
|
PL_TypeMismatch (messages, item, index, element->type, item->type);
|
2020-07-14 03:05:34 +00:00
|
|
|
result = 0;
|
|
|
|
} else {
|
2020-12-21 04:55:54 +00:00
|
|
|
if (!parser (&f, item, eledata, messages, context)) {
|
2020-07-14 03:05:34 +00:00
|
|
|
result = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*(arr_t **) data = arr;
|
|
|
|
return result;
|
|
|
|
}
|
2020-12-24 06:59:33 +00:00
|
|
|
|
|
|
|
VISIBLE int
|
|
|
|
PL_ParseSymtab (const plfield_t *field, const plitem_t *dict, void *data,
|
|
|
|
plitem_t *messages, void *context)
|
|
|
|
{
|
|
|
|
void **list, **l;
|
|
|
|
dictkey_t *current;
|
|
|
|
int result = 1;
|
|
|
|
plparser_t parser;
|
|
|
|
__auto_type tab = (hashtab_t *) data;
|
|
|
|
|
|
|
|
plelement_t *element = (plelement_t *) field->data;
|
|
|
|
plfield_t f = { 0, 0, element->type, element->parser, element->data };
|
|
|
|
|
|
|
|
if (dict->type != QFDictionary) {
|
|
|
|
PL_Message (messages, dict, "error: not a dictionary object");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (f.parser) {
|
|
|
|
parser = f.parser;
|
|
|
|
} else {
|
|
|
|
PL_Message (messages, dict, "no parser set");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(l = list = Hash_GetList ((hashtab_t *) dict->data))) {
|
|
|
|
// empty struct: leave as default
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *obj = element->alloc (element->stride);
|
2020-12-24 15:28:26 +00:00
|
|
|
memset (obj, 0, element->stride);
|
2020-12-24 06:59:33 +00:00
|
|
|
while ((current = (dictkey_t *) *l++)) {
|
|
|
|
const char *key = current->key;
|
|
|
|
plitem_t *item = current->value;
|
|
|
|
|
2021-01-05 14:39:52 +00:00
|
|
|
if (!PL_CheckType (element->type, item->type)) {
|
|
|
|
PL_TypeMismatch (messages, item, key, element->type, item->type);
|
2020-12-24 06:59:33 +00:00
|
|
|
result = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
f.name = key;
|
|
|
|
if (Hash_Find (tab, key)) {
|
|
|
|
PL_Message (messages, item, "duplicate name");
|
|
|
|
result = 0;
|
|
|
|
} else {
|
|
|
|
if (!parser (&f, item, obj, messages, context)) {
|
|
|
|
result = 0;
|
|
|
|
} else {
|
|
|
|
Hash_Add (tab, obj);
|
|
|
|
obj = element->alloc (element->stride);
|
2020-12-24 15:28:26 +00:00
|
|
|
memset (obj, 0, element->stride);
|
2020-12-24 06:59:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Hash_Free (tab, obj);
|
|
|
|
free (list);
|
|
|
|
return result;
|
|
|
|
}
|