-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.c
71 lines (60 loc) · 1.79 KB
/
file.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "file.h"
int** read_map() {
FILE* fp = fopen("map.txt", "rt");
if(fp == NULL) {
puts("Can't open map\nexiting program\n");
exit(1);
}
LIST* first = NULL, *moving = NULL;
int y = 0, x = 0, xmax = 0;
char c;
while((c = (char)fgetc(fp)) != EOF) {
if (isalnum(c)) {
if (first == NULL) {
moving = new_element(x, y, c);
first = moving;
} else {
moving->next = new_element(x, y, c);
moving = moving->next;
}
++x;
}
else if(c == '\n') {
if(y == 0)
xmax = x;
x = 0;
++y;
}
}
int** map = list_to_array(first, xmax, y);
return map;
}
/*------------------------------------------------------------------------*/
/*------------------------------------------------------------------------*/
/*------------------------------------------------------------------------*/
LIST* new_element(int x, int y, char value) {
LIST* new = (LIST*)malloc(sizeof(LIST));
new->x = x;
new->y = y;
new->value = value;
new->next = NULL;
return new;
}
/*------------------------------------------------------------------------*/
/*------------------------------------------------------------------------*/
/*------------------------------------------------------------------------*/
int** list_to_array(LIST* plist, int x, int y) {
LIST* tmp;
int** map;
map = (int**)malloc(y * sizeof(int*));
for(int i = 0; i <= y; ++i) {
map[i] = (int*) malloc(x * sizeof(int));
}
while(plist != NULL) {
map[plist->y][plist->x] = plist->value - '0';
tmp = plist;
plist = plist->next;
free(tmp);
}
return map;
}