This patch adds a new function to libstrutil --

int parse_range(char *range, char ***toks);

parse_range accepts a range of the format typically used to specify node
ranges (for instance, foo[0-100])
and returns the individual node names as separate tokens.

Most of parse_range is stolen from libxcpu/node.c
This patch also removes the libspfs dependency of libstrutil.

Signed-off-by: Abhishek Kulkarni <[email protected]>

Index: include/strutil.h
===================================================================
--- include/strutil.h    (revision 752)
+++ include/strutil.h    (working copy)
@@ -26,3 +26,4 @@
 int tokenize(char *s, char ***ss);
 int cutstr(unsigned char *target, int toffset, int tcount, char *src, int
soffset);
 int cutbuf(unsigned char *target, int toffset, int tcount, char *src, int
soffset, int slen);
+int parse_range(char *range, char ***toks);

Index: libstrutil/Makefile
===================================================================
--- libstrutil/Makefile    (revision 752)
+++ libstrutil/Makefile    (working copy)
@@ -2,15 +2,15 @@
 SYSNAME!=uname
 MULTILIBPATH=${shell test -d /lib64 && echo lib64 || echo lib}
 INCDIR=../include
-SPFSDIR=../spfs
-HFILES=$(SPFSDIR)/include/spfs.h $(INCDIR)/strutil.h
-CFLAGS=-Wall -g -I$(INCDIR) -I$(SPFSDIR)/include
+HFILES=$(INCDIR)/strutil.h
+CFLAGS=-Wall -g -I$(INCDIR)

 LIBFILES=\
     quotestrdup.o\
     unquotestr.o\
     tokenize.o\
     cutstr.o\
+    range.o\

 libstrutil.a: $(LIBFILES)
     ar rc libstrutil.a $(LIBFILES)

Index: libstrutil/range.c
===================================================================
--- libstrutil/range.c    (revision 0)
+++ libstrutil/range.c    (revision 0)
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2006 by Latchesar Ionkov <[email protected]>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
"Software"),
+ * to deal in the Software without restriction, including without
limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
next
+ * paragraph) shall be included in all copies or substantial portions of
the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * LATCHESAR IONKOV AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES
OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <ctype.h>
+#include "strutil.h"
+
+int
+parse_range(char *range, char ***toks)
+{
+    int n, b, e;
+    char *s, *t, *p, *ss;
+
+    s = range;
+    while (*s != '[') {
+        if (*s == '\0')
+            return tokenize(range, toks);
+        s++;
+    }
+
+    t = strchr(s, ']');
+    if (!t)
+        return -1;
+
+    if (*(t+1) != '\0')
+        return -1;
+
+    *t = '\0';
+    t = strchr(s, '-');
+    if (!t)
+        return -1;
+
+    *t = '\0';
+    t++;
+
+    b = strtol(s + 1, &ss, 10);
+    if (*ss != '\0')
+        return -1;
+
+    e = strtol(t, &ss, 10);
+    if (*ss != '\0')
+        return -1;
+
+    p = malloc(((s-range+1) + strlen(t)) * (e-b+1));
+    if (!p)
+        return -1;
+
+    *p = '\0';
+    for (n = b; n <= e; n++) {
+        strncat(p, range, (s-range));
+        sprintf(s, "%d ", n);
+        strncat(p, s, strlen(s));
+    }
+    n = tokenize(p, toks);
+    free(p);
+    return n;
+}

Index: libstrutil/tokenize.c
===================================================================
--- libstrutil/tokenize.c    (revision 752)
+++ libstrutil/tokenize.c    (working copy)
@@ -24,7 +24,6 @@
 #include <string.h>
 #include <stdio.h>
 #include <errno.h>
-#include "spfs.h"
 #include "strutil.h"

 int
@@ -43,26 +42,22 @@
             n++;

     toks = malloc((n+1)*sizeof(char *) + i + 1);
-    if (!toks) {
-        sp_werror(Enomem, ENOMEM);
-        return -1;
-    }
+    if (!toks)
+        return ENOMEM;

     p = (char *) toks + (n+1)*sizeof(char *);
     memmove(p, s, i + 1);

     for(i = 0; *p != '\0'; i++) {
         if (i >= n) {
-            sp_werror("internal error", EIO);
             free(toks);
-            return -1;
+            return EIO;
         }

         toks[i] = unquotestr(p, &e);
         if (!toks[i]) {
-            sp_werror("invalid format", EIO);
             free(toks);
-            return -1;
+            return EIO;
         }

         p = e;

Reply via email to