27 lines
642 B
C
27 lines
642 B
C
#include <stddef.h>
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include "maps.h"
|
|
struct Maps* load_maps(char* filename) {
|
|
FILE* f = fopen(filename, "r");
|
|
if (f == NULL) {
|
|
return NULL;
|
|
}
|
|
int num_maps;
|
|
int num_read = fread(&num_maps, 4, 1, f);
|
|
|
|
if (num_read != 1 || num_maps > 10000) {
|
|
return NULL;
|
|
}
|
|
|
|
int mapfile_size = sizeof(struct Map) * num_maps;
|
|
struct Maps *maps = (struct Maps *)malloc(mapfile_size + 4);
|
|
maps->num_maps = num_maps;
|
|
num_read = fread(&maps->maps, 1, mapfile_size, f);
|
|
if (num_read != mapfile_size) {
|
|
return NULL;
|
|
}
|
|
fclose(f);
|
|
return maps;
|
|
}
|