#include "gpk_std.h"
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include "gpklib.h"
#include <string.h>

 /*VARARGS*/
int csplitcount(char **p)  /* like strlen, but counts pointer sized things. */
{
 char **q, **p0=p;
 assert(p);

 for(q=p0;*q;q++)
	;
 return q-p0;
}




char **csplit(char *s, C char *c)
{
 unsigned ccount;
 C char *q, *pe;
 char *p;
 char **r;
 unsigned i;
 TBOOL isc[256];

 if(!s)	return (char **)0;

 /* Set up a table of separator characters: */
 memset(isc, 0, sizeof(isc));
 for(q=c; *q; q++)
 	isc[(unsigned)(*q)] = 1;

 /* Figure out how much space to allocate for the return value,
 and replace separator characters with nulls. */
 for(ccount=0,p=s; *p; p++)
	if(isc[(unsigned)(*p)])	{
		*p = '\0';
		ccount++;
		}
 ccount++;	/* for the original terminating null */
 pe = p;
 r = (char **)malloc((unsigned)sizeof(char *) * (ccount+1));
 if(!r)	return 0;

 p=s; i=0;
 if(*p)	r[i++] = p;
 p++;
 for(;p<pe;p++)
	if(*(p-1)=='\0' && *p)
		r[i++] = p;
 r[i++] = (char *)NULL;
 assert(i <= ccount+1);
 return r;
}



#ifdef TEST
main(argc, argv)
 int argc;
 char **argv;
{
 char **csplit(), *i, **j;

 j = csplit(argv[1], '/');
 while(*j)
	printf("%s%c", *(j++), '+');
 printf("\n");
 exit(0);
}
#endif
