s_parse.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include "String.h"
  12. #define isspace(c) ((c)==' ' || (c)=='\t' || (c)=='\n')
  13. /* Get the next field from a String. The field is delimited by white space,
  14. * single or double quotes.
  15. */
  16. String *
  17. s_parse(String *from, String *to)
  18. {
  19. if (*from->ptr == '\0')
  20. return 0;
  21. if (to == 0)
  22. to = s_new();
  23. if (*from->ptr == '\'') {
  24. from->ptr++;
  25. for (;*from->ptr != '\'' && *from->ptr != '\0'; from->ptr++)
  26. s_putc(to, *from->ptr);
  27. if (*from->ptr == '\'')
  28. from->ptr++;
  29. } else if (*from->ptr == '"') {
  30. from->ptr++;
  31. for (;*from->ptr != '"' && *from->ptr != '\0'; from->ptr++)
  32. s_putc(to, *from->ptr);
  33. if (*from->ptr == '"')
  34. from->ptr++;
  35. } else {
  36. for (;!isspace(*from->ptr) && *from->ptr != '\0'; from->ptr++)
  37. s_putc(to, *from->ptr);
  38. }
  39. s_terminate(to);
  40. /* crunch trailing white */
  41. while(isspace(*from->ptr))
  42. from->ptr++;
  43. return to;
  44. }