3 * brkstring.c -- (destructively) split a string into
4 * -- an array of substrings
6 * This code is Copyright (c) 2002, by the authors of nmh. See the
7 * COPYRIGHT file in the root directory of the nmh distribution for
8 * complete copyright information.
14 /* allocate this number of pointers at a time */
17 static char **broken = NULL; /* array of substring start addresses */
18 static int len = 0; /* current size of "broken" */
23 static int brkany (char, char *);
27 brkstring (char *str, char *brksep, char *brkterm)
32 /* allocate initial space for pointers on first call */
35 broken = (char **) mh_xmalloc ((size_t) (len * sizeof(*broken)));
39 * scan string, replacing separators with zeroes
40 * and enter start addresses in "broken".
46 /* enlarge pointer array, if necessary */
49 broken = mh_xrealloc (broken, (size_t) (len * sizeof(*broken)));
52 while (brkany (c = *s, brksep))
56 * we are either at the end of the string, or the
57 * terminator found has been found, so finish up.
59 if (!c || brkany (c, brkterm)) {
65 /* set next start addr */
68 while ((c = *++s) && !brkany (c, brksep) && !brkany (c, brkterm))
72 return broken; /* NOT REACHED */
77 * If the character is in the string,
78 * return 1, else return 0.
82 brkany (char c, char *str)
87 for (s = str; *s; s++)