cmd #1: introduce ArgList struct

This commit is contained in:
tylen 2024-09-23 17:06:23 +00:00 committed by Vasily Davydov
parent e5483e68ba
commit 9d40f081ac
3 changed files with 41 additions and 0 deletions

View File

@ -3,6 +3,7 @@
#include "cmd.h"
int main (int argc, char *argv[]) {
init_cmd();
struct CommandLineArg* args = parse_args(argc, argv);
return 0;
}

View File

@ -3,6 +3,7 @@
#include "tools/buffer.h"
#include <stdlib.h>
#include <string.h>
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.");

View File

@ -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__*/