From ac5a7d5cd8a3bf01aec4fe58a4f69b635919d6a7 Mon Sep 17 00:00:00 2001 From: AUnicornWithNoLife <55228370+AUnicornWithNoLife@users.noreply.github.com> Date: Thu, 11 May 2023 14:44:54 +0100 Subject: [PATCH] init --- carg-parse.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++ carg-parse.h | 22 +++++++++++++ test | Bin 0 -> 33224 bytes test.c | 36 ++++++++++++++++++++ 4 files changed, 149 insertions(+) create mode 100644 carg-parse.c create mode 100644 carg-parse.h create mode 100755 test create mode 100755 test.c diff --git a/carg-parse.c b/carg-parse.c new file mode 100644 index 0000000..a2946e3 --- /dev/null +++ b/carg-parse.c @@ -0,0 +1,91 @@ +// c argument parser - Rose (github.com/AUnicornWithNoLife) + +#include "carg-parse.h" +#include +#include +#include +#include + +carg_parse_data* carg_parse(int argc, char** argv) +{ + carg_parse_data *data = (carg_parse_data*)malloc(sizeof(carg_parse_data)); + + char **values = (char**)malloc(sizeof(char*) * argc); + uint32_t values_cp = 0; + + char **lv_labels = (char**)malloc(sizeof(char*) * argc); + char **lv_values = (char**)malloc(sizeof(char*) * argc); + uint32_t lv_cp = 0; + + data->exec_name = argv[0]; + + for (int i = 1; i < argc; i++) + { + if (argv[i][0] == '-') // label + { + lv_labels[lv_cp] = argv[i] + 1; + + if (argc >= i + 1 && argv[i + 1][0] != '-') + { + i++; + + lv_values[lv_cp] = argv[i]; + } + else + { + lv_values[lv_cp] = NULL; + } + + lv_cp++; + } + else // not labelled + { + values[values_cp] = argv[i]; + + values_cp++; + } + } + + data->values_len = values_cp; + if (values_cp > 0) + { + data->values = realloc(values, sizeof(char*) * values_cp); + } + else + { + data->values = NULL; + } + + data->lv_len = lv_cp; + if (lv_cp > 0) + { + data->lv_labels = realloc(lv_labels, sizeof(char*) * lv_cp); + data->lv_values = realloc(lv_values, sizeof(char*) * lv_cp); + } + else + { + data->lv_labels = NULL; + data->lv_values = NULL; + } + + return data; +} + +void carg_parse_free(carg_parse_data* data) +{ + if (data->values != NULL) + { + free(data->values); + } + + if (data->lv_labels != NULL) + { + free(data->lv_labels); + } + if (data->lv_values != NULL) + { + free(data->lv_values); + } + + free(data); +} \ No newline at end of file diff --git a/carg-parse.h b/carg-parse.h new file mode 100644 index 0000000..c6f9eb8 --- /dev/null +++ b/carg-parse.h @@ -0,0 +1,22 @@ +#ifndef CARG_PARSE +#define CARG_PARSE + +#include +#include + +typedef struct +{ + char* exec_name; + + uint32_t values_len; + char** values; + + uint32_t lv_len; + char** lv_labels; + char** lv_values; +} carg_parse_data; + +carg_parse_data* carg_parse(int argc, char** argv); +void carg_parse_free(carg_parse_data* data); + +#endif \ No newline at end of file diff --git a/test b/test new file mode 100755 index 0000000000000000000000000000000000000000..5ffc3d92ab86eea0e483647ab0ab654e6133a1c6 GIT binary patch literal 33224 zcmeI5O>7%Q6vtIw6YyF zIaqSFEyCJX3O6nw4!wX7st`zs19jR6n-jUfDF+`)u_cgtj8H`QznQhOw!)DU|0lnl zd2eRkzWL4gII}y&ug#cDS< zv?R`l=;(Q;owKLgyXf)BcApCEO>m1MB`uzp&&=jS14U=N`NNnD{C8wR5V4zMwWCWF zbs0W9FPEPg&9z*e@rK?o@wSIOns#$QcNlMTE8bWxpPiV#(6Za-dohf6B5W5sDzXMq z@w}PoD-+Y>-o*6zOK9lK@0&25tdWS=oiR*{dCEF`4E4Oh{?q;5u~R39&1xjgJe%A* zOTj5BY-Nvl-i1q%W%$>4Hu`Ewn(?8fgjaN3*7sTJ#K||E!4u6rQtZeJwn$ZP8q*e5 zwf~?}MafTA89#L(w2Lr=k+6}qT!P(=^72)s&Z2xAIs}c1~Pr_kC zpMdV2$z}IWPK@pypPL+qE%Gc>^7wT0lUtu>kKVm8{Pp+t)yj@n;j^I@RH?0iwpG{l zcDCM$?S@y+Pq!=8i}^_$+2>J__r3;|Hkos5{6+kYto2-w&+uF>pPAY-um^cfjG`0d z1*pWY7R|g~3T@#^Z8!vCr!=$Cdn6x&V0pH4i@Wnc}T4PRf z3kvo6b7B+`V~`kYBP?SQT)0_F-Us3cWP8F?bh#kJaBZj#|Z8mcVY)KWO|* zEyn@ls8*as-7#<;xgDoUk}>+ z0Pf$l&~GV7FS?~|)xcQ|(q(_aI#i9{7zq0sk305LB+#`U^D@l4McMx!rzYS;artrNb0z zfwNrt(Oq~Dt3Rzb=7RKcOKzee$4|9UN$S(8lAVsQ-7hR6f}DEB0xlu7Fv2h5DK#V5 zXsoU`e%IF6+mKVOVR#$WW*vuN!@9qiJa;etd{Tw`A>qnDDR5R?e>S1w*Kn{_2EjF( zsJl(WpS9cD?0UFSWn6E2TW?)vhCEm5I6$wk2NUP+skNRjV663gsh?lz=WRTlv(B+T zyC=6rZF##tb;cR)+oF2L?Y_Rynd%>KQqG{g-k8&kC(oo(=xGcY4v|f_Ln@GHiM^`N z0pkp`5|y*+B2vd+31^Wlob8p-T3QvH_$?YEQIr^BOMggFKlva55+DH*AOR8}0TLhq z5+DH*AOR8}0TLhq5+DH*AOR8}0TLhq5+DH*AOR8}0TLhq5+DH*AOR8}0TLhq5+DH* zAOR8}0TLhq5+DH*AOR8}0TLhq5+DH*AORBi&k5Lg!w~d+XcshcF@Uz=(jvDmG)31F z_=HK-+Wi!U1EKfbAFCB<^ zQy0C=Y-VgGpYbkcM#eK)wP|c5d%?RrlFen*{;83PX*Cd2OLLY9dNihL1>9^fk6TFQ zuaqB|NAeOXZs|KG^gN!@Hoo_M#N@Xtv}>;$8{daVe{*kw%=-`&Web5^p%Ar?ci5*o jY~hCwt?TdG%e4@lwynGk=IKS|8L79}VdAa&#vK0vYI-;f literal 0 HcmV?d00001 diff --git a/test.c b/test.c new file mode 100755 index 0000000..b18ffc0 --- /dev/null +++ b/test.c @@ -0,0 +1,36 @@ +// (TESTER FOR) c argument parser - Rose (github.com/AUnicornWithNoLife) + +#include +#include "carg-parse.h" + +int main(int argc, char** argv) +{ + carg_parse_data *data = carg_parse(argc, argv); + + printf("EXEC: %s\n", data->exec_name); + + printf("\nVALUES:\n"); + + for (uint32_t i = 0; i < data->values_len; i++) + { + printf("%d ::: %s\n", i, data->values[i]); + } + + printf("\nLABELED VALUES:\n"); + + for (uint32_t i = 0; i < data->lv_len; i++) + { + if (data->lv_values[i] == NULL) + { + printf("%s ::: NULL\n", data->lv_labels[i]); + } + else + { + printf("%s ::: %s\n", data->lv_labels[i], data->lv_values[i]); + } + } + + carg_parse_free(data); + + return 0; +}