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

Commit

Permalink
virtual-file: add from TR2X
Browse files Browse the repository at this point in the history
  • Loading branch information
rr- committed Aug 20, 2024
1 parent 444fd60 commit 1e158d1
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 1 deletion.
3 changes: 2 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ endif

sources = [
'src/benchmark.c',
'src/filesystem.c',
'src/engine/audio.c',
'src/engine/audio_sample.c',
'src/engine/audio_stream.c',
'src/filesystem.c',
'src/json/bson_parse.c',
'src/json/bson_write.c',
'src/json/json_base.c',
Expand All @@ -59,6 +59,7 @@ sources = [
'src/memory.c',
'src/strings.c',
'src/vector.c',
'src/virtual_file.c',
]

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

#include "memory.h"

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

VFILE *VFile_CreateFromBuffer(const char *data, size_t size)
{
VFILE *const file = Memory_Alloc(sizeof(VFILE));
file->content = Memory_Dup(data, size);
file->size = size;
file->cur_ptr = file->content;
return file;
}

void VFile_Close(VFILE *file)
{
Memory_FreePointer(&file->content);
Memory_FreePointer(&file);
}

size_t VFile_GetPos(const VFILE *file)
{
return file->cur_ptr - file->content;
}

void VFile_Skip(VFILE *file, int32_t offset)
{
const size_t cur_pos = VFile_GetPos(file);
assert(cur_pos + offset <= file->size);
file->cur_ptr += offset;
}

void VFile_Read(VFILE *file, void *target, size_t size)
{
const size_t cur_pos = VFile_GetPos(file);
assert(cur_pos + size <= file->size);
memcpy(target, file->cur_ptr, size);
file->cur_ptr += size;
}

int8_t VFile_ReadS8(VFILE *file)
{
int8_t result;
VFile_Read(file, &result, sizeof(result));
return result;
}

int16_t VFile_ReadS16(VFILE *file)
{
int16_t result;
VFile_Read(file, &result, sizeof(result));
return result;
}

int32_t VFile_ReadS32(VFILE *file)
{
int32_t result;
VFile_Read(file, &result, sizeof(result));
return result;
}

uint8_t VFile_ReadU8(VFILE *file)
{
uint8_t result;
VFile_Read(file, &result, sizeof(result));
return result;
}

uint16_t VFile_ReadU16(VFILE *file)
{
uint16_t result;
VFile_Read(file, &result, sizeof(result));
return result;
}

uint32_t VFile_ReadU32(VFILE *file)
{
uint32_t result;
VFile_Read(file, &result, sizeof(result));
return result;
}
23 changes: 23 additions & 0 deletions src/virtual_file.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include <stdint.h>

typedef struct {
char *content;
size_t size;
char *cur_ptr;
} VFILE;

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

size_t VFile_GetPos(const VFILE *file);
void VFile_Skip(VFILE *file, int32_t offset);

void VFile_Read(VFILE *file, void *target, size_t size);
int8_t VFile_ReadS8(VFILE *file);
int16_t VFile_ReadS16(VFILE *file);
int32_t VFile_ReadS32(VFILE *file);
uint8_t VFile_ReadU8(VFILE *file);
uint16_t VFile_ReadU16(VFILE *file);
uint32_t VFile_ReadU32(VFILE *file);

0 comments on commit 1e158d1

Please sign in to comment.