qse/ase/lsp/name.c

146 lines
3.4 KiB
C
Raw Normal View History

2005-09-18 08:10:50 +00:00
/*
2006-10-24 04:22:40 +00:00
* $Id: name.c,v 1.6 2006-10-24 04:22:39 bacon Exp $
2005-09-18 08:10:50 +00:00
*/
2006-10-24 04:22:40 +00:00
#include <ase/lsp/name.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 (
ase_lsp_name_t* name, ase_word_t capacity)
2005-09-18 08:10:50 +00:00
{
if (capacity == 0)
2006-10-24 04:22:40 +00:00
capacity = ase_countof(name->static_buffer) - 1;
2005-09-18 08:10:50 +00:00
2006-10-24 04:22:40 +00:00
if (name == ASE_NULL) {
name = (ase_lsp_name_t*)
ase_malloc (ase_sizeof(ase_lsp_name_t));
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-24 04:22:40 +00:00
if (capacity < ase_countof(name->static_buffer)) {
2005-09-18 08:10:50 +00:00
name->buffer = name->static_buffer;
}
else {
2006-10-24 04:22:40 +00:00
name->buffer = (ase_char_t*)
ase_malloc ((capacity + 1) * ase_sizeof(ase_char_t));
if (name->buffer == ASE_NULL) {
if (name->__dynamic) ase_free (name);
return ASE_NULL;
2005-09-18 08:10:50 +00:00
}
}
name->size = 0;
name->capacity = capacity;
2006-10-24 04:22:40 +00:00
name->buffer[0] = ASE_CHAR('\0');
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-24 04:22:40 +00:00
if (name->capacity >= ase_countof(name->static_buffer)) {
ase_assert (name->buffer != name->static_buffer);
ase_free (name->buffer);
2005-09-18 08:10:50 +00:00
}
2006-10-24 04:22:40 +00:00
if (name->__dynamic) ase_free (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
{
if (name->size >= name->capacity) {
/* double the capacity. */
2006-10-24 04:22:40 +00:00
ase_size_t new_capacity = name->capacity * 2;
2005-09-18 08:10:50 +00:00
2006-10-24 04:22:40 +00:00
if (new_capacity >= ase_countof(name->static_buffer)) {
ase_char_t* space;
2005-09-18 08:10:50 +00:00
2006-10-24 04:22:40 +00:00
if (name->capacity < ase_countof(name->static_buffer)) {
space = (ase_char_t*)ase_malloc (
(new_capacity + 1) * ase_sizeof(ase_char_t));
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-24 04:22:40 +00:00
ase_memcpy (space, name->buffer,
name->capacity * ase_sizeof(ase_char_t));
2005-09-18 08:10:50 +00:00
}
else {
2006-10-24 04:22:40 +00:00
space = (ase_char_t*)ase_realloc (name->buffer,
(new_capacity + 1) * ase_sizeof(ase_char_t));
if (space == ASE_NULL) return -1;
2005-09-18 08:10:50 +00:00
}
name->buffer = space;
}
name->capacity = new_capacity;
}
name->buffer[name->size++] = c;
2006-10-24 04:22:40 +00:00
name->buffer[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-24 04:22:40 +00:00
while (*s != ASE_CHAR('\0')) {
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
{
name->size = 0;
2006-10-24 04:22:40 +00:00
name->buffer[0] = ASE_CHAR('\0');
2005-09-18 08:10:50 +00:00
}
2006-10-24 04:22:40 +00:00
ase_char_t* ase_lsp_name_yield (ase_lsp_name_t* name, ase_word_t capacity)
2005-09-18 08:10:50 +00:00
{
2006-10-24 04:22:40 +00:00
ase_char_t* old_buffer, * new_buffer;
2005-09-18 08:10:50 +00:00
if (capacity == 0)
2006-10-24 04:22:40 +00:00
capacity = ase_countof(name->static_buffer) - 1;
2005-09-18 08:10:50 +00:00
2006-10-24 04:22:40 +00:00
if (name->capacity < ase_countof(name->static_buffer)) {
old_buffer = (ase_char_t*)
ase_malloc((name->capacity + 1) * ase_sizeof(ase_char_t));
if (old_buffer == ASE_NULL) return ASE_NULL;
ase_memcpy (old_buffer, name->buffer,
(name->capacity + 1) * ase_sizeof(ase_char_t));
2005-09-18 08:10:50 +00:00
}
else old_buffer = name->buffer;
2006-10-24 04:22:40 +00:00
if (capacity < ase_countof(name->static_buffer)) {
2005-09-18 08:10:50 +00:00
new_buffer = name->static_buffer;
}
else {
2006-10-24 04:22:40 +00:00
new_buffer = (ase_char_t*)
ase_malloc((capacity + 1) * ase_sizeof(ase_char_t));
if (new_buffer == ASE_NULL) return ASE_NULL;
2005-09-18 08:10:50 +00:00
}
name->buffer = new_buffer;
name->size = 0;
name->capacity = capacity;
2006-10-24 04:22:40 +00:00
name->buffer[0] = ASE_CHAR('\0');
2005-09-18 08:10:50 +00:00
return old_buffer;
}
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-24 04:22:40 +00:00
ase_char_t* p = name->buffer;
ase_word_t index = 0;
2005-09-18 08:10:50 +00:00
while (index < name->size) {
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
}