qse/ase/lsp/name.c

148 lines
3.3 KiB
C
Raw Normal View History

2005-09-18 08:10:50 +00:00
/*
2005-09-18 11:34:35 +00:00
* $Id: name.c,v 1.2 2005-09-18 11:34:35 bacon Exp $
2005-09-18 08:10:50 +00:00
*/
2005-09-18 11:34:35 +00:00
#include <xp/lsp/name.h>
#include <xp/bas/memory.h>
#include <xp/bas/assert.h>
2005-09-18 08:10:50 +00:00
2005-09-18 11:34:35 +00:00
xp_lsp_name_t* xp_lsp_name_open (
xp_lsp_name_t* name, xp_word_t capacity)
2005-09-18 08:10:50 +00:00
{
if (capacity == 0)
capacity = xp_countof(name->static_buffer) - 1;
if (name == XP_NULL) {
2005-09-18 11:34:35 +00:00
name = (xp_lsp_name_t*)
xp_malloc (xp_sizeof(xp_lsp_name_t));
2005-09-18 08:10:50 +00:00
if (name == XP_NULL) return XP_NULL;
name->__malloced = xp_true;
}
else name->__malloced = xp_false;
if (capacity < xp_countof(name->static_buffer)) {
name->buffer = name->static_buffer;
}
else {
name->buffer = (xp_char_t*)
xp_malloc ((capacity + 1) * xp_sizeof(xp_char_t));
if (name->buffer == XP_NULL) {
if (name->__malloced) xp_free (name);
return XP_NULL;
}
}
name->size = 0;
name->capacity = capacity;
name->buffer[0] = XP_CHAR('\0');
return name;
}
2005-09-18 11:34:35 +00:00
void xp_lsp_name_close (xp_lsp_name_t* name)
2005-09-18 08:10:50 +00:00
{
if (name->capacity >= xp_countof(name->static_buffer)) {
xp_assert (name->buffer != name->static_buffer);
xp_free (name->buffer);
}
if (name->__malloced) xp_free (name);
}
2005-09-18 11:34:35 +00:00
int xp_lsp_name_addc (xp_lsp_name_t* name, xp_cint_t c)
2005-09-18 08:10:50 +00:00
{
if (name->size >= name->capacity) {
/* double the capacity. */
xp_size_t new_capacity = name->capacity * 2;
if (new_capacity >= xp_countof(name->static_buffer)) {
xp_char_t* space;
if (name->capacity < xp_countof(name->static_buffer)) {
space = (xp_char_t*)xp_malloc (
(new_capacity + 1) * xp_sizeof(xp_char_t));
if (space == XP_NULL) return -1;
/* don't need to copy up to the terminating null */
xp_memcpy (space, name->buffer,
name->capacity * xp_sizeof(xp_char_t));
}
else {
space = (xp_char_t*)xp_realloc (name->buffer,
(new_capacity + 1) * xp_sizeof(xp_char_t));
if (space == XP_NULL) return -1;
}
name->buffer = space;
}
name->capacity = new_capacity;
}
name->buffer[name->size++] = c;
name->buffer[name->size] = XP_CHAR('\0');
return 0;
}
2005-09-18 11:34:35 +00:00
int xp_lsp_name_adds (xp_lsp_name_t* name, const xp_char_t* s)
2005-09-18 08:10:50 +00:00
{
while (*s != XP_CHAR('\0')) {
2005-09-18 11:34:35 +00:00
if (xp_lsp_name_addc(name, *s) == -1) return -1;
2005-09-18 08:10:50 +00:00
s++;
}
return 0;
}
2005-09-18 11:34:35 +00:00
void xp_lsp_name_clear (xp_lsp_name_t* name)
2005-09-18 08:10:50 +00:00
{
name->size = 0;
name->buffer[0] = XP_CHAR('\0');
}
2005-09-18 11:34:35 +00:00
xp_char_t* xp_lsp_name_yield (xp_lsp_name_t* name, xp_word_t capacity)
2005-09-18 08:10:50 +00:00
{
xp_char_t* old_buffer, * new_buffer;
if (capacity == 0)
capacity = xp_countof(name->static_buffer) - 1;
if (name->capacity < xp_countof(name->static_buffer)) {
old_buffer = (xp_char_t*)
xp_malloc((name->capacity + 1) * xp_sizeof(xp_char_t));
if (old_buffer == XP_NULL) return XP_NULL;
xp_memcpy (old_buffer, name->buffer,
(name->capacity + 1) * xp_sizeof(xp_char_t));
}
else old_buffer = name->buffer;
if (capacity < xp_countof(name->static_buffer)) {
new_buffer = name->static_buffer;
}
else {
new_buffer = (xp_char_t*)
xp_malloc((capacity + 1) * xp_sizeof(xp_char_t));
if (new_buffer == XP_NULL) return XP_NULL;
}
name->buffer = new_buffer;
name->size = 0;
name->capacity = capacity;
name->buffer[0] = XP_CHAR('\0');
return old_buffer;
}
2005-09-18 11:34:35 +00:00
int xp_lsp_name_compare (xp_lsp_name_t* name, const xp_char_t* str)
2005-09-18 08:10:50 +00:00
{
xp_char_t* p = name->buffer;
xp_word_t index = 0;
while (index < name->size) {
if (*p > *str) return 1;
if (*p < *str) return -1;
index++; p++; str++;
}
return (*str == XP_CHAR('\0'))? 0: -1;
}