aboutsummaryrefslogtreecommitdiff
path: root/src/libString/s_parse.c
diff options
context:
space:
mode:
authorrsc <devnull@localhost>2003-12-11 18:15:57 +0000
committerrsc <devnull@localhost>2003-12-11 18:15:57 +0000
commit7f11104a5737adf261d10bc1a7b85e740f2eb491 (patch)
tree2473f5fbdd42b74b189e2fcc2a46896a50ede030 /src/libString/s_parse.c
parent57ccfb9e8f51138c33ad5f58e14c0e54246cf5c4 (diff)
downloadplan9port-7f11104a5737adf261d10bc1a7b85e740f2eb491.tar.gz
plan9port-7f11104a5737adf261d10bc1a7b85e740f2eb491.tar.bz2
plan9port-7f11104a5737adf261d10bc1a7b85e740f2eb491.zip
Add libString.
Diffstat (limited to 'src/libString/s_parse.c')
-rw-r--r--src/libString/s_parse.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/libString/s_parse.c b/src/libString/s_parse.c
new file mode 100644
index 00000000..376a41e2
--- /dev/null
+++ b/src/libString/s_parse.c
@@ -0,0 +1,40 @@
+#include <u.h>
+#include <libc.h>
+#include "libString.h"
+
+#define isspace(c) ((c)==' ' || (c)=='\t' || (c)=='\n')
+
+/* Get the next field from a String. The field is delimited by white space,
+ * single or double quotes.
+ */
+String *
+s_parse(String *from, String *to)
+{
+ if (*from->ptr == '\0')
+ return 0;
+ if (to == 0)
+ to = s_new();
+ if (*from->ptr == '\'') {
+ from->ptr++;
+ for (;*from->ptr != '\'' && *from->ptr != '\0'; from->ptr++)
+ s_putc(to, *from->ptr);
+ if (*from->ptr == '\'')
+ from->ptr++;
+ } else if (*from->ptr == '"') {
+ from->ptr++;
+ for (;*from->ptr != '"' && *from->ptr != '\0'; from->ptr++)
+ s_putc(to, *from->ptr);
+ if (*from->ptr == '"')
+ from->ptr++;
+ } else {
+ for (;!isspace(*from->ptr) && *from->ptr != '\0'; from->ptr++)
+ s_putc(to, *from->ptr);
+ }
+ s_terminate(to);
+
+ /* crunch trailing white */
+ while(isspace(*from->ptr))
+ from->ptr++;
+
+ return to;
+}