University stuff.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

argparser.c 777B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include "argparser.h"
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. void argparser_parse(char **buf, int max, char *str)
  7. {
  8. // Clear buf array
  9. for (int i = 0; i < max; ++i)
  10. buf[i] = NULL;
  11. int argc = 0;
  12. char c;
  13. for (int i = 0; (c = str[i]) != '\0'; ++i)
  14. {
  15. // Strings in quotes
  16. if (c == '"')
  17. {
  18. int start = i + 1;
  19. do ++i; while (str[i] != '\0' && str[i] != '"');
  20. buf[argc++] = str + start;
  21. if (str[i] == '\0')
  22. break;
  23. else
  24. str[i] = '\0';
  25. }
  26. // String without quotes
  27. else if (!isspace(str[i]))
  28. {
  29. int start = i;
  30. while (str[i] != '\0' && !isspace(str[i])) i += 1;
  31. buf[argc++] = str + start;
  32. if (str[i] == '\0')
  33. break;
  34. else
  35. str[i] = '\0';
  36. }
  37. if (argc >= max)
  38. break;
  39. }
  40. }