]> Pileus Git - ~andy/ct/blob - knot/knot.c
Use dynamic memory, fix CSS for tables
[~andy/ct] / knot / knot.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include "knot.h"
6
7 static void do_tile(row_t *rows, int row, int col)
8 {
9         tile_t *t = &rows[row  ].cols[col  ];
10         tile_t *l = &rows[row  ].cols[col-1];
11         tile_t *u = &rows[row-1].cols[col  ];
12
13         /* Fill in what we know */
14         switch (t->c) {
15         case '-':  t->top = LEFT | RIGHT; break;
16         case '|':  t->top = UP | DOWN;    break;
17         case '\'': t->top = UP;           break;
18         case '.':  t->top = DOWN;         break;
19         }
20
21         /* Follow bottoms */
22         if (t->c == '|' && (l->top | l->bot) & RIGHT)
23                 t->bot = LEFT | RIGHT;
24         if (rows[row-1].ncols >= col)
25                 if (t->c == '-' && (u->top | u->bot) & DOWN)
26                         t->bot = UP | DOWN;
27
28         /* Adds sides for ''s and .'s */
29         if (t->c == '.' || t->c == '\'') {
30                 if ((l->top | l->bot) & RIGHT)
31                         t->top |= LEFT;
32                 else
33                         t->top |= RIGHT;
34         }
35
36 }
37
38 static void print_ptrn(int ptrn)
39 {
40         printf("%c", ptrn & LEFT  ? '<' : '-');
41         printf("%c", ptrn & RIGHT ? '>' : '-');
42         printf("%c", ptrn & UP    ? '^' : '-');
43         printf("%c", ptrn & DOWN  ? 'v' : '-');
44 }
45
46 int main()
47 {
48         /* Init tiles */
49         char c;
50         int row = 1, col = 1;
51         row_t *rows = calloc(sizeof(row_t), (row+2));
52         while ((c = getchar()) != EOF) {
53                 if (c == '\n') {
54                         row++;
55                         col = 1;
56                         rows = realloc(rows, sizeof(row_t) * (row+2));
57                         rows[row+1] = (row_t){};
58                 } else {
59                         rows[row].cols = realloc(rows[row].cols, sizeof(tile_t) * (col+1));
60                         rows[row].ncols = col+1;
61                         rows[row].cols[col] = (tile_t){};
62                         rows[row].cols[col].c = c;
63                         do_tile(rows, row, col);
64                         col++;
65                 }
66         }
67
68         /* Output */
69         //for (row = 1; row < 63; row++) {
70         //      for (col = 1; col < 511; col++) {
71         //              print_ptrn(tiles[row][col].top);
72         //              print_ptrn(tiles[row][col].bot);
73         //              printf(" ");
74         //      }
75         //      printf("\n");
76         //}
77
78         /* HTML */
79         print_index(rows);
80 }