mirror of
https://github.com/thepigeongenerator/mcaselector-lite.git
synced 2025-12-18 09:45:45 +01:00
Compare commits
3 Commits
c3d2e912cb
...
1aa34f7d3f
| Author | SHA1 | Date | |
|---|---|---|---|
| 1aa34f7d3f | |||
| 00719b1933 | |||
| eb45650178 |
@@ -7,7 +7,7 @@
|
||||
# ---------------------------
|
||||
BasedOnStyle: GNU
|
||||
Standard: Auto
|
||||
ColumnLimit: 0
|
||||
ColumnLimit: 100
|
||||
LineEnding: LF
|
||||
UseTab: ForContinuationAndIndentation
|
||||
TabWidth: 8
|
||||
@@ -34,12 +34,12 @@ AlignConsecutiveShortCaseStatements:
|
||||
# short constructs on a single line
|
||||
# ---------------------------
|
||||
AllowShortBlocksOnASingleLine: Always
|
||||
AllowShortFunctionsOnASingleLine: All
|
||||
AllowShortFunctionsOnASingleLine: None
|
||||
AllowShortLambdasOnASingleLine: All
|
||||
AllowShortIfStatementsOnASingleLine: AllIfsAndElse
|
||||
AllowShortIfStatementsOnASingleLine: Never
|
||||
AllowShortCaseLabelsOnASingleLine: true
|
||||
AllowShortEnumsOnASingleLine: true
|
||||
AllowShortLoopsOnASingleLine: true
|
||||
AllowShortLoopsOnASingleLine: false
|
||||
|
||||
# ---------------------------
|
||||
# break and wrapping options
|
||||
@@ -61,7 +61,7 @@ BraceWrapping:
|
||||
AfterCaseLabel: false
|
||||
AfterClass: false
|
||||
AfterEnum: false
|
||||
AfterFunction: false
|
||||
AfterFunction: true
|
||||
AfterNamespace: false
|
||||
AfterObjCDeclaration: false
|
||||
AfterStruct: false
|
||||
|
||||
@@ -25,8 +25,10 @@ enum mcx_compression {
|
||||
MCX_COMPRESSION_CUSTOM = 0x7F,
|
||||
};
|
||||
|
||||
/* first 4 bytes is an i32 indicating remaining bytes, the following byte defines the compression scheme */
|
||||
static int mcx_loadchunk(const u8 *restrict buf, const i32 *restrict table, int idx) {
|
||||
/* First 4 bytes is an i32 indicating remaining bytes.
|
||||
* The following byte defines the compression scheme */
|
||||
static int mcx_loadchunk(const u8 *restrict buf, const i32 *restrict table, int idx)
|
||||
{
|
||||
const u8 *chunk = buf + (be32toh(table[idx]) >> 8) * SECTOR;
|
||||
|
||||
i32 len;
|
||||
@@ -64,7 +66,8 @@ static int mcx_loadchunk(const u8 *restrict buf, const i32 *restrict table, int
|
||||
error("failed to decompress %i bytes of compression type %i", len, *chunk);
|
||||
return 1;
|
||||
}
|
||||
if (size == 0) break;
|
||||
if (size == 0)
|
||||
break;
|
||||
// TODO: handle data
|
||||
}
|
||||
|
||||
@@ -72,9 +75,10 @@ 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`. */
|
||||
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);
|
||||
usize len = src - dst; // acquire the amount of bytes that we shall move
|
||||
uintptr len = src - dst; // acquire the amount of bytes that we shall move
|
||||
assert(!(len % SECTOR));
|
||||
|
||||
// count how many bytes we need to move, whilst updating location data
|
||||
@@ -89,16 +93,20 @@ 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`.
|
||||
* `rmb` is an optional additional offset that can be applied, and signifies bytes already removed.
|
||||
* Returns the bytes removed by this function. */
|
||||
static usize delchunk(u8 *restrict buf, u32 *restrict table, usize rmb, int sidx, int eidx) {
|
||||
static usize delchunk(u8 *restrict buf, u32 *restrict table, usize rmb, int sidx, int eidx)
|
||||
{
|
||||
// load the table data
|
||||
usize slen, bidx, blen;
|
||||
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
|
||||
|
||||
// reset the table data
|
||||
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
|
||||
u8 *dst = buf + bidx - rmb;
|
||||
@@ -107,10 +115,13 @@ static usize delchunk(u8 *restrict buf, u32 *restrict table, usize rmb, int sidx
|
||||
return blen;
|
||||
}
|
||||
|
||||
/* 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
|
||||
* `delchunk` requests more specific parameters, which is confusing outside this module. */
|
||||
usize mcx_delchunk(u8 *restrict buf, int chunk) {
|
||||
/* 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 `delchunk` requests more specific parameters,
|
||||
* which is confusing outside this module. */
|
||||
usize mcx_delchunk(u8 *restrict buf, int chunk)
|
||||
{
|
||||
u32 table[TABLE];
|
||||
memcpy(table, buf, sizeof(table));
|
||||
usize res = delchunk(buf, table, 0, chunk, CHUNKS);
|
||||
@@ -118,7 +129,8 @@ usize mcx_delchunk(u8 *restrict buf, int chunk) {
|
||||
return res;
|
||||
}
|
||||
|
||||
usize 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);
|
||||
u32 table[TABLE];
|
||||
memcpy(table, buf, sizeof(table));
|
||||
@@ -141,20 +153,23 @@ usize mcx_delchunk_range(u8 *restrict buf, int start, int end) {
|
||||
}
|
||||
|
||||
/* comparer function for to be inputted into `qsort` to compare two */
|
||||
static int cmp_chunkids(const void *restrict x, const void *restrict y) {
|
||||
static int cmp_chunkids(const void *restrict x, const void *restrict y)
|
||||
{
|
||||
u16 x2 = *(u16 *)x;
|
||||
u16 y2 = *(u16 *)y;
|
||||
return (x2 > y2) - (x2 < y2);
|
||||
}
|
||||
|
||||
/* 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. */
|
||||
usize mcx_delchunk_bulk(u8 *restrict buf, const u16 *restrict chunks, int chunkc) {
|
||||
* Then performs the deletion in this order.
|
||||
* Making sure to only update the chunks up to the next. */
|
||||
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
|
||||
u16 chunkids[chunkc + 1];
|
||||
memcpy(chunkids, chunks, chunkc);
|
||||
qsort(chunkids, chunkc, sizeof(int), cmp_chunkids);
|
||||
chunkids[chunkc] = CHUNKS; // set the spare chunk to the max chunks, so the rest of the chunks are moved
|
||||
chunkids[chunkc] = CHUNKS; // move the rest of the chunks
|
||||
|
||||
u32 table[TABLE];
|
||||
memcpy(table, buf, sizeof(table));
|
||||
@@ -169,7 +184,8 @@ usize mcx_delchunk_bulk(u8 *restrict buf, const u16 *restrict chunks, int chunkc
|
||||
|
||||
/* 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. */
|
||||
usize mcx_calcsize(const u8 *restrict buf) {
|
||||
usize mcx_calcsize(const u8 *restrict buf)
|
||||
{
|
||||
usize size = 0;
|
||||
for (uint i = 0; i < CHUNKS; i++)
|
||||
size += *(buf + (i * 4) + 3);
|
||||
|
||||
@@ -13,21 +13,24 @@
|
||||
#define MAX_DEPTH 512
|
||||
|
||||
/* Extracts a big endian 16 bit integer from address `buf`, converts it to host byte size if needed and returns. */
|
||||
static inline u16 buftoh16(const void *restrict buf) {
|
||||
static inline u16 buftoh16(const void *restrict buf)
|
||||
{
|
||||
u16 i;
|
||||
memcpy(&i, buf, sizeof(i));
|
||||
return be16toh(i);
|
||||
}
|
||||
|
||||
/* Extracts a big endian 32 bit integer from address `buf`, converts it to host byte size if needed and returns. */
|
||||
static inline u32 buftoh32(const void *restrict buf) {
|
||||
static inline u32 buftoh32(const void *restrict buf)
|
||||
{
|
||||
u32 i;
|
||||
memcpy(&i, buf, sizeof(i));
|
||||
return be32toh(i);
|
||||
}
|
||||
|
||||
/* Extracts a big endian 64 bit integer from address `buf`, converts it to host byte size if needed and returns. */
|
||||
static inline u64 buftoh64(const void *restrict buf) {
|
||||
static inline u64 buftoh64(const void *restrict buf)
|
||||
{
|
||||
u64 i;
|
||||
memcpy(&i, buf, sizeof(i));
|
||||
return be64toh(i);
|
||||
@@ -36,7 +39,8 @@ static inline u64 buftoh64(const void *restrict buf) {
|
||||
/* Processes the incoming array data in `buf`. Which contains `nmem` items of `size`.
|
||||
* 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. */
|
||||
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)
|
||||
{
|
||||
usize len = nmemb * size;
|
||||
*out = (struct nbt_array){
|
||||
out->nmemb = nmemb,
|
||||
@@ -51,7 +55,8 @@ static const u8 *procarr(const u8 *restrict buf, i32 nmemb, uint size, struct nb
|
||||
/* Only include this code for little-endian systems. Since only they require this logic.
|
||||
* Producing optimised code for other platforms. */
|
||||
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
||||
if (size == 1) return buf;
|
||||
if (size == 1)
|
||||
return buf;
|
||||
i32 i = 0;
|
||||
while (i < nmemb) {
|
||||
switch (size) {
|
||||
@@ -67,7 +72,8 @@ static const u8 *procarr(const u8 *restrict buf, i32 nmemb, uint size, struct nb
|
||||
}
|
||||
|
||||
/* calls `procarr` for the simple types available. */
|
||||
static const u8 *proclist(const u8 *restrict buf, struct nbt_array *restrict out) {
|
||||
static const u8 *proclist(const u8 *restrict buf, struct nbt_array *restrict out)
|
||||
{
|
||||
uint size;
|
||||
|
||||
switch (*(u8 *)buf) {
|
||||
@@ -88,7 +94,8 @@ static const u8 *proclist(const u8 *restrict buf, struct nbt_array *restrict out
|
||||
return procarr(buf, len, size, out);
|
||||
}
|
||||
|
||||
const u8 *nbt_proctag(const u8 *restrict buf, u16 slen, void *restrict out) {
|
||||
const u8 *nbt_proctag(const u8 *restrict buf, u16 slen, void *restrict out)
|
||||
{
|
||||
const u8 *ptr, *tmp;
|
||||
ptr = buf + 3 + slen;
|
||||
|
||||
@@ -123,7 +130,8 @@ const u8 *nbt_proctag(const u8 *restrict buf, u16 slen, void *restrict out) {
|
||||
* `ptr` is assumed to be the start of the `NBT_LIST` data, e.i. The list's ID, followed by the list's length.
|
||||
* If `ID` is `NBT_I8`, `NBT_I16`, `NBT_I32`, `NBT_I64`, `NBT_F32`, or `NBT_F64`, the entire list length is computed and returned.
|
||||
* For other types this won't be possible, and thus will add `1` to `dpt`, and write the list data to `lens` and `tags` at this new `dpt`. */
|
||||
static const u8 *nexttag_list(const u8 *restrict ptr, uint *restrict const dpt, i32 *restrict const lens, u8 *restrict const tags) {
|
||||
static const u8 *nexttag_list(const u8 *restrict ptr, uint *restrict const dpt, i32 *restrict const lens, u8 *restrict const tags)
|
||||
{
|
||||
const u8 *tag = ptr;
|
||||
ptr++;
|
||||
switch (*tag) {
|
||||
@@ -151,7 +159,8 @@ static const u8 *nexttag_list(const u8 *restrict ptr, uint *restrict const dpt,
|
||||
* - `lens` shall contain `MAX_DEPTH` of items representing the list length, if the current item is non-zero we shall assume we're in a list.
|
||||
* Where the value is decremented until we reach `0`.
|
||||
* - `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;
|
||||
const u8 *ptr = tag;
|
||||
if (lens[*dpt]) {
|
||||
@@ -193,7 +202,8 @@ static const u8 *nexttag(const u8 *restrict tag, uint *restrict const dpt, i32 *
|
||||
* - compound:list:int32
|
||||
* - string
|
||||
*/
|
||||
const u8 *nbt_nexttag(const u8 *restrict buf) {
|
||||
const u8 *nbt_nexttag(const u8 *restrict buf)
|
||||
{
|
||||
const u8 *tag;
|
||||
u8 tags[MAX_DEPTH] = {0};
|
||||
i32 lens[MAX_DEPTH] = {0};
|
||||
|
||||
@@ -4,8 +4,9 @@
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
void input_callback(GLFWwindow *win, int key, int scancode, int action, int mods) {
|
||||
(void)win, (void)key, (void)scancode, (void)action, (void)mods; // make the compiler shut up as this is fine
|
||||
void input_callback(GLFWwindow *win, int key, int scancode, int action, int mods)
|
||||
{
|
||||
(void)win, (void)key, (void)scancode, (void)action, (void)mods;
|
||||
#ifndef NDEBUG
|
||||
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
|
||||
glfwSetWindowShouldClose(win, 1);
|
||||
|
||||
@@ -17,7 +17,8 @@ static GLuint vao; // vertex array object
|
||||
static GLuint screen_loc; // location to where OpenGL sends to the shaders of the screen dimensions
|
||||
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] = {
|
||||
{0, 0, w, 20 },
|
||||
{0, 20, w, h - 40},
|
||||
@@ -31,7 +32,8 @@ static void screen_resize(int w, int h) {
|
||||
win_h = h;
|
||||
}
|
||||
|
||||
int render_init(void) {
|
||||
int render_init(void)
|
||||
{
|
||||
pipe = glCreateProgram();
|
||||
shader_init(pipe); // initialise and include the shaders
|
||||
glLinkProgram(pipe); // link the application
|
||||
@@ -42,7 +44,7 @@ int render_init(void) {
|
||||
if (len > 0) {
|
||||
char log[len];
|
||||
glGetProgramInfoLog(pipe, len, &len, log);
|
||||
log[len - 1] = '\0'; // terminate the string one character sooner since the log includes a newline
|
||||
log[len - 1] = '\0'; // terminate the string one character sooner
|
||||
fatal("error whilst linking the pipe: '%s'", log);
|
||||
}
|
||||
|
||||
@@ -63,7 +65,8 @@ int render_init(void) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void render_update(GLFWwindow *win) {
|
||||
void render_update(GLFWwindow *win)
|
||||
{
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
glDisable(GL_CULL_FACE);
|
||||
|
||||
@@ -80,7 +83,8 @@ void render_update(GLFWwindow *win) {
|
||||
glDrawArrays(GL_POINTS, 0, VERTC);
|
||||
}
|
||||
|
||||
void render_free(void) {
|
||||
void render_free(void)
|
||||
{
|
||||
glDeleteVertexArrays(1, &vao);
|
||||
glDeleteBuffers(1, &vbo);
|
||||
glDeleteProgram(pipe);
|
||||
|
||||
@@ -72,5 +72,6 @@ enum map_colour_id {
|
||||
MAP_COLOUR_COUNT,
|
||||
};
|
||||
|
||||
/* contains the colours of the map on their' respective index - 1. (NONE should be handled separately) */
|
||||
/* contains the colours of the map on their' respective index - 1.
|
||||
* (NONE should be handled separately) */
|
||||
extern u8vec4 map_colours[MAP_COLOUR_COUNT];
|
||||
|
||||
@@ -9,7 +9,9 @@
|
||||
#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. */
|
||||
extern const char sh_vert_glsl[];
|
||||
extern const char sh_frag_glsl[];
|
||||
extern const char sh_geom_glsl[];
|
||||
@@ -19,7 +21,8 @@ extern const uint sh_geom_glsl_len;
|
||||
|
||||
/* Compiles a shader of `type` from `src` with `len` bytes.
|
||||
* Returns the integer for the shader. */
|
||||
static GLuint shader_compile(GLenum type, const char *src, usize len) {
|
||||
static GLuint shader_compile(GLenum type, const char *src, usize len)
|
||||
{
|
||||
int ilen = len;
|
||||
GLuint shader = glCreateShader(type);
|
||||
glShaderSource(shader, 1, &src, &ilen);
|
||||
@@ -30,14 +33,16 @@ static GLuint shader_compile(GLenum type, const char *src, usize len) {
|
||||
if (ilen > 0) {
|
||||
char log[ilen];
|
||||
glGetShaderInfoLog(shader, ilen, &ilen, log);
|
||||
log[ilen - 1] = '\0'; // terminate the string one character sooner since the log includes a newline
|
||||
log[ilen - 1] = '\0'; // terminate the string one character sooner since the log
|
||||
// includes a newline
|
||||
error("error whilst compiling shader type '0x%X': '%s'", type, log);
|
||||
}
|
||||
|
||||
return shader;
|
||||
}
|
||||
|
||||
int shader_init(GLuint pipe) {
|
||||
int shader_init(GLuint pipe)
|
||||
{
|
||||
GLuint vs = shader_compile(GL_VERTEX_SHADER, sh_vert_glsl, sh_vert_glsl_len);
|
||||
GLuint fs = shader_compile(GL_FRAGMENT_SHADER, sh_frag_glsl, sh_frag_glsl_len);
|
||||
GLuint gs = shader_compile(GL_GEOMETRY_SHADER, sh_geom_glsl, sh_geom_glsl_len);
|
||||
|
||||
@@ -15,7 +15,8 @@ static struct GLFWwindow *win = NULL;
|
||||
|
||||
/* Initialises the GLFW window with some defaults,
|
||||
* then proceed to activate OpenGL on it. */
|
||||
int window_init(void) {
|
||||
int window_init(void)
|
||||
{
|
||||
// initialise the window
|
||||
#ifndef NDEBUG
|
||||
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE);
|
||||
@@ -29,31 +30,33 @@ int window_init(void) {
|
||||
glfwWindowHint(GLFW_GREEN_BITS, 8);
|
||||
glfwWindowHint(GLFW_BLUE_BITS, 8);
|
||||
glfwWindowHint(GLFW_ALPHA_BITS, 0);
|
||||
/* NOTE: on my system; x86_64, GTX 1650 580.82.09-2, X11, i3, this causes one direct, 2 indirect memory leaks.
|
||||
* This is not my fault, and can safely be ignored. */
|
||||
/* NOTE: on my system; x86_64, GTX 1650 580.82.09-2, X11, i3, this causes one direct,
|
||||
* 2 indirect memory leaks. This is not my fault, and can safely be ignored. */
|
||||
win = glfwCreateWindow(640, 480, "MCA-Selector lite", NULL, NULL);
|
||||
if (!win) return 1;
|
||||
if (!win)
|
||||
return 1;
|
||||
|
||||
glfwMakeContextCurrent(win);
|
||||
if (!gladLoadGL(glfwGetProcAddress)) return 1;
|
||||
glfwSwapInterval(1); // wait 1 screen update for a redraw a.k.a. "vsync". (not really applicable in this case but eh)
|
||||
if (!gladLoadGL(glfwGetProcAddress))
|
||||
return 1;
|
||||
/* wait 1 screen update for a redraw a.k.a. "vsync".
|
||||
(not really applicable in this case, but eh) */
|
||||
glfwSwapInterval(1);
|
||||
|
||||
glfwSetKeyCallback(win, input_callback);
|
||||
|
||||
debug(
|
||||
"version info:\n"
|
||||
debug("version info:\n"
|
||||
"\tvendor: %s\n"
|
||||
"\trenderer: %s\n"
|
||||
"\tversion: %s\n"
|
||||
"\tshading lang: %s\n",
|
||||
glGetString(GL_VENDOR),
|
||||
glGetString(GL_RENDERER),
|
||||
glGetString(GL_VERSION),
|
||||
glGetString(GL_VENDOR), glGetString(GL_RENDERER), glGetString(GL_VERSION),
|
||||
glGetString(GL_SHADING_LANGUAGE_VERSION));
|
||||
return 0;
|
||||
}
|
||||
|
||||
void window_loop(void) {
|
||||
void window_loop(void)
|
||||
{
|
||||
assert(win);
|
||||
render_init();
|
||||
while (!glfwWindowShouldClose(win)) {
|
||||
@@ -65,12 +68,14 @@ void window_loop(void) {
|
||||
}
|
||||
}
|
||||
|
||||
void window_close(void) {
|
||||
void window_close(void)
|
||||
{
|
||||
assert(win);
|
||||
glfwSetWindowShouldClose(win, 1);
|
||||
}
|
||||
|
||||
void window_free(void) {
|
||||
void window_free(void)
|
||||
{
|
||||
if (!win) {
|
||||
debug("window has already been freed.");
|
||||
return;
|
||||
|
||||
11
src/main.c
11
src/main.c
@@ -9,11 +9,13 @@
|
||||
#include "util/error.h"
|
||||
|
||||
/* 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)
|
||||
{
|
||||
error("glfw returned (%i); \"%s\"", err, msg);
|
||||
}
|
||||
|
||||
static void quit(void) {
|
||||
static void quit(void)
|
||||
{
|
||||
window_free();
|
||||
|
||||
/* terminates GLFW; destroying any
|
||||
@@ -23,13 +25,14 @@ static void quit(void) {
|
||||
|
||||
|
||||
/* Entry-point of the application. */
|
||||
int main(int argc, char **argv) {
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
(void)argc, (void)argv;
|
||||
printf("debug: [DBG], info: [INF], warning: [WAR], error: [ERR], fatal: [FAT]\n");
|
||||
atexit(quit);
|
||||
|
||||
glfwSetErrorCallback(error_callback);
|
||||
glfwInitHint(GLFW_JOYSTICK_HAT_BUTTONS, GLFW_FALSE); // disable joystick buttons; since we won't need them
|
||||
glfwInitHint(GLFW_JOYSTICK_HAT_BUTTONS, GLFW_FALSE); // we won't need them
|
||||
if (!glfwInit() || window_init())
|
||||
fatal("failed to initialise!");
|
||||
|
||||
|
||||
@@ -11,8 +11,9 @@
|
||||
#error platform not supported!
|
||||
#endif
|
||||
|
||||
/* tests a files access with F_OK, X_OK, R_OK, W_OK OR'd together
|
||||
returns 0 upon success. -1 when errno is set and anything else when one or more of the permissions isn't set */
|
||||
/* Tests a files access with F_OK, X_OK, R_OK, W_OK OR'd together.
|
||||
Returns 0 upon success, -1 when errno is set,
|
||||
anything else when one or more of the permissions isn't set */
|
||||
static inline int faccess(const char *restrict fname, int perms);
|
||||
|
||||
// define the constants if they haven't been
|
||||
@@ -30,17 +31,27 @@ static inline int faccess(const char *restrict fname, int perms);
|
||||
#endif
|
||||
|
||||
#if defined __unix__ && _POSIX_C_SOURCE >= 200809L
|
||||
#define PATH_SEP '/' // contains the path separator as a character. Yes it is extremely annoying that this has to exist.
|
||||
#define PATH_SEP_STR "/" // contains the path separator as a string, useful for concatenation. Yes it is extremely annoying that this has to exist.
|
||||
#define PATH_SEP \
|
||||
'/' // contains the path separator as a character. Yes it is extremely annoying that this
|
||||
// has to exist.
|
||||
#define PATH_SEP_STR \
|
||||
"/" // contains the path separator as a string, useful for concatenation. Yes it is
|
||||
// extremely annoying that this has to exist.
|
||||
|
||||
int faccess(char const *restrict fname, int perms) {
|
||||
int faccess(char const *restrict fname, int perms)
|
||||
{
|
||||
return access(fname, perms);
|
||||
}
|
||||
#elif defined(_WIN32)
|
||||
#define PATH_SEP '\\' // contains the path separator as a character. Yes it is extremely annoying that this has to exist.
|
||||
#define PATH_SEP_STR "\\" // contains the path separator as a string, useful for concatenation. Yes it is extremely annoying that this has to exist.
|
||||
#define PATH_SEP \
|
||||
'\\' // contains the path separator as a character. Yes it is extremely annoying that this
|
||||
// has to exist.
|
||||
#define PATH_SEP_STR \
|
||||
"\\" // contains the path separator as a string, useful for concatenation. Yes it is
|
||||
// extremely annoying that this has to exist.
|
||||
|
||||
int faccess(char const *restrict fname, int perms) {
|
||||
int faccess(char const *restrict fname, int perms)
|
||||
{
|
||||
return _access(fname, perms);
|
||||
}
|
||||
#else
|
||||
|
||||
@@ -14,7 +14,8 @@
|
||||
#include "../util/error.h"
|
||||
#include "atrb.h"
|
||||
|
||||
int conf_procbuf(const char *restrict buf, char *restrict kout, char *restrict vout, usize 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
|
||||
|
||||
// data traversal
|
||||
@@ -28,11 +29,10 @@ int conf_procbuf(const char *restrict buf, char *restrict kout, char *restrict v
|
||||
case '\n':
|
||||
case '\r':
|
||||
case '\0':
|
||||
case '#':
|
||||
brk = true;
|
||||
break;
|
||||
case '#': brk = true; break;
|
||||
}
|
||||
if (brk) break;
|
||||
if (brk)
|
||||
break;
|
||||
|
||||
// everything after `=` is interpreted as a value
|
||||
if (!feq && buf[i] == '=') {
|
||||
@@ -44,8 +44,8 @@ int conf_procbuf(const char *restrict buf, char *restrict kout, char *restrict v
|
||||
*pos = buf[i]; // copy over the buffer's data
|
||||
pos++; // increment the position pointer
|
||||
}
|
||||
// null-terminate what we've got now (yes, there should be enough space for this since \0 isn't stored)
|
||||
// this also ensures the value is valid, even if none is given
|
||||
// null-terminate what we've got now (yes, there should be enough space for this since \0
|
||||
// isn't stored) this also ensures the value is valid, even if none is given
|
||||
*pos = '\0';
|
||||
|
||||
// no data if we didn't move from the key position
|
||||
@@ -53,7 +53,9 @@ int conf_procbuf(const char *restrict buf, char *restrict kout, char *restrict v
|
||||
return (pos == kout) ? CONF_ENODAT : (!feq ? CONF_ESYNTAX : 0);
|
||||
}
|
||||
|
||||
struct conf_entry const *conf_matchopt(struct conf_entry const *opts, usize 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
|
||||
usize i = 0;
|
||||
for (; i < optc; i++) {
|
||||
@@ -63,7 +65,8 @@ struct conf_entry const *conf_matchopt(struct conf_entry const *opts, usize optc
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int conf_procval(struct conf_entry const *opt, const char *restrict val) {
|
||||
int conf_procval(struct conf_entry const *opt, const char *restrict val)
|
||||
{
|
||||
// parse the data
|
||||
errno = 0;
|
||||
char *end;
|
||||
@@ -92,7 +95,8 @@ int conf_procval(struct conf_entry const *opt, const char *restrict val) {
|
||||
// string data parsing
|
||||
case CONF_STR:
|
||||
if (*(char **)opt->out) {
|
||||
free(*(char **)opt->out); // if the same key is given multiple times, free the memory so we don't leak.
|
||||
free(*(char **)opt->out); // if the same key is given multiple times, free
|
||||
// the memory so we don't leak.
|
||||
warn("encountered a dynamic string multiple times, this is sub-optimal.");
|
||||
}
|
||||
*(char **)opt->out = strdup(val);
|
||||
@@ -112,34 +116,66 @@ int conf_procval(struct conf_entry const *opt, const char *restrict val) {
|
||||
}
|
||||
|
||||
switch (opt->type) {
|
||||
case CONF_U8: *(u8 *)opt->out = *(u64 *)dat >= UINT8_MAX ? UINT8_MAX : *(u64 *)dat; return 0;
|
||||
case CONF_U16: *(u16 *)opt->out = *(u64 *)dat >= UINT16_MAX ? UINT16_MAX : *(u64 *)dat; return 0;
|
||||
case CONF_U32: *(u32 *)opt->out = *(u64 *)dat >= UINT32_MAX ? UINT32_MAX : *(u64 *)dat; return 0;
|
||||
case CONF_U64: *(u64 *)opt->out = *(u64 *)dat >= UINT64_MAX ? UINT64_MAX : *(u64 *)dat; return 0;
|
||||
case CONF_I8: *(i8 *)opt->out = *(i64 *)dat >= INT8_MAX ? INT8_MAX : (*(i64 *)dat <= INT8_MIN ? INT8_MIN : *(i64 *)dat); return 0;
|
||||
case CONF_I16: *(i16 *)opt->out = *(i64 *)dat >= INT16_MAX ? INT16_MAX : (*(i64 *)dat <= INT16_MIN ? INT16_MIN : *(i64 *)dat); return 0;
|
||||
case CONF_I32: *(i32 *)opt->out = *(i64 *)dat >= INT32_MAX ? INT32_MAX : (*(i64 *)dat <= INT32_MIN ? INT32_MIN : *(i64 *)dat); return 0;
|
||||
case CONF_I64: *(i64 *)opt->out = *(i64 *)dat >= INT64_MAX ? INT64_MAX : (*(i64 *)dat <= INT64_MIN ? INT64_MIN : *(i64 *)dat); return 0;
|
||||
case CONF_U8:
|
||||
*(u8 *)opt->out = *(u64 *)dat >= UINT8_MAX ? UINT8_MAX : *(u64 *)dat;
|
||||
return 0;
|
||||
case CONF_U16:
|
||||
*(u16 *)opt->out = *(u64 *)dat >= UINT16_MAX ? UINT16_MAX : *(u64 *)dat;
|
||||
return 0;
|
||||
case CONF_U32:
|
||||
*(u32 *)opt->out = *(u64 *)dat >= UINT32_MAX ? UINT32_MAX : *(u64 *)dat;
|
||||
return 0;
|
||||
case CONF_U64:
|
||||
*(u64 *)opt->out = *(u64 *)dat >= UINT64_MAX ? UINT64_MAX : *(u64 *)dat;
|
||||
return 0;
|
||||
case CONF_I8:
|
||||
*(i8 *)opt->out = *(i64 *)dat >= INT8_MAX ?
|
||||
INT8_MAX :
|
||||
(*(i64 *)dat <= INT8_MIN ? INT8_MIN : *(i64 *)dat);
|
||||
return 0;
|
||||
case CONF_I16:
|
||||
*(i16 *)opt->out = *(i64 *)dat >= INT16_MAX ?
|
||||
INT16_MAX :
|
||||
(*(i64 *)dat <= INT16_MIN ? INT16_MIN : *(i64 *)dat);
|
||||
return 0;
|
||||
case CONF_I32:
|
||||
*(i32 *)opt->out = *(i64 *)dat >= INT32_MAX ?
|
||||
INT32_MAX :
|
||||
(*(i64 *)dat <= INT32_MIN ? INT32_MIN : *(i64 *)dat);
|
||||
return 0;
|
||||
case CONF_I64:
|
||||
*(i64 *)opt->out = *(i64 *)dat >= INT64_MAX ?
|
||||
INT64_MAX :
|
||||
(*(i64 *)dat <= INT64_MIN ? INT64_MIN : *(i64 *)dat);
|
||||
return 0;
|
||||
case CONF_F32: *(f32 *)opt->out = *(f32 *)dat; return 0;
|
||||
case CONF_F64: *(f64 *)opt->out = *(f64 *)dat; return 0;
|
||||
default: fatal("invalid switch state, all cases should be handled already"); // abort; this shouldn't be possible, so I blame the programmer
|
||||
default:
|
||||
/* abort; this shouldn't be possible so I blame the programmer. */
|
||||
fatal("invalid switch state, all cases should be handled already");
|
||||
}
|
||||
}
|
||||
|
||||
/* utility function for conf_getpat to concatenate 3 strings, where we already know the size */
|
||||
NONNULL((1, 3))
|
||||
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
|
||||
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
|
||||
char *buf, *ptr;
|
||||
|
||||
// allocate enough data for all three to the buffer
|
||||
ptr = malloc(s1len + s2len + s3len + 1);
|
||||
if (!ptr) return NULL;
|
||||
if (!ptr)
|
||||
return NULL;
|
||||
buf = ptr; // store the head pointer into buf
|
||||
|
||||
// copy 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
|
||||
|
||||
// return the buffer
|
||||
@@ -147,7 +183,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. */
|
||||
char *conf_getpat(const char *restrict str) {
|
||||
char *conf_getpat(const char *restrict str)
|
||||
{
|
||||
char *buf = NULL;
|
||||
usize len;
|
||||
usize str_len = strlen(str);
|
||||
@@ -155,21 +192,24 @@ char *conf_getpat(const char *restrict str) {
|
||||
buf = getenv("XDG_CONFIG_HOME");
|
||||
if (!buf) {
|
||||
buf = getenv("HOME");
|
||||
if (!buf) return NULL;
|
||||
if (!buf)
|
||||
return NULL;
|
||||
len = strlen(buf);
|
||||
return conf_getpat_concat(buf, "/.config", str, len, 8, str_len);
|
||||
}
|
||||
return conf_getpat_concat(buf, NULL, str, strlen(buf), 0, str_len);
|
||||
#elif defined(__APPLE__)
|
||||
buf = getenv("HOME");
|
||||
if (!buf) return NULL;
|
||||
if (!buf)
|
||||
return NULL;
|
||||
len = strlen(buf);
|
||||
return conf_getpat_concat(buf, "/Library/Application Support", str, len, 28, str_len);
|
||||
#elif defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
|
||||
buf = getenv("APPDATA");
|
||||
if (!buf) {
|
||||
buf = getenv("USERPROFILE");
|
||||
if (!buf) return NULL;
|
||||
if (!buf)
|
||||
return NULL;
|
||||
len = strlen(buf);
|
||||
return conf_getpat_concat(buf, "\\AppData\\Roaming", str, len, 16, str_len);
|
||||
}
|
||||
|
||||
@@ -19,18 +19,18 @@ enum conf_err {
|
||||
|
||||
/* defines the primitive types available in the config file */
|
||||
enum conf_primitive {
|
||||
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_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_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_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_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_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_STR = 0, // expects `char**`, will output malloc'd data
|
||||
CONF_I8 = 1, // expects `int8_t*`,
|
||||
CONF_I16 = 2, // expects `int16_t*`,
|
||||
CONF_I32 = 4, // expects `int32_t*`,
|
||||
CONF_I64 = 8, // expects `int64_t*`,
|
||||
CONF_U8 = CONF_I8 | 0x80, // expects `uint8_t*`,
|
||||
CONF_U16 = CONF_I16 | 0x80, // expects `uint16_t*`,
|
||||
CONF_U32 = CONF_I32 | 0x80, // expects `uint32_t*`,
|
||||
CONF_U64 = CONF_I64 | 0x80, // expects `uint64_t*`,
|
||||
CONF_F32 = CONF_I32 | 0x40, // expects `float*`,
|
||||
CONF_F64 = CONF_I64 | 0x40, // expects `double*`,
|
||||
CONF_FSTR = 0x40, // expects `struct conf_fstr*`
|
||||
};
|
||||
|
||||
/* for outputting a fixed string as this config field */
|
||||
@@ -42,29 +42,33 @@ struct conf_fstr {
|
||||
/* defines the structure of a config file entry */
|
||||
struct conf_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; // where data will be written, is read when key is missing or invalid
|
||||
u8 type; // the primitive type which we are querying for
|
||||
};
|
||||
|
||||
/* processes an incoming buffer.
|
||||
* `buf`, `kout` and `vout` mustn't overlap, and must be (at least) `len` bytes long!
|
||||
* `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.
|
||||
* see `CONF_E*` or `enum conf_err` */
|
||||
* 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` */
|
||||
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. */
|
||||
struct conf_entry const *conf_matchopt(struct conf_entry const *opts, usize optc, const char *restrict key);
|
||||
/* 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, usize optc,
|
||||
const char *restrict key);
|
||||
|
||||
/* 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.
|
||||
* returns `0` upon success, non-zero upon failure. For information about specific error codes, see `enum conf_err` */
|
||||
/* 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.
|
||||
* returns `0` upon success, non-zero upon failure.
|
||||
* For information about specific error codes, see`enum conf_err` */
|
||||
int conf_procval(struct conf_entry const *opts, const char *restrict val);
|
||||
|
||||
/* acquires the config file path, appending str to the end (you need to handle path separators yourself)
|
||||
* expecting str to be null-terminated
|
||||
* - linux: reads $XDG_CONFIG_HOME, if empty $HOME/.config is used, if $HOME is empty NULL is returned.
|
||||
* - windows: reads %APPDATA%, if empty %USERPROFILE%\AppData\Roaming is used, if both are empty NULL is returned.
|
||||
* - osx: reads $HOME, uses $HOME/Library/Application Support, if $HOME is empty NULL is returned.
|
||||
* !! A malloc'd null-terminated string is returned !! */
|
||||
/* acquires the config file path, appending str to the end expecting str to be null-terminated
|
||||
* note: you must handle path separators yourself.
|
||||
* checks the following environment variables for each platform in order:
|
||||
* LINUX: `$XDG_CONFIG_HOME`, `$HOME/.config`.
|
||||
* WINDOWS: `%APPDATA%`, `%USERPROFILE%\AppData\Roaming`.
|
||||
* MACOSX: `$HOME/Library/Application Support`.
|
||||
* `NULL` is returned if the path couldn't be found. */
|
||||
char *conf_getpat(const char *) MALLOC NONNULL((1));
|
||||
|
||||
@@ -7,15 +7,16 @@
|
||||
|
||||
#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);
|
||||
|
||||
vfprintf(stream, fmt, ap);
|
||||
|
||||
fprintf(stream, "'\n");
|
||||
}
|
||||
|
||||
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, ...)
|
||||
{
|
||||
#ifndef NDEBUG
|
||||
#else
|
||||
char *env = getenv("DEBUG");
|
||||
@@ -28,28 +29,32 @@ void error_debug(uint ln, const char *restrict file, const char *restrict fmt, .
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
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, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
error_log(stdout, "\033[94mINF\033[0m", ln, file, fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
void error_warn(uint ln, const char *restrict file, const char *restrict fmt, ...) {
|
||||
void error_warn(uint ln, const char *restrict file, const char *restrict fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
error_log(stdout, "\033[93mWAR\033[0m", ln, file, fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
void error_error(uint ln, const char *restrict file, const char *restrict fmt, ...) {
|
||||
void error_error(uint ln, const char *restrict file, const char *restrict fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
error_log(stdout, "\033[91mERR\033[0m", ln, file, fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
void error_fatal(uint ln, const char *restrict file, const char *restrict fmt, ...) {
|
||||
void error_fatal(uint ln, const char *restrict file, const char *restrict fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
error_log(stdout, "\033[101mFAT\033[0m", ln, file, fmt, ap);
|
||||
|
||||
@@ -6,7 +6,8 @@
|
||||
#include "test.h"
|
||||
#include "test_conf.h"
|
||||
|
||||
int main(void) {
|
||||
int main(void)
|
||||
{
|
||||
assert_true(-3 >> 5 == -1); // checking for arithmetic shift, rather than logical shift
|
||||
assert_true(sizeof(u8) == 1);
|
||||
assert_true(sizeof(u16) == 2);
|
||||
@@ -14,7 +15,7 @@ int main(void) {
|
||||
assert_true(sizeof(u64) == 8);
|
||||
assert_true(sizeof(f32) == 4);
|
||||
assert_true(sizeof(f64) == 8);
|
||||
assert_true(sizeof(size_t) == sizeof(intptr_t));
|
||||
assert_true(sizeof(usize) == sizeof(size_t));
|
||||
test_conf_procbuf("key=val", "key", "val", 0);
|
||||
test_conf_procbuf("sometxt", "sometxt", "", CONF_ESYNTAX);
|
||||
test_conf_procbuf("# comment", "", "", CONF_ENODAT);
|
||||
|
||||
@@ -7,7 +7,8 @@
|
||||
uint test_okay = 0;
|
||||
uint test_fail = 0;
|
||||
|
||||
int test_process(int res, const char *restrict file, uint ln, const char *restrict function, const char *restrict expression) {
|
||||
int test_process(int res, const char *restrict file, uint ln, const char *restrict function, const char *restrict expression)
|
||||
{
|
||||
const char *status = res ?
|
||||
"[\033[32;1m OK \033[0m]" :
|
||||
"[\033[31;1m FAIL \033[0m]";
|
||||
|
||||
@@ -9,7 +9,8 @@
|
||||
#include "../src/util/conf.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)
|
||||
{
|
||||
usize len = strlen(buf) + 1;
|
||||
char k[len], v[len];
|
||||
*k = '\0', *v = '\0';
|
||||
@@ -18,20 +19,23 @@ void test_conf_procbuf(const char *restrict buf, const char *restrict expect_key
|
||||
assert_true(!strcmp(v, expect_val)));
|
||||
}
|
||||
|
||||
void test_conf_matchopt(struct conf_entry *opts, usize 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)
|
||||
{
|
||||
usize idx = opts - conf_matchopt(opts, optc, key);
|
||||
idx = (ssize)idx < 0 ? -idx : idx;
|
||||
int i = idx < optc ? (int)idx : -1;
|
||||
assert_true(i == 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)
|
||||
{
|
||||
u8 out[sizeof(u64)] = {0};
|
||||
assert_true(!conf_procval(&(struct conf_entry){NULL, out, type}, val));
|
||||
assert_true(memcmp(out, &expect_value, sizeof(u64)) == 0);
|
||||
}
|
||||
|
||||
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];
|
||||
f32 result;
|
||||
conf_procval(&(struct conf_entry){NULL, out, CONF_F32}, val);
|
||||
@@ -39,40 +43,46 @@ void test_conf_procval_f32(const char *val, f32 expect_value) {
|
||||
assert_true(fabsf(expect_value - result) < 1e-9f);
|
||||
}
|
||||
|
||||
void test_procval_str(void) {
|
||||
void test_procval_str(void)
|
||||
{
|
||||
char *out = NULL;
|
||||
(void)(assert_true(!conf_procval(&(struct conf_entry){NULL, (void *)&out, CONF_STR}, "here comes the sowon")) &&
|
||||
assert_false(strcmp("here comes the sowon", out)));
|
||||
free(out);
|
||||
}
|
||||
|
||||
void test_procval_str_predef(void) {
|
||||
void test_procval_str_predef(void)
|
||||
{
|
||||
char *out = strdup("owo");
|
||||
(void)(assert_true(!conf_procval(&(struct conf_entry){NULL, (void *)&out, CONF_STR}, "i leak if I don't free")) &&
|
||||
assert_true(!strcmp("i leak if I don't free", out)));
|
||||
free(out);
|
||||
}
|
||||
|
||||
void test_procval_fstr(void) {
|
||||
void test_procval_fstr(void)
|
||||
{
|
||||
char buf[16];
|
||||
struct conf_fstr str = {sizeof(buf), buf};
|
||||
(void)(assert_true(!conf_procval(&(struct conf_entry){NULL, &str, CONF_FSTR}, "hewwoo wowld")) &&
|
||||
assert_true(!strcmp(str.out, "hewwoo wowld")));
|
||||
}
|
||||
|
||||
void test_procval_fstr_trunc(void) {
|
||||
void test_procval_fstr_trunc(void)
|
||||
{
|
||||
char buf[8];
|
||||
struct conf_fstr str = {sizeof(buf), buf};
|
||||
(void)(assert_true(!conf_procval(&(struct conf_entry){NULL, &str, CONF_FSTR}, "hewwooo wowld")) &&
|
||||
assert_true(!strcmp(str.out, "hewwooo")));
|
||||
}
|
||||
|
||||
void test_procval_eparse(void) {
|
||||
void test_procval_eparse(void)
|
||||
{
|
||||
i32 out;
|
||||
assert_true(conf_procval(&(struct conf_entry){NULL, &out, CONF_I32}, "owo") == CONF_EPARSE);
|
||||
}
|
||||
|
||||
void test_conf_getpat(void) {
|
||||
void test_conf_getpat(void)
|
||||
{
|
||||
char *path;
|
||||
#if defined(__linux__)
|
||||
/* test without setting environment variables. */
|
||||
|
||||
Reference in New Issue
Block a user