Restructure code and make all public features toggleable

This commit is contained in:
Nick Chambers 2022-06-27 18:25:31 -05:00
parent 7a7313da7f
commit 4e10b72586
10 changed files with 89 additions and 46 deletions

View File

@ -2,12 +2,10 @@
#define __GARGOYLE_H_
#include <gargoyle/codex.h>
#include <gargoyle/scribe.h>
#include <gargoyle/sleuth.h>
#include <gargoyle/privledge.h>
#include <stdint.h>
const uint8_t GARGOYLE_DIGEST_ALL = 1 << 0;
uint8_t gargoyle_digest_args(uint16_t optc, struct gargoyle_opt *optv, int *argc, char ***argv, uint8_t flags);
uint8_t gargoyle_digest_args(uint16_t optc, struct gargoyle_opt *optv, int *argc, char ***argv, gargoyle_flag_type flags);
uint8_t gargoyle_digest_envh(/* ... */);
#endif

View File

@ -15,4 +15,18 @@ static const uint8_t GARGOYLE_ERR_INVALID_UINT = 3;
static const uint8_t GARGOYLE_ERR_INVALID_DBLE = 4;
static const uint8_t GARGOYLE_ERR_UNKNOWN_TYPE = 5;
typedef uint8_t gargoyle_flag_type;
static const gargoyle_flag_type GARGOYLE_FLG_BCASE = 1 << 0;
static const gargoyle_flag_type GARGOYLE_FLG_ECASE = 1 << 1;
static const gargoyle_flag_type GARGOYLE_FLG_ICASE = 1 << 2;
static const gargoyle_flag_type GARGOYLE_FLG_SYMBL = 1 << 3;
static const gargoyle_flag_type GARGOYLE_FLG_GREED = 1 << 4;
static const gargoyle_flag_type GARGOYLE_FLG_FILL0 = 1 << 5;
static const gargoyle_flag_type GARGOYLE_FLG_FLXBL = GARGOYLE_FLG_BCASE |
GARGOYLE_FLG_ECASE |
GARGOYLE_FLG_ICASE |
GARGOYLE_FLG_SYMBL;
#endif

View File

@ -0,0 +1,17 @@
#ifndef __GARGOYLE_PRIVLEDGE_H_
#define __GARGOYLE_PRIVLEDGE_H_
struct gargoyle_opt {
const char *brand;
uint16_t brand_sz;
const char emblem;
void *val;
uint16_t val_sz;
uint8_t type;
};
#define GARGOYLE_MK_OPT(brand) brand, (sizeof(brand) - 1)
#define GARGOYLE_EZ_OPT(brand, val) GARGOYLE_MK_OPT(brand), 0[brand], &val, sizeof(val)
#define GARGOYLE_CS_OPT(brand, val) GARGOYLE_MK_OPT(brand), 0[brand], &val[0], sizeof(val)
#endif

View File

@ -4,7 +4,7 @@
#include <gargoyle/sleuth.h>
#include <stdint.h>
uint8_t gargoyle_from_bool(struct gargoyle_opt *opt, const char *brand);
uint8_t gargoyle_from_rope(struct gargoyle_opt *opt, const char *brand_val);
uint8_t gargoyle_from_bool(struct gargoyle_opt *opt, const char *brand, gargoyle_flag_type flags);
uint8_t gargoyle_from_rope(struct gargoyle_opt *opt, const char *brand_val, gargoyle_flag_type flags);
#endif

View File

@ -1,22 +1,11 @@
#ifndef __GARGOYLE_SLEUTH_H_
#define __GARGOYLE_SLEUTH_H_
#include <gargoyle/codex.h>
#include <gargoyle/privledge.h>
#include <stdint.h>
struct gargoyle_opt {
const char *brand;
uint16_t brand_sz;
const char emblem;
void *val;
uint16_t val_sz;
uint8_t type;
};
#define GARGOYLE_MK_OPT(brand) brand, (sizeof(brand) - 1)
#define GARGOYLE_EZ_OPT(brand, val) GARGOYLE_MK_OPT(brand), 0[brand], &val, sizeof(val)
#define GARGOYLE_CS_OPT(brand, val) GARGOYLE_MK_OPT(brand), 0[brand], &val[0], sizeof(val)
struct gargoyle_opt *gargoyle_find_brand(uint16_t optc, struct gargoyle_opt *optv, const char *brand);
struct gargoyle_opt *gargoyle_find_emblem(uint16_t optc, struct gargoyle_opt *optv, const char emblem);
struct gargoyle_opt *gargoyle_find_brand(uint16_t optc, struct gargoyle_opt *optv, const char *brand, gargoyle_flag_type flags);
struct gargoyle_opt *gargoyle_find_emblem(uint16_t optc, struct gargoyle_opt *optv, const char emblem, gargoyle_flag_type flags);
#endif

