Created
July 27, 2012 18:41
-
-
Save cleichner/3189701 to your computer and use it in GitHub Desktop.
Maze Parsing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ | |
| _ _ _ _ | _ _ _ _ _ | | |
| |_|_ |_|_ _| _|_ _ _| | | |
| |_ _|_ | |_| |_ _ _| |_| | |
|_| |_ _ _|_ _ _| |_ _| | |
| | |_ _ | |_|_|_ | _|_ | | |
| _| | |_|_ _| |_ _ | | | |
| | | _| |_| |_ _ _| | | | |
| |_| |_|_ _ | |_ _|_ _|_ | | | |
|_| _| _ _ _| | | | _ _ | |_| | |
|_ _|_ _ _|_|_|_ |_ _ _ | | | |
| | | _| | |_ _| | | _| | | |
| |_| _ _ _ _ _|_ | | | | | |
| | |_ | _| _| |_ |_|_ _ | | |
| |_ _ _| |_| | |_ |_ _ _| | | | |
| _| _ _ _ | | _ _ | | |
|_ _ _|_ _ _|_ _ _|_ _ _ _ _ _|_| |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static bool space(char cur) { return cur == ' ' || cur == '\t'; } | |
static bool underscore(char cur) { return cur == '_'; } | |
static bool newline(char cur) { return cur == '\n'; } | |
static bool pipe_char(char cur) { return cur == '|'; } | |
static bool underscore_or_space(char cur) { | |
return underscore(cur) || space(cur); | |
} | |
static bool pipe_char_or_space(char cur) { return pipe_char(cur) || space(cur); } | |
static void accept(bool (*accept_f)(char), char* name, char* dest, char cur) { | |
assert(cur != EOF && "Error: End of file was reached while still parsing."); | |
if (accept_f(cur)) { | |
*dest = cur; | |
return; | |
} | |
fprintf(stderr, "Error: expected %s, but found '%c'\n", name, cur); | |
exit(1); | |
} | |
static void ignore_trailing_whitespace(FILE* stream) { | |
int8_t cur = fgetc(stream); | |
while (space(cur)) { | |
cur = fgetc(stream); | |
} | |
char _; | |
accept(newline, "a newline", &_, cur); | |
} | |
static void parse_maze_file(char chars[CHAR_WIDTH][CHAR_HEIGHT], FILE* stream) { | |
int8_t col = 0; | |
int8_t row = HEIGHT; | |
int8_t k = 0; | |
// parse top line | |
accept(space, "a space", &chars[col][row], fgetc(stream)); col++; | |
for (k = 0; k < WIDTH-1; k++) { | |
accept(underscore, "an underscore", &chars[col][row], fgetc(stream)); col++; | |
accept(space, "a space", &chars[col][row], fgetc(stream)); col++; | |
} | |
accept(underscore, "an underscore", &chars[col][row], fgetc(stream)); col++; | |
ignore_trailing_whitespace(stream); col = 0; row--; | |
// parse body | |
for (k = 0; k < HEIGHT; k++) { | |
accept(pipe_char, "a pipe_char", &chars[col][row], fgetc(stream)); col++; | |
int8_t l = 0; | |
for (l = 0; l < WIDTH-1; l++) { | |
accept(underscore_or_space, "an underscore or space", | |
&chars[col][row], fgetc(stream)); col++; | |
accept(pipe_char_or_space, "a pipe_char or space", &chars[col][row], | |
fgetc(stream)); col++; | |
} | |
accept(underscore_or_space, "an underscore or space", &chars[col][row], | |
fgetc(stream)); col++; | |
accept(pipe_char, "a pipe_char", &chars[col][row], fgetc(stream)); col++; | |
ignore_trailing_whitespace(stream); col = 0; row--; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment