12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- #include "argparser.h"
-
- #include <stdlib.h>
- #include <ctype.h>
-
- #include <stdio.h>
- #include <string.h>
-
- void argparser_parse(char **buf, int max, char *str)
- {
- // Clear buf array
- for (int i = 0; i < max; ++i)
- buf[i] = NULL;
-
- int argc = 0;
-
- char c;
- for (int i = 0; (c = str[i]) != '\0'; ++i)
- {
- // Strings in quotes
- if (c == '"')
- {
- int start = i + 1;
- do ++i; while (str[i] != '\0' && str[i] != '"');
- buf[argc++] = str + start;
-
- if (str[i] == '\0')
- break;
- else
- str[i] = '\0';
- }
-
- // String without quotes
- else if (!isspace(str[i]))
- {
- int start = i;
- while (str[i] != '\0' && !isspace(str[i])) i += 1;
- buf[argc++] = str + start;
-
- if (str[i] == '\0')
- break;
- else
- str[i] = '\0';
- }
-
- if (argc >= max)
- break;
- }
- }
|