summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--render.c70
1 files changed, 38 insertions, 32 deletions
diff --git a/render.c b/render.c
index 11db036..65976d1 100644
--- a/render.c
+++ b/render.c
@@ -30,7 +30,8 @@ void parse_indental_line(char *line, struct indental *entry) {
void free_indental_stack(struct indental*);
struct indental *parse_indental(char *filename) {
FILE *file = fopen(filename, "r");
- struct indental *stack = NULL; // next is temporarily a back pointer.
+ struct indental *stack = NULL, // next is temporarily a back pointer.
+ *ret = NULL; // Track the root to be returned.
char *line = NULL;
size_t line_len = 0;
@@ -55,7 +56,7 @@ struct indental *parse_indental(char *filename) {
line = NULL; line_len = 0; // Read memory claimed by `entry`.
// Insert into appropriate parent entry
- if (stack == NULL) stack = entry;
+ if (stack == NULL) stack = ret = entry;
else if (entry->indent > stack->indent) {
// New level of indentation, push new parent to stack
stack->children = entry;
@@ -81,14 +82,14 @@ struct indental *parse_indental(char *filename) {
}
}
- // Tidyup & find root node to return
+ // Tidyup tree
fclose(file);
while (stack->next != NULL) {
struct indental *parent = stack->next;
stack->next = NULL;
stack = parent;
}
- return stack;
+ return ret; // The above would give us the root, unless the file has multiple.
}
void free_indental(struct indental *data) {
@@ -190,29 +191,26 @@ struct attr *indental_to_attr(struct indental *entry, struct trie *dest) {
ret->next = dest->attrs; dest->attrs = ret;
return ret;
}
-struct trie parse_trie(char *filename) {
- struct indental *file = parse_indental(filename);
+struct trie parse_trie(struct indental *file) {
struct trie ret = init_trie();
- for (struct indental *geom = file; geom != NULL; geom = geom->next) {
- // Parse root-level attrs
- for (struct indental *iter = geom->children; iter != NULL; iter = iter->next) {
- if (iter->value != NULL) indental_to_attr(iter, &ret);
- }
- // Parse component zone IDs
- for (struct indental *iter = geom->children; iter != NULL; iter = iter->next) {
- if (iter->value == NULL) {
- struct trie *leaf = insert_trie(&ret, iter->key); // Found one, add to trie
-
- // Parse attrs
- leaf->attrs = ret.attrs; // Include root attrs
- for (struct indental *child = iter->children; child != NULL; child = child->next) {
- indental_to_attr(iter, leaf);
- }
+ // Parse root-level attrs
+ for (struct indental *iter = file->children; iter != NULL; iter = iter->next) {
+ if (iter->value != NULL) indental_to_attr(iter, &ret);
+ }
+ // Parse component zone IDs
+ for (struct indental *iter = file->children; iter != NULL; iter = iter->next) {
+ if (iter->value == NULL) {
+ struct trie *leaf = insert_trie(&ret, iter->key); // Found one, add to trie
+
+ // Parse attrs
+ leaf->attrs = ret.attrs; // Include root attrs
+ for (struct indental *child = iter->children; child != NULL; child = child->next) {
+ indental_to_attr(iter, leaf);
}
}
}
- free_indental(file);
+
return ret;
}
@@ -316,20 +314,28 @@ int main(int argc, char **argv) {
cairo_t *cr = cairo_create(surface);
// Construct specified trie
- struct trie geom = parse_trie(argv[2]);
- struct trie *view = lookup_trie(&geom, argv[1]);
- int depth = 5;
+ struct indental *file = parse_indental(argv[2]);
+ for (struct indental *layer = file; layer != NULL; layer = layer->next) {
+ struct trie geom = parse_trie(layer);
+ struct trie *view = lookup_trie(&geom, argv[1]);
+ int depth = 5;
- // Render the trie
- cairo_set_source_rgb(cr, 0, 0, 0);
- render_trie(cr, view, depth, 0, 0, WIDTH, HEIGHT);
+ // Render the trie
+ cairo_set_source_rgb(cr, 0, 0, 0);
+ render_trie(cr, view, depth, 0, 0, WIDTH, HEIGHT);
+ }
+ free_indental(file);
// Render overlays
for (int i = 4; i < argc; i++) {
- geom = parse_trie(argv[i]);
- view = lookup_trie(&geom, argv[1]);
- cairo_set_source_rgb(cr, 0, 0, 0);
- render_trie(cr, view, depth, 0, 0, WIDTH, HEIGHT);
+ file = parse_indental(argv[i]);
+ for (struct indental *layer = file; layer != NULL; layer = layer->next) {
+ struct trie geom = parse_trie(layer);
+ struct trie *view = lookup_trie(&geom, argv[1]);
+ cairo_set_source_rgb(cr, 0, 0, 0);
+ render_trie(cr, view, 5, 0, 0, WIDTH, HEIGHT);
+ }
+ free_indental(file);
}
// Output the trie