Code cleanup and documentation

This commit is contained in:
2025-10-18 21:35:01 +02:00
parent 6b0e858064
commit e124e3071e
17 changed files with 105 additions and 95 deletions

92
grid.c
View File

@@ -1,3 +1,8 @@
/*
* Created by snapshot112 on 2/10/2025
*/
#include "grid.h"
#include <stdlib.h>
@@ -8,12 +13,15 @@
* The grid type this program is build around.
*/
typedef struct grid_data {
char *rost;
char *locations;
int height;
int width;
state state;
} grid;
/*
* Translate x and y coordinates to a location index on the grid.
*/
static int internal_location(const grid *gp, const int x, const int y) {
return y * (gp->width + 1) + x;
}
@@ -48,12 +56,12 @@ grid *grid_create_from_string(const char* input) {
return NULL;
}
gp->rost = malloc(grid_size * sizeof(char));
gp->locations = malloc(grid_size * sizeof(char));
gp->height = height;
gp->width = width;
gp->state = STATE_BEGIN;
strcpy(gp->rost, input);
strcpy(gp->locations, input);
return gp;
}
@@ -62,8 +70,8 @@ grid *grid_create_from_string(const char* input) {
* Sets a grids width and height
*
* Input:
* fh: the stream to read the grid from.
* rost: a grid file to store the width and height in.
* fh: The stream to read the grid from.
* gp: A pointer to the grid whose width and height you want to set.
*
* Side effects:
* the grid gets its width and height set
@@ -72,27 +80,27 @@ grid *grid_create_from_string(const char* input) {
* 1 if the file width and height seem to match with its size
* 0 otherwise
*/
static int get_grid_sizes(FILE *fh, grid *rost) {
static int get_grid_sizes(FILE *fh, grid *gp) {
while (getc(fh) != '\n') {
if (feof(fh)) {
return 0;
}
rost->width++;
gp->width++;
}
if (rost->width == 0) {
if (gp->width == 0) {
return 0;
}
fseek(fh, 0, SEEK_END);
// Get file size (- 1 for the blank newline and EOF at the end)
if (ftell(fh) % (rost->width + 1) != 0) {
if (ftell(fh) % (gp->width + 1) != 0) {
// Not all lines are the same width
return 0;
}
rost->height = (int)ftell(fh) / (int)sizeof(char) / (rost->width + 1);
gp->height = (int)ftell(fh) / (int)sizeof(char) / (gp->width + 1);
fseek(fh, 0, SEEK_SET);
return 1;
@@ -103,7 +111,7 @@ grid *grid_create_from_file(FILE *fh) {
return NULL;
}
grid rost = {
grid temp_grid = {
NULL,
0,
0,
@@ -111,52 +119,52 @@ grid *grid_create_from_file(FILE *fh) {
};
// Sets the width and height of the grid.
if (get_grid_sizes(fh, &rost) != 1) {
if (get_grid_sizes(fh, &temp_grid) != 1) {
// Unlogical file structure.
return NULL;
}
const int grid_size = (rost.width + 1) * rost.height + 1;
const int grid_size = (temp_grid.width + 1) * temp_grid.height + 1;
rost.rost = malloc(grid_size * sizeof(char));
if (rost.rost == NULL) {
temp_grid.locations = malloc(grid_size * sizeof(char));
if (temp_grid.locations == NULL) {
return NULL;
}
// This makes the strncat() work.
rost.rost[0] = '\0';
temp_grid.locations[0] = '\0';
char *line = malloc((rost.width + 2) * sizeof(char));
char *line = malloc((temp_grid.width + 2) * sizeof(char));
if (line == NULL) {
return NULL;
}
for (int i = 0; i < rost.height; i++) {
if (fgets(line, rost.width + 2, fh) == NULL) {
free(rost.rost);
for (int i = 0; i < temp_grid.height; i++) {
if (fgets(line, temp_grid.width + 2, fh) == NULL) {
free(temp_grid.locations);
free(line);
return NULL;
}
// Validate that the line length is correct
if ((int)strcspn(line, "\n") != rost.width) {
free(rost.rost);
if ((int)strcspn(line, "\n") != temp_grid.width) {
free(temp_grid.locations);
free(line);
return NULL;
}
// Width is without the newline at the end.
strncat(rost.rost, line, rost.width + 1);
strncat(temp_grid.locations, line, temp_grid.width + 1);
}
free(line);
grid *return_grid = malloc(sizeof(rost));
grid *return_grid = malloc(sizeof(temp_grid));
if (return_grid == NULL) {
return NULL;
}
memcpy(return_grid, &rost, sizeof(rost));
memcpy(return_grid, &temp_grid, sizeof(temp_grid));
return return_grid;
}
@@ -192,9 +200,9 @@ void grid_put_state(grid *gp, const state t) {
void grid_cleanup(grid *gp) {
if (gp != NULL) {
if (gp->rost != NULL)
if (gp->locations != NULL)
{
free(gp->rost);
free(gp->locations);
}
free(gp);
}
@@ -215,7 +223,7 @@ int grid_height(const grid *gp) {
}
int grid_contains(const grid *gp, const int x, const int y) {
if (gp != NULL && gp->rost != NULL) {
if (gp != NULL && gp->locations != NULL) {
if (x >= 0 && y >= 0 && x < gp->width && y < gp->height)
{
return 1;
@@ -225,25 +233,25 @@ int grid_contains(const grid *gp, const int x, const int y) {
}
char grid_fetch(const grid *gp, const int x, const int y) {
if (gp != NULL && gp->rost != NULL && grid_contains(gp, x, y) == 1) {
return gp->rost[internal_location(gp, x, y)];
if (gp != NULL && gp->locations != NULL && grid_contains(gp, x, y) == 1) {
return gp->locations[internal_location(gp, x, y)];
}
return '\0';
}
int grid_put(grid *gp, const int x, const int y, const char c) {
if (gp != NULL && gp->rost != NULL && grid_contains(gp, x, y) == 1) {
gp->rost[internal_location(gp, x, y)] = c;
if (gp != NULL && gp->locations != NULL && grid_contains(gp, x, y) == 1) {
gp->locations[internal_location(gp, x, y)] = c;
return 1;
}
return 0;
}
char *grid_fetch_row(const grid *gp, const int y) {
if (gp != NULL && gp->rost != NULL && grid_contains(gp, 0, y) == 1) {
if (gp != NULL && gp->locations != NULL && grid_contains(gp, 0, y) == 1) {
// we're going to remove the newline so this is long enough
char *row = malloc((gp->width + 1) * sizeof(char));
memcpy(row, &gp->rost[internal_location(gp, 0, y)], gp->width * sizeof(char));
memcpy(row, &gp->locations[internal_location(gp, 0, y)], gp->width * sizeof(char));
row[gp->width] = '\0';
return row;
}
@@ -251,11 +259,11 @@ char *grid_fetch_row(const grid *gp, const int y) {
}
grid *grid_copy(const grid *gp) {
if (gp != NULL && gp->rost != NULL) {
if (gp != NULL && gp->locations != NULL) {
const size_t grid_memory = ((gp->width + 1) * gp->height + 1) * sizeof(char);
char *rost = malloc(grid_memory);
if (rost == NULL) {
char *locations = malloc(grid_memory);
if (locations == NULL) {
return NULL;
}
@@ -264,18 +272,18 @@ grid *grid_copy(const grid *gp) {
return NULL;
}
memcpy(rost, gp->rost, grid_memory);
memcpy(locations, gp->locations, grid_memory);
memcpy(new_grid, gp, sizeof(*gp));
new_grid->rost = rost;
new_grid->locations = locations;
return new_grid;
}
return NULL;
}
void grid_find(const grid *gp, const char c, int *x, int *y) {
if (gp == NULL || gp->rost == NULL) {
if (gp == NULL || gp->locations == NULL) {
*x = -1;
*y = -1;
return;
@@ -283,9 +291,9 @@ void grid_find(const grid *gp, const char c, int *x, int *y) {
const char search[2] = {c};
const int char_index = (int)strcspn(gp->rost, search);
const int char_index = (int)strcspn(gp->locations, search);
if (gp->rost[char_index] == '\0')
if (gp->locations[char_index] == '\0')
{
*x = -1;
*y = -1;