]> Pileus Git - ~andy/ct/commitdiff
Memory fixes
authorAndy Spencer <andy753421@gmail.com>
Sat, 16 Jan 2010 23:35:08 +0000 (23:35 +0000)
committerAndy Spencer <andy753421@gmail.com>
Sat, 16 Jan 2010 23:35:08 +0000 (23:35 +0000)
- Don't rely on left/right/top/bottom padding
- Free allocated memory

knot/html.ct
knot/knot.c
knot/mkfile

index fa6e766d20cb8d4c4782bfcee22fdd1240b2296d..a80c21d450d15db9f533d28bbf0c26b5e8702066 100644 (file)
                </style>
        </head>
        <body>
-       <table cellpadding="0" cellspacing="0">
-               <% for (int row = 1; rows[row].cols; row++) { %>
-               <tr>
-                       <% for (int col = 1; col < rows[row].ncols; col++) { %>
-                       <td>
-                               <% print_img(rows[row].cols[col].bot); %>
-                               <% print_img(rows[row].cols[col].top); %>
-                       </td>
+               <table cellpadding="0" cellspacing="0">
+                       <% for (int row = 0; rows[row].ncols >= 0; row++) { %>
+                       <tr>
+                               <% for (int col = 0; col < rows[row].ncols; col++) { %>
+                               <td>
+                                       <% print_img(rows[row].cols[col].bot); %>
+                                       <% print_img(rows[row].cols[col].top); %>
+                               </td>
+                               <% } %>
+                       </tr>
                        <% } %>
-               </tr>
-               <% } %>
                </table>
        </body>
 </html>
index 9743cf4a3536d95209266b256791a9cd0a7b1b4a..a859babf35560df6c537d76f34faa76c81ddd12d 100644 (file)
@@ -6,9 +6,7 @@
 
 static void do_tile(row_t *rows, int row, int col)
 {
-       tile_t *t = &rows[row  ].cols[col  ];
-       tile_t *l = &rows[row  ].cols[col-1];
-       tile_t *u = &rows[row-1].cols[col  ];
+       tile_t *t = &rows[row].cols[col];
 
        /* Fill in what we know */
        switch (t->c) {
@@ -19,15 +17,21 @@ static void do_tile(row_t *rows, int row, int col)
        }
 
        /* Follow bottoms */
-       if (t->c == '|' && (l->top | l->bot) & RIGHT)
-               t->bot = LEFT | RIGHT;
-       if (rows[row-1].ncols >= col)
+       if (col > 0) {
+               tile_t *l = &rows[row].cols[col-1];
+               if (t->c == '|' && (l->top | l->bot) & RIGHT)
+                       t->bot = LEFT | RIGHT;
+       }
+       if (row > 0 && rows[row-1].ncols > col) {
+               tile_t *u = &rows[row-1].cols[col];
                if (t->c == '-' && (u->top | u->bot) & DOWN)
                        t->bot = UP | DOWN;
+       }
 
        /* Adds sides for ''s and .'s */
        if (t->c == '.' || t->c == '\'') {
-               if ((l->top | l->bot) & RIGHT)
+               tile_t *l = &rows[row].cols[col-1];
+               if (col > 0 && (l->top | l->bot) & RIGHT)
                        t->top |= LEFT;
                else
                        t->top |= RIGHT;
@@ -47,34 +51,43 @@ int main()
 {
        /* Init tiles */
        char c;
-       int row = 1, col = 1;
+       int row = 0, col = 0;
        row_t *rows = calloc(sizeof(row_t), (row+2));
        while ((c = getchar()) != EOF) {
                if (c == '\n') {
                        row++;
-                       col = 1;
+                       col = 0;
                        rows = realloc(rows, sizeof(row_t) * (row+2));
-                       rows[row+1] = (row_t){};
+                       rows[row+0] = (row_t){.ncols =  0};
+                       rows[row+1] = (row_t){.ncols = -1};
                } else {
                        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;
+                       rows[row].cols[col] = (tile_t){.c = c};
                        do_tile(rows, row, col);
                        col++;
                }
        }
 
        /* Output */
-       //for (row = 1; row < 63; row++) {
-       //      for (col = 1; col < 511; col++) {
-       //              print_ptrn(tiles[row][col].top);
-       //              print_ptrn(tiles[row][col].bot);
-       //              printf(" ");
-       //      }
-       //      printf("\n");
-       //}
+       if (0)
+       for (row = 0; rows[row].ncols >= 0; row++) {
+               for (col = 0; col > rows[row].ncols; col++) {
+                       print_ptrn(rows[row].cols[col].top);
+                       print_ptrn(rows[row].cols[col].bot);
+                       printf(" ");
+               }
+               printf("\n");
+       }
 
        /* HTML */
        print_index(rows);
+
+       /* Free tile */
+       for (int row = 0; rows[row].ncols >= 0; row++)
+               if (rows[row].cols > 0)
+                       free(rows[row].cols);
+       free(rows);
+
+       return 0;
 }
index ea142e4cbf07b47f0f2ee612036c33089d55fe30..dc787dd0a961e3f2122dff10a384aacd45256397 100644 (file)
@@ -1,7 +1,8 @@
 PROGS=knot
 CLEAN=html.c *.html
 
-default:V: hitch.html
+knots=`{ls *.txt}
+default:V: ${knots:%.txt=%.html}
 
 knot: knot.o html.o knot.h