2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2009-08-17 12:41:51 +02:00
|
|
|
gb_table_temp.h
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2018-02-12 02:53:46 +01:00
|
|
|
(c) 2000-2017 Benoît Minisini <g4mba5@gmail.com>
|
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
|
2011-06-03 02:51:09 +02:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
2011-12-31 03:39:20 +01:00
|
|
|
MA 02110-1301, USA.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
2019-05-08 06:13:04 +02:00
|
|
|
#define __GB_TABLE_C
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
#include "gb_error.h"
|
|
|
|
#include "gb_alloc.h"
|
|
|
|
#include "gb_limit.h"
|
|
|
|
|
|
|
|
#include "gb_table.h"
|
|
|
|
|
|
|
|
#define SYM(table, ind) (TABLE_get_symbol(table, ind))
|
2010-05-27 00:01:28 +02:00
|
|
|
#define SSYM(_symbol, _pos, _size) ((SYMBOL *)((char *)(_symbol) + (_pos) * (_size)))
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2019-05-08 06:13:04 +02:00
|
|
|
bool TABLE_new_symbol = FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
static char _buffer[MAX_SYMBOL_LEN + 1];
|
|
|
|
|
2016-03-20 19:29:12 +01:00
|
|
|
#if TABLE_USE_KEY
|
|
|
|
static inline uint make_key(const char *sym, int len)
|
|
|
|
{
|
|
|
|
static void *jump[] = { &&__LEN_0, &&__LEN_1, &&__LEN_2, &&__LEN_3 };
|
|
|
|
const uchar *p = (uchar *)sym;
|
|
|
|
|
|
|
|
if (len >= 4)
|
|
|
|
return (p[0] << 24) + (p[1] << 16) + (p[2] << 8) + p[3];
|
|
|
|
|
|
|
|
goto *jump[len];
|
|
|
|
|
|
|
|
__LEN_0: return 0;
|
|
|
|
__LEN_1: return (p[0] << 24);
|
|
|
|
__LEN_2: return (p[0] << 24) + (p[1] << 16);
|
|
|
|
__LEN_3: return (p[0] << 24) + (p[1] << 16) + (p[2] << 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint make_key_ignore_case(const char *sym, int len)
|
|
|
|
{
|
|
|
|
static void *jump[] = { &&__LEN_0, &&__LEN_1, &&__LEN_2, &&__LEN_3 };
|
|
|
|
const uchar *p = (uchar *)sym;
|
|
|
|
|
|
|
|
if (len >= 4)
|
|
|
|
return (tolower(p[0]) << 24) + (tolower(p[1]) << 16) + (tolower(p[2]) << 8) + tolower(p[3]);
|
|
|
|
|
|
|
|
goto *jump[len];
|
|
|
|
|
|
|
|
__LEN_0: return 0;
|
|
|
|
__LEN_1: return (tolower(p[0]) << 24);
|
|
|
|
__LEN_2: return (tolower(p[0]) << 24) + (tolower(p[1]) << 16);
|
|
|
|
__LEN_3: return (tolower(p[0]) << 24) + (tolower(p[1]) << 16) + (tolower(p[2]) << 8);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-08-06 22:14:41 +02:00
|
|
|
char TABLE_compare(const char *s1, int len1, const char *s2, int len2)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-09-05 22:54:36 +02:00
|
|
|
int i;
|
|
|
|
int len = (len1 < len2) ? len1 : len2;
|
2018-01-16 09:53:38 +01:00
|
|
|
register unsigned char c1;
|
|
|
|
register unsigned char c2;
|
2009-09-05 22:54:36 +02:00
|
|
|
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
c1 = s1[i];
|
|
|
|
c2 = s2[i];
|
|
|
|
|
2010-05-19 22:18:23 +02:00
|
|
|
if (LIKELY(c1 > c2)) return 1;
|
|
|
|
if (LIKELY(c1 < c2)) return -1;
|
2009-09-05 22:54:36 +02:00
|
|
|
}
|
|
|
|
|
2010-05-19 22:18:23 +02:00
|
|
|
if (LIKELY(len1 < len2))
|
2009-09-05 22:54:36 +02:00
|
|
|
return -1;
|
2010-05-19 22:18:23 +02:00
|
|
|
else if (LIKELY(len1 > len2))
|
2009-09-05 22:54:36 +02:00
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2012-08-06 22:14:41 +02:00
|
|
|
char TABLE_compare_ignore_case(const char *s1, int len1, const char *s2, int len2)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-09-05 22:54:36 +02:00
|
|
|
unsigned int len = (len1 < len2) ? len1 : len2;
|
|
|
|
unsigned int i;
|
|
|
|
int result;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-08-06 22:14:41 +02:00
|
|
|
for (i = 0; len > 0; i++)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
result = toupper(s1[i]) - toupper(s2[i]);
|
2010-05-19 22:18:23 +02:00
|
|
|
if (LIKELY(result))
|
2007-12-30 17:41:49 +01:00
|
|
|
return result; // < 0 ? -1 : 1;
|
2012-05-12 14:04:20 +02:00
|
|
|
len--;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2010-05-19 22:18:23 +02:00
|
|
|
if (LIKELY(len1 < len2))
|
2009-09-05 22:54:36 +02:00
|
|
|
return -1;
|
2010-05-19 22:18:23 +02:00
|
|
|
else if (LIKELY(len1 > len2))
|
2009-09-05 22:54:36 +02:00
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2012-08-06 22:14:41 +02:00
|
|
|
char TABLE_compare_ignore_case_len(const char *s1, int len1, const char *s2, int len2)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-09-05 22:54:36 +02:00
|
|
|
int result;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-05-19 22:18:23 +02:00
|
|
|
if (LIKELY(len1 < len2))
|
2009-09-05 22:54:36 +02:00
|
|
|
return -1;
|
2010-05-19 22:18:23 +02:00
|
|
|
else if (LIKELY(len1 > len2))
|
2009-09-05 22:54:36 +02:00
|
|
|
return 1;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
while (len1)
|
|
|
|
{
|
|
|
|
result = tolower(*s1++) - tolower(*s2++);
|
2010-05-19 22:18:23 +02:00
|
|
|
if (LIKELY(result))
|
2007-12-30 17:41:49 +01:00
|
|
|
return result; // < 0 ? -1 : 1;
|
|
|
|
--len1;
|
|
|
|
}
|
|
|
|
|
2009-09-05 22:54:36 +02:00
|
|
|
return 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2019-05-08 06:13:04 +02:00
|
|
|
static inline int search(void *symbol, ushort *sort, int n_symbol, size_t size, const char *name, int len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-09-05 22:54:36 +02:00
|
|
|
int pos, deb, fin;
|
|
|
|
SYMBOL *sym;
|
2010-05-27 00:01:28 +02:00
|
|
|
int l;
|
2012-10-10 16:58:14 +02:00
|
|
|
int result; // must be an integer (or a short) because uchar - uchar may not fit in a char!
|
2010-05-27 00:01:28 +02:00
|
|
|
const uchar *s1;
|
|
|
|
const uchar *s2;
|
2016-03-20 19:29:12 +01:00
|
|
|
#if TABLE_USE_KEY
|
|
|
|
uint key = make_key(name, len);
|
|
|
|
#endif
|
|
|
|
|
2009-09-05 22:54:36 +02:00
|
|
|
pos = 0;
|
|
|
|
deb = 0;
|
|
|
|
fin = n_symbol; //ARRAY_count(table->symbol);
|
|
|
|
|
2016-03-14 02:40:50 +01:00
|
|
|
for(;;)
|
2009-09-05 22:54:36 +02:00
|
|
|
{
|
2016-03-14 02:40:50 +01:00
|
|
|
if (UNLIKELY(deb >= fin))
|
2019-05-08 06:13:04 +02:00
|
|
|
return (-deb) - 1;
|
2009-09-05 22:54:36 +02:00
|
|
|
|
2016-03-14 02:40:50 +01:00
|
|
|
pos = (deb + fin) >> 1;
|
|
|
|
|
|
|
|
sym = SSYM(symbol, sort[pos], size);
|
|
|
|
|
|
|
|
if (LIKELY(len < sym->len))
|
|
|
|
goto __B_LOWER;
|
|
|
|
else if (LIKELY(len > sym->len))
|
|
|
|
goto __B_GREATER;
|
|
|
|
|
2016-03-20 19:29:12 +01:00
|
|
|
#if TABLE_USE_KEY
|
|
|
|
if (key < sym->key) goto __B_LOWER;
|
|
|
|
if (key > sym->key) goto __B_GREATER;
|
2016-03-14 02:40:50 +01:00
|
|
|
|
2016-03-20 19:29:12 +01:00
|
|
|
l = len - 4;
|
|
|
|
|
|
|
|
if (l > 0)
|
2016-03-14 02:40:50 +01:00
|
|
|
{
|
2016-03-20 19:29:12 +01:00
|
|
|
s1 = (uchar *)name + 4;
|
|
|
|
s2 = (uchar *)sym->name + 4;
|
2009-09-05 22:54:36 +02:00
|
|
|
|
2016-03-20 19:29:12 +01:00
|
|
|
#else
|
|
|
|
{
|
|
|
|
l = len;
|
|
|
|
s1 = (uchar *)name;
|
|
|
|
s2 = (uchar *)sym->name;
|
|
|
|
#endif
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
result = *s1 - *s2;
|
|
|
|
|
|
|
|
if (LIKELY(result < 0))
|
|
|
|
goto __B_LOWER;
|
|
|
|
else if (LIKELY(result > 0))
|
|
|
|
goto __B_GREATER;
|
|
|
|
|
|
|
|
if (UNLIKELY(--l == 0))
|
|
|
|
break;
|
|
|
|
|
|
|
|
s1++;
|
|
|
|
s2++;
|
|
|
|
}
|
2009-09-05 22:54:36 +02:00
|
|
|
}
|
2016-03-14 02:40:50 +01:00
|
|
|
|
2019-05-08 06:13:04 +02:00
|
|
|
return pos;
|
2016-03-14 02:40:50 +01:00
|
|
|
|
|
|
|
__B_LOWER: fin = pos; continue;
|
|
|
|
__B_GREATER: deb = pos + 1; continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-08 06:13:04 +02:00
|
|
|
static inline int search_ignore_case(void *symbol, ushort *sort, int n_symbol, size_t size, const char *name, int len)
|
2016-03-14 02:40:50 +01:00
|
|
|
{
|
|
|
|
int pos, deb, fin;
|
|
|
|
SYMBOL *sym;
|
|
|
|
int l;
|
|
|
|
int result; // must be an integer (or a short) because uchar - uchar may not fit in a char!
|
|
|
|
const uchar *s1;
|
|
|
|
const uchar *s2;
|
2016-03-20 19:29:12 +01:00
|
|
|
#if TABLE_USE_KEY
|
|
|
|
uint key = make_key_ignore_case(name, len);
|
|
|
|
#endif
|
2016-03-14 02:40:50 +01:00
|
|
|
|
|
|
|
pos = 0;
|
|
|
|
deb = 0;
|
|
|
|
fin = n_symbol;
|
|
|
|
|
|
|
|
for(;;)
|
2009-09-05 22:54:36 +02:00
|
|
|
{
|
2016-03-14 02:40:50 +01:00
|
|
|
if (UNLIKELY(deb >= fin))
|
2009-09-05 22:54:36 +02:00
|
|
|
{
|
2019-05-08 06:13:04 +02:00
|
|
|
return -deb - 1;
|
|
|
|
/**index = deb;
|
|
|
|
return FALSE;*/
|
2016-03-14 02:40:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pos = (deb + fin) >> 1;
|
|
|
|
|
|
|
|
sym = SSYM(symbol, sort[pos], size);
|
|
|
|
|
|
|
|
if (LIKELY(len < sym->len))
|
|
|
|
goto __T_LOWER;
|
|
|
|
else if (LIKELY(len > sym->len))
|
|
|
|
goto __T_GREATER;
|
2016-03-20 19:29:12 +01:00
|
|
|
|
|
|
|
#if TABLE_USE_KEY
|
|
|
|
if (key < sym->key) goto __T_LOWER;
|
|
|
|
if (key > sym->key) goto __T_GREATER;
|
2016-03-14 02:40:50 +01:00
|
|
|
|
2016-03-20 19:29:12 +01:00
|
|
|
l = len - 4;
|
2016-03-14 02:40:50 +01:00
|
|
|
|
2016-03-20 19:29:12 +01:00
|
|
|
if (l > 0)
|
2016-03-14 02:40:50 +01:00
|
|
|
{
|
2016-03-20 19:29:12 +01:00
|
|
|
s1 = (uchar *)name + 4;
|
|
|
|
s2 = (uchar *)sym->name + 4;
|
|
|
|
#else
|
|
|
|
{
|
|
|
|
l = len;
|
|
|
|
s1 = (uchar *)name;
|
|
|
|
s2 = (uchar *)sym->name;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
result = tolower(*s1) - tolower(*s2);
|
|
|
|
|
|
|
|
if (LIKELY(result < 0))
|
|
|
|
goto __T_LOWER;
|
|
|
|
else if (LIKELY(result > 0))
|
|
|
|
goto __T_GREATER;
|
|
|
|
|
|
|
|
if (UNLIKELY(--l == 0))
|
|
|
|
break;
|
|
|
|
|
|
|
|
s1++;
|
|
|
|
s2++;
|
|
|
|
}
|
2016-03-14 02:40:50 +01:00
|
|
|
}
|
|
|
|
|
2019-05-08 06:13:04 +02:00
|
|
|
return pos;
|
|
|
|
/**index = pos;
|
|
|
|
return TRUE;*/
|
2016-03-14 02:40:50 +01:00
|
|
|
|
|
|
|
__T_LOWER: fin = pos; continue;
|
|
|
|
__T_GREATER: deb = pos + 1; continue;
|
2009-09-05 22:54:36 +02:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-04 09:29:39 +02:00
|
|
|
const char *TABLE_get_symbol_name(TABLE *table, int index)
|
|
|
|
{
|
2010-05-19 22:18:23 +02:00
|
|
|
if (UNLIKELY((index < 0) || (index >= ARRAY_count(table->symbol))))
|
2009-09-05 22:54:36 +02:00
|
|
|
strcpy(_buffer, "?");
|
|
|
|
else
|
2012-09-04 09:29:39 +02:00
|
|
|
SYMBOL_get_name(SYM(table, index));
|
2009-09-05 22:54:36 +02:00
|
|
|
|
|
|
|
return _buffer;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
const char *TABLE_get_symbol_name_suffix(TABLE *table, int index, const char* suffix)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2013-01-06 17:13:31 +01:00
|
|
|
SYMBOL *sym;
|
|
|
|
|
|
|
|
if (UNLIKELY((index < 0) || (index >= ARRAY_count(table->symbol))))
|
|
|
|
return "?";
|
|
|
|
|
|
|
|
sym = SYM(table, index);
|
|
|
|
if ((sym->len + strlen(suffix)) > MAX_SYMBOL_LEN)
|
|
|
|
return "?";
|
|
|
|
|
|
|
|
SYMBOL_get_name(sym);
|
2009-09-05 22:54:36 +02:00
|
|
|
strcat(_buffer, suffix);
|
|
|
|
return _buffer;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
void TABLE_create_static(TABLE *table, size_t size, TABLE_FLAG flag)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-05-25 13:19:00 +02:00
|
|
|
ARRAY_create_with_size(&table->symbol, Max(size, sizeof(SYMBOL)), 64);
|
2010-05-29 23:10:36 +02:00
|
|
|
ARRAY_create_with_size(&table->sort, sizeof(ushort), 64);
|
2009-09-05 22:54:36 +02:00
|
|
|
table->flag = flag;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
void TABLE_create(TABLE **result, size_t size, TABLE_FLAG flag)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-09-05 22:54:36 +02:00
|
|
|
TABLE *table;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-03-30 00:33:01 +01:00
|
|
|
ALLOC(&table, sizeof(TABLE));
|
2009-09-05 22:54:36 +02:00
|
|
|
TABLE_create_static(table, size, flag);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-09-05 22:54:36 +02:00
|
|
|
*result = table;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
void TABLE_create_from(TABLE **result, size_t size, const char *sym_list[], TABLE_FLAG flag)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-09-05 22:54:36 +02:00
|
|
|
TABLE *table;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-09-05 22:54:36 +02:00
|
|
|
TABLE_create(&table, size, flag);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-09-05 22:54:36 +02:00
|
|
|
while (*sym_list)
|
|
|
|
{
|
2019-05-08 06:13:04 +02:00
|
|
|
TABLE_add_symbol(table, *sym_list, strlen(*sym_list));
|
2009-09-05 22:54:36 +02:00
|
|
|
sym_list++;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-09-05 22:54:36 +02:00
|
|
|
*result = table;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
void TABLE_delete_static(TABLE *table)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-09-05 22:54:36 +02:00
|
|
|
ARRAY_delete(&table->symbol);
|
2010-05-27 15:51:09 +02:00
|
|
|
ARRAY_delete(&table->sort);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
void TABLE_delete(TABLE **p_table)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-09-05 22:54:36 +02:00
|
|
|
if (*p_table)
|
|
|
|
{
|
|
|
|
TABLE_delete_static(*p_table);
|
2013-03-30 00:33:01 +01:00
|
|
|
FREE(p_table);
|
2009-09-05 22:54:36 +02:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-25 13:19:00 +02:00
|
|
|
bool TABLE_find_symbol(TABLE *table, const char *name, int len, int *index)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-09-05 22:54:36 +02:00
|
|
|
int ind;
|
2010-05-27 15:51:09 +02:00
|
|
|
SYMBOL *tsym;
|
2009-09-05 22:54:36 +02:00
|
|
|
int count;
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
tsym = table->symbol;
|
|
|
|
count = ARRAY_count(tsym);
|
|
|
|
size = ARRAY_size(tsym);
|
|
|
|
|
2016-03-14 02:40:50 +01:00
|
|
|
if (table->flag)
|
2019-05-08 06:13:04 +02:00
|
|
|
ind = search_ignore_case(tsym, table->sort, count, size, name, len);
|
2016-03-14 02:40:50 +01:00
|
|
|
else
|
2019-05-08 06:13:04 +02:00
|
|
|
ind = search(tsym, table->sort, count, size, name, len);
|
2009-09-05 22:54:36 +02:00
|
|
|
|
2019-05-08 06:13:04 +02:00
|
|
|
if (ind >= 0)
|
|
|
|
{
|
2010-05-27 15:51:09 +02:00
|
|
|
*index = table->sort[ind];
|
2019-05-08 06:13:04 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2016-03-20 02:27:31 +01:00
|
|
|
#if 0
|
2009-07-08 21:57:50 +02:00
|
|
|
void TABLE_add_new_symbol_without_sort(TABLE *table, const char *name, int len, int sort, SYMBOL **symbol, int *index)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-09-05 22:54:36 +02:00
|
|
|
SYMBOL *sym;
|
|
|
|
int count;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-09-05 22:54:36 +02:00
|
|
|
len = Min(len, MAX_SYMBOL_LEN);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-09-05 22:54:36 +02:00
|
|
|
count = ARRAY_count(table->symbol);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-05-29 23:10:36 +02:00
|
|
|
sym = (SYMBOL *)ARRAY_add_void_size(&table->symbol);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-09-05 22:54:36 +02:00
|
|
|
sym->name = (char *)name;
|
|
|
|
sym->len = len;
|
2010-05-27 15:51:09 +02:00
|
|
|
|
|
|
|
*((ushort *)ARRAY_add(&table->sort)) = sort;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-09-05 22:54:36 +02:00
|
|
|
if (symbol) *symbol = sym; /*&table->symbol[ind];*/
|
|
|
|
if (index) *index = count;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
2016-03-20 02:27:31 +01:00
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2019-05-08 06:13:04 +02:00
|
|
|
int TABLE_add_symbol(TABLE *table, const char *name, int len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-09-05 22:54:36 +02:00
|
|
|
int ind;
|
|
|
|
SYMBOL *sym;
|
|
|
|
int count;
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
/*len = Min(len, MAX_SYMBOL_LEN);*/
|
2010-05-27 15:51:09 +02:00
|
|
|
//len = Min(len, 65535);
|
2009-09-05 22:54:36 +02:00
|
|
|
|
|
|
|
count = ARRAY_count(table->symbol);
|
|
|
|
size = ARRAY_size(table->symbol);
|
|
|
|
|
2016-03-14 02:40:50 +01:00
|
|
|
if (table->flag)
|
2019-05-08 06:13:04 +02:00
|
|
|
ind = search_ignore_case(table->symbol, table->sort, count, size, name, len);
|
2016-03-14 02:40:50 +01:00
|
|
|
else
|
2019-05-08 06:13:04 +02:00
|
|
|
ind = search(table->symbol, table->sort, count, size, name, len);
|
2009-09-05 22:54:36 +02:00
|
|
|
|
2019-05-08 06:13:04 +02:00
|
|
|
if (ind < 0)
|
2009-09-05 22:54:36 +02:00
|
|
|
{
|
2019-05-08 06:13:04 +02:00
|
|
|
ind = -ind - 1;
|
2010-05-29 23:10:36 +02:00
|
|
|
sym = (SYMBOL *)ARRAY_add_void_size(&table->symbol);
|
2009-09-05 22:54:36 +02:00
|
|
|
|
|
|
|
sym->name = (char *)name;
|
2010-05-27 15:51:09 +02:00
|
|
|
sym->len = len;
|
2016-03-20 19:29:12 +01:00
|
|
|
#if TABLE_USE_KEY
|
|
|
|
sym->key = table->flag ? make_key_ignore_case(name, len) : make_key(name, len);
|
|
|
|
#endif
|
2010-05-27 15:51:09 +02:00
|
|
|
|
2009-09-05 22:54:36 +02:00
|
|
|
/*
|
|
|
|
printf("TABLE_add_symbol: %.*s %d %d\n", len, name, ((CLASS_SYMBOL *)sym)->global.type,
|
|
|
|
((CLASS_SYMBOL *)sym)->local.type);
|
|
|
|
*/
|
|
|
|
|
|
|
|
//s1 = (SYMBOL *)((char *)table->symbol + count * size);
|
2010-05-27 15:51:09 +02:00
|
|
|
ARRAY_add(&table->sort);
|
|
|
|
if (count > ind)
|
|
|
|
memmove(&table->sort[ind + 1], &table->sort[ind], sizeof(ushort) * (count - ind));
|
|
|
|
|
|
|
|
table->sort[ind] = (ushort)count;
|
2009-09-05 22:54:36 +02:00
|
|
|
ind = count;
|
2019-05-08 06:13:04 +02:00
|
|
|
TABLE_new_symbol = TRUE;
|
2009-09-05 22:54:36 +02:00
|
|
|
}
|
|
|
|
else
|
2010-05-27 15:51:09 +02:00
|
|
|
ind = table->sort[ind]; //SYM(table, ind)->sort; /*table->symbol[ind].sort;*/
|
2009-09-05 22:54:36 +02:00
|
|
|
|
2019-05-08 06:13:04 +02:00
|
|
|
return ind;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
void TABLE_print(TABLE *table, bool sort)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-09-05 22:54:36 +02:00
|
|
|
int i;
|
|
|
|
SYMBOL *sym;
|
|
|
|
|
|
|
|
fprintf(stderr, "capacity %i\n", ARRAY_count(table->symbol));
|
|
|
|
|
|
|
|
/*
|
|
|
|
for (i = 0; i < ARRAY_count(table->symbol); i++)
|
|
|
|
{
|
|
|
|
sym = SYM(table, i);
|
|
|
|
printf("%*s (%li) ", (int)sym->len, sym->name, sym->sort);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_count(table->symbol); i++)
|
|
|
|
{
|
|
|
|
if (sort)
|
|
|
|
{
|
2010-05-27 15:51:09 +02:00
|
|
|
sym = SYM(table, table->sort[i]);
|
2009-09-05 22:54:36 +02:00
|
|
|
fprintf(stderr, "%.*s ", (int)sym->len, sym->name);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
2009-09-05 22:54:36 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
sym = SYM(table, i);
|
2010-05-27 15:51:09 +02:00
|
|
|
fprintf(stderr, "%d %.*s ", (int)table->sort[i], (int)sym->len, sym->name);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2009-09-05 22:54:36 +02:00
|
|
|
//if ((i > 0) && (!(i & 0xF)))
|
|
|
|
// fprintf(stderr, "\n");
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-09-05 22:54:36 +02:00
|
|
|
fprintf(stderr, "\n\n");
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-08 06:13:04 +02:00
|
|
|
int TABLE_copy_symbol_with_prefix(TABLE *table, int ind_src, char prefix)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-09-05 22:54:36 +02:00
|
|
|
SYMBOL *sym;
|
2010-03-05 09:49:38 +01:00
|
|
|
char *ptr;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-09-05 22:54:36 +02:00
|
|
|
sym = TABLE_get_symbol(table, ind_src);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-03-05 09:49:38 +01:00
|
|
|
ptr = (char *)sym->name - 1;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-05-19 22:18:23 +02:00
|
|
|
if (UNLIKELY(!isspace((unsigned char)*ptr)))
|
2009-09-05 22:54:36 +02:00
|
|
|
ERROR_panic("Cannot add prefix to symbol");
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-09-05 22:54:36 +02:00
|
|
|
*ptr = prefix;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2019-05-08 06:13:04 +02:00
|
|
|
return TABLE_add_symbol(table, ptr, sym->len + 1);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
SYMBOL *TABLE_get_symbol_sort(TABLE *table, int index)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-05-27 15:51:09 +02:00
|
|
|
return TABLE_get_symbol(table, table->sort[index]);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2016-03-20 19:29:12 +01:00
|
|
|
// Symbol tables with no TABLE structure
|
|
|
|
|
|
|
|
int SYMBOL_find(void *symbol, ushort *sort, int n_symbol, size_t s_symbol, int flag, const char *name, int len, const char *prefix)
|
|
|
|
{
|
|
|
|
int index;
|
|
|
|
int len_prefix;
|
|
|
|
|
|
|
|
if (UNLIKELY(prefix != NULL))
|
|
|
|
{
|
|
|
|
len_prefix = strlen(prefix);
|
|
|
|
|
|
|
|
if (UNLIKELY((len + len_prefix) > MAX_SYMBOL_LEN))
|
|
|
|
ERROR_panic("SYMBOL_find: prefixed symbol too long");
|
|
|
|
|
|
|
|
strcpy(_buffer, prefix);
|
|
|
|
strcpy(&_buffer[len_prefix], name);
|
|
|
|
len += len_prefix;
|
|
|
|
name = _buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flag)
|
2019-05-08 06:13:04 +02:00
|
|
|
index = search_ignore_case(symbol, sort, n_symbol, s_symbol, name, len);
|
2016-03-20 19:29:12 +01:00
|
|
|
else
|
2019-05-08 06:13:04 +02:00
|
|
|
index = search(symbol, sort, n_symbol, s_symbol, name, len);
|
2016-03-20 19:29:12 +01:00
|
|
|
|
2019-05-08 06:13:04 +02:00
|
|
|
if (index >= 0)
|
|
|
|
return sort[index];
|
|
|
|
else
|
|
|
|
return NO_SYMBOL;
|
2016-03-20 19:29:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char *SYMBOL_get_name(SYMBOL *sym)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
|
|
|
|
len = Min(MAX_SYMBOL_LEN, sym->len);
|
|
|
|
memcpy(_buffer, sym->name, len);
|
|
|
|
_buffer[len] = 0;
|
|
|
|
|
|
|
|
return _buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if TABLE_USE_KEY
|
|
|
|
void SYMBOL_compute_keys(void *symbol, int n_symbol, size_t s_symbol, int flag)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
SYMBOL *sym;
|
|
|
|
|
|
|
|
if (flag)
|
|
|
|
{
|
|
|
|
for (i = 0; i < n_symbol; i++)
|
|
|
|
{
|
|
|
|
sym = SSYM(symbol, i, s_symbol);
|
|
|
|
sym->key = make_key_ignore_case(sym->name, sym->len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (i = 0; i < n_symbol; i++)
|
|
|
|
{
|
|
|
|
sym = SSYM(symbol, i, s_symbol);
|
|
|
|
sym->key = make_key(sym->name, sym->len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|