2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2009-08-17 12:41:51 +02:00
|
|
|
gbx_subr_conv.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.
|
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
|
|
|
|
#include "gbx_value.h"
|
|
|
|
#include "gbx_subr.h"
|
|
|
|
#include "gbx_local.h"
|
|
|
|
|
|
|
|
#include "gbx_string.h"
|
|
|
|
#include "gbx_date.h"
|
|
|
|
#include "gbx_number.h"
|
|
|
|
|
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
void SUBR_is_type(ushort code)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-06-05 01:48:53 +02:00
|
|
|
static void *jump[] = {
|
2010-11-28 00:00:08 +01:00
|
|
|
&&__BAD, &&__BOOLEAN, &&__BAD, &&__BAD, &&__INTEGER, &&__LONG, &&__BAD, &&__FLOAT,
|
|
|
|
&&__DATE, &&__BAD, &&__BAD, &&__BAD, &&__BAD, &&__BAD, &&__NUMBER, &&__NULL,
|
|
|
|
&&__BAD, &&__BAD
|
2010-06-05 01:48:53 +02:00
|
|
|
};
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
bool test;
|
2010-11-28 00:00:08 +01:00
|
|
|
char *addr;
|
|
|
|
int len;
|
|
|
|
VALUE temp;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
SUBR_ENTER_PARAM(1);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-28 00:00:08 +01:00
|
|
|
code &= 0x3F;
|
|
|
|
|
2010-11-30 16:03:47 +01:00
|
|
|
if (code == T_NULL)
|
|
|
|
goto __NULL;
|
|
|
|
|
|
|
|
VALUE_conv_string(PARAM);
|
|
|
|
VALUE_get_string(PARAM, &addr, &len);
|
|
|
|
VALUE_from_string(&temp, addr, len);
|
2010-11-28 00:00:08 +01:00
|
|
|
|
|
|
|
goto *jump[code];
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
__BOOLEAN:
|
2010-11-28 00:00:08 +01:00
|
|
|
|
|
|
|
test = temp.type == T_BOOLEAN;
|
|
|
|
goto __END;
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
__INTEGER:
|
|
|
|
|
2010-11-28 00:00:08 +01:00
|
|
|
test = temp.type == T_INTEGER;
|
|
|
|
goto __END;
|
2009-09-20 19:32:12 +02:00
|
|
|
|
2010-11-28 00:00:08 +01:00
|
|
|
__LONG:
|
2009-09-20 19:32:12 +02:00
|
|
|
|
2010-11-28 00:00:08 +01:00
|
|
|
test = temp.type == T_LONG || temp.type == T_INTEGER;
|
2010-06-05 01:48:53 +02:00
|
|
|
goto __END;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-28 00:00:08 +01:00
|
|
|
__FLOAT:
|
2010-12-21 15:05:42 +01:00
|
|
|
__NUMBER:
|
2009-09-20 19:32:12 +02:00
|
|
|
|
2010-12-21 15:05:42 +01:00
|
|
|
test = temp.type == T_FLOAT || temp.type == T_LONG || temp.type == T_INTEGER;
|
2010-11-28 00:00:08 +01:00
|
|
|
goto __END;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-28 00:00:08 +01:00
|
|
|
__DATE:
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-28 00:00:08 +01:00
|
|
|
test = temp.type == T_DATE;
|
2010-06-05 01:48:53 +02:00
|
|
|
goto __END;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-28 00:00:08 +01:00
|
|
|
__NULL:
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-28 00:00:08 +01:00
|
|
|
test = VALUE_is_null(PARAM);
|
2010-06-05 01:48:53 +02:00
|
|
|
goto __END;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
__BAD:
|
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
THROW_ILLEGAL();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
__END:
|
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
RELEASE(PARAM);
|
|
|
|
SP--;
|
|
|
|
SP->type = T_BOOLEAN;
|
|
|
|
SP->_integer.value = (-test);
|
|
|
|
SP++;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
void SUBR_conv(ushort code)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-06-05 01:48:53 +02:00
|
|
|
VALUE_convert(SP - 1, code & 0x3F);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
void SUBR_type(ushort code)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
TYPE type;
|
2010-05-27 00:01:28 +02:00
|
|
|
int val;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
SUBR_ENTER_PARAM(1);
|
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
if (code & 0x3F)
|
2010-05-27 00:01:28 +02:00
|
|
|
{
|
2010-08-31 14:02:08 +02:00
|
|
|
val = TYPE_sizeof_memory(SUBR_get_integer(PARAM));
|
2010-05-27 00:01:28 +02:00
|
|
|
}
|
|
|
|
else
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-08-31 14:02:08 +02:00
|
|
|
type = PARAM->type;
|
|
|
|
if (type == T_VARIANT)
|
|
|
|
type = PARAM->_variant.vtype;
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
if (type == T_CSTRING)
|
2010-05-27 00:01:28 +02:00
|
|
|
val = T_STRING;
|
2009-09-17 22:58:27 +02:00
|
|
|
else if (TYPE_is_object(type) && type != T_NULL)
|
2010-05-27 00:01:28 +02:00
|
|
|
val = T_OBJECT;
|
2007-12-30 17:41:49 +01:00
|
|
|
else
|
2010-05-27 00:01:28 +02:00
|
|
|
val = type;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2010-05-27 00:01:28 +02:00
|
|
|
RETURN->_integer.value = val;
|
2007-12-30 17:41:49 +01:00
|
|
|
RETURN->type = T_INTEGER;
|
|
|
|
|
|
|
|
SUBR_LEAVE();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void SUBR_str(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
char *addr;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
SUBR_ENTER_PARAM(1);
|
|
|
|
|
|
|
|
VALUE_to_string(PARAM, &addr, &len);
|
|
|
|
STRING_new_temp_value(RETURN, addr, len);
|
|
|
|
|
|
|
|
SUBR_LEAVE();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void SUBR_val(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
char *addr;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
SUBR_ENTER_PARAM(1);
|
|
|
|
|
2010-09-23 03:34:15 +02:00
|
|
|
if (SUBR_check_string(PARAM))
|
|
|
|
RETURN->type = T_NULL;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
VALUE_get_string(PARAM, &addr, &len);
|
|
|
|
VALUE_from_string(RETURN, addr, len);
|
|
|
|
VALUE_conv_variant(RETURN);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
SUBR_LEAVE();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
void SUBR_format(ushort code)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
int fmt_type;
|
|
|
|
char *format = NULL;
|
|
|
|
int len = 0;
|
|
|
|
DATE_SERIAL *date;
|
|
|
|
char *str;
|
|
|
|
int len_str;
|
|
|
|
|
|
|
|
SUBR_ENTER();
|
|
|
|
|
|
|
|
if (NPARAM == 1)
|
|
|
|
fmt_type = LF_STANDARD;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (PARAM[1].type == T_VARIANT)
|
|
|
|
VARIANT_undo(&PARAM[1]);
|
|
|
|
|
|
|
|
if (TYPE_is_string(PARAM[1].type))
|
|
|
|
{
|
|
|
|
fmt_type = LF_USER;
|
|
|
|
VALUE_get_string(&PARAM[1], &format, &len);
|
|
|
|
}
|
|
|
|
else if (TYPE_is_integer(PARAM[1].type))
|
|
|
|
{
|
|
|
|
fmt_type = PARAM[1]._integer.value;
|
2009-05-01 18:27:45 +02:00
|
|
|
if (fmt_type <= LF_USER || fmt_type >= LF_MAX)
|
|
|
|
THROW(E_ARG);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
THROW(E_TYPE, TYPE_get_name(T_INTEGER), TYPE_get_name(PARAM[1].type));
|
2010-09-22 01:49:32 +02:00
|
|
|
|
|
|
|
if (!len)
|
|
|
|
fmt_type = LF_STANDARD;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (PARAM->type == T_VARIANT)
|
|
|
|
VARIANT_undo(PARAM);
|
|
|
|
|
|
|
|
if (PARAM->type == T_DATE)
|
|
|
|
{
|
|
|
|
date = DATE_split(PARAM);
|
|
|
|
if (LOCAL_format_date(date, fmt_type, format, len, &str, &len_str))
|
|
|
|
THROW(E_FORMAT);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-06-03 01:23:50 +02:00
|
|
|
VALUE_conv_float(PARAM);
|
2007-12-30 17:41:49 +01:00
|
|
|
if (LOCAL_format_number(PARAM->_float.value, fmt_type, format, len, &str, &len_str, TRUE))
|
|
|
|
THROW(E_FORMAT);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*if (NPARAM >= 2)
|
|
|
|
RELEASE_STRING(&PARAM[1]);*/
|
|
|
|
|
|
|
|
STRING_new_temp_value(RETURN, str, len_str);
|
|
|
|
|
|
|
|
SUBR_LEAVE();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-20 04:26:00 +01:00
|
|
|
void SUBR_hex_bin(ushort code)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-12-20 04:26:00 +01:00
|
|
|
static const int base[] = { 2, 16, 8 };
|
|
|
|
static const int max_prec[] = { 64, 16, 22 };
|
|
|
|
|
|
|
|
int mode;
|
2007-12-30 17:41:49 +01:00
|
|
|
int prec = 0;
|
|
|
|
|
|
|
|
SUBR_ENTER();
|
|
|
|
|
|
|
|
VALUE_conv(PARAM, T_LONG);
|
|
|
|
|
2010-12-20 04:26:00 +01:00
|
|
|
mode = (code >> 8) - CODE_BIN;
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
if (NPARAM == 2)
|
|
|
|
{
|
2010-06-03 01:23:50 +02:00
|
|
|
VALUE_conv_integer(&PARAM[1]);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
prec = PARAM[1]._integer.value;
|
|
|
|
|
2010-12-20 04:26:00 +01:00
|
|
|
if (prec < 1 || prec > max_prec[mode])
|
2007-12-30 17:41:49 +01:00
|
|
|
THROW(E_ARG);
|
|
|
|
}
|
|
|
|
|
2010-12-20 04:26:00 +01:00
|
|
|
NUMBER_int_to_string(PARAM->_long.value, prec, base[mode], RETURN);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
SUBR_LEAVE();
|
|
|
|
}
|
|
|
|
|
|
|
|
|