Compare commits
6 Commits
31d78bfcd3
...
f3ae1aef64
Author | SHA1 | Date | |
---|---|---|---|
f3ae1aef64 | |||
c04e8bd4a3 | |||
c5c447d5c5 | |||
490ccfc3c6 | |||
041167d26f | |||
7beac7e475 |
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,3 +1,5 @@
|
||||
.cache
|
||||
compile_commands.json
|
||||
pckr
|
||||
pakrd.a
|
||||
dist
|
||||
|
19
compile
19
compile
@ -21,6 +21,7 @@ FG_BR_CYAN="\033[96"
|
||||
FG_BR_WHITE="\033[97"
|
||||
|
||||
CC=clang
|
||||
AR=ar
|
||||
CFLAGS="-Wall -Werror -pedantic -Iinclude "
|
||||
|
||||
PCKR_SRC="\
|
||||
@ -32,6 +33,13 @@ PCKR_SRC="\
|
||||
src/pckr.c \
|
||||
src/main.c"
|
||||
|
||||
PAKRD_SRC="\
|
||||
src/path_utils.c \
|
||||
src/io.c \
|
||||
src/darr.c \
|
||||
src/pak.c"
|
||||
PAKRD_FLAGS="-c -fPIC"
|
||||
|
||||
PCKR_TEST_SRC="\
|
||||
src/path_utils.c \
|
||||
src/io.c \
|
||||
@ -66,9 +74,13 @@ done
|
||||
if [[ $BUILD_TYPE == "debug" ]]; then
|
||||
CFLAGS+="-g "
|
||||
PCKR_OUT="pckr"
|
||||
PAKRD_OUT="pakrd.a"
|
||||
else
|
||||
mkdir dist
|
||||
cp ./include/pak.h dist
|
||||
CFLAGS+="-O3 "
|
||||
PCKR_OUT="../proj/pckr"
|
||||
PCKR_OUT="dist/pckr"
|
||||
PAKRD_OUT="dist/pakrd.a"
|
||||
fi
|
||||
|
||||
|
||||
@ -76,6 +88,11 @@ fi
|
||||
(set -x ; $CC $CFLAGS $PCKR_SRC -o $PCKR_OUT)
|
||||
|
||||
|
||||
# Build pakrd static library
|
||||
(set -x ; $CC $CFLAGS $PAKRD_FLAGS $PAKRD_SRC ; $AR r $PAKRD_OUT *.o)
|
||||
rm *.o
|
||||
|
||||
|
||||
# Build pckr_test executable
|
||||
(set -x ; $CC $CFLAGS $PCKR_TEST_SRC -o $PCKR_TEST_OUT)
|
||||
|
||||
|
@ -5,8 +5,10 @@
|
||||
#include <linux/limits.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define STREQ(S1, S2) (strcmp(S1, S2) == 0)
|
||||
#define STRNEQ(S1, S2, COUNT) (strncmp(S1, S2, COUNT) == 0)
|
||||
|
||||
typedef struct dirwalk dirwalk_t;
|
||||
typedef struct {
|
||||
|
@ -6,7 +6,7 @@
|
||||
|
||||
typedef struct {
|
||||
u64 size;
|
||||
u8 *data;
|
||||
u8 data[];
|
||||
} buf_t;
|
||||
|
||||
typedef struct {
|
||||
@ -41,7 +41,8 @@ typedef struct asset_pack asset_pack_t;
|
||||
|
||||
bool create_asset_pack(const char *dirpath, const char *output);
|
||||
asset_pack_t *load_asset_pack(const char *filepath);
|
||||
// buf_t read_file_from_pack(pak_read_t *pack, const char *filename);
|
||||
buf_t *read_file_from_pack(asset_pack_t *pack, const char *filename);
|
||||
void close_pack_file(buf_t **buf);
|
||||
void close_asset_pack(asset_pack_t **pack);
|
||||
|
||||
#endif // !PAK_H
|
||||
|
11
include/test_utils.h
Normal file
11
include/test_utils.h
Normal file
@ -0,0 +1,11 @@
|
||||
#ifndef TEST_UTILS_H
|
||||
#define TEST_UTILS_H
|
||||
|
||||
#define ARRLEN(ARR) (sizeof ARR / sizeof ARR[0])
|
||||
|
||||
typedef struct {
|
||||
const char *name;
|
||||
const char *contents;
|
||||
} asset_t;
|
||||
|
||||
#endif // !TEST_UTILS_H
|
40
src/pak.c
40
src/pak.c
@ -176,7 +176,45 @@ asset_pack_t *load_asset_pack(const char *filepath) {
|
||||
return pack;
|
||||
}
|
||||
|
||||
// buf_t read_file_from_pack(pak_read_t *pack, const char *filename) {}
|
||||
buf_t *read_file_from_pack(asset_pack_t *pack, const char *filename) {
|
||||
toc_entry_t *entry = NULL;
|
||||
buf_t *output = NULL;
|
||||
|
||||
for (u64 i = 0; i < pack->header.toc.count; ++i) {
|
||||
entry = pack->toc_entries[i];
|
||||
|
||||
if (STRNEQ(filename, entry->name, entry->length)) {
|
||||
fseek(pack->fp, pack->assets_offset + entry->offset, SEEK_SET);
|
||||
|
||||
u64 size = 0;
|
||||
fread(&size, sizeof(u64), 1, pack->fp);
|
||||
|
||||
output = (buf_t *)malloc(sizeof(buf_t) + size);
|
||||
if (!output) {
|
||||
break;
|
||||
}
|
||||
|
||||
output->size = size;
|
||||
|
||||
fread(&(output->data), size, 1, pack->fp);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
void close_pack_file(buf_t **buf) {
|
||||
buf_t *b = *buf;
|
||||
|
||||
if (!b || b->size == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
free(*buf);
|
||||
*buf = NULL;
|
||||
}
|
||||
|
||||
void close_asset_pack(asset_pack_t **pack) {
|
||||
if (!(*pack)) {
|
||||
|
@ -1,4 +1,6 @@
|
||||
#include "io.h"
|
||||
#include "pak.h"
|
||||
#include "test_utils.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
@ -10,5 +12,40 @@ int main(int argc, char *argv[]) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
asset_t assets[] = {
|
||||
(asset_t){.name = "file01", .contents = "Hello\n"},
|
||||
(asset_t){.name = "name.txt", .contents = "Abdelrahman\n"},
|
||||
(asset_t){.name = "file02", .contents = "This is the second file\n"},
|
||||
};
|
||||
|
||||
for (u64 i = 0; i < ARRLEN(assets); ++i) {
|
||||
buf_t *buf = read_file_from_pack(pack, assets[i].name);
|
||||
if (!buf || buf->size == 0) {
|
||||
printf("Failed to read file %s\n", assets[i].name);
|
||||
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (!(STREQ((char *)(buf->data), assets[i].contents))) {
|
||||
printf("File contents mismatch for file %s\n", assets[i].name);
|
||||
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
close_pack_file(&buf);
|
||||
if (buf) {
|
||||
printf("Failed to close file %s\n", assets[i].name);
|
||||
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
close_asset_pack(&pack);
|
||||
if (pack) {
|
||||
printf("Failed to close asset pack");
|
||||
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
@ -1,19 +1,13 @@
|
||||
#include "aliases.h"
|
||||
#include "io.h"
|
||||
#include "pak.h"
|
||||
#include "test_utils.h"
|
||||
#include <linux/limits.h>
|
||||
#include <stdbool.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[]) {
|
||||
char filepath[PATH_MAX] = {0};
|
||||
realpath("assets.pak", filepath);
|
||||
|
Loading…
Reference in New Issue
Block a user