cmd #1: introduce ArgList struct
This commit is contained in:
parent
e5483e68ba
commit
9d40f081ac
@ -3,6 +3,7 @@
|
||||
#include "cmd.h"
|
||||
|
||||
int main (int argc, char *argv[]) {
|
||||
init_cmd();
|
||||
struct CommandLineArg* args = parse_args(argc, argv);
|
||||
return 0;
|
||||
}
|
||||
|
||||
29
src/cmd.c
29
src/cmd.c
@ -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.");
|
||||
|
||||
11
src/cmd.h
11
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__*/
|
||||
Loading…
x
Reference in New Issue
Block a user