Add pckr_test
This commit is contained in:
parent
e162e078c5
commit
945bb3ff51
111
src/pckr_test.c
Normal file
111
src/pckr_test.c
Normal file
@ -0,0 +1,111 @@
|
||||
#include "aliases.h"
|
||||
#include "io.h"
|
||||
#include "pak.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define ARRLEN(ARR) (sizeof ARR / sizeof ARR[0])
|
||||
|
||||
typedef struct {
|
||||
const char *name;
|
||||
const char *contents;
|
||||
} asset_t;
|
||||
|
||||
i32 main(i32 argc, char *argv[]) {
|
||||
FILE *fp = fopen("assets.pak", "rb");
|
||||
if (!fp) {
|
||||
printf("Failed to open file\n");
|
||||
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
asset_t assets[] = {
|
||||
(asset_t){.name = "file01", .contents = "Hello\n"},
|
||||
(asset_t){.name = "NAME", .contents = "Abdelrahman\n"},
|
||||
(asset_t){.name = "file02", .contents = "This is the second file\n"},
|
||||
};
|
||||
|
||||
u64 expected_pak_size = sizeof(pak_t);
|
||||
|
||||
u64 expected_count = ARRLEN(assets);
|
||||
|
||||
u64 expected_entries_size = 0;
|
||||
|
||||
for (u64 i = 0; i < expected_count; ++i) {
|
||||
u64 name_length = strlen(assets[i].name) + 1;
|
||||
u64 contents_length = strlen(assets[i].contents) + 1;
|
||||
|
||||
expected_pak_size += name_length + contents_length + sizeof(toc_entry_t) +
|
||||
sizeof(pak_entry_t);
|
||||
|
||||
expected_entries_size += name_length + sizeof(toc_entry_t);
|
||||
}
|
||||
|
||||
u64 real_size = get_file_length(fp);
|
||||
|
||||
if (expected_pak_size != real_size) {
|
||||
printf("Real size (%zu) doesn't match expected size (%zu)\n", real_size,
|
||||
expected_pak_size);
|
||||
|
||||
fclose(fp);
|
||||
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
pak_t pack = {0};
|
||||
fread((void *)&pack, sizeof(pak_t), 1, fp);
|
||||
|
||||
if (pack.toc.count != expected_count) {
|
||||
printf("Pack file count (%zu) doesn't match expected count (%zu)\n",
|
||||
pack.toc.count, expected_count);
|
||||
|
||||
fclose(fp);
|
||||
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (pack.toc.size != expected_entries_size) {
|
||||
printf(
|
||||
"Table of contents real size (%zu) doesn't match expected size (%zu)\n",
|
||||
pack.toc.size, expected_entries_size);
|
||||
|
||||
fclose(fp);
|
||||
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
toc_entry_t *entries[pack.toc.count];
|
||||
memset((void *)entries, 0, sizeof(toc_entry_t *) * 3);
|
||||
|
||||
for (u64 i = 0; i < pack.toc.count; ++i) {
|
||||
u64 pos = ftell(fp);
|
||||
|
||||
u64 data[2] = {0};
|
||||
|
||||
fread(data, ARRLEN(data), 1, fp);
|
||||
|
||||
u64 size = sizeof(toc_entry_t) + data[1];
|
||||
|
||||
entries[i] = malloc(size);
|
||||
if (!(entries[i])) {
|
||||
printf("Allocation failure\n");
|
||||
|
||||
fclose(fp);
|
||||
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
fseek(fp, pos, SEEK_SET);
|
||||
|
||||
fread(&(entries[i]), size, 1, fp);
|
||||
}
|
||||
|
||||
for (u64 i = 0; i < pack.toc.count; ++i) {
|
||||
free(entries[i]);
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
Loading…
Reference in New Issue
Block a user