Add an argument parser

This commit is contained in:
Alex 2023-03-25 12:02:26 +02:00
parent 7218575b48
commit 9718defcaa
Signed by untrusted user who does not match committer: enderice2
GPG Key ID: EACC3AD603BAB4DD
2 changed files with 45 additions and 0 deletions

30
Library/targp.c Normal file
View File

@ -0,0 +1,30 @@
#include <targp.h>
#include <memory.hpp>
#include <convert.h>
void targp_parse(const char *cmd, char **argv, int *argc)
{
const char delim[] = " ";
char *token = strtok((char *)cmd, delim);
while (token != NULL)
{
char *quote = strchr(token, '"');
if (quote != NULL)
{
memmove(quote, quote + 1, strlen(quote));
char *endQuote = strchr(quote, '"');
if (endQuote != NULL)
*endQuote = '\0';
}
char *arg = (char *)kmalloc(strlen(token) + 1);
strcpy(arg, token);
argv[(*argc)++] = arg;
token = strtok(NULL, delim);
}
argv[(*argc)] = NULL;
}

15
include/targp.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef TinyArgumentParser_H__
#define TinyArgumentParser_H__
#ifdef __cplusplus
extern "C"
{
#endif
void targp_parse(const char *cmd, char **argv, int *argc);
#ifdef __cplusplus
}
#endif
#endif // !TinyArgumentParser_H__