change the dict and array add functions to return true/false and add some

more docs
This commit is contained in:
Bill Currie 2004-01-07 06:19:11 +00:00
parent e8fb5ba83b
commit 9c92a916ff
5 changed files with 44 additions and 27 deletions

View file

@ -155,9 +155,26 @@ plitem_t *PL_ObjectAtIndex (plitem_t *, int);
*/ */
plitem_t *PL_D_AllKeys (plitem_t *); plitem_t *PL_D_AllKeys (plitem_t *);
plitem_t *PL_D_AddObject (plitem_t *, plitem_t *, plitem_t *); //@{
plitem_t *PL_A_AddObject (plitem_t *, plitem_t *); /**
plitem_t *PL_A_InsertObjectAtIndex (plitem_t *, plitem_t *, int ind); \fn plitem_t *PL_D_AddObject (plitem_t *dict, plitem_t *key, plitem_t *value)
\fn plitem_t *PL_A_AddObject (plitem_t *array, plitem_t *item)
\fn plitem_t *PL_A_InsertObjectAtIndex (plitem_t *array, plitem_t *item, int ind)
\param dict The dictionary to add the key/value pair to
\param array The array to add the item to
\param item The item to be added to the array
\param key The key of the key/value pair to be added to the dictionary
\param value The value of the key/value pair to be added to the dictionary
\return true on success, false on failure
Note: the dictionary or array becomes the owner of the added item/pair.
*/
int PL_D_AddObject (plitem_t *, plitem_t *, plitem_t *);
int PL_A_AddObject (plitem_t *, plitem_t *);
int PL_A_InsertObjectAtIndex (plitem_t *, plitem_t *, int ind);
//@}
plitem_t *PL_NewDictionary (void); plitem_t *PL_NewDictionary (void);
plitem_t *PL_NewArray (void); plitem_t *PL_NewArray (void);

View file

@ -106,24 +106,24 @@ bi_PL_ObjectAtIndex (progs_t *pr)
static void static void
bi_PL_D_AddObject (progs_t *pr) bi_PL_D_AddObject (progs_t *pr)
{ {
return_plitem (pr, PL_D_AddObject (p_plitem (pr, 0), R_INT (pr) = PL_D_AddObject (p_plitem (pr, 0),
remove_plitem (pr, p_plitem (pr, 1)), remove_plitem (pr, p_plitem (pr, 1)),
remove_plitem (pr, p_plitem (pr, 2)))); remove_plitem (pr, p_plitem (pr, 2)));
} }
static void static void
bi_PL_A_AddObject (progs_t *pr) bi_PL_A_AddObject (progs_t *pr)
{ {
return_plitem (pr, PL_A_AddObject (p_plitem (pr, 0), R_INT (pr) = PL_A_AddObject (p_plitem (pr, 0),
remove_plitem (pr, p_plitem (pr, 1)))); remove_plitem (pr, p_plitem (pr, 1)));
} }
static void static void
bi_PL_A_InsertObjectAtIndex (progs_t *pr) bi_PL_A_InsertObjectAtIndex (progs_t *pr)
{ {
return_plitem (pr, PL_A_InsertObjectAtIndex (p_plitem (pr, 0), R_INT (pr) = PL_A_InsertObjectAtIndex (p_plitem (pr, 0),
remove_plitem (pr, p_plitem (pr, 1)), remove_plitem (pr, p_plitem (pr, 1)),
P_INT (pr, 2))); P_INT (pr, 2));
} }
static void static void

View file

