]>
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.
10 #include "brkstring.h"
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
= 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
))
76 * If the character is in the string,
77 * return 1, else return 0.
81 brkany (char c
, char *str
)
83 return str
&& c
&& strchr(str
, c
);