2 * brkstring.c -- (destructively) split a string into
3 * -- an array of substrings
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
13 /* allocate this number of pointers at a time */
16 static char **broken = NULL; /* array of substring start addresses */
17 static int len = 0; /* current size of "broken" */
22 static int brkany (char, char *);
26 brkstring (char *str, char *brksep, char *brkterm)
31 /* allocate initial space for pointers on first call */
34 broken = (char **) mh_xmalloc ((size_t) (len * sizeof(*broken)));
38 * scan string, replacing separators with zeroes
39 * and enter start addresses in "broken".
45 /* enlarge pointer array, if necessary */
48 broken = mh_xrealloc (broken, (size_t) (len * sizeof(*broken)));
51 while (brkany (c = *s, brksep))
55 * we are either at the end of the string, or the
56 * terminator found has been found, so finish up.
58 if (!c || brkany (c, brkterm)) {
64 /* set next start addr */
67 while ((c = *++s) && !brkany (c, brksep) && !brkany (c, brkterm))
71 return broken; /* NOT REACHED */
76 * If the character is in the string,
77 * return 1, else return 0.
81 brkany (char c, char *str)
86 for (s = str; *s; s++)