mirror of
https://github.com/thepigeongenerator/mcaselector-lite.git
synced 2025-12-18 17:35:45 +01:00
apply new formatting rules
This commit is contained in:
@@ -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(char const *restrict fname, int perms);
|
||||
|
||||
// define the constants if they haven't been
|
||||
#ifndef F_OK
|
||||
@@ -33,14 +33,14 @@ static inline int faccess(char const* restrict fname, int perms);
|
||||
#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.
|
||||
|
||||
int faccess(char const* restrict fname, int perms) {
|
||||
int faccess(char const *restrict fname, int perms) {
|
||||
return _access(fname, perms);
|
||||
}
|
||||
#else
|
||||
|
||||
@@ -14,11 +14,11 @@
|
||||
#include "atrb.h"
|
||||
#include "types.h"
|
||||
|
||||
int conf_procbuf(char const* restrict buf, char* restrict kout, char* restrict vout, size_t len) {
|
||||
int conf_procbuf(char const *restrict buf, char *restrict kout, char *restrict vout, size_t len) {
|
||||
bool feq = false; // whether we've found the equal sign
|
||||
|
||||
// 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
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
@@ -50,12 +50,10 @@ int conf_procbuf(char const* restrict buf, char* restrict kout, char* restrict v
|
||||
|
||||
// no data if we didn't move from the key position
|
||||
// syntax error if we couldn't find the equal sign
|
||||
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, char const* restrict key) {
|
||||
struct conf_entry const *conf_matchopt(struct conf_entry const *opts, size_t optc, char const *restrict key) {
|
||||
// find a match for the current key
|
||||
size_t i = 0;
|
||||
for (; i < optc; i++) {
|
||||
@@ -65,10 +63,10 @@ struct conf_entry const* conf_matchopt(struct conf_entry const* opts, size_t opt
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int conf_procval(struct conf_entry const* opt, char const* restrict val) {
|
||||
int conf_procval(struct conf_entry const *opt, char const *restrict val) {
|
||||
// parse the data
|
||||
errno = 0;
|
||||
char* end;
|
||||
char *end;
|
||||
u8 dat[sizeof(u64)];
|
||||
|
||||
switch (opt->type) {
|
||||
@@ -77,30 +75,30 @@ int conf_procval(struct conf_entry const* opt, char const* restrict val) {
|
||||
case CONF_I16:
|
||||
case CONF_I32:
|
||||
case CONF_I64:
|
||||
*(i64*)dat = strtoll(val, &end, 10); // for signed integer types
|
||||
*(i64 *)dat = strtoll(val, &end, 10); // for signed integer types
|
||||
break;
|
||||
// unsigned integer data parsing
|
||||
case CONF_U8:
|
||||
case CONF_U16:
|
||||
case CONF_U32:
|
||||
case CONF_U64:
|
||||
*(u64*)dat = strtoull(val, &end, 10); // for unsigned integer types
|
||||
*(u64 *)dat = strtoull(val, &end, 10); // for unsigned integer types
|
||||
break;
|
||||
|
||||
// floating-point data parsing
|
||||
case CONF_F32: *(f32*)dat = strtof(val, &end); break;
|
||||
case CONF_F64: *(f64*)dat = strtod(val, &end); break;
|
||||
case CONF_F32: *(f32 *)dat = strtof(val, &end); break;
|
||||
case CONF_F64: *(f64 *)dat = strtod(val, &end); break;
|
||||
|
||||
// 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.
|
||||
if (*(char **)opt->out) {
|
||||
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);
|
||||
*(char **)opt->out = strdup(val);
|
||||
return 0;
|
||||
case CONF_FSTR: {
|
||||
struct conf_fstr* s = opt->out;
|
||||
struct conf_fstr *s = opt->out;
|
||||
strncpy(s->out, val, s->len);
|
||||
s->out[s->len - 1] = '\0'; // ensure the string is null-terminated
|
||||
return 0;
|
||||
@@ -114,22 +112,22 @@ int conf_procval(struct conf_entry const* opt, char const* 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_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
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
/* utility function for conf_getpat to concatenate 3 strings, where we already know the size */
|
||||
atrb_nonnull(1, 3) static char* conf_getpat_concat(char const* restrict s1, char const* restrict s2, char const* restrict s3, size_t s1len, size_t s2len, size_t s3len) {
|
||||
atrb_nonnull(1, 3) static char *conf_getpat_concat(char const *restrict s1, char const *restrict s2, char const *restrict s3, size_t s1len, size_t s2len, size_t s3len) {
|
||||
assert(s2 || (!s2 && !s2len)); // ensuring the programmer passes both s2 and s2len as 0, if they intend to
|
||||
char *buf, *ptr;
|
||||
|
||||
@@ -148,8 +146,8 @@ atrb_nonnull(1, 3) static char* conf_getpat_concat(char const* restrict s1, char
|
||||
}
|
||||
|
||||
/* appends str to the config directory string we acquire from environment variables. */
|
||||
char* conf_getpat(char const* restrict str) {
|
||||
char* buf = NULL;
|
||||
char *conf_getpat(char const *restrict str) {
|
||||
char *buf = NULL;
|
||||
size_t len;
|
||||
size_t str_len = strlen(str);
|
||||
#if defined(__linux__)
|
||||
|
||||
@@ -36,13 +36,13 @@ enum conf_primitive {
|
||||
/* for outputting a fixed string as this config field */
|
||||
struct conf_fstr {
|
||||
size_t 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 */
|
||||
struct conf_entry {
|
||||
char const* 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
|
||||
char const *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(char const *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, char const *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, char const *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(char const *);
|
||||
|
||||
Reference in New Issue
Block a user