3 * brkstring.c -- (destructively) split a string into
4 * -- an array of substrings
8 * This code is Copyright (c) 2002, by the authors of nmh. See the
9 * COPYRIGHT file in the root directory of the nmh distribution for
10 * complete copyright information.
16 /* allocate this number of pointers at a time */
19 static char **broken = NULL; /* array of substring start addresses */
20 static int len = 0; /* current size of "broken" */
25 static int brkany (char, char *);
29 brkstring (char *str, char *brksep, char *brkterm)
34 /* allocate initial space for pointers on first call */
37 broken = (char **) mh_xmalloc ((size_t) (len * sizeof(*broken)));
41 * scan string, replacing separators with zeroes
42 * and enter start addresses in "broken".
48 /* enlarge pointer array, if necessary */
51 broken = mh_xrealloc (broken, (size_t) (len * sizeof(*broken)));
54 while (brkany (c = *s, brksep))
58 * we are either at the end of the string, or the
59 * terminator found has been found, so finish up.
61 if (!c || brkany (c, brkterm)) {
67 /* set next start addr */
70 while ((c = *++s) && !brkany (c, brksep) && !brkany (c, brkterm))
74 return broken; /* NOT REACHED */
79 * If the character is in the string,
80 * return 1, else return 0.
84 brkany (char c, char *str)
89 for (s = str; *s; s++)