home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume11 / watcher / part01 / line_to_vec.c < prev    next >
C/C++ Source or Header  |  1987-09-27  |  2KB  |  90 lines

  1. /*
  2.    line_to_vec: take a string and separate it into a vector of strings,
  3.     splitting it at characters which are supplied in 'splits'.
  4.     Ignore any 'splits' at the beginning.  Multiple 'splits' are
  5.     condensed into one.  Splits are discarded.
  6.  
  7.    Assumptions:
  8.     line is null terminated.  
  9.     no single word is longer than MAX_STR.
  10.  
  11.    Arguments:
  12.     line: line to split.
  13.     vec: split line.
  14.     splits: array of characters on which to split.  Null terminated.
  15.  
  16.    Global data used:
  17.     none.
  18.    
  19.    Local variables:
  20.     len: length of 'word' so far.
  21.     name: current entry in the vector we are building.
  22.     word: pointer into name.
  23.  
  24.    Returns:
  25.     The number of vectors created; -1 if malloc fails.
  26.     The argument vec is left with a NULL pointer after the last word.
  27.  
  28.    Author:
  29.     Kenneth Ingham
  30.  
  31.    Date:
  32.     Thu Sep  5 13:59:21 MDT 1985
  33.  
  34.    Copyright (C) 1987 The University of New Mexico
  35. */
  36.  
  37. #include "defs.h"
  38.  
  39. line_to_vec(line, vec, splits)
  40. char *line, *vec[], *splits;
  41. {
  42.     register int i, v, j;
  43.     register unsigned len;
  44.     int n;
  45.     char word[MAX_STR];
  46.  
  47.     if (line == NULL || line[0] == '\0')
  48.         return 0;
  49.     
  50.     /* skip any splits in the beginning */
  51.     for (i=0; line[i] && index(splits, line[i]) != 0; i++)
  52.         ;
  53.     
  54.     j = 0;
  55.     len = 0;
  56.     v = 0;
  57.     n = 0;
  58.     while (line[i]) {
  59.         if (index(splits, line[i]) != 0) {
  60.             word[j] = '\0';
  61.             vec[v] = malloc(len+1);
  62.             if (vec[v] == NULL)
  63.                 return -1;
  64.             strcpy(vec[v], word);
  65.             j = 0;
  66.             len = 0;
  67.             v++;
  68.             n++;
  69.             i++;
  70.             for ( ; line[i] && index(splits, line[i]) != 0; i++)
  71.                 ;
  72.         }
  73.         else {
  74.             word[j++] = line[i++];
  75.             len++;
  76.         }
  77.     }
  78.  
  79.     if (index(splits, line[i]) != 0) {
  80.         word[j] = '\0';
  81.         vec[v] = malloc(len+1);
  82.         if (vec[v] == NULL)
  83.             return -1;
  84.         strcpy(vec[v], word);
  85.         n++;
  86.     }
  87.  
  88.     return n;
  89. }
  90.