home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Spezial
/
SPEZIAL2_97.zip
/
SPEZIAL2_97.iso
/
ANWEND
/
ONLINE
/
IJB20OS2
/
SOURCE
/
SSPLIT.C
< prev
next >
Wrap
C/C++ Source or Header
|
1997-09-16
|
2KB
|
104 lines
char *ssplit_rcs = "$Id: ssplit.c,v 1.2 1997/04/20 17:06:18 ACJC Exp $";
/* Written and copyright by the Anonymous Coders and Junkbusters Corporation.
* Will be made available under the GNU General Public License.
* This software comes with NO WARRANTY.
*/
/* ssplit() - split a string (in-place) into fields
* s = string to split
* c = list of characters to be used as field separators
* (if NULL, use default separators of space, tab, and newline)
*
* v = vector into which field pointers are placed
* n = number of fields in vector
*
* m = flag indicating whether to treat strings of field
* separators as indicating multiple fields
*
* l = flag indicating whether to ignore leading field separators
*/
#include <string.h>
int ssplit(char *s, char *c, char *v[], int n, int m, int l)
{
static char t[256];
static char *x[1024];
unsigned char *p, b;
int xi = 0;
int vi = 0;
int i;
int last_was_null;
if (!s)
return (-1);
memset(t, '\0', sizeof(t));
p = (unsigned char *) c;
if (!p)
p = (unsigned char *) " \t"; /* default field separators */
while (*p)
t[*p++] = 1; /* separator */
t['\0'] = 2; /* terminator */
t['\n'] = 2; /* terminator */
p = (unsigned char *) s;
if(l) { /* are we to skip leading separators ? */
while((b = t[*p]) != 2) {
if(b != 1) break;
p++;
}
}
x[xi++] = (char *) p; /* first pointer is the beginning of string */
/* first pass: save pointers to the field separators */
while((b = t[*p]) != 2) {
if(b == 1) { /* if the char is a separator ... */
*p++ = '\0'; /* null terminate the substring */
x[xi++] = (char *) p; /* save pointer to beginning of next string */
} else {
p++;
}
}
*p = '\0'; /* null terminate the substring */
#ifdef DEBUG
print(x, xi); /* debugging */
#endif
/* second pass: copy the relevant pointers to the output vector */
last_was_null = 0;
for(i=0 ; i < xi; i++) {
if(m) {
/* there are NO null fields */
if(*x[i] == 0) continue;
}
if(vi < n) {
v[vi++] = x[i];
} else {
return(-1); /* overflow */
}
}
#ifdef DEBUG
print(v, vi); /* debugging */
#endif
return (vi);
}
#ifdef DEBUG
print(char **v, int n)
{
int i;
printf("dump %d strings\n", n);
for(i=0; i < n; i++) {
printf("%d '%s'\n", i, v[i]);
}
}
#endif