qse/ase/lsp/token.c

94 lines
2.1 KiB
C
Raw Normal View History

2005-02-04 15:39:11 +00:00
/*
2005-02-05 05:43:55 +00:00
* $Id: token.c,v 1.6 2005-02-05 05:43:55 bacon Exp $
2005-02-04 15:39:11 +00:00
*/
2005-02-05 05:30:25 +00:00
#include <xp/lisp/token.h>
2005-02-05 05:43:55 +00:00
#include <xp/c/memory.h>
2005-02-05 05:30:25 +00:00
#include <xp/c/assert.h>
2005-02-04 15:39:11 +00:00
xp_lisp_token_t* xp_lisp_token_new (xp_size_t capacity)
{
xp_lisp_token_t* token;
2005-02-05 05:18:20 +00:00
xp_assert (capacity > 0);
2005-02-04 15:39:11 +00:00
2005-02-05 05:30:25 +00:00
token = (xp_lisp_token_t*)xp_malloc (sizeof(xp_lisp_token_t));
2005-02-04 15:39:11 +00:00
if (token == XP_NULL) return XP_NULL;
2005-02-05 05:30:25 +00:00
token->buffer = (xp_lisp_char*)xp_malloc ((capacity + 1) * sizeof(xp_lisp_char));
2005-02-04 15:39:11 +00:00
if (token->buffer == XP_NULL) {
2005-02-05 05:30:25 +00:00
xp_free (token);
2005-02-04 15:39:11 +00:00
return XP_NULL;
}
token->ivalue = 0;
token->fvalue = .0;
token->size = 0;
token->capacity = capacity;
2005-02-04 16:23:34 +00:00
token->buffer[0] = XP_CHAR('\0');
2005-02-04 15:39:11 +00:00
return token;
}
void xp_lisp_token_free (xp_lisp_token_t* token)
{
2005-02-05 05:30:25 +00:00
xp_free (token->buffer);
xp_free (token);
2005-02-04 15:39:11 +00:00
}
int xp_lisp_token_addc (xp_lisp_token_t* token, xp_lisp_cint c)
{
if (token->size >= token->capacity) {
// double the capacity.
xp_lisp_char* new_buffer = (xp_lisp_char*)realloc (
token->buffer, (token->capacity * 2 + 1) * sizeof(xp_lisp_char));
if (new_buffer == XP_NULL) return -1;
token->buffer = new_buffer;
token->capacity = token->capacity * 2;
}
token->buffer[token->size++] = c;
2005-02-04 16:23:34 +00:00
token->buffer[token->size] = XP_CHAR('\0');
2005-02-04 15:39:11 +00:00
return 0;
}
void xp_lisp_token_clear (xp_lisp_token_t* token)
{
token->ivalue = 0;
token->fvalue = .0;
token->size = 0;
2005-02-04 16:23:34 +00:00
token->buffer[0] = XP_CHAR('\0');
2005-02-04 15:39:11 +00:00
}
xp_lisp_char* xp_lisp_token_transfer (xp_lisp_token_t* token, xp_size_t capacity)
{
xp_lisp_char* old_buffer, * new_buffer;
2005-02-05 05:30:25 +00:00
new_buffer = (xp_lisp_char*)xp_malloc((capacity + 1) * sizeof(xp_lisp_char));
2005-02-04 15:39:11 +00:00
if (new_buffer == XP_NULL) return XP_NULL;
old_buffer = token->buffer;
token->buffer = new_buffer;
token->size = 0;
token->capacity = capacity;
2005-02-04 16:23:34 +00:00
token->buffer[0] = XP_CHAR('\0');
2005-02-04 15:39:11 +00:00
return old_buffer;
}
int xp_lisp_token_compare (xp_lisp_token_t* token, const xp_lisp_char* str)
{
xp_lisp_char* p = token->buffer;
xp_size_t index = 0;
while (index < token->size) {
if (*p > *str) return 1;
if (*p < *str) return -1;
index++; p++; str++;
}
2005-02-04 16:23:34 +00:00
return (*str == XP_CHAR('\0'))? 0: -1;
2005-02-04 15:39:11 +00:00
}