View File

@ -4,7 +4,8 @@
#include <stdint.h>
uint8_t is_sep(char tok);
uint8_t gargoyle_cmp(const char *s1, const char *s2, uint16_t len, uint8_t flags);
char *gargoyle_cpy(char *dst, const char *src, uint16_t len);
uint8_t is_eql(char lhs, char rhs);
uint8_t gargoyle_cmp(const char *s1, const char *s2, uint16_t len, gargoyle_flag_type flags);
char *gargoyle_cpy(char *dst, const char *src, uint16_t len, gargoyle_flag_type flags);
#endif

View File

@ -1,12 +1,18 @@
#include <gargoyle.h>
#include <gargoyle/scribe.h>
#include <gargoyle/sleuth.h>
#include <stddef.h>
uint8_t gargoyle_digest_args(uint16_t optc, struct gargoyle_opt *optv, int *argc, char ***argv, uint8_t flags) {
if(!(flags & GARGOYLE_DIGEST_ALL)) {
uint8_t gargoyle_digest_args(uint16_t optc, struct gargoyle_opt *optv, int *argc, char ***argv, gargoyle_flag_type flags) {
if(!(flags & GARGOYLE_FLG_GREED)) {
*argc -= 1;
*argv += 1;
}
if(flags & GARGOYLE_FLG_BCASE) {
flags |= GARGOYLE_FLG_ICASE;
}
while(1) {
const char *arg = **argv;
@ -22,7 +28,7 @@ uint8_t gargoyle_digest_args(uint16_t optc, struct gargoyle_opt *optv, int *argc
if(*(arg + 1) == '-') {
const char *brand = arg + 2;
opt = gargoyle_find_brand(optc, optv, brand);
opt = gargoyle_find_brand(optc, optv, brand, flags);
if(!opt) {
return GARGOYLE_ERR_UNKNOWN_OPT;
@ -31,9 +37,9 @@ uint8_t gargoyle_digest_args(uint16_t optc, struct gargoyle_opt *optv, int *argc
uint8_t res = 0;
if(opt->type & GARGOYLE_TYPE_BOOL) {
res = gargoyle_from_bool(opt, brand);
res = gargoyle_from_bool(opt, brand, flags);
} else if(*(brand + opt->brand_sz) == '=') {
res = gargoyle_from_rope(opt, brand + opt->brand_sz + 1);
res = gargoyle_from_rope(opt, brand + opt->brand_sz + 1, flags);
} else {
if(!*(*argv + 1)) {
return GARGOYLE_ERR_VALUE_REQUIRED;
@ -41,7 +47,7 @@ uint8_t gargoyle_digest_args(uint16_t optc, struct gargoyle_opt *optv, int *argc
*argc -= 1;
*argv += 1;
res = gargoyle_from_rope(opt, **argv);
res = gargoyle_from_rope(opt, **argv, flags);
}
if(res) {
@ -51,7 +57,7 @@ uint8_t gargoyle_digest_args(uint16_t optc, struct gargoyle_opt *optv, int *argc
const char *idx = arg + 1;
for(; *idx; idx += 1) {
opt = gargoyle_find_emblem(optc, optv, *idx);
opt = gargoyle_find_emblem(optc, optv, *idx, flags);
if(!opt) {
return GARGOYLE_ERR_UNKNOWN_OPT;
@ -65,3 +71,7 @@ uint8_t gargoyle_digest_args(uint16_t optc, struct gargoyle_opt *optv, int *argc
return GARGOYLE_ERR_SUCCESS;
}
uint8_t gargoyle_digest_envh(/* ... */) {
return 0;
}

View File

@ -3,16 +3,16 @@
#include <gargoyle/twine.h>
#include <stdlib.h>
uint8_t gargoyle_from_bool(struct gargoyle_opt *opt, const char *brand) {
uint8_t gargoyle_from_bool(struct gargoyle_opt *opt, const char *brand, gargoyle_flag_type flags) {
if(opt->val) {
uint8_t *val = opt->val;
*val = !gargoyle_cmp(brand, "no-", 3, GARGOYLE_CMP_ICASE);
*val = !gargoyle_cmp(brand, "no-", 3, flags);
}
return 0;
}
uint8_t gargoyle_from_rope(struct gargoyle_opt *opt, const char *brand_val) {
uint8_t gargoyle_from_rope(struct gargoyle_opt *opt, const char *brand_val, gargoyle_flag_type flags) {
if(opt->type & GARGOYLE_TYPE_UINT) {
char *end = NULL;
uint64_t *val = opt->val;
@ -31,8 +31,7 @@ uint8_t gargoyle_from_rope(struct gargoyle_opt *opt, const char *brand_val) {
}
} else if(opt->type & GARGOYLE_TYPE_ROPE) {
char *val = opt->val;
char *end = gargoyle_cpy(val, brand_val, opt->val_sz - 1);
*end = '\0';
gargoyle_cpy(val, brand_val, opt->val_sz, flags | GARGOYLE_FLG_FILL0);
} else {
return GARGOYLE_ERR_UNKNOWN_TYPE;
}

View File

@ -1,20 +1,25 @@
#include <ctype.h>
#include <gargoyle/codex.h>
#include <gargoyle/sleuth.h>
#include <gargoyle/twine.h>
#include <stddef.h>
struct gargoyle_opt *gargoyle_find_brand(uint16_t optc, struct gargoyle_opt *optv, const char *brand) {
struct gargoyle_opt *gargoyle_find_brand(uint16_t optc, struct gargoyle_opt *optv, const char *brand, gargoyle_flag_type flags) {
if(flags & GARGOYLE_FLG_BCASE) {
flags |= GARGOYLE_FLG_ICASE;
}
for(; optc; optc -= 1, optv += 1) {
const char *idx = brand;
if(optv->type & GARGOYLE_TYPE_BOOL && gargoyle_cmp(idx, "no-", 3, GARGOYLE_CMP_ICASE)) {
if(optv->type & GARGOYLE_TYPE_BOOL && gargoyle_cmp(idx, "no-", 3, flags)) {
idx += 3;
}
uint8_t end = !*(idx + optv->brand_sz);
uint8_t eql = *(idx + optv->brand_sz) == '=';
if(gargoyle_cmp(idx, optv->brand, optv->brand_sz, GARGOYLE_CMP_ICASE) && (end || eql)) {
if(gargoyle_cmp(idx, optv->brand, optv->brand_sz, flags) && (end || eql)) {
return optv;
}
}
@ -22,10 +27,12 @@ struct gargoyle_opt *gargoyle_find_brand(uint16_t optc, struct gargoyle_opt *opt
return NULL;
}
struct gargoyle_opt *gargoyle_find_emblem(uint16_t optc, struct gargoyle_opt *optv, const char emblem) {
struct gargoyle_opt *gargoyle_find_emblem(uint16_t optc, struct gargoyle_opt *optv, const char emblem, gargoyle_flag_type flags) {
for(; optc; optc -= 1, optv += 1) {
if(emblem == optv->emblem) {
return optv;
} else if(flags & GARGOYLE_FLG_ECASE && is_eql(emblem, optv->emblem)) {
return optv;
}
}

View File

@ -1,15 +1,20 @@
#include <ctype.h>
#include <gargoyle/codex.h>
#include <gargoyle/twine.h>
uint8_t is_sep(char tok) {
return (tok == '_' || tok == '-');
}
uint8_t gargoyle_cmp(const char *s1, const char *s2, uint16_t len, uint8_t flags) {
uint8_t is_eql(char lhs, char rhs) {
return lhs == rhs;
}
uint8_t gargoyle_cmp(const char *s1, const char *s2, uint16_t len, gargoyle_flag_type flags) {
while(*s1 && *s2 && len) {
if(flags & GARGOYLE_CMP_ICASE && isalpha(*s1) && tolower(*s1) == tolower(*s2)) {
if(flags & GARGOYLE_FLG_ICASE && isalpha(*s1) && is_eql(*s1, *s2)) {
s1 += 1, s2 += 1;
} else if(flags & GARGOYLE_CMP_SYMBL && is_sep(*s1) && is_sep(*s2)) {
} else if(flags & GARGOYLE_FLG_SYMBL && is_sep(*s1) && is_sep(*s2)) {
s1 += 1, s2 += 1;
} else if(*s1 == *s2) {
s1 += 1, s2 += 1;
@ -23,10 +28,13 @@ uint8_t gargoyle_cmp(const char *s1, const char *s2, uint16_t len, uint8_t flags
return 1;
}
char *gargoyle_cpy(char *dst, const char *src, uint16_t len) {
uint16_t idx = 1;
char *gargoyle_cpy(char *dst, const char *src, uint16_t len, gargoyle_flag_type flags) {
if(flags & GARGOYLE_FLG_FILL0) {
*(dst + len - 1) = '\0';
len -= 1;
}
for(; len; dst += 1, len -= 1, idx += 1) {
for(; len; dst += 1, len -= 1) {
if(*src) {
*dst = *src;
src += 1;