2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
gbx_c_collection.c
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-08-03 17:38:01 +02:00
|
|
|
(c) 2000-2013 Benoît Minisini <gambas@users.sourceforge.net>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-12-31 03:39:20 +01: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, or (at your option)
|
|
|
|
any later version.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
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.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
MA 02110-1301, USA.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __GBX_C_COLLECTION_C
|
|
|
|
|
|
|
|
#include "gbx_info.h"
|
|
|
|
|
|
|
|
#ifndef GBX_INFO
|
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
#include "gb_error.h"
|
|
|
|
|
|
|
|
#include "gbx_variant.h"
|
|
|
|
#include "gambas.h"
|
|
|
|
#include "gbx_api.h"
|
|
|
|
#include "gbx_class.h"
|
|
|
|
#include "gbx_object.h"
|
|
|
|
|
|
|
|
#include "gbx_c_collection.h"
|
|
|
|
|
|
|
|
|
2010-12-15 16:55:16 +01:00
|
|
|
static void clear(CCOLLECTION *col)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-12-15 16:55:16 +01:00
|
|
|
VARIANT *value;
|
|
|
|
HASH_ENUM iter;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-15 16:55:16 +01:00
|
|
|
CLEAR(&iter);
|
2009-06-17 16:13:50 +02:00
|
|
|
col->locked = TRUE;
|
|
|
|
|
2010-12-15 16:55:16 +01:00
|
|
|
for(;;)
|
|
|
|
{
|
2014-04-05 02:28:41 +02:00
|
|
|
value = HASH_TABLE_next(col->hash_table, &iter, FALSE);
|
2010-12-15 16:55:16 +01:00
|
|
|
if (value == NULL)
|
|
|
|
break;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-15 16:55:16 +01:00
|
|
|
VARIANT_free(value);
|
2009-06-17 16:13:50 +02:00
|
|
|
value->type = T_NULL;
|
2010-12-15 16:55:16 +01:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-15 16:55:16 +01:00
|
|
|
HASH_TABLE_delete(&col->hash_table);
|
2009-06-17 16:13:50 +02:00
|
|
|
col->locked = FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2014-04-05 02:28:41 +02:00
|
|
|
#define get_key(_col, _key, _len, _set_last) HASH_TABLE_lookup((_col)->hash_table, (_key), (_len), _set_last)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-09-21 14:42:56 +02:00
|
|
|
#define add_key(_col, _key, _len) ((_len) == 0 ? (GB_Error((char *)E_VKEY), NULL) : HASH_TABLE_insert(((CCOLLECTION *)(_col))->hash_table, (_key), (_len)))
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-15 16:55:16 +01:00
|
|
|
static void remove_key(CCOLLECTION *col, const char *key, int len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-12-15 16:55:16 +01:00
|
|
|
void *value;
|
|
|
|
HASH_NODE *last;
|
2012-09-06 18:20:54 +02:00
|
|
|
void *save_enum;
|
2010-12-15 16:55:16 +01:00
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
{
|
|
|
|
GB_Error((char *)E_VKEY);
|
|
|
|
return;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-04-05 02:28:41 +02:00
|
|
|
value = HASH_TABLE_lookup(col->hash_table, key, len, FALSE);
|
2010-12-15 16:55:16 +01:00
|
|
|
if (value == NULL)
|
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-15 16:55:16 +01:00
|
|
|
last = col->hash_table->last;
|
2015-10-07 01:29:35 +02:00
|
|
|
|
|
|
|
if (last)
|
2010-12-15 16:55:16 +01:00
|
|
|
{
|
2015-10-07 01:29:35 +02:00
|
|
|
save_enum = GB_BeginEnum(col);
|
|
|
|
while (!GB_NextEnum())
|
|
|
|
{
|
|
|
|
HASH_ENUM *iter = (HASH_ENUM *)GB_GetEnum();
|
|
|
|
if (iter->next == last)
|
|
|
|
iter->next = iter->next->snext;
|
|
|
|
}
|
|
|
|
GB_EndEnum(save_enum);
|
2010-12-15 16:55:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
VARIANT_free((VARIANT *)value);
|
2010-03-12 02:18:20 +01:00
|
|
|
|
2009-06-17 16:13:50 +02:00
|
|
|
if (!col->locked)
|
|
|
|
HASH_TABLE_remove(col->hash_table, key, len);
|
2010-03-12 02:18:20 +01:00
|
|
|
else // Prevent the freed variant to be freed twice if the collection is locked (i.e. being destroyed)
|
|
|
|
((VARIANT *)value)->type = T_NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-15 16:55:16 +01:00
|
|
|
BEGIN_METHOD(Collection_new, GB_INTEGER mode)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-15 16:55:16 +01:00
|
|
|
int mode = MISSING(mode) ? 0 : VARG(mode);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-15 16:55:16 +01:00
|
|
|
HASH_TABLE_create(&THIS->hash_table, TYPE_sizeof(T_VARIANT), mode);
|
|
|
|
THIS->last = NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
2010-12-15 16:55:16 +01:00
|
|
|
BEGIN_METHOD_VOID(Collection_free)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-15 16:55:16 +01:00
|
|
|
clear(THIS);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
2010-12-14 02:18:41 +01:00
|
|
|
BEGIN_PROPERTY(Collection_Count)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-15 16:55:16 +01:00
|
|
|
GB_ReturnInt(CCOLLECTION_get_count(THIS));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
2010-12-14 02:18:41 +01:00
|
|
|
BEGIN_PROPERTY(Collection_Key)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-15 16:55:16 +01:00
|
|
|
char *key;
|
|
|
|
int len;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-04-05 02:28:41 +02:00
|
|
|
if (READ_PROPERTY)
|
|
|
|
{
|
|
|
|
if (HASH_TABLE_get_last_key(THIS->hash_table, &key, &len))
|
|
|
|
GB_ReturnVoidString();
|
|
|
|
else
|
|
|
|
GB_ReturnNewString(key, len);
|
|
|
|
}
|
2010-12-15 16:55:16 +01:00
|
|
|
else
|
2014-04-05 02:28:41 +02:00
|
|
|
HASH_TABLE_set_last_key(THIS->hash_table, PSTRING(), PLENGTH());
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
2010-12-14 02:18:41 +01:00
|
|
|
BEGIN_METHOD(Collection_Add, GB_VARIANT value; GB_STRING key)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-15 16:55:16 +01:00
|
|
|
void *data;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-15 16:55:16 +01:00
|
|
|
data = add_key(THIS, STRING(key), LENGTH(key));
|
|
|
|
if (!data)
|
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-15 16:55:16 +01:00
|
|
|
GB_StoreVariant(ARG(value), data);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
2010-12-14 02:18:41 +01:00
|
|
|
BEGIN_METHOD(Collection_Exist, GB_STRING key)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-04-05 02:28:41 +02:00
|
|
|
GB_ReturnBoolean(get_key(THIS, STRING(key), LENGTH(key), FALSE) != NULL);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
2010-12-14 02:18:41 +01:00
|
|
|
BEGIN_METHOD(Collection_Remove, GB_STRING key)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-15 16:55:16 +01:00
|
|
|
remove_key(THIS, STRING(key), LENGTH(key));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
2010-12-14 02:18:41 +01:00
|
|
|
BEGIN_METHOD_VOID(Collection_Clear)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-15 16:55:16 +01:00
|
|
|
int mode = THIS->hash_table->mode;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-15 16:55:16 +01:00
|
|
|
/* Stops all iterators */
|
|
|
|
GB_StopAllEnum(THIS);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-15 16:55:16 +01:00
|
|
|
clear(THIS);
|
|
|
|
HASH_TABLE_create(&THIS->hash_table, TYPE_sizeof(T_VARIANT), mode);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
2010-12-14 02:18:41 +01:00
|
|
|
BEGIN_METHOD_VOID(Collection_next)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-15 16:55:16 +01:00
|
|
|
void *value;
|
|
|
|
HASH_TABLE *hash_table = OBJECT(CCOLLECTION)->hash_table;
|
|
|
|
HASH_ENUM *iter = (HASH_ENUM *)GB_GetEnum();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-04-05 02:28:41 +02:00
|
|
|
value = HASH_TABLE_next(hash_table, iter, !DEBUG_inside_eval);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-15 16:55:16 +01:00
|
|
|
if (value == NULL)
|
|
|
|
GB_StopEnum();
|
|
|
|
else
|
|
|
|
GB_ReturnVariant(value);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
2010-12-14 02:18:41 +01:00
|
|
|
BEGIN_METHOD(Collection_get, GB_STRING key)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-04-05 02:28:41 +02:00
|
|
|
GB_ReturnVariant(get_key(THIS, STRING(key), LENGTH(key), !DEBUG_inside_eval));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
2010-12-14 02:18:41 +01:00
|
|
|
BEGIN_METHOD(Collection_put, GB_VARIANT value; GB_STRING key)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-15 16:55:16 +01:00
|
|
|
void *data;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-15 16:55:16 +01:00
|
|
|
if (VARIANT_is_null((VARIANT *)&VARG(value)))
|
|
|
|
remove_key(THIS, STRING(key), LENGTH(key));
|
|
|
|
else
|
|
|
|
{
|
|
|
|
data = add_key(THIS, STRING(key), LENGTH(key));
|
|
|
|
if (!data)
|
|
|
|
return;
|
|
|
|
GB_StoreVariant(ARG(value), data);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2010-12-15 16:55:16 +01:00
|
|
|
|
2010-12-14 02:18:41 +01:00
|
|
|
BEGIN_METHOD_VOID(Collection_Copy)
|
|
|
|
|
|
|
|
GB_COLLECTION col;
|
|
|
|
GB_COLLECTION_ITER iter;;
|
|
|
|
GB_VARIANT value;
|
|
|
|
char *key;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
GB_CollectionNew(&col, THIS->mode);
|
|
|
|
|
|
|
|
CLEAR(&iter);
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
if (GB_CollectionEnum(THIS, &iter, &value, &key, &len))
|
|
|
|
break;
|
|
|
|
|
|
|
|
GB_CollectionSet(col, key, len, &value);
|
|
|
|
}
|
|
|
|
|
|
|
|
GB_ReturnObject(col);
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
GB_DESC NATIVE_Collection[] =
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-12-15 16:55:16 +01:00
|
|
|
GB_DECLARE("Collection", sizeof(CCOLLECTION)),
|
|
|
|
|
|
|
|
GB_METHOD("_new", NULL, Collection_new, "[(Mode)i]"),
|
|
|
|
GB_METHOD("_free", NULL, Collection_free, NULL),
|
|
|
|
|
|
|
|
GB_PROPERTY_READ("Count", "i", Collection_Count),
|
|
|
|
GB_PROPERTY_READ("Length", "i", Collection_Count),
|
2014-04-05 02:28:41 +02:00
|
|
|
GB_PROPERTY("Key", "s", Collection_Key),
|
2010-12-15 16:55:16 +01:00
|
|
|
|
|
|
|
GB_METHOD("Add", NULL, Collection_Add, "(Value)v(Key)s"),
|
|
|
|
GB_METHOD("Exist", "b", Collection_Exist, "(Key)s"),
|
|
|
|
GB_METHOD("Remove", NULL, Collection_Remove, "(Key)s"),
|
|
|
|
GB_METHOD("Clear", NULL, Collection_Clear, NULL),
|
|
|
|
/*GB_METHOD("_first", NULL, collection_first, NULL),*/
|
|
|
|
GB_METHOD("_next", "v", Collection_next, NULL),
|
|
|
|
GB_METHOD("_get", "v", Collection_get, "(Key)s"),
|
|
|
|
GB_METHOD("_put", NULL, Collection_put, "(Value)v(Key)s"),
|
|
|
|
GB_METHOD("Copy", "Collection", Collection_Copy, NULL),
|
|
|
|
|
|
|
|
GB_END_DECLARE
|
2007-12-30 17:41:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#ifndef GBX_INFO
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void GB_CollectionNew(GB_COLLECTION *col, int mode)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-12-15 16:55:16 +01:00
|
|
|
VALUE param;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-15 16:55:16 +01:00
|
|
|
param._integer.type = GB_T_INTEGER;
|
|
|
|
param._integer.value = mode;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-15 16:55:16 +01:00
|
|
|
*col = OBJECT_create_native(CLASS_Collection, ¶m);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
int GB_CollectionCount(GB_COLLECTION col)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-12-15 16:55:16 +01:00
|
|
|
return HASH_TABLE_size(((CCOLLECTION *)col)->hash_table);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2011-05-16 04:16:22 +02:00
|
|
|
bool GB_CollectionSet(GB_COLLECTION col, const char *key, int len, GB_VARIANT *value)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-12-15 16:55:16 +01:00
|
|
|
VARIANT *data;
|
|
|
|
|
|
|
|
if (VARIANT_is_null((VARIANT *)&value->value))
|
|
|
|
remove_key((CCOLLECTION *)col, key, len);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
data = (VARIANT *)add_key((CCOLLECTION *)col, key, len);
|
|
|
|
if (!data)
|
|
|
|
return TRUE;
|
|
|
|
GB_StoreVariant(value, data);
|
|
|
|
}
|
|
|
|
return FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2011-05-16 04:16:22 +02:00
|
|
|
bool GB_CollectionGet(GB_COLLECTION col, const char *key, int len, GB_VARIANT *value)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-12-15 16:55:16 +01:00
|
|
|
VARIANT *var;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-04-05 02:28:41 +02:00
|
|
|
var = (VARIANT *)get_key((CCOLLECTION *)col, key, len, !DEBUG_inside_eval);
|
2010-12-15 16:55:16 +01:00
|
|
|
value->type = T_VARIANT;
|
|
|
|
if (var)
|
|
|
|
{
|
|
|
|
value->value.type = var->type;
|
2009-12-24 03:02:05 +01:00
|
|
|
value->value.value.data = var->value.data;
|
2010-12-15 16:55:16 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
value->value.type = GB_T_NULL;
|
|
|
|
return TRUE;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2011-05-16 04:16:22 +02:00
|
|
|
bool GB_CollectionEnum(GB_COLLECTION col, GB_COLLECTION_ITER *iter, GB_VARIANT *value, char **key, int *len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-12-15 16:55:16 +01:00
|
|
|
VARIANT *val;
|
|
|
|
HASH_TABLE *hash_table = ((CCOLLECTION *)col)->hash_table;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (!value || !key)
|
|
|
|
{
|
2010-10-06 14:03:27 +02:00
|
|
|
CLEAR(iter);
|
2007-12-30 17:41:49 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2014-04-05 02:28:41 +02:00
|
|
|
val = HASH_TABLE_next(hash_table, (HASH_ENUM *)iter, !DEBUG_inside_eval);
|
2010-12-15 16:55:16 +01:00
|
|
|
if (!val)
|
|
|
|
return TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-15 16:55:16 +01:00
|
|
|
value->type = T_VARIANT;
|
|
|
|
value->value.type = val->type;
|
2009-12-24 03:02:05 +01:00
|
|
|
value->value.value.data = val->value.data;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
HASH_TABLE_get_last_key(hash_table, key, len);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|