Compare commits

..

No commits in common. "f3ae1aef6434c448c15737e1115e1154fae5e58f" and "31d78bfcd3e39217b256345cdf82ba96cdbf59ae" have entirely different histories.

8 changed files with 11 additions and 113 deletions

2
.gitignore vendored
View File

@ -1,5 +1,3 @@
.cache .cache
compile_commands.json compile_commands.json
pckr pckr
pakrd.a
dist

19
compile
View File

@ -21,7 +21,6 @@ FG_BR_CYAN="\033[96"
FG_BR_WHITE="\033[97" FG_BR_WHITE="\033[97"
CC=clang CC=clang
AR=ar
CFLAGS="-Wall -Werror -pedantic -Iinclude " CFLAGS="-Wall -Werror -pedantic -Iinclude "
PCKR_SRC="\ PCKR_SRC="\
@ -33,13 +32,6 @@ PCKR_SRC="\
src/pckr.c \ src/pckr.c \
src/main.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="\ PCKR_TEST_SRC="\
src/path_utils.c \ src/path_utils.c \
src/io.c \ src/io.c \
@ -74,13 +66,9 @@ done
if [[ $BUILD_TYPE == "debug" ]]; then if [[ $BUILD_TYPE == "debug" ]]; then
CFLAGS+="-g " CFLAGS+="-g "
PCKR_OUT="pckr" PCKR_OUT="pckr"
PAKRD_OUT="pakrd.a"
else else
mkdir dist
cp ./include/pak.h dist
CFLAGS+="-O3 " CFLAGS+="-O3 "
PCKR_OUT="dist/pckr" PCKR_OUT="../proj/pckr"
PAKRD_OUT="dist/pakrd.a"
fi fi
@ -88,11 +76,6 @@ fi
(set -x ; $CC $CFLAGS $PCKR_SRC -o $PCKR_OUT) (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 # Build pckr_test executable
(set -x ; $CC $CFLAGS $PCKR_TEST_SRC -o $PCKR_TEST_OUT) (set -x ; $CC $CFLAGS $PCKR_TEST_SRC -o $PCKR_TEST_OUT)

View File

@ -5,10 +5,8 @@
#include <linux/limits.h> #include <linux/limits.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#define STREQ(S1, S2) (strcmp(S1, S2) == 0) #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 dirwalk dirwalk_t;
typedef struct { typedef struct {

View File

@ -6,7 +6,7 @@
typedef struct { typedef struct {
u64 size; u64 size;
u8 data[]; u8 *data;
} buf_t; } buf_t;
typedef struct { typedef struct {
@ -41,8 +41,7 @@ typedef struct asset_pack asset_pack_t;
bool create_asset_pack(const char *dirpath, const char *output); bool create_asset_pack(const char *dirpath, const char *output);
asset_pack_t *load_asset_pack(const char *filepath); asset_pack_t *load_asset_pack(const char *filepath);
buf_t *read_file_from_pack(asset_pack_t *pack, const char *filename); // buf_t read_file_from_pack(pak_read_t *pack, const char *filename);
void close_pack_file(buf_t **buf);
void close_asset_pack(asset_pack_t **pack); void close_asset_pack(asset_pack_t **pack);
#endif // !PAK_H #endif // !PAK_H

View File

@ -1,11 +0,0 @@
#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

View File

@ -176,45 +176,7 @@ asset_pack_t *load_asset_pack(const char *filepath) {
return pack; return pack;
} }
buf_t *read_file_from_pack(asset_pack_t *pack, const char *filename) { // buf_t read_file_from_pack(pak_read_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) { void close_asset_pack(asset_pack_t **pack) {
if (!(*pack)) { if (!(*pack)) {

View File

@ -1,6 +1,4 @@
#include "io.h"
#include "pak.h" #include "pak.h"
#include "test_utils.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -12,40 +10,5 @@ int main(int argc, char *argv[]) {
return EXIT_FAILURE; 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; return EXIT_SUCCESS;
} }

View File

@ -1,13 +1,19 @@
#include "aliases.h" #include "aliases.h"
#include "io.h" #include "io.h"
#include "pak.h" #include "pak.h"
#include "test_utils.h"
#include <linux/limits.h> #include <linux/limits.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.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[]) { i32 main(i32 argc, char *argv[]) {
char filepath[PATH_MAX] = {0}; char filepath[PATH_MAX] = {0};
realpath("assets.pak", filepath); realpath("assets.pak", filepath);