home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Spezial / SPEZIAL2_97.zip / SPEZIAL2_97.iso / ANWEND / ONLINE / IJB20OS2 / SOURCE / SSPLIT.C < prev    next >
C/C++ Source or Header  |  1997-09-16  |  2KB  |  104 lines

  1. char *ssplit_rcs = "$Id: ssplit.c,v 1.2 1997/04/20 17:06:18 ACJC Exp $";
  2. /* Written and copyright by the Anonymous Coders and Junkbusters Corporation.
  3.  * Will be made available under the GNU General Public License.
  4.  * This software comes with NO WARRANTY.
  5.  */
  6.  
  7. /* ssplit() - split a string (in-place) into fields
  8.  *      s = string to split
  9.  *      c = list of characters to be used as field separators
  10.  *          (if NULL, use default separators of space, tab, and newline)
  11.  *
  12.  *      v = vector into which field pointers are placed
  13.  *      n = number of fields in vector
  14.  *
  15.  *      m = flag indicating whether to treat strings of field
  16.  *          separators as indicating multiple fields
  17.  *
  18.  *      l = flag indicating whether to ignore leading field separators
  19.  */
  20.  
  21. #include <string.h>
  22.  
  23. int ssplit(char *s, char *c, char *v[], int n, int m, int l)
  24. {
  25.     static char t[256];
  26.     static char *x[1024];
  27.     unsigned char *p, b;
  28.     int xi = 0;
  29.     int vi = 0;
  30.     int i;
  31.     int last_was_null;
  32.  
  33.     if (!s)
  34.     return (-1);
  35.  
  36.     memset(t, '\0', sizeof(t));
  37.  
  38.     p = (unsigned char *) c;
  39.  
  40.     if (!p)
  41.     p = (unsigned char *) " \t";    /* default field separators */
  42.  
  43.     while (*p)
  44.     t[*p++] = 1;    /* separator  */
  45.  
  46.     t['\0'] = 2;    /* terminator */
  47.     t['\n'] = 2;    /* terminator */
  48.  
  49.     p = (unsigned char *) s;
  50.  
  51.     if(l) {    /* are we to skip leading separators ? */
  52.         while((b = t[*p]) != 2) {
  53.         if(b != 1) break;
  54.         p++;
  55.     }
  56.     }
  57.  
  58.     x[xi++] = (char *) p;    /* first pointer is the beginning of string */
  59.  
  60.     /* first pass:  save pointers to the field separators */
  61.     while((b = t[*p]) != 2) {
  62.         if(b == 1) {        /* if the char is a separator ... */
  63.         *p++    = '\0';    /* null terminate the substring */
  64.         x[xi++] = (char *) p;    /* save pointer to beginning of next string */
  65.     } else {
  66.         p++;
  67.     }
  68.     }
  69.     *p = '\0';        /* null terminate the substring */
  70.  
  71. #ifdef DEBUG
  72. print(x, xi); /* debugging */
  73. #endif
  74.  
  75.     /* second pass: copy the relevant pointers to the output vector */
  76.     last_was_null = 0;
  77.     for(i=0 ; i < xi; i++) {
  78.     if(m) {
  79.         /* there are NO null fields */
  80.         if(*x[i] == 0) continue;
  81.     }
  82.     if(vi < n) {
  83.         v[vi++] = x[i];
  84.     } else {
  85.         return(-1);    /* overflow */
  86.     }
  87.     }
  88. #ifdef DEBUG
  89. print(v, vi); /* debugging  */
  90. #endif
  91.     return (vi);
  92. }
  93.  
  94. #ifdef DEBUG
  95. print(char **v, int n)
  96. {
  97.     int i;
  98.     printf("dump %d strings\n", n);
  99.     for(i=0; i < n; i++) {
  100.         printf("%d '%s'\n", i, v[i]);
  101.     }
  102. }
  103. #endif
  104.