From efd411289e62545bc61833455df8b460e2ec5bee Mon Sep 17 00:00:00 2001 From: Andy Spencer Date: Tue, 12 Jan 2010 21:28:45 +0000 Subject: [PATCH] Use dynamic memory, fix CSS for tables --- knot/html.ct | 14 ++++++-------- knot/knot.c | 41 ++++++++++++++++++----------------------- knot/knot.h | 7 ++++++- knot/mkfile | 1 + mkfile | 2 +- 5 files changed, 32 insertions(+), 33 deletions(-) diff --git a/knot/html.ct b/knot/html.ct index 40fbe44..fa6e766 100644 --- a/knot/html.ct +++ b/knot/html.ct @@ -9,7 +9,7 @@ <% } %> <% } %> -<% void print_index(tile_t **tiles) { %> +<% void print_index(row_t *rows) { %> @@ -17,20 +17,18 @@ - <% for (int row = 1; row < 10; row++) { %> + <% for (int row = 1; rows[row].cols; row++) { %> - <% for (int col = 1; col < 30; col++) { %> + <% for (int col = 1; col < rows[row].ncols; col++) { %> <% } %> diff --git a/knot/knot.c b/knot/knot.c index 13954d1..9743cf4 100644 --- a/knot/knot.c +++ b/knot/knot.c @@ -1,18 +1,17 @@ #include #include +#include #include "knot.h" -static void do_tile(tile_t **tiles, int row, int col) +static void do_tile(row_t *rows, int row, int col) { - tile_t *t = &tiles[row][col]; - tile_t *l = &tiles[row][col-1]; - tile_t *r = &tiles[row][col+1]; - tile_t *u = &tiles[row-1][col]; - //tile_t *d = &tiles[row+1][col]; + tile_t *t = &rows[row ].cols[col ]; + tile_t *l = &rows[row ].cols[col-1]; + tile_t *u = &rows[row-1].cols[col ]; /* Fill in what we know */ - switch (tiles[row][col].c) { + switch (t->c) { case '-': t->top = LEFT | RIGHT; break; case '|': t->top = UP | DOWN; break; case '\'': t->top = UP; break; @@ -22,8 +21,9 @@ static void do_tile(tile_t **tiles, int row, int col) /* Follow bottoms */ if (t->c == '|' && (l->top | l->bot) & RIGHT) t->bot = LEFT | RIGHT; - if (t->c == '-' && (u->top | u->bot) & DOWN) - t->bot = UP | DOWN; + if (rows[row-1].ncols >= col) + if (t->c == '-' && (u->top | u->bot) & DOWN) + t->bot = UP | DOWN; /* Adds sides for ''s and .'s */ if (t->c == '.' || t->c == '\'') { @@ -46,30 +46,25 @@ static void print_ptrn(int ptrn) int main() { /* Init tiles */ - tile_t *tiles[64]; - for (int i = 0; i < 64; i++) - tiles[i] = calloc(512, sizeof(tile_t)); - - /* Read data */ char c; int row = 1, col = 1; + row_t *rows = calloc(sizeof(row_t), (row+2)); while ((c = getchar()) != EOF) { - if (row > 63 || col > 511) - exit(-1); if (c == '\n') { row++; col = 1; + rows = realloc(rows, sizeof(row_t) * (row+2)); + rows[row+1] = (row_t){}; } else { - tiles[row][col].c = c; + rows[row].cols = realloc(rows[row].cols, sizeof(tile_t) * (col+1)); + rows[row].ncols = col+1; + rows[row].cols[col] = (tile_t){}; + rows[row].cols[col].c = c; + do_tile(rows, row, col); col++; } } - /* Process */ - for (row = 1; row < 63; row++) - for (col = 1; col < 511; col++) - do_tile(tiles, row, col); - /* Output */ //for (row = 1; row < 63; row++) { // for (col = 1; col < 511; col++) { @@ -81,5 +76,5 @@ int main() //} /* HTML */ - print_index(tiles); + print_index(rows); } diff --git a/knot/knot.h b/knot/knot.h index 0e77797..52a1aef 100644 --- a/knot/knot.h +++ b/knot/knot.h @@ -11,4 +11,9 @@ typedef struct { int bot; } tile_t; -void print_index(tile_t **tiles); +typedef struct { + tile_t *cols; + int ncols; +} row_t; + +void print_index(row_t *rows); diff --git a/knot/mkfile b/knot/mkfile index f38e2c9..ea142e4 100644 --- a/knot/mkfile +++ b/knot/mkfile @@ -2,6 +2,7 @@ PROGS=knot CLEAN=html.c *.html default:V: hitch.html + knot: knot.o html.o knot.h %.html: $PROGS %.txt diff --git a/mkfile b/mkfile index cbbaef1..ab46194 100644 --- a/mkfile +++ b/mkfile @@ -1,6 +1,6 @@ PROGS=ct lib PKGS=glib-2.0 -default:V: run +default:V: all lib-run:V: lib ./lib ct-run:V: ct -- 2.43.2
- <% print_img(tiles[row][col].bot); %> - <% print_img(tiles[row][col].top); %> + <% print_img(rows[row].cols[col].bot); %> + <% print_img(rows[row].cols[col].top); %>