mirror of
https://github.com/thepigeongenerator/mcaselector-lite.git
synced 2025-12-18 20:35:45 +01:00
Compare commits
7 Commits
6dfcb2b5b9
...
b1950e282a
| Author | SHA1 | Date | |
|---|---|---|---|
| b1950e282a | |||
| 875f7666cc | |||
| cc445442c6 | |||
| 8cc2e21088 | |||
| 3a5dac150c | |||
| dd96178c7a | |||
| b8159c92df |
@@ -87,7 +87,7 @@ BinPackArguments: true
|
||||
DerivePointerAlignment: false
|
||||
PointerAlignment: Right
|
||||
ReferenceAlignment: Pointer
|
||||
QualifierAlignment: Right
|
||||
QualifierAlignment: Left
|
||||
|
||||
# ---------------------------
|
||||
# include settings and sorting
|
||||
|
||||
@@ -7,20 +7,16 @@
|
||||
#include "../util/compat/endian.h"
|
||||
#include "../util/types.h"
|
||||
|
||||
/* returns the string length from a specific location in the buffer */
|
||||
static inline u16 nbt_strlen(u8 const *restrict buf) {
|
||||
return be16toh(*(u16 *)(buf));
|
||||
}
|
||||
|
||||
/* returns the length of an array from a specific location in the buffer */
|
||||
static inline i32 nbt_arrlen(u8 const *restrict buf) {
|
||||
return be32toh(*(i32 *)(buf));
|
||||
const u8 *nbt_nexttag(const u8 *restrict buf, u16 naml) {
|
||||
size_t len = nbt_tagdatlen(buf);
|
||||
if (!len) return NULL; // TODO: compound tags should be handled here
|
||||
return buf + naml + len + 3;
|
||||
}
|
||||
|
||||
/* compares the string in `buf` to `matstr`.
|
||||
* returns `=0` if equal, `>0` if buf is greater, `<0` if matstr is greater. */
|
||||
static int nbt_cmpstr(char const *restrict matstr, u8 const *restrict buf) {
|
||||
u16 len = nbt_strlen(buf);
|
||||
static int nbt_cmpstr(const char *restrict matstr, const u8 *restrict buf) {
|
||||
u16 len = be16toh(*(u16 *)buf);
|
||||
|
||||
// allocate and copy bytes
|
||||
char str[len + 1];
|
||||
@@ -30,18 +26,10 @@ static int nbt_cmpstr(char const *restrict matstr, u8 const *restrict buf) {
|
||||
return strncmp(str, matstr, len);
|
||||
}
|
||||
|
||||
/* returns the (expected) pointer of the tag following this one.
|
||||
* `NULL` is returned if anything went wrong. */
|
||||
static u8 const *nbt_nexttag(u8 const *restrict buf, u16 naml) {
|
||||
size_t len = nbt_tagdatlen(buf);
|
||||
if (!len) return NULL; // TODO: compound tags should be handled here
|
||||
return buf + naml + len + 3;
|
||||
}
|
||||
|
||||
// TODO: not actually doing anything
|
||||
/* readies the output data for export, returns the new buffer position, or `NULL` upon an error (may be out of bounds) */
|
||||
static u8 const *nbt_proctag(u8 const *restrict buf, u16 slen) {
|
||||
u8 const *ptr = buf + 3 + slen;
|
||||
static const u8 *nbt_proctag(const u8 *restrict buf, u16 slen) {
|
||||
const u8 *ptr = buf + 3 + slen;
|
||||
u8 dat[8];
|
||||
size_t arrlen = 0;
|
||||
|
||||
@@ -70,7 +58,7 @@ static u8 const *nbt_proctag(u8 const *restrict buf, u16 slen) {
|
||||
}
|
||||
|
||||
/* finds which of `pats` is equivalent to `cmp`, assumes `cmp` is `≥len` bytes long */
|
||||
static char const *getpat(struct nbt_path const *restrict pats, uint npats, i16 dpt, char const *restrict cmp, u16 len) {
|
||||
static const char *getpat(struct nbt_path const *restrict pats, uint npats, i16 dpt, const char *restrict cmp, u16 len) {
|
||||
for (uint i = 0; i < npats; i++) {
|
||||
if (strncmp(pats[i].pat[dpt], cmp, len) == 0)
|
||||
return pats[i].pat[dpt];
|
||||
@@ -79,7 +67,7 @@ static char const *getpat(struct nbt_path const *restrict pats, uint npats, i16
|
||||
}
|
||||
|
||||
// TODO: make the user do the looping
|
||||
int nbt_proc(struct nbt_path const *restrict pats, uint npats, u8 const *restrict buf, size_t len) {
|
||||
int nbt_proc(struct nbt_path const *restrict pats, uint npats, const u8 *restrict buf, size_t len) {
|
||||
// ensure first and last tag(s) are valid
|
||||
if (buf[0] != NBT_COMPOUND || buf[len - 1] != NBT_END)
|
||||
return 1;
|
||||
@@ -95,14 +83,14 @@ int nbt_proc(struct nbt_path const *restrict pats, uint npats, u8 const *restric
|
||||
assert(mdpt > 0);
|
||||
|
||||
// storing the segments of the current path
|
||||
char const *cpat[mdpt - 1];
|
||||
const char *cpat[mdpt - 1];
|
||||
memset((void *)cpat, 0, mdpt - 1);
|
||||
|
||||
// looping through the different tags
|
||||
u8 const *ptr = buf + nbt_strlen(buf + 1) + 3;
|
||||
const u8 *ptr = buf + nbt_namelen(buf) + 3;
|
||||
while (ptr < (buf + len) && dpt >= 0) {
|
||||
u16 naml = nbt_strlen(ptr + 1);
|
||||
char const *mat = getpat(pats, npats, dpt, (char *)(ptr + 3), naml);
|
||||
u16 naml = nbt_namelen(ptr);
|
||||
const char *mat = getpat(pats, npats, dpt, (char *)(ptr + 3), naml);
|
||||
cpat[dpt] = mat;
|
||||
|
||||
if (mat) {
|
||||
@@ -133,7 +121,7 @@ int nbt_primsize(u8 tag) {
|
||||
}
|
||||
}
|
||||
|
||||
size_t nbt_tagdatlen(u8 const *restrict buf) {
|
||||
size_t nbt_tagdatlen(const u8 *restrict buf) {
|
||||
i32 mems = 0;
|
||||
|
||||
switch (*buf) {
|
||||
@@ -146,13 +134,13 @@ size_t nbt_tagdatlen(u8 const *restrict buf) {
|
||||
|
||||
case NBT_ARR_I64: mems += sizeof(i64) - sizeof(i32); __attribute__((fallthrough));
|
||||
case NBT_ARR_I32: mems += sizeof(i32) - sizeof(i8); __attribute__((fallthrough));
|
||||
case NBT_ARR_I8: return +mems * nbt_arrlen(buf) + 4;
|
||||
case NBT_ARR_I8: return +mems * (i32)be32toh(*(u32 *)(buf)) + 4;
|
||||
|
||||
case NBT_STR: return nbt_strlen(buf) + 2;
|
||||
case NBT_STR: return be16toh(*(u16 *)buf) + 2;
|
||||
|
||||
case NBT_LIST:
|
||||
mems = nbt_primsize(*buf);
|
||||
if (mems > 0) return mems * nbt_arrlen(buf + 1) + 5;
|
||||
if (mems > 0) return mems * (i32)be32toh(*(u32 *)(buf + 1)) + 5;
|
||||
return 0;
|
||||
default: return 0;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// Licensed under the MIT Licence. See LICENSE for details
|
||||
#pragma once
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
@@ -35,19 +36,29 @@ enum nbt_tagid {
|
||||
};
|
||||
|
||||
struct nbt_path {
|
||||
char const **restrict pat; // specifies the NBT path components as separate elements
|
||||
const char **restrict pat; // specifies the NBT path components as separate elements
|
||||
i16 len; // specifies the length of the NBT elements
|
||||
};
|
||||
|
||||
/* returns the name length of a specific tag. `buf` is the pointer to start of the tag */
|
||||
atrb_pure atrb_nonnull(1) static inline u16 nbt_namelen(const u8 *restrict buf) {
|
||||
assert(*buf != NBT_END);
|
||||
return be16toh(*(u16 *)(buf + 1));
|
||||
}
|
||||
|
||||
/* returns the (expected) pointer of the tag following this one.
|
||||
* `NULL` is returned if anything went wrong. */
|
||||
atrb_pure atrb((nonnull(1))) const u8 *nbt_nexttag(const u8 *restrict buf, u16 naml);
|
||||
|
||||
/* checks whether the tag is a primitive data tag. (not recommended for filtering tags, use a `switch`)
|
||||
* returns a boolean value. */
|
||||
atrb_const int nbt_isprim(u8 tag);
|
||||
|
||||
/* gets the byte size of an NBT tag's data (excluding id and name), returns `0` upon error. */
|
||||
atrb_const size_t nbt_tagdatlen(u8 const *buf);
|
||||
atrb_pure size_t nbt_tagdatlen(const u8 *buf);
|
||||
|
||||
/* gets the tag size of primitive types, returns `>0` on success, `<0` on failure */
|
||||
atrb_const int nbt_primsize(u8 tag);
|
||||
|
||||
/* processes the uncompressed `NBT` data in `buf`, with a size of `len`. */
|
||||
atrb_nonnull(1, 3) int nbt_proc(struct nbt_path const *restrict paths, uint npaths, u8 const *restrict buf, size_t len);
|
||||
atrb_nonnull(1, 3) int nbt_proc(struct nbt_path const *restrict paths, uint npaths, const u8 *restrict buf, size_t len);
|
||||
|
||||
@@ -17,5 +17,5 @@
|
||||
#define fatal(s, ...) \
|
||||
do { \
|
||||
printf("\033[101m" __FILE__ ":" MACRO_STR2(__LINE__) ": [FAT]: " s "\033[0m\n", ##__VA_ARGS__); \
|
||||
exit(1); \
|
||||
exit(EXIT_FAILURE); \
|
||||
} while (0)
|
||||
|
||||
@@ -24,7 +24,7 @@ DEF_GLSL(sh_geom_glsl);
|
||||
// NOLINTEND
|
||||
|
||||
/* compile a shader */
|
||||
static GLuint shader_compile(GLenum type, char const* src, size_t len) {
|
||||
static GLuint shader_compile(GLenum type, const char *src, size_t len) {
|
||||
int ilen = len;
|
||||
GLuint shader = glCreateShader(type);
|
||||
glShaderSource(shader, 1, &src, &ilen);
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
#define WIN_DEFAULT_HEIGHT 480
|
||||
|
||||
// callback for GLFW errors
|
||||
static void error_callback(int err, char const *const msg) {
|
||||
static void error_callback(int err, const char *const msg) {
|
||||
fprintf(stderr, "\033[91mE: glfw returned (%i); \"%s\"\033[0m\n", err, msg);
|
||||
}
|
||||
|
||||
|
||||
@@ -14,11 +14,11 @@
|
||||
#define atrb_format(...) __attribute__((format(__VA_ARGS__)))
|
||||
#define atrb_nonnull(...) __attribute__((nonnull(__VA_ARGS__)))
|
||||
#elif defined(_MSC_VER)
|
||||
#define atrb __declspec
|
||||
#define atrb(...) __declspec(__VA_ARGS__)
|
||||
#define atrb_deprecated __declspec(deprecated)
|
||||
#define atrb_noreturn __declspec(noreturn)
|
||||
#else
|
||||
#define atrb
|
||||
#define atrb()
|
||||
#define atrb_deprecated
|
||||
#define atrb_unused
|
||||
#define atrb_pure
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
/* 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 */
|
||||
static inline int faccess(char const *restrict fname, int perms);
|
||||
static inline int faccess(const char *restrict fname, int perms);
|
||||
|
||||
// define the constants if they haven't been
|
||||
#ifndef F_OK
|
||||
|
||||
@@ -41,7 +41,7 @@ struct conf_fstr {
|
||||
|
||||
/* defines the structure of a config file entry */
|
||||
struct conf_entry {
|
||||
char const *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
|
||||
uint8_t type; // the primitive type which we are querying for
|
||||
};
|
||||
@@ -51,15 +51,15 @@ struct conf_entry {
|
||||
* `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` */
|
||||
int conf_procbuf(char const *restrict buf, char *restrict kout, char *restrict vout, size_t len);
|
||||
int conf_procbuf(const char *restrict buf, char *restrict kout, char *restrict vout, size_t 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, size_t optc, char const *restrict key);
|
||||
struct conf_entry const *conf_matchopt(struct conf_entry const *opts, size_t 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` */
|
||||
int conf_procval(struct conf_entry const *opts, char const *restrict val);
|
||||
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
|
||||
@@ -67,4 +67,4 @@ int conf_procval(struct conf_entry const *opts, char const *restrict val);
|
||||
* - 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 !! */
|
||||
atrb_malloc atrb_nonnull(1) char *conf_getpat(char const *);
|
||||
atrb_malloc atrb_nonnull(1) char *conf_getpat(const char *);
|
||||
|
||||
@@ -19,7 +19,7 @@ struct test_getpat_envdat {
|
||||
|
||||
/* save the current environment variables */
|
||||
static void env_save(struct test_getpat_envdat *s) {
|
||||
char const *tmp;
|
||||
const char *tmp;
|
||||
|
||||
tmp = getenv("XDG_CONFIG_HOME");
|
||||
s->xdg_config_home = tmp ? strdup(tmp) : NULL;
|
||||
@@ -64,9 +64,9 @@ static void env_restore(struct test_getpat_envdat *s) {
|
||||
|
||||
/* check procbuf's functionality */
|
||||
struct test_procbuf {
|
||||
char const *in; // data in
|
||||
char const *xkey; // expected key
|
||||
char const *xval; // expected value
|
||||
const char *in; // data in
|
||||
const char *xkey; // expected key
|
||||
const char *xval; // expected value
|
||||
int xret; // expected return type
|
||||
};
|
||||
int test_procbuf(void *arg) {
|
||||
@@ -81,7 +81,7 @@ int test_procbuf(void *arg) {
|
||||
|
||||
/* check matchopt functionality */
|
||||
struct test_matchopt {
|
||||
char const *key; // key to search for (key1, key2, key3)
|
||||
const char *key; // key to search for (key1, key2, key3)
|
||||
int xidx; // expect index (<0 is NULL, may not be more than 2)
|
||||
};
|
||||
int test_matchopt(void *arg) {
|
||||
@@ -96,7 +96,7 @@ int test_matchopt(void *arg) {
|
||||
}
|
||||
|
||||
struct test_procval_int {
|
||||
char const *val;
|
||||
const char *val;
|
||||
u64 xres;
|
||||
u8 type;
|
||||
};
|
||||
|
||||
@@ -3,12 +3,12 @@
|
||||
#pragma once
|
||||
#include <stdio.h>
|
||||
|
||||
char const *test_ctest;
|
||||
const char *test_ctest;
|
||||
size_t test_runs = 0;
|
||||
|
||||
// evaluates the test
|
||||
// returns 1 upon error
|
||||
static inline int assert_helper(int cond, char const *restrict fname, unsigned ln, char const *restrict fnname, char const *restrict expr) {
|
||||
static inline int assert_helper(int cond, const char *restrict fname, unsigned ln, const char *restrict fnname, const char *restrict expr) {
|
||||
test_runs++;
|
||||
if (cond)
|
||||
printf("[\033[32;1m OK \033[0m] %s %s -> %s:%u (%s)\n", test_ctest, fnname, fname, ln, expr);
|
||||
@@ -22,7 +22,7 @@ static inline int assert_helper(int cond, char const *restrict fname, unsigned l
|
||||
|
||||
// contains the data for executing a single test
|
||||
struct testdat {
|
||||
char const *name; // test name
|
||||
const char *name; // test name
|
||||
int (*test)(void *); // test, returns 0 upon success, non-zero upon failure
|
||||
void *args; // arguments to the test
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user