From 9d40f081ac8e690eac59e6c58e91ed827e20e62f Mon Sep 17 00:00:00 2001 From: tylen Date: Mon, 23 Sep 2024 17:06:23 +0000 Subject: [PATCH] cmd #1: introduce ArgList struct --- src/cdo.c | 1 + src/cmd.c | 29 +++++++++++++++++++++++++++++ src/cmd.h | 11 +++++++++++ 3 files changed, 41 insertions(+) diff --git a/src/cdo.c b/src/cdo.c index 4218eef..662742b 100644 --- a/src/cdo.c +++ b/src/cdo.c @@ -3,6 +3,7 @@ #include "cmd.h" int main (int argc, char *argv[]) { + init_cmd(); struct CommandLineArg* args = parse_args(argc, argv); return 0; } diff --git a/src/cmd.c b/src/cmd.c index 325799a..843579c 100644 --- a/src/cmd.c +++ b/src/cmd.c @@ -3,6 +3,7 @@ #include "tools/buffer.h" #include +#include const struct CommandLineArg supported_args [] = { { @@ -29,6 +30,34 @@ void usage(void) { buffer_flush(usage_lines); } +void init_cmd(void) { + return +} + +void create_arg_list(struct ArgList* list, size_t capacity) { + list = (struct ArgList*) malloc(sizeof(struct ArgList)); + list->args = (struct CommandLineArg*) malloc(capacity * sizeof(struct CommandLineArg)); + list->size = 0; + list->capacity = capacity; +} + +void append_arg(struct ArgList* list, struct CommandLineArg arg) { + if (list->size + 1 >= list->capacity) { + list->capacity *= 2; + list->args = + (struct CommandLineArg*) + realloc( + list->args, + list->capacity * sizeof(struct CommandLineArg) + ); + } + memcpy( + list->args + (list->size * sizeof(struct CommandLineArg)), + &arg, + sizeof(struct CommandLineArg) + ); +} + struct CommandLineArg* parse_args(const int argc, char* argv[]) { if (argc < 2) { LOG_ERROR_USER("At least 1 argument required."); diff --git a/src/cmd.h b/src/cmd.h index 0b70ee5..4eb0f13 100644 --- a/src/cmd.h +++ b/src/cmd.h @@ -18,7 +18,18 @@ struct CommandLineArg { CmdType type; }; +struct ArgList { + struct CommandLineArg* args; + size_t capacity; + size_t size; +}; + +struct ArgList* supported_arg_list; + struct CommandLineArg* parse_args(const int argc, char* argv[]); +void init_cmd(void); +void create_arg_list(struct ArgList* list, size_t capacity); +void append_arg(struct ArgList* list, struct CommandLineArg arg); void usage(void); #endif /*__CMD_H__*/ \ No newline at end of file