plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

s_parse.c (913B)


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