sf_split(3)

NAME
split, splitf, splitquotable, sjoin, join, split_network - split and join functions

SYNOPSIS
#include <strfunc.h>

Split string and add tokens to the string array
svect *
split(char *string, char *delim, int flags);
int
splitf(svect *, char *string, char *delim, int flags);
int
splitquotable(svect *, char *string);

Join the array tokens together
char *
join(char **, char *delimiter);
char *
sjoin(svect *, char *delimiter);

Stand-alone network/mask splitting function
int
split_network(const char *ip_mask, unsigned int *ip, unsigned int *mask);

DESCRIPTION
These routines allows to split or join strings by specified tokens.

int splitf(svect *, char *string, char *delim, int flags) is the basic splitting function. string specifies the source string. delim specifies the delimiter to be used to split string into tokens. flags is the bitwise OR of the following values:

1   two or more joined delimiters will be considered to be distinct instead of assuming them as one delimiter and skipping.

2   delimiter will be considered as the pointer to delimiting string instead of assuming it as the pointer to character set.

4   delimiter is the regular expression. Regular expression can be prefixed with and followed by /'es if there is a need to put some flags after it. Refer to sf_sed(3) to obtain a flags and additional information about regular expressions. Setting this flag automaticaly discards 1 & 2.

Return value is the number of tokens recognized and added to the vector, or -1 in case of an error caused by memory shortage or regular expressions handling failure.

svect * split(char *string, char *set, int flags) is identical to the splitf() in one exception that it will create a new svect * structure for you. The structure will always be allocated, but it can contain no data if splitf() will be unable to find any tokens.

int splitquotable(svect *sl, char *string) is the function that recognizes single and double quotes and splits string according to them. There is a small hack that allows to know whether this or that piece of string (token) was originally inside the double or single quotes. The character (byte) right after the end of string within the list sl->list[N][sl->lens[N] + 1] will contain the ' ', '1' or '2' if the appropriate token was originaly found within the unquoted text, single or double quotes, respectively.

To join vector elements together, sjoin(svect *, char *delimiter) function may be called. Another call, join(char **list, char *delimiter), may be required to join the char ** values together without using an intermediate svect * structure.

int split_network(const char *ip_mask, unsigned int *ip, unsigned int *mask) used to split IP/Mask strings to binary IP address and mask. Both ip and mask arguments should be supplied. They will be filled with recognized ip address and mask in network byte order. Refer to byteorder(3) or ntohl(3). split_network() regognizes the standart forms of ip address/mask pairs:

    	a.b.c.d/masklen
    	a.b.c.d/0xHEXMASK
    	a.b.c.d 0xHEXMASK
    	a.b.c.d/a.b.c.d
    	a.b.c.d a.b.c.d
    	or, more common
    	a[.[b[.c[.d]]]]/a.b[.c[.d]]]
    	and
    	a[.[b[.c[.d]]]] a.b[.c[.d]]]

EXAMPLES

Example split function...

    void removeMultipleSpaces(char *string) {
    	svect *sl;
    	/* Split string="some   bad   string"
    	 * and add tokens to the vector
    	 */
    	sl = split(string, NULL, 0);
    	/* And join them together to form
    	 * "some bad string"
    	 */
    	printf("%s\n", sjoin(sl, " "));
    	/* Destroy the string vector */
    	sfree(sl);
    };

    Split according to quotes

        void quotes(char *string) {
        	svect *sl;
        	int i;
        	/* Create new string vector */
        	sl = sinit();
        	/* Split string=" one 'two three' four
        	 * and add tokens to the vector
        	 */
        	sl = splitquotable(sl, string);
        	/* And join them together to form
        	 * "[one], [two three], [four], [five]"
        	 */
        	printf("[%s]\n", sjoin(sl, "], ["));
        	/* Single or double quoted? */
        	for(i = 0; i < sl->count; i++)
        		printf("%s: %s\n", sl->list[i],
        			(sl->list[i][sl->lens[i] + 1] == 0)?"plain text":
        			((sl->list[i][sl->lens[i] + 1] == 1)?"single quoted":
        			((sl->list[i][sl->lens[i] + 1] == 2)?"double quoted"))
        		);
        	/* Destroy the list */
        	sfree(sl);
        };

SEE ALSO
strfunc(3), sf_svect(3), sf_sed(3), byteorder(3).

AUTHORS
Lev Walkin <vlm@spelio.net.ru>