2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2009-08-17 12:41:51 +02:00
|
|
|
gbx_c_collection.c
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-08-17 12:41:51 +02:00
|
|
|
(c) 2000-2009 Benoît Minisini <gambas@users.sourceforge.net>
|
2007-12-30 17:41:49 +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
|
2009-08-17 12:41:51 +02:00
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
2007-12-30 17:41:49 +01:00
|
|
|
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 the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#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"
|
|
|
|
|
|
|
|
|
|
|
|
static void collection_free(CCOLLECTION *col)
|
|
|
|
{
|
|
|
|
VARIANT *value;
|
|
|
|
HASH_ENUM iter;
|
|
|
|
|
|
|
|
CLEAR(&iter);
|
2009-06-17 16:13:50 +02:00
|
|
|
col->locked = TRUE;
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
value = HASH_TABLE_next(col->hash_table, &iter);
|
|
|
|
if (value == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
VARIANT_free(value);
|
2009-06-17 16:13:50 +02:00
|
|
|
value->type = T_NULL;
|
2007-12-30 17:41:49 +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
|
|
|
}
|
|
|
|
|
2010-06-09 01:08:04 +02:00
|
|
|
/*static void *collection_get_key(CCOLLECTION *col, const char *key, int len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-09-13 17:35:13 +02:00
|
|
|
if (len == 0)
|
2007-12-30 17:41:49 +01:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (len <= 0)
|
|
|
|
len = strlen(key);
|
|
|
|
|
|
|
|
return HASH_TABLE_lookup(col->hash_table, key, len);
|
2010-06-09 01:08:04 +02:00
|
|
|
}*/
|
|
|
|
|
|
|
|
#define collection_get_key(_col, _key, _len) HASH_TABLE_lookup((_col)->hash_table, (_key), (_len))
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
static void *collection_add_key(CCOLLECTION *col, const char *key, int len)
|
|
|
|
{
|
2008-09-13 17:35:13 +02:00
|
|
|
if (len == 0)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-09-17 22:58:27 +02:00
|
|
|
GB_Error((char *)E_VKEY);
|
2007-12-30 17:41:49 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len <= 0)
|
|
|
|
len = strlen(key);
|
|
|
|
|
|
|
|
return HASH_TABLE_insert(col->hash_table, key, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void collection_remove_key(CCOLLECTION *col, const char *key, int len)
|
|
|
|
{
|
|
|
|
void *value;
|
|
|
|
HASH_NODE *last;
|
|
|
|
|
2008-09-13 17:35:13 +02:00
|
|
|
if (len == 0)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-09-17 22:58:27 +02:00
|
|
|
GB_Error((char *)E_VKEY);
|
2007-12-30 17:41:49 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len < 0)
|
|
|
|
len = strlen(key);
|
|
|
|
|
|
|
|
value = HASH_TABLE_lookup(col->hash_table, key, len);
|
|
|
|
if (value == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
last = col->hash_table->last;
|
|
|
|
|
|
|
|
GB_ListEnum(col);
|
|
|
|
while (!GB_NextEnum())
|
|
|
|
{
|
|
|
|
HASH_ENUM *iter = (HASH_ENUM *)GB_GetEnum();
|
|
|
|
/*char *ekey;
|
2008-01-17 22:39:26 +01:00
|
|
|
int elen;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
HASH_TABLE_get_key(col->hash_table, iter->node, &ekey, &elen);
|
|
|
|
fprintf(stderr, "iter->node: %.*s\n", elen, ekey);
|
|
|
|
HASH_TABLE_get_key(col->hash_table, iter->next, &ekey, &elen);
|
|
|
|
fprintf(stderr, "iter->next: %.*s\n", elen, ekey);*/
|
|
|
|
|
|
|
|
if (iter->next == last)
|
|
|
|
{
|
|
|
|
iter->next = iter->next->snext;
|
|
|
|
/*HASH_TABLE_get_key(col->hash_table, iter->next, &ekey, &elen);
|
|
|
|
fprintf(stderr, "iter->next: => %.*s\n", elen, ekey);*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//col->hash_table->last = last;
|
|
|
|
|
2010-06-13 14:03:29 +02: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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_METHOD(CCOLLECTION_new, GB_INTEGER mode)
|
|
|
|
|
|
|
|
int mode = MISSING(mode) ? 0 : VARG(mode);
|
|
|
|
|
|
|
|
HASH_TABLE_create(&THIS->hash_table, TYPE_sizeof(T_VARIANT), mode);
|
|
|
|
THIS->last = NULL;
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_METHOD_VOID(CCOLLECTION_free)
|
|
|
|
|
|
|
|
collection_free(THIS);
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(collection_count)
|
|
|
|
|
2010-09-27 21:34:27 +02:00
|
|
|
GB_ReturnInt(CCOLLECTION_get_count(THIS));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(collection_key)
|
|
|
|
|
|
|
|
char *key;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (HASH_TABLE_get_last_key(THIS->hash_table, &key, &len))
|
|
|
|
GB_ReturnNull();
|
|
|
|
else
|
|
|
|
GB_ReturnNewString(key, len);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_METHOD(collection_add, GB_VARIANT value; GB_STRING key)
|
|
|
|
|
|
|
|
void *data;
|
|
|
|
|
|
|
|
data = collection_add_key(THIS, STRING(key), LENGTH(key));
|
|
|
|
if (!data)
|
|
|
|
return;
|
|
|
|
|
|
|
|
GB_StoreVariant(ARG(value), data);
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_METHOD(collection_exist, GB_STRING key)
|
|
|
|
|
|
|
|
/*if (LENGTH(key) == 0)
|
|
|
|
GB_ReturnBoolean(FALSE);
|
|
|
|
else*/
|
|
|
|
GB_ReturnBoolean(collection_get_key(THIS, STRING(key), LENGTH(key)) != NULL);
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_METHOD(collection_remove, GB_STRING key)
|
|
|
|
|
|
|
|
collection_remove_key(THIS, STRING(key), LENGTH(key));
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_METHOD_VOID(collection_clear)
|
|
|
|
|
|
|
|
int mode = THIS->hash_table->mode;
|
|
|
|
|
|
|
|
/* Stops all iterators */
|
|
|
|
GB_StopAllEnum(THIS);
|
|
|
|
|
|
|
|
collection_free(THIS);
|
|
|
|
HASH_TABLE_create(&THIS->hash_table, TYPE_sizeof(T_VARIANT), mode);
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
BEGIN_METHOD_VOID(collection_first)
|
|
|
|
|
|
|
|
HASH_ENUM *iter = (HASH_ENUM *)GB_GetEnum();
|
|
|
|
|
|
|
|
iter->index = (-1);
|
|
|
|
iter->node = NULL;
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_METHOD_VOID(collection_next)
|
|
|
|
|
|
|
|
void *value;
|
|
|
|
HASH_TABLE *hash_table = OBJECT(CCOLLECTION)->hash_table;
|
|
|
|
HASH_ENUM *iter = (HASH_ENUM *)GB_GetEnum();
|
|
|
|
|
|
|
|
value = HASH_TABLE_next(hash_table, iter);
|
|
|
|
|
|
|
|
if (value == NULL)
|
|
|
|
GB_StopEnum();
|
|
|
|
else
|
2010-05-22 20:02:34 +02:00
|
|
|
GB_ReturnVariant(value);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
BEGIN_METHOD(collection_copy, CCOLLECTION *copy)
|
|
|
|
|
|
|
|
CCOLLECTION *copy = PARAM(copy);
|
|
|
|
HASH_TABLE *hash_table = THIS->hash_table;
|
|
|
|
CCOL_ENUM enum_state;
|
|
|
|
void *value;
|
|
|
|
|
|
|
|
collection_clear(THIS, NULL);
|
|
|
|
|
|
|
|
enum_state.i = -1;
|
|
|
|
enum_state.node = NULL;
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
value = HASH_TABLE_next(hash_table, &enum_state->i, &enum_state->node);
|
|
|
|
if (value == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
GB_ReturnPtr(OBJECT(CCOLLECTION)->type, value);
|
|
|
|
data = collection_add_key(THIS, PARAM(key).addr, PARAM(key).len);
|
|
|
|
GB_Store(T_VARIANT, &PARAM(value), data);
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
*/
|
|
|
|
|
|
|
|
BEGIN_METHOD(collection_get, GB_STRING key)
|
|
|
|
|
2010-05-22 20:02:34 +02:00
|
|
|
GB_ReturnVariant(collection_get_key(THIS, STRING(key), LENGTH(key)));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_METHOD(collection_put, GB_VARIANT value; GB_STRING key)
|
|
|
|
|
|
|
|
void *data;
|
|
|
|
|
|
|
|
if (VARIANT_is_null((VARIANT *)&VARG(value)))
|
|
|
|
collection_remove_key(THIS, STRING(key), LENGTH(key));
|
|
|
|
else
|
|
|
|
{
|
|
|
|
data = collection_add_key(THIS, STRING(key), LENGTH(key));
|
|
|
|
if (!data)
|
|
|
|
return;
|
|
|
|
GB_StoreVariant(ARG(value), data);
|
|
|
|
}
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
GB_DESC NATIVE_Collection[] =
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
GB_DECLARE("Collection", sizeof(CCOLLECTION)),
|
|
|
|
|
|
|
|
GB_METHOD("_new", NULL, CCOLLECTION_new, "[(Mode)i]"),
|
|
|
|
GB_METHOD("_free", NULL, CCOLLECTION_free, NULL),
|
|
|
|
|
|
|
|
GB_PROPERTY_READ("Count", "i", collection_count),
|
|
|
|
GB_PROPERTY_READ("Length", "i", collection_count),
|
|
|
|
GB_PROPERTY_READ("Key", "s", collection_key),
|
|
|
|
|
|
|
|
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_END_DECLARE
|
|
|
|
};
|
|
|
|
|
|
|
|
#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
|
|
|
{
|
2009-12-24 03:02:05 +01:00
|
|
|
VALUE param;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-12-24 03:02:05 +01:00
|
|
|
param._integer.type = GB_T_INTEGER;
|
|
|
|
param._integer.value = mode;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-06-18 02:14:53 +02: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
|
|
|
{
|
|
|
|
return HASH_TABLE_size(((CCOLLECTION *)col)->hash_table);
|
|
|
|
}
|
|
|
|
|
2008-01-24 11:42:42 +01:00
|
|
|
int GB_CollectionSet(GB_COLLECTION col, const char *key, int len, GB_VARIANT *value)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
VARIANT *data;
|
|
|
|
|
|
|
|
if (VARIANT_is_null((VARIANT *)&value->value))
|
|
|
|
collection_remove_key((CCOLLECTION *)col, key, len);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
data = (VARIANT *)collection_add_key((CCOLLECTION *)col, key, len);
|
2008-01-24 11:42:42 +01:00
|
|
|
if (!data)
|
|
|
|
return TRUE;
|
|
|
|
GB_StoreVariant(value, data);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
2008-01-24 11:42:42 +01:00
|
|
|
return FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
int GB_CollectionGet(GB_COLLECTION col, const char *key, int len, GB_VARIANT *value)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-03-23 01:51:50 +01:00
|
|
|
VARIANT *var;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-03-23 01:51:50 +01:00
|
|
|
var = (VARIANT *)collection_get_key((CCOLLECTION *)col, key, len);
|
2010-09-23 03:34:15 +02:00
|
|
|
value->type = T_VARIANT;
|
2008-03-23 01:51:50 +01:00
|
|
|
if (var)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-03-23 01:51:50 +01:00
|
|
|
value->value.type = var->type;
|
2009-12-24 03:02:05 +01:00
|
|
|
value->value.value.data = var->value.data;
|
2007-12-30 17:41:49 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-09-23 03:34:15 +02:00
|
|
|
value->value.type = GB_T_NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-06 14:03:27 +02:00
|
|
|
int GB_CollectionEnum(GB_COLLECTION col, GB_COLLECTION_ITER *iter, GB_VARIANT *value, char **key, int *len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-12-07 18:14:32 +01:00
|
|
|
VARIANT *val;
|
2007-12-30 17:41:49 +01:00
|
|
|
HASH_TABLE *hash_table = ((CCOLLECTION *)col)->hash_table;
|
|
|
|
|
|
|
|
if (!value || !key)
|
|
|
|
{
|
2010-10-06 14:03:27 +02:00
|
|
|
CLEAR(iter);
|
2007-12-30 17:41:49 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2010-10-06 14:03:27 +02:00
|
|
|
val = HASH_TABLE_next(hash_table, (HASH_ENUM *)iter);
|
2007-12-30 17:41:49 +01:00
|
|
|
if (!val)
|
|
|
|
return TRUE;
|
|
|
|
|
2010-05-19 14:43:57 +02:00
|
|
|
value->type = T_VARIANT;
|
2010-07-21 06:40:45 +02:00
|
|
|
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
|