mirror of
https://github.com/thepigeongenerator/mcaselector-lite.git
synced 2025-12-18 20:35:45 +01:00
Compare commits
5 Commits
1e24a4ee92
...
d4fabf437c
| Author | SHA1 | Date | |
|---|---|---|---|
| d4fabf437c | |||
| cebe0df8a2 | |||
| aa58d931aa | |||
| 27c862c215 | |||
| 6eb7d126da |
@@ -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
|
||||||
|
|
||||||
|
|||||||
@@ -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.
|
||||||
|
|||||||
@@ -10,8 +10,8 @@
|
|||||||
#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
|
||||||
@@ -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,9 +89,9 @@ 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
|
||||||
@@ -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));
|
||||||
@@ -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);
|
||||||
|
|||||||
@@ -4,12 +4,12 @@
|
|||||||
|
|
||||||
#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
|
||||||
};
|
};
|
||||||
@@ -18,20 +18,20 @@ struct mcx_chunk {
|
|||||||
* 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;
|
||||||
|
|||||||
@@ -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,7 +37,7 @@ 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),
|
||||||
|
|||||||
@@ -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,
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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,7 +19,7 @@ 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);
|
||||||
|
|||||||
@@ -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"
|
||||||
|
|
||||||
|
|||||||
@@ -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
27
src/types.h
Normal 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
|
||||||
@@ -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]) {
|
||||||
@@ -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;
|
||||||
@@ -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;
|
||||||
|
|
||||||
@@ -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) {
|
||||||
|
|||||||
@@ -5,8 +5,8 @@
|
|||||||
#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 {
|
||||||
@@ -35,7 +35,7 @@ enum conf_primitive {
|
|||||||
|
|
||||||
/* 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
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -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.
|
||||||
|
|||||||
@@ -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);
|
||||||
@@ -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, ...);
|
||||||
@@ -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
|
|
||||||
@@ -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`
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
@@ -5,11 +5,12 @@
|
|||||||
#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) &&
|
||||||
@@ -17,9 +18,9 @@ void test_conf_procbuf(const char *restrict buf, const char *restrict 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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
Reference in New Issue
Block a user