home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
unix
/
volume11
/
watcher
/
part01
/
line_to_vec.c
< prev
next >
Wrap
C/C++ Source or Header
|
1987-09-27
|
2KB
|
90 lines
/*
line_to_vec: take a string and separate it into a vector of strings,
splitting it at characters which are supplied in 'splits'.
Ignore any 'splits' at the beginning. Multiple 'splits' are
condensed into one. Splits are discarded.
Assumptions:
line is null terminated.
no single word is longer than MAX_STR.
Arguments:
line: line to split.
vec: split line.
splits: array of characters on which to split. Null terminated.
Global data used:
none.
Local variables:
len: length of 'word' so far.
name: current entry in the vector we are building.
word: pointer into name.
Returns:
The number of vectors created; -1 if malloc fails.
The argument vec is left with a NULL pointer after the last word.
Author:
Kenneth Ingham
Date:
Thu Sep 5 13:59:21 MDT 1985
Copyright (C) 1987 The University of New Mexico
*/
#include "defs.h"
line_to_vec(line, vec, splits)
char *line, *vec[], *splits;
{
register int i, v, j;
register unsigned len;
int n;
char word[MAX_STR];
if (line == NULL || line[0] == '\0')
return 0;
/* skip any splits in the beginning */
for (i=0; line[i] && index(splits, line[i]) != 0; i++)
;
j = 0;
len = 0;
v = 0;
n = 0;
while (line[i]) {
if (index(splits, line[i]) != 0) {
word[j] = '\0';
vec[v] = malloc(len+1);
if (vec[v] == NULL)
return -1;
strcpy(vec[v], word);
j = 0;
len = 0;
v++;
n++;
i++;
for ( ; line[i] && index(splits, line[i]) != 0; i++)
;
}
else {
word[j++] = line[i++];
len++;
}
}
if (index(splits, line[i]) != 0) {
word[j] = '\0';
vec[v] = malloc(len+1);
if (vec[v] == NULL)
return -1;
strcpy(vec[v], word);
n++;
}
return n;
}