2005-09-18 08:10:50 +00:00
|
|
|
/*
|
2006-10-23 14:44:43 +00:00
|
|
|
* $Id: name.c,v 1.5 2006-10-23 14:42:38 bacon Exp $
|
2005-09-18 08:10:50 +00:00
|
|
|
*/
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
#include <sse/lsp/name.h>
|
2005-09-18 08:10:50 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_lsp_name_t* sse_lsp_name_open (
|
|
|
|
sse_lsp_name_t* name, sse_word_t capacity)
|
2005-09-18 08:10:50 +00:00
|
|
|
{
|
|
|
|
if (capacity == 0)
|
2006-10-22 13:10:46 +00:00
|
|
|
capacity = sse_countof(name->static_buffer) - 1;
|
2005-09-18 08:10:50 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
if (name == SSE_NULL) {
|
|
|
|
name = (sse_lsp_name_t*)
|
|
|
|
sse_malloc (sse_sizeof(sse_lsp_name_t));
|
|
|
|
if (name == SSE_NULL) return SSE_NULL;
|
|
|
|
name->__dynamic = sse_true;
|
2005-09-18 08:10:50 +00:00
|
|
|
}
|
2006-10-22 13:10:46 +00:00
|
|
|
else name->__dynamic = sse_false;
|
2005-09-18 08:10:50 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
if (capacity < sse_countof(name->static_buffer)) {
|
2005-09-18 08:10:50 +00:00
|
|
|
name->buffer = name->static_buffer;
|
|
|
|
}
|
|
|
|
else {
|
2006-10-22 13:10:46 +00:00
|
|
|
name->buffer = (sse_char_t*)
|
|
|
|
sse_malloc ((capacity + 1) * sse_sizeof(sse_char_t));
|
|
|
|
if (name->buffer == SSE_NULL) {
|
|
|
|
if (name->__dynamic) sse_free (name);
|
|
|
|
return SSE_NULL;
|
2005-09-18 08:10:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
name->size = 0;
|
|
|
|
name->capacity = capacity;
|
2006-10-22 13:10:46 +00:00
|
|
|
name->buffer[0] = SSE_CHAR('\0');
|
2005-09-18 08:10:50 +00:00
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
void sse_lsp_name_close (sse_lsp_name_t* name)
|
2005-09-18 08:10:50 +00:00
|
|
|
{
|
2006-10-22 13:10:46 +00:00
|
|
|
if (name->capacity >= sse_countof(name->static_buffer)) {
|
|
|
|
sse_assert (name->buffer != name->static_buffer);
|
|
|
|
sse_free (name->buffer);
|
2005-09-18 08:10:50 +00:00
|
|
|
}
|
2006-10-22 13:10:46 +00:00
|
|
|
if (name->__dynamic) sse_free (name);
|
2005-09-18 08:10:50 +00:00
|
|
|
}
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
int sse_lsp_name_addc (sse_lsp_name_t* name, sse_cint_t c)
|
2005-09-18 08:10:50 +00:00
|
|
|
{
|
|
|
|
if (name->size >= name->capacity) {
|
|
|
|
/* double the capacity. */
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_size_t new_capacity = name->capacity * 2;
|
2005-09-18 08:10:50 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
if (new_capacity >= sse_countof(name->static_buffer)) {
|
|
|
|
sse_char_t* space;
|
2005-09-18 08:10:50 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
if (name->capacity < sse_countof(name->static_buffer)) {
|
|
|
|
space = (sse_char_t*)sse_malloc (
|
|
|
|
(new_capacity + 1) * sse_sizeof(sse_char_t));
|
|
|
|
if (space == SSE_NULL) return -1;
|
2005-09-18 08:10:50 +00:00
|
|
|
|
|
|
|
/* don't need to copy up to the terminating null */
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_memcpy (space, name->buffer,
|
|
|
|
name->capacity * sse_sizeof(sse_char_t));
|
2005-09-18 08:10:50 +00:00
|
|
|
}
|
|
|
|
else {
|
2006-10-22 13:10:46 +00:00
|
|
|
space = (sse_char_t*)sse_realloc (name->buffer,
|
|
|
|
(new_capacity + 1) * sse_sizeof(sse_char_t));
|
|
|
|
if (space == SSE_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-22 13:10:46 +00:00
|
|
|
name->buffer[name->size] = SSE_CHAR('\0');
|
2005-09-18 08:10:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
int sse_lsp_name_adds (sse_lsp_name_t* name, const sse_char_t* s)
|
2005-09-18 08:10:50 +00:00
|
|
|
{
|
2006-10-22 13:10:46 +00:00
|
|
|
while (*s != SSE_CHAR('\0')) {
|
|
|
|
if (sse_lsp_name_addc(name, *s) == -1) return -1;
|
2005-09-18 08:10:50 +00:00
|
|
|
s++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
void sse_lsp_name_clear (sse_lsp_name_t* name)
|
2005-09-18 08:10:50 +00:00
|
|
|
{
|
|
|
|
name->size = 0;
|
2006-10-22 13:10:46 +00:00
|
|
|
name->buffer[0] = SSE_CHAR('\0');
|
2005-09-18 08:10:50 +00:00
|
|
|
}
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_char_t* sse_lsp_name_yield (sse_lsp_name_t* name, sse_word_t capacity)
|
2005-09-18 08:10:50 +00:00
|
|
|
{
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_char_t* old_buffer, * new_buffer;
|
2005-09-18 08:10:50 +00:00
|
|
|
|
|
|
|
if (capacity == 0)
|
2006-10-22 13:10:46 +00:00
|
|
|
capacity = sse_countof(name->static_buffer) - 1;
|
2005-09-18 08:10:50 +00:00
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
if (name->capacity < sse_countof(name->static_buffer)) {
|
|
|
|
old_buffer = (sse_char_t*)
|
|
|
|
sse_malloc((name->capacity + 1) * sse_sizeof(sse_char_t));
|
|
|
|
if (old_buffer == SSE_NULL) return SSE_NULL;
|
|
|
|
sse_memcpy (old_buffer, name->buffer,
|
|
|
|
(name->capacity + 1) * sse_sizeof(sse_char_t));
|
2005-09-18 08:10:50 +00:00
|
|
|
}
|
|
|
|
else old_buffer = name->buffer;
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
if (capacity < sse_countof(name->static_buffer)) {
|
2005-09-18 08:10:50 +00:00
|
|
|
new_buffer = name->static_buffer;
|
|
|
|
}
|
|
|
|
else {
|
2006-10-22 13:10:46 +00:00
|
|
|
new_buffer = (sse_char_t*)
|
|
|
|
sse_malloc((capacity + 1) * sse_sizeof(sse_char_t));
|
|
|
|
if (new_buffer == SSE_NULL) return SSE_NULL;
|
2005-09-18 08:10:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
name->buffer = new_buffer;
|
|
|
|
name->size = 0;
|
|
|
|
name->capacity = capacity;
|
2006-10-22 13:10:46 +00:00
|
|
|
name->buffer[0] = SSE_CHAR('\0');
|
2005-09-18 08:10:50 +00:00
|
|
|
|
|
|
|
return old_buffer;
|
|
|
|
}
|
|
|
|
|
2006-10-22 13:10:46 +00:00
|
|
|
int sse_lsp_name_compare (sse_lsp_name_t* name, const sse_char_t* str)
|
2005-09-18 08:10:50 +00:00
|
|
|
{
|
2006-10-22 13:10:46 +00:00
|
|
|
sse_char_t* p = name->buffer;
|
|
|
|
sse_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-22 13:10:46 +00:00
|
|
|
return (*str == SSE_CHAR('\0'))? 0: -1;
|
2005-09-18 08:10:50 +00:00
|
|
|
}
|