]> git.mdlowis.com Git - proto/labwc.git/commitdiff
Add src/theme/xbm/parse.c
authorJohan Malm <jgm323@gmail.com>
Tue, 23 Jun 2020 06:17:07 +0000 (07:17 +0100)
committerJohan Malm <jgm323@gmail.com>
Tue, 23 Jun 2020 06:17:07 +0000 (07:17 +0100)
include/xbm.h
src/theme/xbm/parse.c [new file with mode: 0644]
src/theme/xbm/tokenize.c
tools/xbm/.gitignore
tools/xbm/Makefile
tools/xbm/xbm-parse.c [new file with mode: 0644]
tools/xbm/xbm-tokenize.c

index f09264ba378e89b7f035b80b15997b99cde6173d..127545daa5b243d7aa14e3f81286ab2cb98d36e1 100644 (file)
@@ -16,11 +16,24 @@ struct token {
        enum token_type type;
 };
 
+/**
+ * xbm_create_bitmap - parse xbm tokens and create pixmap
+ * @tokens: token vector
+ */
+void xbm_create_bitmap(struct token *tokens);
+
 /**
  * tokenize - tokenize xbm file
  * @buffer: buffer containing xbm file
- * returns vector of tokens
+ * return token vector
+ */
+struct token *xbm_tokenize(char *buffer);
+
+/**
+ * xbm_read_file - read file into buffer (as it's easier to tokenize that way)
+ * @filename: file to be read
+ * return allocated memory
  */
-struct token *tokenize(char *buffer);
+char *xbm_read_file(const char *filename);
 
 #endif /* XBM_H */
diff --git a/src/theme/xbm/parse.c b/src/theme/xbm/parse.c
new file mode 100644 (file)
index 0000000..6951f25
--- /dev/null
@@ -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);
+}
index 301bab69ed6ff941a38f61e9d4bcbc9d6cb95a18..746502769183f151605106a806a68f0d88f7ece5 100644 (file)
@@ -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;
index 1f1cfd694b13e8dc832f24b6c8ca2a28d1d5bf16..22f659d4abc8ed226e379c0c7fc2d6bbd8135968 100644 (file)
@@ -1 +1,2 @@
 xbm-tokenize
+xbm-parse
index 365c67398e592ff5f1c120034a29a6dee94fc4fd..af07e3a2b9c034256d75b2ad5816fc3066495b2c 100644 (file)
@@ -3,13 +3,17 @@ CFLAGS      += -I../../include
 LDFLAGS     += `pkg-config --cflags --libs cairo`
 ASAN        += -fsanitize=address
 
-PROGS        = xbm-tokenize
+PROGS        = xbm-tokenize xbm-parse
 DEP_TOKENIZE = ../../src/common/buf.c ../../src/theme/xbm/tokenize.c
+DEP_PARSE    = $(DEP_TOKENIZE) ../../src/theme/xbm/parse.c
 
 all: $(PROGS)
 
 xbm-tokenize: xbm-tokenize.c $(DEP_TOKENIZE)
        $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(ASAN)
 
+xbm-parse: xbm-parse.c $(DEP_PARSE)
+       $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(ASAN)
+
 clean :
        $(RM) $(PROGS)
diff --git a/tools/xbm/xbm-parse.c b/tools/xbm/xbm-parse.c
new file mode 100644 (file)
index 0000000..fc9b52d
--- /dev/null
@@ -0,0 +1,23 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "xbm.h"
+
+int main(int argc, char **argv)
+{
+       struct token *tokens;
+
+       if (argc != 2) {
+               fprintf(stderr, "usage: %s <xbm-file>\n", argv[0]);
+               return 1;
+       }
+
+       char *buffer = xbm_read_file(argv[1]);
+       if (!buffer)
+               exit(EXIT_FAILURE);
+       tokens = xbm_tokenize(buffer);
+       free(buffer);
+
+       xbm_create_bitmap(tokens);
+       free(tokens);
+}
index 5eabbe3f49fa7e4a47898b251f5d8ba7b98d9bfa..d38827d68fd8209c9aedd6d42f84d79b71054a8b 100644 (file)
@@ -41,7 +41,7 @@ int main(int argc, char **argv)
        char *buffer = read_file(argv[1]);
        if (!buffer)
                exit(EXIT_FAILURE);
-       tokens = tokenize(buffer);
+       tokens = xbm_tokenize(buffer);
        free(buffer);
        for (struct token *t = tokens; t->type; t++)
                printf("%s\n", t->name);