qse/ase/lsp/name.c

162 lines
3.3 KiB
C
Raw Normal View History

2005-09-18 08:10:50 +00:00
/*
2006-10-29 13:00:39 +00:00
* $Id: name.c,v 1.10 2006-10-29 13:00:39 bacon Exp $
2005-09-18 08:10:50 +00:00
*/
2006-10-26 08:17:38 +00:00
#include <ase/lsp/lsp_i.h>
2005-09-18 08:10:50 +00:00
2006-10-24 04:22:40 +00:00
ase_lsp_name_t* ase_lsp_name_open (
2006-10-26 08:17:38 +00:00
ase_lsp_name_t* name, ase_size_t capa, ase_lsp_t* lsp)
2005-09-18 08:10:50 +00:00
{
2006-10-29 13:00:39 +00:00
if (capa == 0) capa = ase_countof(name->static_buf) - 1;
2005-09-18 08:10:50 +00:00
2006-10-26 08:17:38 +00:00
if (name == ASE_NULL)
{
2006-10-24 04:22:40 +00:00
name = (ase_lsp_name_t*)
2006-10-26 08:17:38 +00:00
ASE_LSP_MALLOC (lsp, ase_sizeof(ase_lsp_name_t));
2006-10-24 04:22:40 +00:00
if (name == ASE_NULL) return ASE_NULL;
name->__dynamic = ase_true;
2005-09-18 08:10:50 +00:00
}
2006-10-24 04:22:40 +00:00
else name->__dynamic = ase_false;
2005-09-18 08:10:50 +00:00
2006-10-26 08:17:38 +00:00
if (capa < ase_countof(name->static_buf))
{
name->buf = name->static_buf;
2005-09-18 08:10:50 +00:00
}
2006-10-26 08:17:38 +00:00
else
{
name->buf = (ase_char_t*)
ASE_LSP_MALLOC (lsp, (capa+1)*ase_sizeof(ase_char_t));
if (name->buf == ASE_NULL)
{
if (name->__dynamic) ASE_LSP_FREE (lsp, name);
2006-10-24 04:22:40 +00:00
return ASE_NULL;
2005-09-18 08:10:50 +00:00
}
}
2006-10-26 08:17:38 +00:00
name->size = 0;
name->capa = capa;
name->buf[0] = ASE_CHAR('\0');
name->lsp = lsp;
2005-09-18 08:10:50 +00:00
return name;
}
2006-10-24 04:22:40 +00:00
void ase_lsp_name_close (ase_lsp_name_t* name)
2005-09-18 08:10:50 +00:00
{
2006-10-26 08:17:38 +00:00
if (name->capa >= ase_countof(name->static_buf))
{
2006-10-26 09:31:28 +00:00
ASE_LSP_ASSERT (name->lsp, name->buf != name->static_buf);
2006-10-26 08:17:38 +00:00
ASE_LSP_FREE (name->lsp, name->buf);
2005-09-18 08:10:50 +00:00
}
2006-10-26 08:17:38 +00:00
if (name->__dynamic) ASE_LSP_FREE (name->lsp, name);
2005-09-18 08:10:50 +00:00
}
2006-10-24 04:22:40 +00:00
int ase_lsp_name_addc (ase_lsp_name_t* name, ase_cint_t c)
2005-09-18 08:10:50 +00:00
{
2006-10-26 08:17:38 +00:00
if (name->size >= name->capa)
{
/* double the capa. */
ase_size_t new_capa = name->capa * 2;
2005-09-18 08:10:50 +00:00
2006-10-26 08:17:38 +00:00
if (new_capa >= ase_countof(name->static_buf))
{
2006-10-24 04:22:40 +00:00
ase_char_t* space;
2005-09-18 08:10:50 +00:00
2006-10-26 08:17:38 +00:00
if (name->capa < ase_countof(name->static_buf))
{
space = (ase_char_t*) ASE_LSP_MALLOC (
name->lsp, (new_capa+1)*ase_sizeof(ase_char_t));
2006-10-24 04:22:40 +00:00
if (space == ASE_NULL) return -1;
2005-09-18 08:10:50 +00:00
/* don't need to copy up to the terminating null */
2006-10-26 08:17:38 +00:00
ASE_LSP_MEMCPY (name->lsp, space, name->buf,
name->capa*ase_sizeof(ase_char_t));
2005-09-18 08:10:50 +00:00
}
2006-10-26 08:17:38 +00:00
else
{
space = (ase_char_t*) ASE_LSP_REALLOC (
name->lsp, name->buf,
(new_capa+1)*ase_sizeof(ase_char_t));
2006-10-24 04:22:40 +00:00
if (space == ASE_NULL) return -1;
2005-09-18 08:10:50 +00:00
}
2006-10-26 08:17:38 +00:00
name->buf = space;
2005-09-18 08:10:50 +00:00
}
2006-10-26 08:17:38 +00:00
name->capa = new_capa;
2005-09-18 08:10:50 +00:00
}
2006-10-26 08:17:38 +00:00
name->buf[name->size++] = c;
name->buf[name->size] = ASE_CHAR('\0');
2005-09-18 08:10:50 +00:00
return 0;
}
2006-10-24 04:22:40 +00:00
int ase_lsp_name_adds (ase_lsp_name_t* name, const ase_char_t* s)
2005-09-18 08:10:50 +00:00
{
2006-10-26 08:17:38 +00:00
while (*s != ASE_CHAR('\0'))
{
2006-10-24 04:22:40 +00:00
if (ase_lsp_name_addc(name, *s) == -1) return -1;
2005-09-18 08:10:50 +00:00
s++;
}
return 0;
}
2006-10-24 04:22:40 +00:00
void ase_lsp_name_clear (ase_lsp_name_t* name)
2005-09-18 08:10:50 +00:00
{
2006-10-26 08:17:38 +00:00
name->size = 0;
name->buf[0] = ASE_CHAR('\0');
2005-09-18 08:10:50 +00:00
}
2006-10-26 08:17:38 +00:00
ase_char_t* ase_lsp_name_yield (ase_lsp_name_t* name, ase_size_t capa)
2005-09-18 08:10:50 +00:00
{
2006-10-26 08:17:38 +00:00
ase_char_t* old_buf, * new_buf;
2005-09-18 08:10:50 +00:00
2006-10-26 08:17:38 +00:00
if (capa == 0) capa = ase_countof(name->static_buf) - 1;
2005-09-18 08:10:50 +00:00
2006-10-26 08:17:38 +00:00
if (name->capa < ase_countof(name->static_buf))
{
old_buf = (ase_char_t*) ASE_LSP_MALLOC (
name->lsp, (name->capa+1)*ase_sizeof(ase_char_t));
if (old_buf == ASE_NULL) return ASE_NULL;
ASE_LSP_MEMCPY (
name->lsp, old_buf, name->buf,
(name->capa+1)*ase_sizeof(ase_char_t));
2005-09-18 08:10:50 +00:00
}
2006-10-26 08:17:38 +00:00
else old_buf = name->buf;
2005-09-18 08:10:50 +00:00
2006-10-26 08:17:38 +00:00
if (capa < ase_countof(name->static_buf))
{
new_buf = name->static_buf;
2005-09-18 08:10:50 +00:00
}
2006-10-26 08:17:38 +00:00
else
{
new_buf = (ase_char_t*) ASE_LSP_MALLOC (
name->lsp, (capa+1)*ase_sizeof(ase_char_t));
if (new_buf == ASE_NULL) return ASE_NULL;
2005-09-18 08:10:50 +00:00
}
2006-10-26 08:17:38 +00:00
name->buf = new_buf;
name->size = 0;
name->capa = capa;
name->buf[0] = ASE_CHAR('\0');
2005-09-18 08:10:50 +00:00
2006-10-26 08:17:38 +00:00
return old_buf;
2005-09-18 08:10:50 +00:00
}
2006-10-24 04:22:40 +00:00
int ase_lsp_name_compare (ase_lsp_name_t* name, const ase_char_t* str)
2005-09-18 08:10:50 +00:00
{
2006-10-26 08:17:38 +00:00
ase_char_t* p = name->buf;
2006-10-25 14:42:40 +00:00
ase_size_t index = 0;
2005-09-18 08:10:50 +00:00
2006-10-26 08:17:38 +00:00
while (index < name->size)
{
2005-09-18 08:10:50 +00:00
if (*p > *str) return 1;
if (*p < *str) return -1;
index++; p++; str++;
}
2006-10-24 04:22:40 +00:00
return (*str == ASE_CHAR('\0'))? 0: -1;
2005-09-18 08:10:50 +00:00
}