chiark / gitweb /
Add new `rest' argument for `str_split'.
authormdw <mdw>
Wed, 26 May 1999 20:52:57 +0000 (20:52 +0000)
committermdw <mdw>
Wed, 26 May 1999 20:52:57 +0000 (20:52 +0000)
str.c
str.h

diff --git a/str.c b/str.c
index 51a980501800948a85abf8c0da22cb473244d2af..b6e481309ab8ff31b6c03d9946b41a8ca67f3241 100644 (file)
--- a/str.c
+++ b/str.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: str.c,v 1.1 1999/05/17 20:37:01 mdw Exp $
+ * $Id: str.c,v 1.2 1999/05/26 20:52:57 mdw Exp $
  *
  * Functions for hacking with strings
  *
@@ -30,6 +30,9 @@
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: str.c,v $
+ * Revision 1.2  1999/05/26 20:52:57  mdw
+ * Add new `rest' argument for `str_split'.
+ *
  * Revision 1.1  1999/05/17 20:37:01  mdw
  * Some trivial string hacks.
  *
@@ -83,6 +86,7 @@ char *str_getword(char **pp)
  * Arguments:  @char *p@ = pointer to string
  *             @char *v[]@ = pointer to array to fill in
  *             @size_t c@ = count of strings to fill in
+ *             @char **rest@ = where to store the remainder of the string
  *
  * Returns:    Number of strings filled in.
  *
@@ -92,10 +96,12 @@ char *str_getword(char **pp)
  *             and trailing space stripped off.  No more than @c@ words
  *             are read; the actual number is returned as the value of the
  *             function.  Unused slots in the array are populated with
- *             null bytes.
+ *             null bytes.  If there's any string left, the address of the
+ *             remainder is stored in @rest@ (if it's non-null); otherwise
+ *             @rest@ is set to a null pointer.
  */
 
-size_t str_split(char *p, char *v[], size_t c)
+size_t str_split(char *p, char *v[], size_t c, char **rest)
 {
   size_t n = 0;
   char *q;
@@ -110,6 +116,19 @@ size_t str_split(char *p, char *v[], size_t c)
     *v++ = 0;
     c--;
   }
+
+  if (rest) {
+    if (!p)
+      *rest = 0;
+    else {
+      while (isspace((unsigned char)*p))
+       p++;
+      if (*p)
+       *rest = p;
+      else
+       *rest = 0;
+    }
+  }
   return (n);
 }
 
diff --git a/str.h b/str.h
index ee33d112d14df1825ee8bcfee5828b0cc3c02b38..9854a1b8651f27ee2bca31c9735fca3340268f4f 100644 (file)
--- a/str.h
+++ b/str.h
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: str.h,v 1.1 1999/05/17 20:37:01 mdw Exp $
+ * $Id: str.h,v 1.2 1999/05/26 20:52:57 mdw Exp $
  *
  * Functions for hacking with strings
  *
@@ -30,6 +30,9 @@
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: str.h,v $
+ * Revision 1.2  1999/05/26 20:52:57  mdw
+ * Add new `rest' argument for `str_split'.
+ *
  * Revision 1.1  1999/05/17 20:37:01  mdw
  * Some trivial string hacks.
  *
@@ -65,6 +68,7 @@ extern char *str_getword(char **/*pp*/);
  * Arguments:  @char *p@ = pointer to string
  *             @char *v[]@ = pointer to array to fill in
  *             @size_t c@ = count of strings to fill in
+ *             @char **rest@ = where to store the remainder of the string
  *
  * Returns:    Number of strings filled in.
  *
@@ -74,10 +78,13 @@ extern char *str_getword(char **/*pp*/);
  *             and trailing space stripped off.  No more than @c@ words
  *             are read; the actual number is returned as the value of the
  *             function.  Unused slots in the array are populated with
- *             null bytes.
+ *             null bytes.  If there's any string left, the address of the
+ *             remainder is stored in @rest@ (if it's non-null); otherwise
+ *             @rest@ is set to a null pointer.
  */
 
-extern size_t str_split(char */*p*/, char */*v*/[], size_t /*c*/);
+extern size_t str_split(char */*p*/, char */*v*/[],
+                       size_t /*c*/, char **/*rest*/);
 
 /* --- @str_sanitize@ --- *
  *