[PVFS2-CVS]
commit by neill in pvfs2/src/common/misc: str-utils.c str-utils.h
CVS commit program
cvs at parl.clemson.edu
Fri Mar 19 10:59:19 EST 2004
Update of /projects/cvsroot/pvfs2/src/common/misc
In directory parlweb:/tmp/cvs-serv13980/src/common/misc
Modified Files:
str-utils.c str-utils.h
Log Message:
- consolidate the string split functions in flow/bmi by adding a new
str util function that does what they did
- some cleanups
Index: str-utils.c
===================================================================
RCS file: /projects/cvsroot/pvfs2/src/common/misc/str-utils.c,v
diff -p -u -r1.12 -r1.13
--- str-utils.c 16 Oct 2003 14:36:30 -0000 1.12
+++ str-utils.c 19 Mar 2004 15:59:19 -0000 1.13
@@ -336,6 +336,82 @@ int PINT_get_next_path(char *path, char
return(0);
}
+/*
+ * PINT_split_string_list()
+ *
+ * separates a comma delimited list of items into an array of strings
+ *
+ * returns the number of strings successfully parsed
+ */
+int PINT_split_string_list(char ***tokens, const char *comma_list)
+{
+
+ const char *holder = NULL;
+ const char *holder2 = NULL;
+ const char *end = NULL;
+ int tokencount = 1;
+ int i = -1;
+
+ if (!comma_list || !tokens)
+ {
+ return (0);
+ }
+
+ /* count how many commas we have first */
+ holder = comma_list;
+ while ((holder = index(holder, ',')))
+ {
+ tokencount++;
+ holder++;
+ }
+
+ /* allocate pointers for each */
+ *tokens = (char **) malloc(sizeof(char **));
+ if (!(*tokens))
+ {
+ return 0;
+ }
+
+ /* copy out all of the tokenized strings */
+ holder = comma_list;
+ end = comma_list + strlen(comma_list);
+ for (i = 0; i < tokencount; i++)
+ {
+ holder2 = index(holder, ',');
+ if (!holder2)
+ {
+ holder2 = end;
+ }
+ (*tokens)[i] = (char *) malloc((holder2 - holder) + 1);
+ if (!(*tokens)[i])
+ {
+ goto failure;
+ }
+ strncpy((*tokens)[i], holder, (holder2 - holder));
+ (*tokens)[i][(holder2 - holder)] = '\0';
+ holder = holder2 + 1;
+
+ }
+
+ return (tokencount);
+
+ failure:
+
+ /* free up any memory we allocated if we failed */
+ if (*tokens)
+ {
+ for (i = 0; i < tokencount; i++)
+ {
+ if ((*tokens)[i])
+ {
+ free((*tokens)[i]);
+ }
+ }
+ free(*tokens);
+ }
+ return (0);
+}
+
#ifndef HAVE_STRNLEN
/* a naive implementation of strnlen for systems w/o glibc */
size_t strnlen(const char *s, size_t limit)
Index: str-utils.h
===================================================================
RCS file: /projects/cvsroot/pvfs2/src/common/misc/str-utils.h,v
diff -p -u -r1.10 -r1.11
--- str-utils.h 22 Jan 2004 13:41:08 -0000 1.10
+++ str-utils.h 19 Mar 2004 15:59:19 -0000 1.11
@@ -33,6 +33,9 @@ int PINT_get_next_path(
char* path,
char** newpath,
int skip);
+int PINT_split_string_list(
+ char ***tokens,
+ const char *comma_list);
#ifndef HAVE_STRNLEN
size_t strnlen(const char *s, size_t limit);
More information about the PVFS2-CVS
mailing list