Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
vfile: add path-based factory
Browse files Browse the repository at this point in the history
  • Loading branch information
rr- committed Aug 20, 2024
1 parent 9aea412 commit f17ae2b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/libtrx/virtual_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ typedef struct {
char *cur_ptr;
} VFILE;

VFILE *VFile_CreateFromPath(const char *path);
VFILE *VFile_CreateFromBuffer(const char *data, size_t size);
void VFile_Close(VFILE *file);

Expand Down
28 changes: 28 additions & 0 deletions src/virtual_file.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,38 @@
#include "virtual_file.h"

#include "filesystem.h"
#include "log.h"
#include "memory.h"

#include <assert.h>
#include <string.h>

VFILE *VFile_CreateFromPath(const char *const path)
{
MYFILE *fp = File_Open(path, FILE_OPEN_READ);
if (!fp) {
LOG_ERROR("Can't open file %s", path);
return NULL;
}

const size_t data_size = File_Size(fp);
char *data = Memory_Alloc(data_size);
File_ReadData(fp, data, data_size);
if (File_Pos(fp) != data_size) {
LOG_ERROR("Can't read file %s", path);
Memory_FreePointer(&data);
File_Close(fp);
return NULL;
}
File_Close(fp);

VFILE *const file = Memory_Alloc(sizeof(VFILE));
file->content = data;
file->size = data_size;
file->cur_ptr = file->content;
return file;
}

VFILE *VFile_CreateFromBuffer(const char *data, size_t size)
{
VFILE *const file = Memory_Alloc(sizeof(VFILE));
Expand Down

0 comments on commit f17ae2b

Please sign in to comment.