s_parse.c 897 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include <u.h>
  2. #include <libc.h>
  3. #include "String.h"
  4. #define isspace(c) ((c)==' ' || (c)=='\t' || (c)=='\n')
  5. /* Get the next field from a String. The field is delimited by white space,
  6. * single or double quotes.
  7. */
  8. String *
  9. s_parse(String *from, String *to)
  10. {
  11. if (*from->ptr == '\0')
  12. return 0;
  13. if (to == 0)
  14. to = s_new();
  15. if (*from->ptr == '\'') {
  16. from->ptr++;
  17. for (;*from->ptr != '\'' && *from->ptr != '\0'; from->ptr++)
  18. s_putc(to, *from->ptr);
  19. if (*from->ptr == '\'')
  20. from->ptr++;
  21. } else if (*from->ptr == '"') {
  22. from->ptr++;
  23. for (;*from->ptr != '"' && *from->ptr != '\0'; from->ptr++)
  24. s_putc(to, *from->ptr);
  25. if (*from->ptr == '"')
  26. from->ptr++;
  27. } else {
  28. for (;!isspace(*from->ptr) && *from->ptr != '\0'; from->ptr++)
  29. s_putc(to, *from->ptr);
  30. }
  31. s_terminate(to);
  32. /* crunch trailing white */
  33. while(isspace(*from->ptr))
  34. from->ptr++;
  35. return to;
  36. }