Added quick, easy file read function

This commit is contained in:
Eric-Paul Ickhorn 2024-01-01 19:24:55 +01:00
parent ba3619ede2
commit 77b4091a66
Signed by: epickh
GPG Key ID: F5EBBE013924D95F
3 changed files with 50 additions and 8 deletions

View File

@ -6,7 +6,7 @@ GCC_ARGUMENTS="-std=c11 -Wall"
MODULES=(
"core"
"json"
"platform"
)
# Arguments:
@ -41,16 +41,12 @@ function compile_folder() {
done
ar -rvs $PROJECT_ROOT/.build/$PROJECT_NAME-$1.a $OBJECTS_FOLDER/*.o
cd $PROJECT_ROOT
}
function compile_all_modules() {
for MODULE in $MODULES
do
echo "============ CURRENT MODULE: $MODULE ============"
compile_folder \
"$MODULE" \
"-I $PROJECT_ROOT/$MODULE/exports/"
done
compile_folder "core" "-I $PROJECT_ROOT/core/exports/"
compile_folder "platform" "-I $PROJECT_ROOT/core/exports/ -I $PROJECT_ROOT/platform/exports/"
}
case $1 in

View File

@ -0,0 +1,10 @@
#ifndef RR_FS_QUICK_H
#define RR_FS_QUICK_H
#include <librr/types.h>
usz_t rr_get_file_length(const char *path);
void * rr_load_whole_file(const char *path);
#endif // RR_FS_QUICK_H

36
platform/src-c/fs/quick.c Normal file
View File

@ -0,0 +1,36 @@
#include <librr/fs/quick.h>
#include <stdio.h>
#include <stdlib.h>
usz_t rr_get_file_length(const char *path)
{
FILE *file = fopen(path, "rb");
if(file == NULL)
return 0;
fseek(file, 0, SEEK_END);
usz_t length = ftell(file);
fclose(file);
return length;
}
void * rr_load_whole_file(const char *path)
{
FILE *file = fopen(path, "rb");
if(file == NULL)
return NULL;
fseek(file, 0, SEEK_END);
usz_t length = ftell(file);
fseek(file, 0, SEEK_SET);
char *data = malloc(length+1);
usz_t read_length = fread(data, length, 1, file);
if(read_length < length)
data = realloc(data, read_length);
fclose(file);
return data;
}