qse/ase/lsp/token.c

94 lines
2.0 KiB
C
Raw Normal View History

2005-02-04 15:39:11 +00:00
/*
2005-04-24 07:48:16 +00:00
* $Id: token.c,v 1.8 2005-04-24 07:48:16 bacon Exp $
2005-02-04 15:39:11 +00:00
*/
2005-02-05 05:30:25 +00:00
#include <xp/lisp/token.h>
2005-04-24 07:48:16 +00:00
#include <xp/bas/memory.h>
#include <xp/bas/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-07 15:10:41 +00:00
token->buffer = (xp_char_t*)xp_malloc ((capacity + 1) * sizeof(xp_char_t));
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
}
2005-02-07 15:10:41 +00:00
int xp_lisp_token_addc (xp_lisp_token_t* token, xp_cint_t c)
2005-02-04 15:39:11 +00:00
{
if (token->size >= token->capacity) {
// double the capacity.
2005-02-07 15:10:41 +00:00
xp_char_t* new_buffer = (xp_char_t*)realloc (
token->buffer, (token->capacity * 2 + 1) * sizeof(xp_char_t));
2005-02-04 15:39:11 +00:00
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
}
2005-02-07 15:10:41 +00:00
xp_char_t* xp_lisp_token_transfer (xp_lisp_token_t* token, xp_size_t capacity)
2005-02-04 15:39:11 +00:00
{
2005-02-07 15:10:41 +00:00
xp_char_t* old_buffer, * new_buffer;
2005-02-04 15:39:11 +00:00
2005-02-07 15:10:41 +00:00
new_buffer = (xp_char_t*)xp_malloc((capacity + 1) * sizeof(xp_char_t));
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;
}
2005-02-07 15:10:41 +00:00
int xp_lisp_token_compare (xp_lisp_token_t* token, const xp_char_t* str)
2005-02-04 15:39:11 +00:00
{
2005-02-07 15:10:41 +00:00
xp_char_t* p = token->buffer;
2005-02-04 15:39:11 +00:00
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
}