mirror of
https://github.com/tailix/libkernaux.git
synced 2024-11-13 11:04:27 -05:00
Some basic implementation of command line parser
This commit is contained in:
parent
48a88f1abf
commit
67aba6718f
1 changed files with 29 additions and 0 deletions
|
@ -31,5 +31,34 @@ kernaux_bool kernaux_cmdline_parse(
|
|||
|
||||
kernaux_memset(buffer, '\0', argv_count_max * arg_size_max);
|
||||
|
||||
if (cmdline[0] == '\0') {
|
||||
return KERNAUX_TRUE;
|
||||
}
|
||||
|
||||
unsigned int start = 0;
|
||||
|
||||
for (unsigned int index = 1; ; ++index) {
|
||||
const char prev = cmdline[index - 1];
|
||||
const char cur = cmdline[index];
|
||||
|
||||
if ((cur == ' ' || cur == '\0') && prev != ' ') {
|
||||
const unsigned size = index - start + 1;
|
||||
|
||||
// TODO: check size
|
||||
|
||||
argv[(*argc)++] = buffer;
|
||||
kernaux_strncpy(buffer, &cmdline[start], size - 1);
|
||||
buffer += size;
|
||||
}
|
||||
|
||||
if (prev == ' ' && cur != ' ' && cur != '\0') {
|
||||
start = index;
|
||||
}
|
||||
|
||||
if (cur == '\0') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return KERNAUX_TRUE;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue