Skip to content

Commit

Permalink
Add parsing / providing of command line args
Browse files Browse the repository at this point in the history
  • Loading branch information
ahjmorton committed Apr 4, 2020
1 parent 97417db commit 94ba959
Showing 1 changed file with 78 additions and 31 deletions.
109 changes: 78 additions & 31 deletions cli/maze_solve.c
Original file line number Diff line number Diff line change
@@ -1,47 +1,94 @@
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <inttypes.h>
#include <errno.h>

#include "lib_maze_solve.h"

int main(void) {
maze_solve_coors first = {
.x = 0, .y = 1
};
maze_solve_coors second = {
.x = 5, .y = 2
};
maze_solve_coors start = {
.x = 0, .y = 0
};
maze_solve_coors end = {
.x = 5, .y = 0
};
static inline bool safe_parse_num(char * num, uint8_t * result) {
char * endptr;

errno = 0;
(*result) = strtoumax(num, &endptr, 10);
strerror(errno);

if(*endptr != '\0') {
return false;
}
if(errno != 0) {
return false;
}
return true;
}

static inline bool parse_coors(char* arg, maze_solve_coors * coors) {
char * x = strtok(arg, ",");
if(x == NULL) {
return false;
}
char * y = strtok(NULL, ",");
if(y == NULL) {
return false;
}
if(!safe_parse_num(x, &(coors->x))) {
return false;
}
if(!safe_parse_num(y, &(coors->y))) {
return false;
}
return true;
}

int main(int argc, char** argv) {
if(argc != 5) {
return 1;
}
maze_solve_coors first, second, start, end;
if(!parse_coors(argv[1], &first)) {
return 2;
}
if(!parse_coors(argv[2], &second)) {
return 3;
}
if(!parse_coors(argv[3], &start)) {
return 4;
}
if(!parse_coors(argv[4], &end)) {
return 5;
}

maze_solve_result * result = maze_solve(
&first,
&second,
&start,
&end
);


int rc = 0;
enum maze_solve_error error_code = maze_error_code(result);
printf("error: %i\n", maze_error_code(result));
uint8_t dir_length = maze_directions_count(result);
printf("direction_count: %i\n", dir_length);
const enum maze_solve_direction * directions =
maze_directions(result);
for(uint8_t i = 0; i < dir_length; i++) {
const enum maze_solve_direction direction =
directions[i];
if(direction == D_Up) {
printf("Up,");
} else if(direction == D_Right) {
printf("Right,");
} else if(direction == D_Down) {
printf("Down,");
} else if(direction == D_Left) {
printf("Left,");
if(error_code != E_None) {
rc = 10 + error_code;
} else {
uint8_t dir_length = maze_directions_count(result);
const enum maze_solve_direction * directions =
maze_directions(result);
for(uint8_t i = 0; i < dir_length; i++) {
const enum maze_solve_direction direction =
directions[i];
if(direction == D_Up) {
printf("Up,");
} else if(direction == D_Right) {
printf("Right,");
} else if(direction == D_Down) {
printf("Down,");
} else if(direction == D_Left) {
printf("Left,");
}
}
printf("\n");
}
printf("\n");

free_maze_solve_result(result);
return rc;
}

0 comments on commit 94ba959

Please sign in to comment.