]> Pileus Git - ~andy/ct/blobdiff - knot/knot.c
Use dynamic memory, fix CSS for tables
[~andy/ct] / knot / knot.c
index 13954d1aaf647800691c2355fe6acfb776dada24..9743cf4a3536d95209266b256791a9cd0a7b1b4a 100644 (file)
@@ -1,18 +1,17 @@
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
 #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);
 }