aboutsummaryrefslogtreecommitdiff
path: root/src/mazerator.c
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-01-01 13:51:51 +0100
committerHampusM <hampus@hampusmat.com>2022-01-01 13:51:51 +0100
commit1bed3ac57906b26ef05b25c2bc5c1dca424dba4a (patch)
treebd445f7800d27112b3c45199c797e8a048b0306b /src/mazerator.c
parent31c6239cb2fcb75aa2ec846ce88bd57a631bbd32 (diff)
refactor: fix memory leaks & general improvements
Diffstat (limited to 'src/mazerator.c')
-rw-r--r--src/mazerator.c77
1 files changed, 46 insertions, 31 deletions
diff --git a/src/mazerator.c b/src/mazerator.c
index 03bfce3..d231173 100644
--- a/src/mazerator.c
+++ b/src/mazerator.c
@@ -6,25 +6,36 @@
void validate_number_optarg(char *optarg, int c)
{
- char *base_error = malloc((38 + 1) * sizeof(char));
- sprintf(base_error, "Error: Invalid option argument for -%c.", c);
+ unsigned int is_invalid = 0;
+
+ size_t error_length = 40;
+ char *error = malloc(error_length + 1);
if (!is_number(optarg))
{
- printf("%s It must be a number\n", base_error);
- exit(1);
+ is_invalid = 1;
+ snprintf(error, error_length, "It must be a number");
+ }
+ else if (atoi(optarg) < 1)
+ {
+ is_invalid = 1;
+ snprintf(error, error_length, "It must be greater than 0");
}
- if (atoi(optarg) < 1)
+ if (is_invalid == 1)
{
- printf("%s It must be greater than 0\n", base_error);
+ printf("Error: Invalid option argument for -%c. %s\n", c, error);
+ free(error);
exit(1);
}
+
+ free(error);
}
void validate_start_coords(int start_x, int start_y, int width, int height)
{
- char *error_format = "Error: The %s start coordinate is not allowed to be higher than the maze's %s\n";
+ char *error_format =
+ "Error: The %s start coordinate is not allowed to be higher than the maze's %s\n";
if (start_x > width)
{
@@ -46,15 +57,14 @@ void get_seed(int *seed_dst)
fclose(urandom);
}
-const struct option options[] = {
- {"width", required_argument, NULL, 'w'},
- {"heigth", required_argument, NULL, 'h'},
- {"wall", required_argument, NULL, 'W'},
- {"seed", required_argument, NULL, 's'},
- {"start-x", required_argument, NULL, 'x'},
- {"start-y", required_argument, NULL, 'y'},
- {"help", no_argument, NULL, 0},
- {NULL, 0, NULL, 0}};
+const struct option options[] = {{"width", required_argument, NULL, 'w'},
+ {"heigth", required_argument, NULL, 'h'},
+ {"wall", required_argument, NULL, 'W'},
+ {"seed", required_argument, NULL, 's'},
+ {"start-x", required_argument, NULL, 'x'},
+ {"start-y", required_argument, NULL, 'y'},
+ {"help", no_argument, NULL, 0},
+ {NULL, 0, NULL, 0}};
int main(int argc, char *argv[])
{
@@ -94,17 +104,20 @@ int main(int argc, char *argv[])
seed = atoi(optarg);
break;
case 0:
- printf(
- "Usage: %s [OPTION]...\n\n"
- "Options:\n"
- " -w, --width WIDTH The width of the maze (Default: 40)\n"
- " -h, --heigth HEIGHT The heigth of the maze (Default: 20)\n"
- " -x, --start-x X The x coordinate for the start position (Default: 0)\n"
- " -y, --start-y Y The y coordinate for the start position (Default: 0)\n"
- " -W, --wall WALL Single character used as maze walls (Default: '█')\n"
- " -s, --seed SEED The randomization seed used for maze generation (Any number)\n"
- " --help Displays usage information\n",
- argv[0]);
+ printf("Usage: %s [OPTION]...\n\n"
+ "Options:\n"
+ " -w, --width WIDTH The width of the maze (Default: 40)\n"
+ " -h, --heigth HEIGHT The heigth of the maze (Default: 20)\n"
+ " -x, --start-x X The x coordinate for the start position "
+ "(Default: 0)\n"
+ " -y, --start-y Y The y coordinate for the start position "
+ "(Default: 0)\n"
+ " -W, --wall WALL Single character used as maze walls "
+ "(Default: '█')\n"
+ " -s, --seed SEED The randomization seed used for maze "
+ "generation (Any number)\n"
+ " --help Displays usage information\n",
+ argv[0]);
exit(0);
case '?':
printf("\nTry '%s --help' for more information\n", argv[0]);
@@ -121,13 +134,15 @@ int main(int argc, char *argv[])
srand(seed);
- struct Dimensions dimens = {.width = maze_width, .height = maze_height};
+ Dimensions dimens = {.width = maze_width, .height = maze_height};
- struct Maze maze = maze_create(dimens, wall);
+ Maze maze = maze_create(dimens, wall);
- struct Position start_pos = {.x = start_x, .y = start_y};
+ Position start_pos = {.x = start_x, .y = start_y};
- maze_excavate(maze, start_pos);
+ maze_excavate(&maze, start_pos);
maze_print(maze);
+
+ maze_destroy(&maze);
}