2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2009-08-17 12:41:51 +02:00
|
|
|
gbx_array.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 __GB_ARRAY_C
|
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
#include "gb_error.h"
|
|
|
|
#include "gbx_api.h"
|
|
|
|
#include "gambas.h"
|
|
|
|
#include "gbx_class.h"
|
|
|
|
#include "gbx_exec.h"
|
|
|
|
|
|
|
|
#include "gbx_array.h"
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
size_t ARRAY_get_size(ARRAY_DESC *desc)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-01-17 22:39:26 +01:00
|
|
|
ssize_t size;
|
2007-12-30 17:41:49 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
size = 1;
|
|
|
|
|
|
|
|
for (i = 0;; i++)
|
|
|
|
{
|
|
|
|
size *= desc->dim[i];
|
|
|
|
if (size < 0)
|
|
|
|
{
|
|
|
|
size = (-size);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
size *= TYPE_sizeof_memory(desc->type.id);
|
|
|
|
|
|
|
|
return (size_t)size;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void ARRAY_new(void **data, ARRAY_DESC *desc)
|
|
|
|
{
|
2008-01-24 02:36:20 +01:00
|
|
|
ALLOC_ZERO(data, ARRAY_get_size(desc), "ARRAY_new");
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void ARRAY_free_data(void *data, ARRAY_DESC *desc)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-01-17 22:39:26 +01:00
|
|
|
ssize_t size;
|
|
|
|
intptr_t i;
|
2007-12-30 17:41:49 +01:00
|
|
|
char *array;
|
|
|
|
|
|
|
|
size = 1;
|
|
|
|
|
|
|
|
for (i = 0;; i++)
|
|
|
|
{
|
|
|
|
size *= desc->dim[i];
|
|
|
|
if (size < 0)
|
|
|
|
{
|
|
|
|
size = (-size);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
array = data;
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
if (desc->type.id == T_STRING)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
{
|
|
|
|
STRING_unref((char **)array);
|
|
|
|
array += sizeof(char *);
|
|
|
|
}
|
|
|
|
}
|
2008-01-17 22:39:26 +01:00
|
|
|
else if (desc->type.id == T_OBJECT)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
{
|
2008-09-22 01:22:07 +02:00
|
|
|
OBJECT_UNREF(*((void **)array), "ARRAY_free_data");
|
2007-12-30 17:41:49 +01:00
|
|
|
array += sizeof(void *);
|
|
|
|
}
|
|
|
|
}
|
2008-01-17 22:39:26 +01:00
|
|
|
else if (desc->type.id == T_VARIANT)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
{
|
|
|
|
VARIANT_free((VARIANT *)array);
|
|
|
|
array += sizeof(VARIANT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void ARRAY_free(void **data, ARRAY_DESC *desc)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
if (*data == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ARRAY_free_data(*data, desc);
|
|
|
|
|
|
|
|
FREE(data, "ARRAY_free");
|
|
|
|
}
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void *ARRAY_get_address(ARRAY_DESC *desc, void *addr, int nparam, int *param)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-01-17 22:39:26 +01:00
|
|
|
int max;
|
2007-12-30 17:41:49 +01:00
|
|
|
int i;
|
2009-07-08 21:57:50 +02:00
|
|
|
bool stop = FALSE;
|
2008-01-17 22:39:26 +01:00
|
|
|
offset_t pos = 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
for (i = 0;; i++)
|
|
|
|
{
|
|
|
|
if (i >= nparam)
|
|
|
|
THROW(E_NDIM);
|
|
|
|
|
|
|
|
max = desc->dim[i];
|
|
|
|
if (max < 0)
|
|
|
|
{
|
|
|
|
max = (-max);
|
|
|
|
stop = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (param[i] < 0 || param[i] >= max)
|
|
|
|
THROW(E_BOUND);
|
|
|
|
|
|
|
|
pos *= max;
|
|
|
|
pos += param[i];
|
|
|
|
|
|
|
|
if (stop)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
return (char *)addr + pos * TYPE_sizeof_memory(desc->type.id);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|