home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / cnews.tar / libc / split.c < prev    next >
C/C++ Source or Header  |  1991-09-05  |  7KB  |  316 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. /*
  5.  * split - divide a string into fields, like awk split()
  6.  */
  7. int                /* number of fields, including overflow */
  8. split(string, fields, nfields, sep)
  9. char *string;
  10. char *fields[];            /* list is not NULL-terminated */
  11. int nfields;            /* number of entries available in fields[] */
  12. char *sep;            /* "" white, "c" single char, "ab" [ab]+ */
  13. {
  14.     register char *p = string;
  15.     register char c;            /* latest character */
  16.     register char sepc = sep[0];
  17.     register char sepc2;
  18.     register int fn;
  19.     register char **fp = fields;
  20.     register char *sepp;
  21.     register int trimtrail;
  22.  
  23.     /* white space */
  24.     if (sepc == '\0') {
  25.         while ((c = *p++) == ' ' || c == '\t')
  26.             continue;
  27.         p--;
  28.         trimtrail = 1;
  29.         sep = " \t";    /* note, code below knows this is 2 long */
  30.         sepc = ' ';
  31.     } else
  32.         trimtrail = 0;
  33.     sepc2 = sep[1];        /* now we can safely pick this up */
  34.  
  35.     /* catch empties */
  36.     if (*p == '\0')
  37.         return(0);
  38.  
  39.     /* single separator */
  40.     if (sepc2 == '\0') {
  41.         fn = nfields;
  42.         for (;;) {
  43.             *fp++ = p;
  44.             fn--;
  45.             if (fn == 0)
  46.                 break;
  47.             while ((c = *p++) != sepc)
  48.                 if (c == '\0')
  49.                     return(nfields - fn);
  50.             *(p-1) = '\0';
  51.         }
  52.         /* we have overflowed the fields vector -- just count them */
  53.         fn = nfields;
  54.         for (;;) {
  55.             while ((c = *p++) != sepc)
  56.                 if (c == '\0')
  57.                     return(fn);
  58.             fn++;
  59.         }
  60.         /* not reached */
  61.     }
  62.  
  63.     /* two separators */
  64.     if (sep[2] == '\0') {
  65.         fn = nfields;
  66.         for (;;) {
  67.             *fp++ = p;
  68.             fn--;
  69.             while ((c = *p++) != sepc && c != sepc2)
  70.                 if (c == '\0') {
  71.                     if (trimtrail && **(fp-1) == '\0')
  72.                         fn++;
  73.                     return(nfields - fn);
  74.                 }
  75.             if (fn == 0)
  76.                 break;
  77.             *(p-1) = '\0';
  78.             while ((c = *p++) == sepc || c == sepc2)
  79.                 continue;
  80.             p--;
  81.         }
  82.         /* we have overflowed the fields vector -- just count them */
  83.         fn = nfields;
  84.         while (c != '\0') {
  85.             while ((c = *p++) == sepc || c == sepc2)
  86.                 continue;
  87.             p--;
  88.             fn++;
  89.             while ((c = *p++) != '\0' && c != sepc && c != sepc2)
  90.                 continue;
  91.         }
  92.         /* might have to trim trailing white space */
  93.         if (trimtrail) {
  94.             p--;
  95.             while ((c = *--p) == sepc || c == sepc2)
  96.                 continue;
  97.             p++;
  98.             if (*p != '\0') {
  99.                 if (fn == nfields+1)
  100.                     *p = '\0';
  101.                 fn--;
  102.             }
  103.         }
  104.         return(fn);
  105.     }
  106.  
  107.     /* n separators */
  108.     fn = 0;
  109.     for (;;) {
  110.         if (fn < nfields)
  111.             *fp++ = p;
  112.         fn++;
  113.         for (;;) {
  114.             c = *p++;
  115.             if (c == '\0')
  116.                 return(fn);
  117.             sepp = sep;
  118.             while ((sepc = *sepp++) != '\0' && sepc != c)
  119.                 continue;
  120.             if (sepc != '\0')    /* it was a separator */
  121.                 break;
  122.         }
  123.         if (fn < nfields)
  124.             *(p-1) = '\0';
  125.         for (;;) {
  126.             c = *p++;
  127.             sepp = sep;
  128.             while ((sepc = *sepp++) != '\0' && sepc != c)
  129.                 continue;
  130.             if (sepc == '\0')    /* it wasn't a separator */
  131.                 break;
  132.         }
  133.         p--;
  134.     }
  135.  
  136.     /* not reached */
  137. }
  138.  
  139. #ifdef TEST_SPLIT
  140.  
  141.  
  142. /*
  143.  * test program
  144.  * pgm        runs regression
  145.  * pgm sep    splits stdin lines by sep
  146.  * pgm str sep    splits str by sep
  147.  * pgm str sep n    splits str by sep n times
  148.  */
  149. int
  150. main(argc, argv)
  151. int argc;
  152. char *argv[];
  153. {
  154.     char buf[512];
  155.     register int n;
  156. #    define    MNF    10
  157.     char *fields[MNF];
  158.  
  159.     if (argc > 4)
  160.         for (n = atoi(argv[3]); n > 0; n--) {
  161.             (void) strcpy(buf, argv[1]);
  162.         }
  163.     else if (argc > 3)
  164.         for (n = atoi(argv[3]); n > 0; n--) {
  165.             (void) strcpy(buf, argv[1]);
  166.             (void) split(buf, fields, MNF, argv[2]);
  167.         }
  168.     else if (argc > 2)
  169.         dosplit(argv[1], argv[2]);
  170.     else if (argc > 1)
  171.         while (fgets(buf, sizeof(buf), stdin) != NULL) {
  172.             buf[strlen(buf)-1] = '\0';    /* stomp newline */
  173.             dosplit(buf, argv[1]);
  174.         }
  175.     else
  176.         regress();
  177.  
  178.     exit(0);
  179. }
  180.  
  181. dosplit(string, seps)
  182. char *string;
  183. char *seps;
  184. {
  185. #    define    NF    5
  186.     char *fields[NF];
  187.     register int nf;
  188.  
  189.     nf = split(string, fields, NF, seps);
  190.     print(nf, NF, fields);
  191. }
  192.  
  193. print(nf, nfp, fields)
  194. int nf;
  195. int nfp;
  196. char *fields[];
  197. {
  198.     register int fn;
  199.     register int bound;
  200.  
  201.     bound = (nf > nfp) ? nfp : nf;
  202.     printf("%d:\t", nf);
  203.     for (fn = 0; fn < bound; fn++)
  204.         printf("\"%s\"%s", fields[fn], (fn+1 < nf) ? ", " : "\n");
  205. }
  206.  
  207. #define    RNF    5        /* some table entries know this */
  208. struct {
  209.     char *str;
  210.     char *seps;
  211.     int nf;
  212.     char *fi[RNF];
  213. } tests[] = {
  214.     "",        " ",    0,    { "" },
  215.     " ",        " ",    2,    { "", "" },
  216.     "x",        " ",    1,    { "x" },
  217.     "xy",        " ",    1,    { "xy" },
  218.     "x y",        " ",    2,    { "x", "y" },
  219.     "abc def  g ",    " ",    5,    { "abc", "def", "", "g", "" },
  220.     "  a bcd",    " ",    4,    { "", "", "a", "bcd" },
  221.     "a b c d e f",    " ",    6,    { "a", "b", "c", "d", "e f" },
  222.     " a b c d ",    " ",    6,    { "", "a", "b", "c", "d " },
  223.  
  224.     "",        " _",    0,    { "" },
  225.     " ",        " _",    2,    { "", "" },
  226.     "x",        " _",    1,    { "x" },
  227.     "x y",        " _",    2,    { "x", "y" },
  228.     "ab _ cd",    " _",    2,    { "ab", "cd" },
  229.     " a_b  c ",    " _",    5,    { "", "a", "b", "c", "" },
  230.     "a b c_d e f",    " _",    6,    { "a", "b", "c", "d", "e f" },
  231.     " a b c d ",    " _",    6,    { "", "a", "b", "c", "d " },
  232.  
  233.     "",        " _~",    0,    { "" },
  234.     " ",        " _~",    2,    { "", "" },
  235.     "x",        " _~",    1,    { "x" },
  236.     "x y",        " _~",    2,    { "x", "y" },
  237.     "ab _~ cd",    " _~",    2,    { "ab", "cd" },
  238.     " a_b  c~",    " _~",    5,    { "", "a", "b", "c", "" },
  239.     "a b_c d~e f",    " _~",    6,    { "a", "b", "c", "d", "e f" },
  240.     "~a b c d ",    " _~",    6,    { "", "a", "b", "c", "d " },
  241.  
  242.     "",        " _~-",    0,    { "" },
  243.     " ",        " _~-",    2,    { "", "" },
  244.     "x",        " _~-",    1,    { "x" },
  245.     "x y",        " _~-",    2,    { "x", "y" },
  246.     "ab _~- cd",    " _~-",    2,    { "ab", "cd" },
  247.     " a_b  c~",    " _~-",    5,    { "", "a", "b", "c", "" },
  248.     "a b_c-d~e f",    " _~-",    6,    { "a", "b", "c", "d", "e f" },
  249.     "~a-b c d ",    " _~-",    6,    { "", "a", "b", "c", "d " },
  250.  
  251.     "",        "  ",    0,    { "" },
  252.     " ",        "  ",    2,    { "", "" },
  253.     "x",        "  ",    1,    { "x" },
  254.     "xy",        "  ",    1,    { "xy" },
  255.     "x y",        "  ",    2,    { "x", "y" },
  256.     "abc def  g ",    "  ",    4,    { "abc", "def", "g", "" },
  257.     "  a bcd",    "  ",    3,    { "", "a", "bcd" },
  258.     "a b c d e f",    "  ",    6,    { "a", "b", "c", "d", "e f" },
  259.     " a b c d ",    "  ",    6,    { "", "a", "b", "c", "d " },
  260.  
  261.     "",        "",    0,    { "" },
  262.     " ",        "",    0,    { "" },
  263.     "x",        "",    1,    { "x" },
  264.     "xy",        "",    1,    { "xy" },
  265.     "x y",        "",    2,    { "x", "y" },
  266.     "abc def  g ",    "",    3,    { "abc", "def", "g" },
  267.     "\t a bcd",    "",    2,    { "a", "bcd" },
  268.     "  a \tb\t c ",    "",    3,    { "a", "b", "c" },
  269.     "a b c d e ",    "",    5,    { "a", "b", "c", "d", "e" },
  270.     "a b\tc d e f",    "",    6,    { "a", "b", "c", "d", "e f" },
  271.     " a b c d e f ",    "",    6,    { "a", "b", "c", "d", "e f " },
  272.  
  273.     NULL,        NULL,    0,    { NULL },
  274. };
  275.  
  276. regress()
  277. {
  278.     char buf[512];
  279.     register int n;
  280.     char *fields[RNF+1];
  281.     register int nf;
  282.     register int i;
  283.     register int printit;
  284.     register char *f;
  285.  
  286.     for (n = 0; tests[n].str != NULL; n++) {
  287.         (void) strcpy(buf, tests[n].str);
  288.         fields[RNF] = NULL;
  289.         nf = split(buf, fields, RNF, tests[n].seps);
  290.         printit = 0;
  291.         if (nf != tests[n].nf) {
  292.             printf("split `%s' by `%s' gave %d fields, not %d\n",
  293.                 tests[n].str, tests[n].seps, nf, tests[n].nf);
  294.             printit = 1;
  295.         } else if (fields[RNF] != NULL) {
  296.             printf("split() went beyond array end\n");
  297.             printit = 1;
  298.         } else {
  299.             for (i = 0; i < nf && i < RNF; i++) {
  300.                 f = fields[i];
  301.                 if (f == NULL)
  302.                     f = "(NULL)";
  303.                 if (strcmp(f, tests[n].fi[i]) != 0) {
  304.                     printf("split `%s' by `%s', field %d is `%s', not `%s'\n",
  305.                         tests[n].str, tests[n].seps,
  306.                         i, fields[i], tests[n].fi[i]);
  307.                     printit = 1;
  308.                 }
  309.             }
  310.         }
  311.         if (printit)
  312.             print(nf, RNF, fields);
  313.     }
  314. }
  315. #endif
  316.