initial
This commit is contained in:
26
farm/maps.c
Normal file
26
farm/maps.c
Normal file
@@ -0,0 +1,26 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user