#include "ESPReader.h"

#include <stdio.h>
#include <stdlib.h>
#undef NDEBUG
#include <assert.h>

int main(void) {
	FILE *fp;
	errno_t ret = fopen_s(&fp, "Skyrim.esm", "rb");

	if (ret || !fp)
		return ret;

	fseek(fp, 0L, SEEK_END);
	size_t size = ftell(fp);
	rewind(fp);

	char *buffer = malloc(size);
	if (!buffer)
		return errno;

	size_t read = fread(buffer, sizeof(char), size, fp);
	assert(read == size);

	size_t dc_size = espr_decompressed_size(buffer, size);
	
	char *decompressed = malloc(dc_size);
	if (!decompressed)
		return errno;

	espr_decompress(buffer, size, decompressed, dc_size);

	size_t formid_count = espr_formid_count(decompressed, dc_size);

	printf("FormID Count: %zu\n", formid_count);

	return 0;
}
