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 0000000..5ffc3d9 Binary files /dev/null and b/test differ 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; +}