-
Notifications
You must be signed in to change notification settings - Fork 5
/
parse.c
51 lines (40 loc) · 1.01 KB
/
parse.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdio.h>
#include <stdlib.h>
#include "defs.h"
#include "game.h"
int mc_parsefile(const char *file) {
FILE *f;
int i, j;
char c;
/* These are to be passed to init_game() */
int **field;
int size_x, size_y;
f = fopen(file, "r");
if(!f) return FALSE;
if(fscanf(f, "%dx%d\n", &size_x, &size_y) != 2) return FALSE;
/*
* FIXME: This is a bit screwed up: The matrix is not filled in the
* same order that we would need to allocate it.
*/
field = (int **)malloc(sizeof(int *)*size_x);
for(i=0; i < size_x; i++)
field[i] = (int *)malloc(sizeof(int)*size_y);
for(i=0; i < size_y; i++) {
for(j=0; j < size_x; j++) {
c = getc(f);
switch(c) {
case '#':
field[j][i] = AREA_BLOCKED;
break;
case '\n': /* Parsing error */
return FALSE;
default: /* By default we assume a free area */
field[j][i] = AREA_CLEAR;
}
}
/* We expect '\n' here */
if(getc(f) != '\n') return FALSE;
}
/* Succesfully parsed! */
return init_game(field, size_x, size_y);
}