mirror of
https://github.com/labwc/labwc.git
synced 2025-11-04 13:30:07 -05:00
Add src/theme/xbm/parse.c
This commit is contained in:
parent
d80a7b518f
commit
f86394a997
7 changed files with 125 additions and 13 deletions
70
src/theme/xbm/parse.c
Normal file
70
src/theme/xbm/parse.c
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "buf.h"
|
||||
#include "xbm.h"
|
||||
|
||||
static void process_bytes(int height, int width, struct token *tokens)
|
||||
{
|
||||
struct token *t = tokens;
|
||||
for (int row = 0; row < height; row++) {
|
||||
int byte = 1;
|
||||
for (int col = 0; col < width; col++) {
|
||||
if (col == byte * 8) {
|
||||
++byte;
|
||||
++t;
|
||||
}
|
||||
if (!t->type)
|
||||
return;
|
||||
int value = (int)strtol(t->name, NULL, 0);
|
||||
int bit = 1 << (col % 8);
|
||||
if (value & bit)
|
||||
printf(".");
|
||||
else
|
||||
printf(" ");
|
||||
}
|
||||
++t;
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
void xbm_create_bitmap(struct token *tokens)
|
||||
{
|
||||
int width = 0, height = 0;
|
||||
for (struct token *t = tokens; t->type; t++) {
|
||||
if (width && height) {
|
||||
if (t->type != TOKEN_INT)
|
||||
continue;
|
||||
process_bytes(width, height, t);
|
||||
return;
|
||||
}
|
||||
if (strstr(t->name, "width"))
|
||||
width = atoi((++t)->name);
|
||||
else if (strstr(t->name, "height"))
|
||||
height = atoi((++t)->name);
|
||||
}
|
||||
}
|
||||
|
||||
char *xbm_read_file(const char *filename)
|
||||
{
|
||||
char *line = NULL;
|
||||
size_t len = 0;
|
||||
FILE *stream = fopen(filename, "r");
|
||||
if (!stream) {
|
||||
fprintf(stderr, "warn: cannot read '%s'\n", filename);
|
||||
return NULL;
|
||||
}
|
||||
struct buf buffer;
|
||||
buf_init(&buffer);
|
||||
while ((getline(&line, &len, stream) != -1)) {
|
||||
char *p = strrchr(line, '\n');
|
||||
if (p)
|
||||
*p = '\0';
|
||||
buf_add(&buffer, line);
|
||||
}
|
||||
free(line);
|
||||
fclose(stream);
|
||||
return (buffer.buf);
|
||||
}
|
||||
|
|
@ -31,8 +31,9 @@ static void get_identifier_token()
|
|||
switch (current_buffer_position[0]) {
|
||||
case '\0':
|
||||
return;
|
||||
case 'a'...'z':
|
||||
case 'A'...'Z':
|
||||
case 'a' ... 'z':
|
||||
case 'A' ... 'Z':
|
||||
case '0' ... '9':
|
||||
case '_':
|
||||
case '#':
|
||||
get_identifier_token();
|
||||
|
|
@ -53,9 +54,9 @@ static void get_number_token()
|
|||
switch (current_buffer_position[0]) {
|
||||
case '\0':
|
||||
return;
|
||||
case '0'...'9':
|
||||
case 'a'...'f':
|
||||
case 'A'...'F':
|
||||
case '0' ... '9':
|
||||
case 'a' ... 'f':
|
||||
case 'A' ... 'F':
|
||||
case 'x':
|
||||
get_number_token();
|
||||
break;
|
||||
|
|
@ -71,7 +72,7 @@ static void get_special_char_token()
|
|||
current_buffer_position++;
|
||||
}
|
||||
|
||||
struct token *tokenize(char *buffer)
|
||||
struct token *xbm_tokenize(char *buffer)
|
||||
{
|
||||
current_buffer_position = buffer;
|
||||
|
||||
|
|
@ -79,14 +80,14 @@ struct token *tokenize(char *buffer)
|
|||
switch (current_buffer_position[0]) {
|
||||
case '\0':
|
||||
goto out;
|
||||
case 'a'...'z':
|
||||
case 'A'...'Z':
|
||||
case 'a' ... 'z':
|
||||
case 'A' ... 'Z':
|
||||
case '_':
|
||||
case '#':
|
||||
add_token(TOKEN_IDENT);
|
||||
get_identifier_token();
|
||||
continue;
|
||||
case '0'...'9':
|
||||
case '0' ... '9':
|
||||
add_token(TOKEN_INT);
|
||||
get_number_token();
|
||||
continue;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue