Compare commits

..

5 Commits

23 changed files with 154 additions and 148 deletions

View File

@@ -24,9 +24,9 @@ AlignTrailingComments: true
AlignConsecutiveMacros: AcrossEmptyLines AlignConsecutiveMacros: AcrossEmptyLines
AlignEscapedNewlines: Left AlignEscapedNewlines: Left
AlignArrayOfStructures: Left AlignArrayOfStructures: Left
AlignConsecutiveAssignments: None AlignConsecutiveAssignments: Consecutive
AlignConsecutiveBitFields: AcrossEmptyLines AlignConsecutiveBitFields: AcrossEmptyLines
AlignConsecutiveDeclarations: None AlignConsecutiveDeclarations: Consecutive
AlignConsecutiveShortCaseStatements: AlignConsecutiveShortCaseStatements:
Enabled: true Enabled: true

View File

@@ -21,7 +21,7 @@ Where we have dependencies on:
| [openGL](https://www.opengl.org/) | hardware accelleration, for handling graphics. | | [openGL](https://www.opengl.org/) | hardware accelleration, for handling graphics. |
It is intended to be platform-agnostic, within reason. But the main focus is for [Linux](https://wikipedia.org/wiki/Linux) systems with [x86_64](https://wikipedia.org/wiki/X86-64) architecture. It is intended to be platform-agnostic, within reason. But the main focus is for [Linux](https://wikipedia.org/wiki/Linux) systems with [x86_64](https://wikipedia.org/wiki/X86-64) architecture.
Within [intdef.h](/src/util/intdef.h) there live definitions for fixed-width integer types. Within [types.h](/src/types.h) there live definitions for fixed-width integer types.
### style guide ### style guide
- Code must be written correctly, read [Correct C](./correct-c.md) if more information is required. - Code must be written correctly, read [Correct C](./correct-c.md) if more information is required.

View File

@@ -10,18 +10,18 @@
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include "../error.h" #include "../types.h"
#include "../util/intdef.h" #include "../util/error.h"
#define SECTOR 0x1000 // sector size #define SECTOR 0x1000 // sector size
#define TABLE 0x800 // table (total) element count #define TABLE 0x800 // table (total) element count
#define CHUNKS 0x400 // amount of chunks in a file #define CHUNKS 0x400 // amount of chunks in a file
enum mcx_compression { enum mcx_compression {
MCX_COMPRESSION_GZIP = 0x01, MCX_COMPRESSION_GZIP = 0x01,
MCX_COMPRESSION_ZLIB = 0x02, MCX_COMPRESSION_ZLIB = 0x02,
MCX_COMPRESSION_NONE = 0x03, MCX_COMPRESSION_NONE = 0x03,
MCX_COMPRESSION_LZ4 = 0x04, MCX_COMPRESSION_LZ4 = 0x04,
MCX_COMPRESSION_CUSTOM = 0x7F, MCX_COMPRESSION_CUSTOM = 0x7F,
}; };
@@ -56,7 +56,7 @@ static int mcx_loadchunk(const u8 *restrict buf, const i32 *restrict table, int
return 1; return 1;
} }
ssize_t size = -1; ssize size = -1;
for (;;) { for (;;) {
// TODO: handle buffer // TODO: handle buffer
// size = archive_read_data(archive, , ); // size = archive_read_data(archive, , );
@@ -74,11 +74,11 @@ static int mcx_loadchunk(const u8 *restrict buf, const i32 *restrict table, int
/* Moves chunks `src_s` to `src_e` (inclusive) from `src`, back onto `dst`. */ /* Moves chunks `src_s` to `src_e` (inclusive) from `src`, back onto `dst`. */
static void mvchunks(u8 *dst, u8 *src, u32 *restrict table, int src_s, int src_e) { static void mvchunks(u8 *dst, u8 *src, u32 *restrict table, int src_s, int src_e) {
assert(src > dst); assert(src > dst);
size_t len = src - dst; // acquire the amount of bytes that we shall move usize len = src - dst; // acquire the amount of bytes that we shall move
assert(!(len % SECTOR)); assert(!(len % SECTOR));
// count how many bytes we need to move, whilst updating location data // count how many bytes we need to move, whilst updating location data
size_t blen = 0; usize blen = 0;
for (src_s++; src_s <= src_e; src_s++) { for (src_s++; src_s <= src_e; src_s++) {
blen += (be32toh(table[src_s]) & 0xFF) * SECTOR; blen += (be32toh(table[src_s]) & 0xFF) * SECTOR;
table[src_s] -= htobe32((len / SECTOR) << 8); table[src_s] -= htobe32((len / SECTOR) << 8);
@@ -89,15 +89,15 @@ static void mvchunks(u8 *dst, u8 *src, u32 *restrict table, int src_s, int src_e
/* Deletes chunk `sidx` by moving chunks up to `eidx` back over `sidx` in `buf`. /* Deletes chunk `sidx` by moving chunks up to `eidx` back over `sidx` in `buf`.
* `rmb` is an optional additional offset that can be applied, and signifies bytes already removed. * `rmb` is an optional additional offset that can be applied, and signifies bytes already removed.
* Returns the bytes removed by this function. */ * Returns the bytes removed by this function. */
static size_t delchunk(u8 *restrict buf, u32 *restrict table, size_t rmb, int sidx, int eidx) { static usize delchunk(u8 *restrict buf, u32 *restrict table, usize rmb, int sidx, int eidx) {
// load the table data // load the table data
size_t slen, bidx, blen; usize slen, bidx, blen;
slen = be32toh(table[sidx]) & 0xFF; // acquire the sector length of the chunk slen = be32toh(table[sidx]) & 0xFF; // acquire the sector length of the chunk
bidx = (be32toh(table[sidx]) >> 8) * SECTOR; // acquire and compute the byte offset the chunk starts at bidx = (be32toh(table[sidx]) >> 8) * SECTOR; // acquire and compute the byte offset the chunk starts at
blen = slen * SECTOR; // compute the byte length of the chunk blen = slen * SECTOR; // compute the byte length of the chunk
// reset the table data // reset the table data
table[sidx] = 0; table[sidx] = 0;
table[sidx + CHUNKS] = htobe32(time(NULL)); // assign the current time to the timestamp, for correctness NOTE: might need to zero-out instead table[sidx + CHUNKS] = htobe32(time(NULL)); // assign the current time to the timestamp, for correctness NOTE: might need to zero-out instead
// move the succeeding chunks over the deleted chunk // move the succeeding chunks over the deleted chunk
@@ -110,15 +110,15 @@ static size_t delchunk(u8 *restrict buf, u32 *restrict table, size_t rmb, int si
/* Call `delchunk` with the parameters and some defaults. Ensuring the table is copied correctly as well. /* Call `delchunk` with the parameters and some defaults. Ensuring the table is copied correctly as well.
* This is done instead of `delchunk` being globally linked, because * This is done instead of `delchunk` being globally linked, because
* `delchunk` requests more specific parameters, which is confusing outside this module. */ * `delchunk` requests more specific parameters, which is confusing outside this module. */
size_t mcx_delchunk(u8 *restrict buf, int chunk) { usize mcx_delchunk(u8 *restrict buf, int chunk) {
u32 table[TABLE]; u32 table[TABLE];
memcpy(table, buf, sizeof(table)); memcpy(table, buf, sizeof(table));
size_t res = delchunk(buf, table, 0, chunk, CHUNKS); usize res = delchunk(buf, table, 0, chunk, CHUNKS);
memcpy(buf, table, sizeof(table)); memcpy(buf, table, sizeof(table));
return res; return res;
} }
size_t mcx_delchunk_range(u8 *restrict buf, int start, int end) { usize mcx_delchunk_range(u8 *restrict buf, int start, int end) {
assert(start < end && end < CHUNKS); assert(start < end && end < CHUNKS);
u32 table[TABLE]; u32 table[TABLE];
memcpy(table, buf, sizeof(table)); memcpy(table, buf, sizeof(table));
@@ -129,7 +129,7 @@ size_t mcx_delchunk_range(u8 *restrict buf, int start, int end) {
// zeroes-out the chunk data within this range. (and set the timestamp) // zeroes-out the chunk data within this range. (and set the timestamp)
u32 ts = htobe32(time(NULL)); u32 ts = htobe32(time(NULL));
for (int i = start; i <= end; i++) { for (int i = start; i <= end; i++) {
table[i] = 0; table[i] = 0;
table[i + CHUNKS] = ts; table[i + CHUNKS] = ts;
} }
@@ -149,7 +149,7 @@ static int cmp_chunkids(const void *restrict x, const void *restrict y) {
/* Sorts the chunks marked for deletion from smallest to greatest index. /* Sorts the chunks marked for deletion from smallest to greatest index.
* Then performs the deletion in this order. Making sure to only update the chunks up to the next. */ * Then performs the deletion in this order. Making sure to only update the chunks up to the next. */
size_t mcx_delchunk_bulk(u8 *restrict buf, const u16 *restrict chunks, int chunkc) { usize mcx_delchunk_bulk(u8 *restrict buf, const u16 *restrict chunks, int chunkc) {
// ensure the chunks ids we're working on are sorted from least to greatest // ensure the chunks ids we're working on are sorted from least to greatest
u16 chunkids[chunkc + 1]; u16 chunkids[chunkc + 1];
memcpy(chunkids, chunks, chunkc); memcpy(chunkids, chunks, chunkc);
@@ -159,7 +159,7 @@ size_t mcx_delchunk_bulk(u8 *restrict buf, const u16 *restrict chunks, int chunk
u32 table[TABLE]; u32 table[TABLE];
memcpy(table, buf, sizeof(table)); memcpy(table, buf, sizeof(table));
size_t rmb = 0; usize rmb = 0;
for (int i = 0; i < chunkc; i++) for (int i = 0; i < chunkc; i++)
rmb += delchunk(buf, table, rmb, chunkids[i], chunkids[i + 1]); rmb += delchunk(buf, table, rmb, chunkids[i], chunkids[i + 1]);
@@ -169,8 +169,8 @@ size_t mcx_delchunk_bulk(u8 *restrict buf, const u16 *restrict chunks, int chunk
/* Sum together the 4th byte in each location integer to compute the sector size of all chunks. /* Sum together the 4th byte in each location integer to compute the sector size of all chunks.
* Multiplying by `SECTOR`, and adding the size of the table itself. */ * Multiplying by `SECTOR`, and adding the size of the table itself. */
size_t mcx_calcsize(const u8 *restrict buf) { usize mcx_calcsize(const u8 *restrict buf) {
size_t size = 0; usize size = 0;
for (uint i = 0; i < CHUNKS; i++) for (uint i = 0; i < CHUNKS; i++)
size += *(buf + (i * 4) + 3); size += *(buf + (i * 4) + 3);
return (size * SECTOR) + (TABLE * 4); return (size * SECTOR) + (TABLE * 4);

View File

@@ -4,34 +4,34 @@
#include <stdlib.h> #include <stdlib.h>
#include "../types.h"
#include "../util/atrb.h" #include "../util/atrb.h"
#include "../util/intdef.h"
/* contains chunk metadata */ /* contains chunk metadata */
struct mcx_chunk { struct mcx_chunk {
size_t idx; // byte offset for start of chunk data usize idx; // byte offset for start of chunk data
u32 len; // byte length of chunk (+ padding) u32 len; // byte length of chunk (+ padding)
u32 time; // modification time in epoch seconds u32 time; // modification time in epoch seconds
}; };
/* Deletes a single chunk (`chunk`) out of `buf`. /* Deletes a single chunk (`chunk`) out of `buf`.
* The chunk's location data shall become `0`, and timestamp data the current time. * The chunk's location data shall become `0`, and timestamp data the current time.
* All succeeding chunks shall be moved back, freeing space. * All succeeding chunks shall be moved back, freeing space.
* Returns the amount of bytes removed. */ * Returns the amount of bytes removed. */
size_t mcx_delchunk(u8 *restrict buf, int chunk) NONNULL((1)); usize mcx_delchunk(u8 *restrict buf, int chunk) NONNULL((1));
/* Deletes the range defined by `start`—`end` (inclusive) of chunks out of `buf`. /* Deletes the range defined by `start`—`end` (inclusive) of chunks out of `buf`.
* The chunk's location data shall become `0`, and timestamp data the current time. * The chunk's location data shall become `0`, and timestamp data the current time.
* All succeeding chunks shall be moved back, freeing space. * All succeeding chunks shall be moved back, freeing space.
* Returns the amount of bytes removed */ * Returns the amount of bytes removed */
size_t mcx_delchunk_range(u8 *restrict buf, int start, int end) NONNULL((1)); usize mcx_delchunk_range(u8 *restrict buf, int start, int end) NONNULL((1));
/* Deletes a `chunkc` chunks from `chunks` out of `buf`. /* Deletes a `chunkc` chunks from `chunks` out of `buf`.
* If the `chunks` indices are known to be sequential, i.e. have a constant difference of `1`, `mcx_delchunk_range` should be preferred. * If the `chunks` indices are known to be sequential, i.e. have a constant difference of `1`, `mcx_delchunk_range` should be preferred.
* The chunk's location data shall become `0`, and timestamp data the current time. * The chunk's location data shall become `0`, and timestamp data the current time.
* All succeeding chunks shall be moved back, freeing space. * All succeeding chunks shall be moved back, freeing space.
* Returns the amount of bytes removed */ * Returns the amount of bytes removed */
size_t mcx_delchunk_bulk(u8 *restrict buf, const u16 *restrict chunks, int chunkc) NONNULL((1, 2)); usize mcx_delchunk_bulk(u8 *restrict buf, const u16 *restrict chunks, int chunkc) NONNULL((1, 2));
/* Computes the byte size of the `*.mcX` file in `buf` and returns it. */ /* Computes the byte size of the `*.mcX` file in `buf` and returns it. */
size_t mcx_calcsize(const u8 *restrict buf) NONNULL((1)) PURE; usize mcx_calcsize(const u8 *restrict buf) NONNULL((1)) PURE;

View File

@@ -8,7 +8,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "../util/intdef.h" #include "../types.h"
#define MAX_DEPTH 512 #define MAX_DEPTH 512
@@ -37,11 +37,11 @@ static inline u64 buftoh64(const void *restrict buf) {
* The data shall be converted to little-endian on little-endian systems * The data shall be converted to little-endian on little-endian systems
* Outputs the allocated data to `out`, returns where the next pointer would be. */ * Outputs the allocated data to `out`, returns where the next pointer would be. */
static const u8 *procarr(const u8 *restrict buf, i32 nmemb, uint size, struct nbt_array *restrict out) { static const u8 *procarr(const u8 *restrict buf, i32 nmemb, uint size, struct nbt_array *restrict out) {
size_t len = nmemb * size; usize len = nmemb * size;
*out = (struct nbt_array){ *out = (struct nbt_array){
out->nmemb = nmemb, out->nmemb = nmemb,
out->dat = malloc(len), out->dat = malloc(len),
}; };
if (!out->dat) if (!out->dat)
return buf + len; return buf + len;
@@ -92,7 +92,7 @@ const u8 *nbt_proctag(const u8 *restrict buf, u16 slen, void *restrict out) {
const u8 *ptr, *tmp; const u8 *ptr, *tmp;
ptr = buf + 3 + slen; ptr = buf + 3 + slen;
i32 nmem; i32 nmem;
uint size; uint size;
switch (*buf) { switch (*buf) {
@@ -152,7 +152,7 @@ static const u8 *nexttag_list(const u8 *restrict ptr, uint *restrict const dpt,
* Where the value is decremented until we reach `0`. * Where the value is decremented until we reach `0`.
* - `tags` shall contain `MAX_DEPTH` of items representing the list's stored type. */ * - `tags` shall contain `MAX_DEPTH` of items representing the list's stored type. */
static const u8 *nexttag(const u8 *restrict tag, uint *restrict const dpt, i32 *restrict const lens, u8 *restrict const tags) { static const u8 *nexttag(const u8 *restrict tag, uint *restrict const dpt, i32 *restrict const lens, u8 *restrict const tags) {
u8 type; u8 type;
const u8 *ptr = tag; const u8 *ptr = tag;
if (lens[*dpt]) { if (lens[*dpt]) {
type = tags[*dpt]; type = tags[*dpt];
@@ -195,9 +195,9 @@ static const u8 *nexttag(const u8 *restrict tag, uint *restrict const dpt, i32 *
*/ */
const u8 *nbt_nexttag(const u8 *restrict buf) { const u8 *nbt_nexttag(const u8 *restrict buf) {
const u8 *tag; const u8 *tag;
u8 tags[MAX_DEPTH] = {0}; u8 tags[MAX_DEPTH] = {0};
i32 lens[MAX_DEPTH] = {0}; i32 lens[MAX_DEPTH] = {0};
uint dpt = 0; uint dpt = 0;
tag = buf; tag = buf;
do { do {

View File

@@ -7,8 +7,8 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include "../types.h"
#include "../util/atrb.h" #include "../util/atrb.h"
#include "../util/intdef.h"
/* NBT (named binary tag) is a tree data structure. Tags have a numeric type ID, name and a payload. /* NBT (named binary tag) is a tree data structure. Tags have a numeric type ID, name and a payload.
* NBT files are a compressed `compound` tag. GZip is the compression used in most cases, * NBT files are a compressed `compound` tag. GZip is the compression used in most cases,
@@ -21,23 +21,23 @@
/* specifies the NBT tag IDs. /* specifies the NBT tag IDs.
* NOTE: every type is stored as BE (big-endian) in the file. */ * NOTE: every type is stored as BE (big-endian) in the file. */
enum nbt_tagid { enum nbt_tagid {
NBT_END = 0x00, // signifies the end of a compound tag NBT_END = 0x00, // signifies the end of a compound tag
NBT_I8 = 0x01, // next byte is for an 8 bit signed integer. NBT_I8 = 0x01, // next byte is for an 8 bit signed integer.
NBT_I16 = 0x02, // next 2 bytes are for a 16 bit signed integer NBT_I16 = 0x02, // next 2 bytes are for a 16 bit signed integer
NBT_I32 = 0x03, // next 4 bytes are for a 32 bit signed integer NBT_I32 = 0x03, // next 4 bytes are for a 32 bit signed integer
NBT_I64 = 0x04, // next 8 bytes are for a 64 bit signed integer NBT_I64 = 0x04, // next 8 bytes are for a 64 bit signed integer
NBT_F32 = 0x05, // next 4 bytes are for a single-precision floating-point NBT_F32 = 0x05, // next 4 bytes are for a single-precision floating-point
NBT_F64 = 0x06, // next 8 bytes are for a double-precision floating-point NBT_F64 = 0x06, // next 8 bytes are for a double-precision floating-point
NBT_ARR_I8 = 0x07, // starts with a i32, denoting size, followed by the i8 data NBT_ARR_I8 = 0x07, // starts with a i32, denoting size, followed by the i8 data
NBT_STR = 0x08, // starts with a u16, denoting size, followed by the UTF-8 data NBT_STR = 0x08, // starts with a u16, denoting size, followed by the UTF-8 data
NBT_LIST = 0x09, // starts with an ID, followed by a 32 bit signed integer denoting the size NBT_LIST = 0x09, // starts with an ID, followed by a 32 bit signed integer denoting the size
NBT_COMPOUND = 0x0A, // compound tag, contains tags and is delimited by `NBT_END` NBT_COMPOUND = 0x0A, // compound tag, contains tags and is delimited by `NBT_END`
NBT_ARR_I32 = 0x0B, // starts with a i32, denoting size, followed by the i32 data NBT_ARR_I32 = 0x0B, // starts with a i32, denoting size, followed by the i32 data
NBT_ARR_I64 = 0x0C, // starts with a i32, denoting size, followed by the u32 data NBT_ARR_I64 = 0x0C, // starts with a i32, denoting size, followed by the u32 data
}; };
struct nbt_array { struct nbt_array {
i32 nmemb; i32 nmemb;
void *dat; void *dat;
}; };

View File

@@ -6,8 +6,8 @@
#include <glad/gl.h> #include <glad/gl.h>
#include <stdint.h> #include <stdint.h>
#include "../error.h" #include "../types.h"
#include "../util/intdef.h" #include "../util/error.h"
#include "shader.h" #include "shader.h"
#define VERTC 3 #define VERTC 3
@@ -15,7 +15,7 @@ static GLuint pipe;
static GLuint vbo; // vertex buffer object static GLuint vbo; // vertex buffer object
static GLuint vao; // vertex array object static GLuint vao; // vertex array object
static GLuint screen_loc; // location to where OpenGL sends to the shaders of the screen dimensions static GLuint screen_loc; // location to where OpenGL sends to the shaders of the screen dimensions
static int win_w, win_h; static int win_w, win_h;
static void screen_resize(int w, int h) { static void screen_resize(int w, int h) {
i32 verts[VERTC][4] = { i32 verts[VERTC][4] = {
@@ -84,7 +84,7 @@ void render_free(void) {
glDeleteVertexArrays(1, &vao); glDeleteVertexArrays(1, &vao);
glDeleteBuffers(1, &vbo); glDeleteBuffers(1, &vbo);
glDeleteProgram(pipe); glDeleteProgram(pipe);
vbo = 0; vbo = 0;
vao = 0; vao = 0;
pipe = 0; pipe = 0;
} }

View File

@@ -5,6 +5,6 @@
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <glad/gl.h> #include <glad/gl.h>
int render_init(void); int render_init(void);
void render_update(GLFWwindow *win); void render_update(GLFWwindow *win);
void render_free(void); void render_free(void);

View File

@@ -6,7 +6,7 @@
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include "../error.h" #include "../util/error.h"
// NOTE: we are currently just sucking up the memory costs for ease. We can either include the source files themselves. Or use compression, where I'd prefer the latter for ease of installation. // NOTE: we are currently just sucking up the memory costs for ease. We can either include the source files themselves. Or use compression, where I'd prefer the latter for ease of installation.
@@ -19,8 +19,8 @@ extern const uint sh_geom_glsl_len;
/* Compiles a shader of `type` from `src` with `len` bytes. /* Compiles a shader of `type` from `src` with `len` bytes.
* Returns the integer for the shader. */ * Returns the integer for the shader. */
static GLuint shader_compile(GLenum type, const char *src, size_t len) { static GLuint shader_compile(GLenum type, const char *src, usize len) {
int ilen = len; int ilen = len;
GLuint shader = glCreateShader(type); GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, &src, &ilen); glShaderSource(shader, 1, &src, &ilen);
glCompileShader(shader); glCompileShader(shader);

View File

@@ -6,8 +6,8 @@
#include <assert.h> #include <assert.h>
#include <glad/gl.h> #include <glad/gl.h>
#include "../error.h" #include "../types.h"
#include "../util/intdef.h" #include "../util/error.h"
#include "input.h" #include "input.h"
#include "render.h" #include "render.h"

View File

@@ -5,8 +5,8 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "error.h"
#include "io/window.h" #include "io/window.h"
#include "util/error.h"
/* reroutes GLFW errors to our logging system. */ /* reroutes GLFW errors to our logging system. */
static void error_callback(int err, const char *const msg) { static void error_callback(int err, const char *const msg) {

27
src/types.h Normal file
View File

@@ -0,0 +1,27 @@
/* Copyright (c) 2025 Quinn
* Licensed under the MIT Licence. See LICENSE for details */
#pragma once
typedef signed long long int llong;
typedef unsigned short int ushort;
typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned long long ullong;
typedef __INT8_TYPE__ i8;
typedef __INT16_TYPE__ i16;
typedef __INT32_TYPE__ i32;
typedef __INT64_TYPE__ i64;
typedef __UINT8_TYPE__ u8;
typedef __UINT16_TYPE__ u16;
typedef __UINT32_TYPE__ u32;
typedef __UINT64_TYPE__ u64;
typedef __SIZE_TYPE__ usize;
typedef __INTPTR_TYPE__ intptr;
typedef __UINTPTR_TYPE__ uintptr;
#if __SIZEOF_FLOAT__ == 4
typedef float f32;
#endif
#if __SIZEOF_DOUBLE__ == 8
typedef double f64;
#endif

View File

@@ -10,18 +10,18 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "../error.h" #include "../types.h"
#include "../util/error.h"
#include "atrb.h" #include "atrb.h"
#include "intdef.h"
int conf_procbuf(const char *restrict buf, char *restrict kout, char *restrict vout, size_t len) { int conf_procbuf(const char *restrict buf, char *restrict kout, char *restrict vout, usize len) {
bool feq = false; // whether we've found the equal sign bool feq = false; // whether we've found the equal sign
// data traversal // data traversal
char *pos = kout; // will point to the next point in the buffer, where we'll write data char *pos = kout; // will point to the next point in the buffer, where we'll write data
// acquire data // acquire data
for (size_t i = 0; i < len; i++) { for (usize i = 0; i < len; i++) {
// handling of termination tokens // handling of termination tokens
bool brk = false; // whether we broke out of the loop, and are done reading bool brk = false; // whether we broke out of the loop, and are done reading
switch (buf[i]) { switch (buf[i]) {
@@ -36,9 +36,9 @@ int conf_procbuf(const char *restrict buf, char *restrict kout, char *restrict v
// everything after `=` is interpreted as a value // everything after `=` is interpreted as a value
if (!feq && buf[i] == '=') { if (!feq && buf[i] == '=') {
feq = true; feq = true;
*pos = '\0'; // terminate string *pos = '\0'; // terminate string
pos = vout; // move pointer to start of value data pos = vout; // move pointer to start of value data
continue; continue;
} }
*pos = buf[i]; // copy over the buffer's data *pos = buf[i]; // copy over the buffer's data
@@ -53,9 +53,9 @@ int conf_procbuf(const char *restrict buf, char *restrict kout, char *restrict v
return (pos == kout) ? CONF_ENODAT : (!feq ? CONF_ESYNTAX : 0); return (pos == kout) ? CONF_ENODAT : (!feq ? CONF_ESYNTAX : 0);
} }
struct conf_entry const *conf_matchopt(struct conf_entry const *opts, size_t optc, const char *restrict key) { struct conf_entry const *conf_matchopt(struct conf_entry const *opts, usize optc, const char *restrict key) {
// find a match for the current key // find a match for the current key
size_t i = 0; usize i = 0;
for (; i < optc; i++) { for (; i < optc; i++) {
if (strcmp(opts[i].key, key) == 0) if (strcmp(opts[i].key, key) == 0)
return opts + i; return opts + i;
@@ -67,7 +67,7 @@ int conf_procval(struct conf_entry const *opt, const char *restrict val) {
// parse the data // parse the data
errno = 0; errno = 0;
char *end; char *end;
u8 dat[sizeof(u64)]; u8 dat[sizeof(u64)];
switch (opt->type) { switch (opt->type) {
// signed integer data parsing // signed integer data parsing
@@ -128,7 +128,7 @@ int conf_procval(struct conf_entry const *opt, const char *restrict val) {
/* utility function for conf_getpat to concatenate 3 strings, where we already know the size */ /* utility function for conf_getpat to concatenate 3 strings, where we already know the size */
NONNULL((1, 3)) NONNULL((1, 3))
static char *conf_getpat_concat(const char *restrict s1, const char *restrict s2, const char *restrict s3, size_t s1len, size_t s2len, size_t s3len) { static char *conf_getpat_concat(const char *restrict s1, const char *restrict s2, const char *restrict s3, usize s1len, usize s2len, usize s3len) {
assert(s2 || (!s2 && !s2len)); // ensuring the programmer passes both s2 and s2len as 0, if they intend to assert(s2 || (!s2 && !s2len)); // ensuring the programmer passes both s2 and s2len as 0, if they intend to
char *buf, *ptr; char *buf, *ptr;
@@ -140,7 +140,7 @@ static char *conf_getpat_concat(const char *restrict s1, const char *restrict s2
// copy data to the buffer // copy data to the buffer
ptr = memcpy(ptr, s1, s1len) + s1len; // copy s1 data to the buffer ptr = memcpy(ptr, s1, s1len) + s1len; // copy s1 data to the buffer
if (s2len) ptr = memcpy(ptr, s2, s2len) + s2len; // copy s2 data to the buffer (excluding null-terminator) if (s2len) ptr = memcpy(ptr, s2, s2len) + s2len; // copy s2 data to the buffer (excluding null-terminator)
(void)strcpy(ptr, s3); // copy s3 as a string, thus including null-terminator (void)strcpy(ptr, s3); // copy s3 as a string, thus including null-terminator
// return the buffer // return the buffer
return buf; return buf;
@@ -149,8 +149,8 @@ static char *conf_getpat_concat(const char *restrict s1, const char *restrict s2
/* appends str to the config directory string we acquire from environment variables. */ /* appends str to the config directory string we acquire from environment variables. */
char *conf_getpat(const char *restrict str) { char *conf_getpat(const char *restrict str) {
char *buf = NULL; char *buf = NULL;
size_t len; usize len;
size_t str_len = strlen(str); usize str_len = strlen(str);
#if defined(__linux__) #if defined(__linux__)
buf = getenv("XDG_CONFIG_HOME"); buf = getenv("XDG_CONFIG_HOME");
if (!buf) { if (!buf) {

View File

@@ -5,45 +5,45 @@
#include <stddef.h> #include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
#include "../types.h"
#include "atrb.h" #include "atrb.h"
#include "intdef.h"
/* error codes */ /* error codes */
enum conf_err { enum conf_err {
CONF_ENODAT = -1, // no data was found CONF_ENODAT = -1, // no data was found
CONF_ESYNTAX = 1, // couldn't extract clear key/val data CONF_ESYNTAX = 1, // couldn't extract clear key/val data
CONF_ENOMATCH = 2, // couldn't find a match for the inputted key CONF_ENOMATCH = 2, // couldn't find a match for the inputted key
CONF_EINVALIDTYPE = 3, // the type inputted in conf_entry was invalid CONF_EINVALIDTYPE = 3, // the type inputted in conf_entry was invalid
CONF_EPARSE = 4, // something went wrong whilst parsing CONF_EPARSE = 4, // something went wrong whilst parsing
}; };
/* defines the primitive types available in the config file */ /* defines the primitive types available in the config file */
enum conf_primitive { enum conf_primitive {
CONF_STR = 0, // expects: `char**`, will output malloc'd data !!must be freed!! CONF_STR = 0, // expects: `char**`, will output malloc'd data !!must be freed!!
CONF_I8 = 1, // expects: `int8_t*`, will point to a location in memory where an i8 is stored. CONF_I8 = 1, // expects: `int8_t*`, will point to a location in memory where an i8 is stored.
CONF_I16 = 2, // expects: `int16_t*`, will point to a location in memory where an i16 is stored. CONF_I16 = 2, // expects: `int16_t*`, will point to a location in memory where an i16 is stored.
CONF_I32 = 4, // expects: `int32_t*`, will point to a location in memory where an i32 is stored. CONF_I32 = 4, // expects: `int32_t*`, will point to a location in memory where an i32 is stored.
CONF_I64 = 8, // expects: `int64_t*`, will point to a location in memory where an i64 is stored. CONF_I64 = 8, // expects: `int64_t*`, will point to a location in memory where an i64 is stored.
CONF_U8 = CONF_I8 | 0x80, // expects: `uint8_t*`, will point to a location in memory where an u8 is stored. CONF_U8 = CONF_I8 | 0x80, // expects: `uint8_t*`, will point to a location in memory where an u8 is stored.
CONF_U16 = CONF_I16 | 0x80, // expects: `uint16_t*`, will point to a location in memory where an u16 is stored. CONF_U16 = CONF_I16 | 0x80, // expects: `uint16_t*`, will point to a location in memory where an u16 is stored.
CONF_U32 = CONF_I32 | 0x80, // expects: `uint32_t*`, will point to a location in memory where an u32 is stored. CONF_U32 = CONF_I32 | 0x80, // expects: `uint32_t*`, will point to a location in memory where an u32 is stored.
CONF_U64 = CONF_I64 | 0x80, // expects: `uint64_t*`, will point to a location in memory where an u64 is stored. CONF_U64 = CONF_I64 | 0x80, // expects: `uint64_t*`, will point to a location in memory where an u64 is stored.
CONF_F32 = CONF_I32 | 0x40, // expects: `float*`, will point to a location in memory where an f32 is stored. CONF_F32 = CONF_I32 | 0x40, // expects: `float*`, will point to a location in memory where an f32 is stored.
CONF_F64 = CONF_I64 | 0x40, // expects: `double*`, will point to a location in memory where an f64 is stored. CONF_F64 = CONF_I64 | 0x40, // expects: `double*`, will point to a location in memory where an f64 is stored.
CONF_FSTR = 0x40, // expects: `struct conf_fstr*`, which contains the data for a fixed-width string CONF_FSTR = 0x40, // expects: `struct conf_fstr*`, which contains the data for a fixed-width string
}; };
/* for outputting a fixed string as this config field */ /* for outputting a fixed string as this config field */
struct conf_fstr { struct conf_fstr {
size_t len; // length in BYTES of the output data usize len; // length in BYTES of the output data
char *out; // where we will output the data char *out; // where we will output the data
}; };
/* defines the structure of a config file entry */ /* defines the structure of a config file entry */
struct conf_entry { struct conf_entry {
const char *key; // the key of this entry const char *key; // the key of this entry
void *out; // the pointer to which the data is written value is read if the given option is incorrect or missing void *out; // the pointer to which the data is written value is read if the given option is incorrect or missing
u8 type; // the primitive type which we are querying for u8 type; // the primitive type which we are querying for
}; };
/* processes an incoming buffer. /* processes an incoming buffer.
@@ -51,10 +51,10 @@ struct conf_entry {
* `kout` and `vout` will contain a null-terminated string if the function returned successfully. * `kout` and `vout` will contain a null-terminated string if the function returned successfully.
* returns `0` on success, `<0` when no data was found. `>0` when data was invalid but something went wrong. * returns `0` on success, `<0` when no data was found. `>0` when data was invalid but something went wrong.
* see `CONF_E*` or `enum conf_err` */ * see `CONF_E*` or `enum conf_err` */
int conf_procbuf(const char *restrict buf, char *restrict kout, char *restrict vout, size_t len); int conf_procbuf(const char *restrict buf, char *restrict kout, char *restrict vout, usize len);
/* matches the key with one of the options and returns the pointer. Returns NULL if none could be found. */ /* matches the key with one of the options and returns the pointer. Returns NULL if none could be found. */
struct conf_entry const *conf_matchopt(struct conf_entry const *opts, size_t optc, const char *restrict key); struct conf_entry const *conf_matchopt(struct conf_entry const *opts, usize optc, const char *restrict key);
/* processes the value belonging to the key and outputs the result to opts. /* processes the value belonging to the key and outputs the result to opts.
* - `val` points to a null-terminated string which contains the key and value. * - `val` points to a null-terminated string which contains the key and value.

View File

@@ -5,7 +5,7 @@
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include "util/intdef.h" #include "../types.h"
static void error_log(FILE *restrict stream, const char *restrict pfx, uint ln, const char *restrict file, const char *restrict fmt, va_list ap) { static void error_log(FILE *restrict stream, const char *restrict pfx, uint ln, const char *restrict file, const char *restrict fmt, va_list ap) {
fprintf(stream, "(%s:%u) [%s] '", file, ln, pfx); fprintf(stream, "(%s:%u) [%s] '", file, ln, pfx);

View File

@@ -5,9 +5,9 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "util/atrb.h" #include "../types.h"
#include "util/intdef.h" #include "../util/atrb.h"
#include "util/macro.h" #include "../util/macro.h"
void error_debug(uint ln, const char *restrict file, const char *restrict fmt, ...); void error_debug(uint ln, const char *restrict file, const char *restrict fmt, ...);
void error_info(uint ln, const char *restrict file, const char *restrict fmt, ...); void error_info(uint ln, const char *restrict file, const char *restrict fmt, ...);

View File

@@ -1,24 +0,0 @@
/* Copyright (c) 2025 Quinn
* Licensed under the MIT Licence. See LICENSE for details */
#pragma once
/* variable-width integer types */
typedef unsigned int uint; // ≥16 bit unsigned integer
typedef unsigned long ulong; // ≥32 bit unsigned integer
typedef signed long long llong; // ≥64 bit signed integer
typedef unsigned long long ullong; // ≥64 bit unsigned integer
/* fixed-width integer types */
#include <stdint.h>
typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
/* floating point types */
typedef float f32; // single-precision floating-point
typedef double f64; // double-precision floating-point

View File

@@ -2,7 +2,7 @@
* Licensed under the MIT Licence. See LICENSE for details */ * Licensed under the MIT Licence. See LICENSE for details */
#pragma once #pragma once
#include "intdef.h" #include "../types.h"
#if defined(__has_attribute) && __has_attribute(vector_size) #if defined(__has_attribute) && __has_attribute(vector_size)
typedef float fvec2 __attribute__((vector_size(sizeof(float) * 2))); // SMID vector for 2 `float` typedef float fvec2 __attribute__((vector_size(sizeof(float) * 2))); // SMID vector for 2 `float`

View File

@@ -1,5 +1,6 @@
/* Copyright (c) 2025 Quinn /* Copyright (c) 2025 Quinn
* Licensed under the MIT Licence. See LICENSE for details */ * Licensed under the MIT Licence. See LICENSE for details */
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include "test.h" #include "test.h"
@@ -13,6 +14,7 @@ int main(void) {
assert_true(sizeof(u64) == 8); assert_true(sizeof(u64) == 8);
assert_true(sizeof(f32) == 4); assert_true(sizeof(f32) == 4);
assert_true(sizeof(f64) == 8); assert_true(sizeof(f64) == 8);
assert_true(sizeof(size_t) == sizeof(intptr_t));
test_conf_procbuf("key=val", "key", "val", 0); test_conf_procbuf("key=val", "key", "val", 0);
test_conf_procbuf("sometxt", "sometxt", "", CONF_ESYNTAX); test_conf_procbuf("sometxt", "sometxt", "", CONF_ESYNTAX);
test_conf_procbuf("# comment", "", "", CONF_ENODAT); test_conf_procbuf("# comment", "", "", CONF_ENODAT);

View File

@@ -2,7 +2,7 @@
* Licensed under the MIT Licence. See LICENSE for details */ * Licensed under the MIT Licence. See LICENSE for details */
#include "test.h" #include "test.h"
#include "../src/util/intdef.h" #include "../src/types.h"
uint test_okay = 0; uint test_okay = 0;
uint test_fail = 0; uint test_fail = 0;

View File

@@ -3,7 +3,7 @@
#pragma once #pragma once
#include <stdio.h> #include <stdio.h>
#include "../src/util/intdef.h" #include "../src/types.h"
extern uint test_okay; extern uint test_okay;
extern uint test_fail; extern uint test_fail;

View File

@@ -5,22 +5,23 @@
#include <math.h> #include <math.h>
#include <string.h> #include <string.h>
#include "../src/types.h"
#include "../src/util/conf.h" #include "../src/util/conf.h"
#include "test.h" #include "test.h"
void test_conf_procbuf(const char *restrict buf, const char *restrict expect_key, const char *restrict expect_val, int expect_return) { void test_conf_procbuf(const char *restrict buf, const char *restrict expect_key, const char *restrict expect_val, int expect_return) {
size_t len = strlen(buf) + 1; usize len = strlen(buf) + 1;
char k[len], v[len]; char k[len], v[len];
*k = '\0', *v = '\0'; *k = '\0', *v = '\0';
(void)(assert_true(conf_procbuf(buf, k, v, len) == expect_return) && (void)(assert_true(conf_procbuf(buf, k, v, len) == expect_return) &&
assert_true(!strcmp(k, expect_key)) && assert_true(!strcmp(k, expect_key)) &&
assert_true(!strcmp(v, expect_val))); assert_true(!strcmp(v, expect_val)));
} }
void test_conf_matchopt(struct conf_entry *opts, size_t optc, const char *restrict key, int expect_index) { void test_conf_matchopt(struct conf_entry *opts, usize optc, const char *restrict key, int expect_index) {
size_t idx = opts - conf_matchopt(opts, optc, key); usize idx = opts - conf_matchopt(opts, optc, key);
idx = (ssize_t)idx < 0 ? -idx : idx; idx = (ssize)idx < 0 ? -idx : idx;
int i = idx < optc ? (int)idx : -1; int i = idx < optc ? (int)idx : -1;
assert_true(i == expect_index); assert_true(i == expect_index);
} }
@@ -31,7 +32,7 @@ void test_conf_procval_int(const char *val, u64 expect_value, int type) {
} }
void test_conf_procval_f32(const char *val, f32 expect_value) { void test_conf_procval_f32(const char *val, f32 expect_value) {
u8 out[4]; u8 out[4];
f32 result; f32 result;
conf_procval(&(struct conf_entry){NULL, out, CONF_F32}, val); conf_procval(&(struct conf_entry){NULL, out, CONF_F32}, val);
memcpy(&result, out, 4); memcpy(&result, out, 4);
@@ -53,14 +54,14 @@ void test_procval_str_predef(void) {
} }
void test_procval_fstr(void) { void test_procval_fstr(void) {
char buf[16]; char buf[16];
struct conf_fstr str = {sizeof(buf), buf}; struct conf_fstr str = {sizeof(buf), buf};
(void)(assert_true(!conf_procval(&(struct conf_entry){NULL, &str, CONF_FSTR}, "hewwoo wowld")) && (void)(assert_true(!conf_procval(&(struct conf_entry){NULL, &str, CONF_FSTR}, "hewwoo wowld")) &&
assert_true(!strcmp(str.out, "hewwoo wowld"))); assert_true(!strcmp(str.out, "hewwoo wowld")));
} }
void test_procval_fstr_trunc(void) { void test_procval_fstr_trunc(void) {
char buf[8]; char buf[8];
struct conf_fstr str = {sizeof(buf), buf}; struct conf_fstr str = {sizeof(buf), buf};
(void)(assert_true(!conf_procval(&(struct conf_entry){NULL, &str, CONF_FSTR}, "hewwooo wowld")) && (void)(assert_true(!conf_procval(&(struct conf_entry){NULL, &str, CONF_FSTR}, "hewwooo wowld")) &&
assert_true(!strcmp(str.out, "hewwooo"))); assert_true(!strcmp(str.out, "hewwooo")));

View File

@@ -2,11 +2,11 @@
* Licensed under the MIT Licence. See LICENSE for details */ * Licensed under the MIT Licence. See LICENSE for details */
#pragma once #pragma once
#include "../src/types.h"
#include "../src/util/conf.h" #include "../src/util/conf.h"
#include "../src/util/intdef.h"
void test_conf_procbuf(const char *restrict buf, const char *restrict expect_key, const char *restrict expect_val, int expect_return); void test_conf_procbuf(const char *restrict buf, const char *restrict expect_key, const char *restrict expect_val, int expect_return);
void test_conf_matchopt(struct conf_entry *restrict opts, size_t optc, const char *restrict key, int expect_index); void test_conf_matchopt(struct conf_entry *restrict opts, usize optc, const char *restrict key, int expect_index);
void test_conf_procval_int(const char *val, u64 expect_value, int type); void test_conf_procval_int(const char *val, u64 expect_value, int type);
void test_conf_procval_f32(const char *val, f32 expect_value); void test_conf_procval_f32(const char *val, f32 expect_value);
void test_conf_procval_fstr(const char *val, u64 expect_value, int type); void test_conf_procval_fstr(const char *val, u64 expect_value, int type);