2006-01-29 18:28:14 +00:00
|
|
|
/*
|
2006-09-25 06:17:19 +00:00
|
|
|
* $Id: tab.c,v 1.18 2006-09-25 06:17:19 bacon Exp $
|
2006-01-29 18:28:14 +00:00
|
|
|
*/
|
|
|
|
|
2006-03-31 16:35:37 +00:00
|
|
|
#include <xp/awk/awk_i.h>
|
2006-01-30 14:45:12 +00:00
|
|
|
|
2006-04-16 04:31:38 +00:00
|
|
|
#ifndef XP_AWK_STAND_ALONE
|
2006-01-29 18:28:14 +00:00
|
|
|
#include <xp/bas/assert.h>
|
|
|
|
#endif
|
|
|
|
|
2006-08-31 15:09:24 +00:00
|
|
|
xp_awk_tab_t* xp_awk_tab_open (xp_awk_tab_t* tab, xp_awk_t* awk)
|
2006-01-29 18:28:14 +00:00
|
|
|
{
|
2006-03-07 15:55:14 +00:00
|
|
|
if (tab == XP_NULL)
|
|
|
|
{
|
2006-08-31 15:09:24 +00:00
|
|
|
tab = (xp_awk_tab_t*) XP_AWK_MALLOC (
|
|
|
|
awk, xp_sizeof(xp_awk_tab_t));
|
2006-01-29 18:28:14 +00:00
|
|
|
if (tab == XP_NULL) return XP_NULL;
|
|
|
|
tab->__dynamic = xp_true;
|
|
|
|
}
|
|
|
|
else tab->__dynamic = xp_false;
|
|
|
|
|
2006-08-31 15:09:24 +00:00
|
|
|
tab->awk = awk;
|
2006-01-29 18:28:14 +00:00
|
|
|
tab->buf = XP_NULL;
|
|
|
|
tab->size = 0;
|
|
|
|
tab->capa = 0;
|
|
|
|
|
|
|
|
return tab;
|
|
|
|
}
|
|
|
|
|
|
|
|
void xp_awk_tab_close (xp_awk_tab_t* tab)
|
|
|
|
{
|
|
|
|
xp_awk_tab_clear (tab);
|
2006-03-07 15:55:14 +00:00
|
|
|
if (tab->buf != XP_NULL)
|
|
|
|
{
|
2006-08-31 15:09:24 +00:00
|
|
|
XP_AWK_FREE (tab->awk, tab->buf);
|
2006-01-29 18:28:14 +00:00
|
|
|
tab->buf = XP_NULL;
|
|
|
|
tab->capa = 0;
|
|
|
|
}
|
|
|
|
|
2006-08-31 15:09:24 +00:00
|
|
|
if (tab->__dynamic) XP_AWK_FREE (tab->awk, tab);
|
2006-01-29 18:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
xp_size_t xp_awk_tab_getsize (xp_awk_tab_t* tab)
|
|
|
|
{
|
|
|
|
return tab->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
xp_size_t xp_awk_tab_getcapa (xp_awk_tab_t* tab)
|
|
|
|
{
|
|
|
|
return tab->capa;
|
|
|
|
}
|
|
|
|
|
|
|
|
xp_awk_tab_t* xp_awk_tab_setcapa (xp_awk_tab_t* tab, xp_size_t capa)
|
|
|
|
{
|
2006-08-03 06:06:27 +00:00
|
|
|
void* tmp;
|
2006-01-29 18:28:14 +00:00
|
|
|
|
2006-03-07 15:55:14 +00:00
|
|
|
if (tab->size > capa)
|
|
|
|
{
|
|
|
|
xp_awk_tab_remove (tab, capa, tab->size - capa);
|
2006-01-29 18:28:14 +00:00
|
|
|
xp_assert (tab->size <= capa);
|
|
|
|
}
|
|
|
|
|
2006-03-07 15:55:14 +00:00
|
|
|
if (capa > 0)
|
|
|
|
{
|
2006-08-31 15:11:17 +00:00
|
|
|
if (tab->awk->syscas->realloc != XP_NULL)
|
2006-08-16 11:35:54 +00:00
|
|
|
{
|
2006-08-31 15:11:17 +00:00
|
|
|
tmp = XP_AWK_REALLOC (tab->awk,
|
|
|
|
tab->buf, xp_sizeof(*tab->buf) * capa);
|
|
|
|
if (tmp == XP_NULL) return XP_NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tmp = XP_AWK_MALLOC (
|
|
|
|
tab->awk, xp_sizeof(*tab->buf) * capa);
|
|
|
|
if (tmp == XP_NULL) return XP_NULL;
|
|
|
|
if (tab->buf != XP_NULL)
|
|
|
|
{
|
|
|
|
xp_size_t x;
|
|
|
|
x = (capa > tab->capa)? tab->capa: capa;
|
2006-09-25 06:17:19 +00:00
|
|
|
XP_AWK_MEMCPY (
|
|
|
|
tab->awk, tmp, tab->buf,
|
|
|
|
xp_sizeof(*tab->buf) * x);
|
2006-08-31 15:11:17 +00:00
|
|
|
XP_AWK_FREE (tab->awk, tab->buf);
|
|
|
|
}
|
2006-08-16 11:35:54 +00:00
|
|
|
}
|
2006-01-29 18:28:14 +00:00
|
|
|
}
|
2006-03-07 15:55:14 +00:00
|
|
|
else
|
|
|
|
{
|
2006-08-31 15:09:24 +00:00
|
|
|
if (tab->buf != XP_NULL) XP_AWK_FREE (tab->awk, tab->buf);
|
2006-01-29 18:28:14 +00:00
|
|
|
tmp = XP_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
tab->buf = tmp;
|
|
|
|
tab->capa = capa;
|
|
|
|
|
|
|
|
return tab;
|
|
|
|
}
|
|
|
|
|
|
|
|
void xp_awk_tab_clear (xp_awk_tab_t* tab)
|
|
|
|
{
|
|
|
|
xp_size_t i;
|
|
|
|
|
|
|
|
xp_assert (tab != XP_NULL);
|
|
|
|
|
2006-03-07 15:55:14 +00:00
|
|
|
for (i = 0; i < tab->size; i++)
|
|
|
|
{
|
2006-08-31 15:09:24 +00:00
|
|
|
XP_AWK_FREE (tab->awk, tab->buf[i].name);
|
2006-08-03 06:06:27 +00:00
|
|
|
tab->buf[i].name = XP_NULL;
|
|
|
|
tab->buf[i].name_len = 0;
|
2006-01-29 18:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tab->size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-03-07 15:55:14 +00:00
|
|
|
xp_size_t xp_awk_tab_insert (
|
2006-08-03 06:06:27 +00:00
|
|
|
xp_awk_tab_t* tab, xp_size_t index,
|
|
|
|
const xp_char_t* str, xp_size_t len)
|
2006-01-29 18:28:14 +00:00
|
|
|
{
|
|
|
|
xp_size_t i;
|
2006-08-03 05:05:48 +00:00
|
|
|
xp_char_t* str_dup;
|
2006-01-29 18:28:14 +00:00
|
|
|
|
2006-09-01 03:44:51 +00:00
|
|
|
str_dup = xp_awk_strxdup (tab->awk, str, len);
|
2006-08-03 05:05:48 +00:00
|
|
|
if (str_dup == XP_NULL) return (xp_size_t)-1;
|
2006-01-29 18:28:14 +00:00
|
|
|
|
2006-03-07 15:55:14 +00:00
|
|
|
if (index >= tab->capa)
|
|
|
|
{
|
2006-01-29 18:28:14 +00:00
|
|
|
xp_size_t capa;
|
|
|
|
|
|
|
|
if (tab->capa <= 0) capa = (index + 1);
|
2006-03-07 15:55:14 +00:00
|
|
|
else
|
|
|
|
{
|
2006-01-29 18:28:14 +00:00
|
|
|
do { capa = tab->capa * 2; } while (index >= capa);
|
|
|
|
}
|
|
|
|
|
2006-03-07 15:55:14 +00:00
|
|
|
if (xp_awk_tab_setcapa(tab,capa) == XP_NULL)
|
|
|
|
{
|
2006-08-31 15:09:24 +00:00
|
|
|
XP_AWK_FREE (tab->awk, str_dup);
|
2006-01-29 18:28:14 +00:00
|
|
|
return (xp_size_t)-1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = tab->size; i > index; i--) tab->buf[i] = tab->buf[i-1];
|
2006-08-03 06:06:27 +00:00
|
|
|
tab->buf[index].name = str_dup;
|
|
|
|
tab->buf[index].name_len = len;
|
2006-01-29 18:28:14 +00:00
|
|
|
|
|
|
|
if (index > tab->size) tab->size = index + 1;
|
|
|
|
else tab->size++;
|
|
|
|
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2006-03-07 15:55:14 +00:00
|
|
|
xp_size_t xp_awk_tab_remove (
|
2006-01-29 18:28:14 +00:00
|
|
|
xp_awk_tab_t* tab, xp_size_t index, xp_size_t count)
|
|
|
|
{
|
|
|
|
xp_size_t i, j, k;
|
|
|
|
|
|
|
|
if (index >= tab->size) return 0;
|
|
|
|
if (count > tab->size - index) count = tab->size - index;
|
|
|
|
|
|
|
|
i = index;
|
|
|
|
j = index + count;
|
|
|
|
k = index + count;
|
|
|
|
|
2006-03-07 15:55:14 +00:00
|
|
|
while (i < k)
|
|
|
|
{
|
2006-08-31 15:09:24 +00:00
|
|
|
XP_AWK_FREE (tab->awk, tab->buf[i].name);
|
2006-03-07 15:55:14 +00:00
|
|
|
|
2006-01-29 18:28:14 +00:00
|
|
|
if (j >= tab->size)
|
2006-03-07 15:55:14 +00:00
|
|
|
{
|
2006-08-03 06:06:27 +00:00
|
|
|
tab->buf[i].name = XP_NULL;
|
|
|
|
tab->buf[i].name_len = 0;
|
2006-03-07 15:55:14 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-08-03 06:06:27 +00:00
|
|
|
tab->buf[i].name = tab->buf[j].name;
|
|
|
|
tab->buf[i].name_len = tab->buf[j].name_len;
|
2006-03-07 15:55:14 +00:00
|
|
|
i++; j++;
|
|
|
|
}
|
2006-01-29 18:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tab->size -= count;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2006-08-03 06:06:27 +00:00
|
|
|
xp_size_t xp_awk_tab_add (
|
|
|
|
xp_awk_tab_t* tab, const xp_char_t* str, xp_size_t len)
|
2006-01-29 18:28:14 +00:00
|
|
|
{
|
2006-08-03 06:06:27 +00:00
|
|
|
return xp_awk_tab_insert (tab, tab->size, str, len);
|
2006-01-29 18:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
xp_size_t xp_awk_tab_find (
|
2006-08-03 06:06:27 +00:00
|
|
|
xp_awk_tab_t* tab, xp_size_t index,
|
|
|
|
const xp_char_t* str, xp_size_t len)
|
2006-01-29 18:28:14 +00:00
|
|
|
{
|
|
|
|
xp_size_t i;
|
|
|
|
|
2006-08-03 05:05:48 +00:00
|
|
|
for (i = index; i < tab->size; i++)
|
|
|
|
{
|
2006-09-01 06:23:58 +00:00
|
|
|
if (xp_awk_strxncmp (
|
2006-08-03 06:06:27 +00:00
|
|
|
tab->buf[i].name, tab->buf[i].name_len,
|
|
|
|
str, len) == 0) return i;
|
2006-01-29 18:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (xp_size_t)-1;
|
|
|
|
}
|
|
|
|
|
2006-02-05 13:45:59 +00:00
|
|
|
xp_size_t xp_awk_tab_rfind (
|
2006-08-03 06:06:27 +00:00
|
|
|
xp_awk_tab_t* tab, xp_size_t index,
|
|
|
|
const xp_char_t* str, xp_size_t len)
|
2006-02-05 13:45:59 +00:00
|
|
|
{
|
|
|
|
xp_size_t i;
|
|
|
|
|
2006-02-05 14:21:18 +00:00
|
|
|
if (index >= tab->size) return (xp_size_t)-1;
|
|
|
|
|
2006-08-03 05:05:48 +00:00
|
|
|
for (i = index + 1; i-- > 0; )
|
|
|
|
{
|
2006-09-01 06:23:58 +00:00
|
|
|
if (xp_awk_strxncmp (
|
2006-08-03 06:06:27 +00:00
|
|
|
tab->buf[i].name, tab->buf[i].name_len,
|
|
|
|
str, len) == 0) return i;
|
2006-02-05 13:45:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (xp_size_t)-1;
|
|
|
|
}
|
2006-02-05 14:21:18 +00:00
|
|
|
|
|
|
|
xp_size_t xp_awk_tab_rrfind (
|
2006-08-03 06:06:27 +00:00
|
|
|
xp_awk_tab_t* tab, xp_size_t index,
|
|
|
|
const xp_char_t* str, xp_size_t len)
|
2006-02-05 14:21:18 +00:00
|
|
|
{
|
|
|
|
xp_size_t i;
|
|
|
|
|
|
|
|
if (index >= tab->size) return (xp_size_t)-1;
|
|
|
|
|
2006-08-03 05:05:48 +00:00
|
|
|
for (i = tab->size - index; i-- > 0; )
|
|
|
|
{
|
2006-09-01 06:23:58 +00:00
|
|
|
if (xp_awk_strxncmp (
|
2006-08-03 06:06:27 +00:00
|
|
|
tab->buf[i].name, tab->buf[i].name_len,
|
|
|
|
str, len) == 0) return i;
|
2006-02-05 14:21:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (xp_size_t)-1;
|
|
|
|
}
|