@ -190,16 +190,16 @@ PL_ObjectAtIndex (plitem_t *item, int index)
return index >= 0 && index < array->numvals ? array->values[index] : NULL; return index >= 0 && index < array->numvals ? array->values[index] : NULL;
} }
plitem_t * int
PL_D_AddObject (plitem_t *dict, plitem_t *key, plitem_t *value) PL_D_AddObject (plitem_t *dict, plitem_t *key, plitem_t *value)
{ {
dictkey_t *k; dictkey_t *k;
if (dict->type != QFDictionary) if (dict->type != QFDictionary)
return NULL; return 0;
if (key->type != QFString) if (key->type != QFString)
return NULL; return 0;
if ((k = Hash_Find ((hashtab_t *)dict->data, key->data))) { if ((k = Hash_Find ((hashtab_t *)dict->data, key->data))) {
PL_Free ((plitem_t *) k->value); PL_Free ((plitem_t *) k->value);
@ -208,23 +208,23 @@ PL_D_AddObject (plitem_t *dict, plitem_t *key, plitem_t *value)
k = malloc (sizeof (dictkey_t)); k = malloc (sizeof (dictkey_t));
if (!k) if (!k)
return NULL; return 0;
k->key = strdup ((char *) key->data); k->key = strdup ((char *) key->data);
k->value = value; k->value = value;
Hash_Add ((hashtab_t *)dict->data, k); Hash_Add ((hashtab_t *)dict->data, k);
} }
return dict; return 1;
} }
plitem_t * int
PL_A_InsertObjectAtIndex (plitem_t *array_item, plitem_t *item, int index) PL_A_InsertObjectAtIndex (plitem_t *array_item, plitem_t *item, int index)
{ {
plarray_t *array; plarray_t *array;
if (array_item->type != QFArray) if (array_item->type != QFArray)
return NULL; return 0;
array = (plarray_t *)array_item->data; array = (plarray_t *)array_item->data;
@ -233,7 +233,7 @@ PL_A_InsertObjectAtIndex (plitem_t *array_item, plitem_t *item, int index)
plitem_t **tmp = realloc (array->values, size); plitem_t **tmp = realloc (array->values, size);
if (!tmp) if (!tmp)
return NULL; return 0;
array->maxvals += 128; array->maxvals += 128;
array->values = tmp; array->values = tmp;
memset (array->values + array->numvals, 0, memset (array->values + array->numvals, 0,
@ -244,16 +244,16 @@ PL_A_InsertObjectAtIndex (plitem_t *array_item, plitem_t *item, int index)
index = array->numvals; index = array->numvals;
if (index < 0 || index > array->numvals) if (index < 0 || index > array->numvals)
return NULL; return 0;
memmove (array->values + index + 1, array->values + index, memmove (array->values + index + 1, array->values + index,
(array->numvals - index) * sizeof (plitem_t *)); (array->numvals - index) * sizeof (plitem_t *));
array->values[index] = item; array->values[index] = item;
array->numvals++; array->numvals++;
return array_item; return 1;
} }
plitem_t * int
PL_A_AddObject (plitem_t *array_item, plitem_t *item) PL_A_AddObject (plitem_t *array_item, plitem_t *item)
{ {
return PL_A_InsertObjectAtIndex (array_item, item, -1); return PL_A_InsertObjectAtIndex (array_item, item, -1);

View file

@ -8,9 +8,9 @@ typedef struct plitem_s plitem_t;
@extern plitem_t (plitem_t item, string key) PL_ObjectForKey; @extern plitem_t (plitem_t item, string key) PL_ObjectForKey;
@extern plitem_t (plitem_t item, integer index) PL_ObjectAtIndex; @extern plitem_t (plitem_t item, integer index) PL_ObjectAtIndex;
@extern plitem_t (plitem_t dict, plitem_t key, plitem_t value) PL_D_AddObject; @extern integer (plitem_t dict, plitem_t key, plitem_t value) PL_D_AddObject;
@extern plitem_t (plitem_t array_item, plitem_t item) PL_A_AddObject; @extern integer (plitem_t array_item, plitem_t item) PL_A_AddObject;
@extern plitem_t (plitem_t array_item, plitem_t item, integer index) PL_A_InsertObjectAtIndex; @extern integer (plitem_t array_item, plitem_t item, integer index) PL_A_InsertObjectAtIndex;
@extern plitem_t () PL_NewDictionary; @extern plitem_t () PL_NewDictionary;
@extern plitem_t () PL_NewArray; @extern plitem_t () PL_NewArray;

View file

@ -4,9 +4,9 @@ plitem_t (string str) PL_GetPropertyList = #0;
plitem_t (plitem_t item, string key) PL_ObjectForKey = #0; plitem_t (plitem_t item, string key) PL_ObjectForKey = #0;
plitem_t (plitem_t item, integer index) PL_ObjectAtIndex = #0; plitem_t (plitem_t item, integer index) PL_ObjectAtIndex = #0;
plitem_t (plitem_t dict, plitem_t key, plitem_t value) PL_D_AddObject = #0; integer (plitem_t dict, plitem_t key, plitem_t value) PL_D_AddObject = #0;
plitem_t (plitem_t array_item, plitem_t item) PL_A_AddObject = #0; integer (plitem_t array_item, plitem_t item) PL_A_AddObject = #0;
plitem_t (plitem_t array_item, plitem_t item, integer index) PL_A_InsertObjectAtIndex = #0; integer (plitem_t array_item, plitem_t item, integer index) PL_A_InsertObjectAtIndex = #0;
plitem_t () PL_NewDictionary = #0; plitem_t () PL_NewDictionary = #0;
plitem_t () PL_NewArray = #0; plitem_t () PL_NewArray = #0;