]>
diplodocus.org Git - nmh/blob - sbr/brkstring.c
3 * brkstring.c -- (destructively) split a string into
4 * -- an array of substrings
11 /* allocate this number of pointers at a time */
14 static char **broken
= NULL
; /* array of substring start addresses */
15 static int len
= 0; /* current size of "broken" */
20 static int brkany (char, char *);
24 brkstring (char *str
, char *brksep
, char *brkterm
)
29 /* allocate initial space for pointers on first call */
32 if (!(broken
= (char **) malloc ((size_t) (len
* sizeof(*broken
)))))
33 adios (NULL
, "unable to malloc array in brkstring");
37 * scan string, replacing separators with zeroes
38 * and enter start addresses in "broken".
44 /* enlarge pointer array, if necessary */
47 if (!(broken
= realloc (broken
, (size_t) (len
* sizeof(*broken
)))))
48 adios (NULL
, "unable to realloc array in brkstring");
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
++)