2005-09-18 08:10:50 +00:00
|
|
|
/*
|
2007-02-03 10:52:36 +00:00
|
|
|
* $Id: name.c,v 1.14 2007-02-03 10:51:52 bacon Exp $
|
|
|
|
*
|
|
|
|
* {License}
|
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-11-29 02:54:17 +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-11-29 02:54:17 +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-11-29 02:54:17 +00:00
|
|
|
if (capa < ASE_COUNTOF(name->static_buf))
|
2006-10-26 08:17:38 +00:00
|
|
|
{
|
|
|
|
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*)
|
2006-11-29 02:54:17 +00:00
|
|
|
ASE_LSP_MALLOC (lsp, (capa+1)*ASE_SIZEOF(ase_char_t));
|
2006-10-26 08:17:38 +00:00
|
|
|
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;
|
2006-12-05 03:38:11 +00:00
|
|
|
name->buf[0] = ASE_T('\0');
|
2006-10-26 08:17:38 +00:00
|
|
|
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-11-29 02:54:17 +00:00
|
|
|
if (name->capa >= ASE_COUNTOF(name->static_buf))
|
2006-10-26 08:17:38 +00:00
|
|
|
{
|
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)
|
|
|
|
{
|
2006-11-02 10:12:01 +00:00
|
|
|
/* double the capacity */
|
2006-10-26 08:17:38 +00:00
|
|
|
ase_size_t new_capa = name->capa * 2;
|
2005-09-18 08:10:50 +00:00
|
|
|
|
2006-11-29 02:54:17 +00:00
|
|
|
if (new_capa >= ASE_COUNTOF(name->static_buf))
|
2006-10-26 08:17:38 +00:00
|
|
|
{
|
2006-10-24 04:22:40 +00:00
|
|
|
ase_char_t* space;
|
2005-09-18 08:10:50 +00:00
|
|
|
|
2006-11-29 02:54:17 +00:00
|
|
|
if (name->capa < ASE_COUNTOF(name->static_buf))
|
2006-10-26 08:17:38 +00:00
|
|
|
{
|
|
|
|
space = (ase_char_t*) ASE_LSP_MALLOC (
|
2006-11-29 02:54:17 +00:00
|
|
|
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,
|
2006-11-29 02:54:17 +00:00
|
|
|
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,
|
2006-11-29 02:54:17 +00:00
|
|
|
(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;
|
2006-12-05 03:38:11 +00:00
|
|
|
name->buf[name->size] = ASE_T('\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-12-05 03:38:11 +00:00
|
|
|
while (*s != ASE_T('\0'))
|
2006-10-26 08:17:38 +00:00
|
|
|
{
|
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;
|
2006-12-05 03:38:11 +00:00
|
|
|
name->buf[0] = ASE_T('\0');
|
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-12-05 03:38:11 +00:00
|
|
|
return (*str == ASE_T('\0'))? 0: -1;
|
2005-09-18 08:10:50 +00:00
|
|
|
}
|