int height;
};
-static uint32_t color;
-static char *current_buffer_position;
-static struct token *tokens;
-static int nr_tokens, alloc_tokens;
+struct tokenizer_context {
+ char *buffer_position;
+ struct token *tokens;
+ int nr_tokens, alloc_tokens;
+};
static void
-add_token(enum token_type token_type)
+add_token(struct tokenizer_context *ctx, enum token_type token_type)
{
- if (nr_tokens == alloc_tokens) {
- alloc_tokens = (alloc_tokens + 16) * 2;
- tokens = xrealloc(tokens, alloc_tokens * sizeof(struct token));
+ if (ctx->nr_tokens == ctx->alloc_tokens) {
+ ctx->alloc_tokens = (ctx->alloc_tokens + 16) * 2;
+ ctx->tokens = xrealloc(ctx->tokens,
+ ctx->alloc_tokens * sizeof(struct token));
}
- struct token *token = tokens + nr_tokens;
+ struct token *token = ctx->tokens + ctx->nr_tokens;
memset(token, 0, sizeof(*token));
- nr_tokens++;
+ ctx->nr_tokens++;
token->type = token_type;
}
static void
-get_identifier_token(void)
+get_identifier_token(struct tokenizer_context *ctx)
{
- struct token *token = tokens + nr_tokens - 1;
- token->name[token->pos] = current_buffer_position[0];
+ struct token *token = ctx->tokens + ctx->nr_tokens - 1;
+ token->name[token->pos] = ctx->buffer_position[0];
token->pos++;
if (token->pos == MAX_TOKEN_SIZE - 1) {
return;
}
- current_buffer_position++;
- switch (current_buffer_position[0]) {
+ ctx->buffer_position++;
+ switch (ctx->buffer_position[0]) {
case '\0':
return;
case 'a' ... 'z':
case '0' ... '9':
case '_':
case '#':
- get_identifier_token();
+ get_identifier_token(ctx);
break;
default:
break;
}
static void
-get_number_token(void)
+get_number_token(struct tokenizer_context *ctx)
{
- struct token *token = tokens + nr_tokens - 1;
- token->name[token->pos] = current_buffer_position[0];
+ struct token *token = ctx->tokens + ctx->nr_tokens - 1;
+ token->name[token->pos] = ctx->buffer_position[0];
token->pos++;
if (token->pos == MAX_TOKEN_SIZE - 1) {
return;
}
- current_buffer_position++;
- switch (current_buffer_position[0]) {
+ ctx->buffer_position++;
+ switch (ctx->buffer_position[0]) {
case '\0':
return;
case '0' ... '9':
case 'a' ... 'f':
case 'A' ... 'F':
case 'x':
- get_number_token();
+ get_number_token(ctx);
break;
default:
break;
}
static void
-get_special_char_token(void)
+get_special_char_token(struct tokenizer_context *ctx)
{
- struct token *token = tokens + nr_tokens - 1;
- token->name[0] = current_buffer_position[0];
- current_buffer_position++;
+ struct token *token = ctx->tokens + ctx->nr_tokens - 1;
+ token->name[0] = ctx->buffer_position[0];
+ ctx->buffer_position++;
}
/**
static struct token *
tokenize_xbm(char *buffer)
{
- tokens = NULL;
- nr_tokens = 0;
- alloc_tokens = 0;
-
- current_buffer_position = buffer;
+ struct tokenizer_context ctx = {0};
+ ctx.buffer_position = buffer;
for (;;) {
- switch (current_buffer_position[0]) {
+ switch (ctx.buffer_position[0]) {
case '\0':
goto out;
case 'a' ... 'z':
case 'A' ... 'Z':
case '_':
case '#':
- add_token(TOKEN_IDENT);
- get_identifier_token();
+ add_token(&ctx, TOKEN_IDENT);
+ get_identifier_token(&ctx);
continue;
case '0' ... '9':
- add_token(TOKEN_INT);
- get_number_token();
- struct token *token = tokens + nr_tokens - 1;
+ add_token(&ctx, TOKEN_INT);
+ get_number_token(&ctx);
+ struct token *token = ctx.tokens + ctx.nr_tokens - 1;
token->value = (int)strtol(token->name, NULL, 0);
continue;
case '{':
- add_token(TOKEN_SPECIAL);
- get_special_char_token();
+ add_token(&ctx, TOKEN_SPECIAL);
+ get_special_char_token(&ctx);
continue;
default:
break;
}
- ++current_buffer_position;
+ ++ctx.buffer_position;
}
out:
- add_token(TOKEN_NONE); /* vector end marker */
- return tokens;
+ add_token(&ctx, TOKEN_NONE); /* vector end marker */
+ return ctx.tokens;
}
static uint32_t
}
static void
-process_bytes(struct pixmap *pixmap, struct token *tokens)
+process_bytes(struct pixmap *pixmap, struct token *tokens, uint32_t color)
{
pixmap->data = znew_n(uint32_t, pixmap->width * pixmap->height);
struct token *t = tokens;
* @tokens: token vector
*/
static struct pixmap
-parse_xbm_tokens(struct token *tokens)
+parse_xbm_tokens(struct token *tokens, uint32_t color)
{
struct pixmap pixmap = { 0 };
if (t->type != TOKEN_INT) {
continue;
}
- process_bytes(&pixmap, t);
+ process_bytes(&pixmap, t, color);
goto out;
}
if (strstr(t->name, "width")) {
* @button: button byte array (xbm format)
*/
static struct pixmap
-parse_xbm_builtin(const char *button, int size)
+parse_xbm_builtin(const char *button, int size, uint32_t color)
{
struct pixmap pixmap = { 0 };
t[i].type = TOKEN_INT;
}
t[size].type = 0;
- process_bytes(&pixmap, t);
+ process_bytes(&pixmap, t, color);
return pixmap;
}
struct lab_data_buffer *
img_xbm_load_from_bitmap(const char *bitmap, float *rgba)
{
- struct pixmap pixmap = {0};
- color = argb32(rgba);
- pixmap = parse_xbm_builtin(bitmap, 6);
+ uint32_t color = argb32(rgba);
+ struct pixmap pixmap = parse_xbm_builtin(bitmap, 6, color);
return buffer_create_from_data(pixmap.data, pixmap.width, pixmap.height,
pixmap.width * 4);
if (string_null_or_empty(filename)) {
return NULL;
}
- color = argb32(rgba);
+ uint32_t color = argb32(rgba);
/* Read file into memory as it's easier to tokenize that way */
struct buf token_buf = grab_file(filename);
if (token_buf.len) {
struct token *tokens = tokenize_xbm(token_buf.data);
- pixmap = parse_xbm_tokens(tokens);
+ pixmap = parse_xbm_tokens(tokens, color);
if (tokens) {
free(tokens);
}