]>
diplodocus.org Git - nmh/blob - sbr/brkstring.c
1 /* brkstring.c -- (destructively) split a string into
2 * -- an array of substrings
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
12 /* allocate this number of pointers at a time */
15 static char **broken
= NULL
; /* array of substring start addresses */
16 static int len
= 0; /* current size of "broken" */
21 static int brkany (char, char *);
25 brkstring (char *str
, char *brksep
, char *brkterm
)
30 /* allocate initial space for pointers on first call */
33 broken
= mh_xmalloc ((size_t) (len
* sizeof(*broken
)));
37 * scan string, replacing separators with zeroes
38 * and enter start addresses in "broken".
44 /* enlarge pointer array, if necessary */
47 broken
= mh_xrealloc (broken
, (size_t) (len
* sizeof(*broken
)));
50 while (brkany (c
= *s
, brksep
))
54 * we are either at the end of the string, or the
55 * terminator found has been found, so finish up.
57 if (!c
|| brkany (c
, brkterm
)) {
63 /* set next start addr */
66 while ((c
= *++s
) && !brkany (c
, brksep
) && !brkany (c
, brkterm
))
75 * If the character is in the string,
76 * return 1, else return 0.
80 brkany (char c
, char *str
)
82 return str
&& c
&& strchr(str
, c
);