home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d6xx / d663 / unixutils.lha / UnixUtils / Source / token.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-21  |  3.3 KB  |  105 lines

  1. /*---------------------------------------------------*
  2.  | File: token.c - String parsing routines for sort. |
  3.  | v1.00 MLO 911230 - see the comments in sort.c     |
  4.  *---------------------------------------------------*/
  5.  
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <ctype.h>
  9. #include "mlo.h"
  10. #include "sort.h"
  11.  
  12. void GetTokBlk(
  13.   char *string,     /* String to be parsed */
  14.   int i,            /* Field number (1, 2, ... ) */
  15.   char *token       /* Output */
  16. ){
  17.  
  18. /*------------------------------------------------------------------*
  19.  | This procedure splits the given string in fields separated from  |
  20.  | one or more whitespaces, and returns in "token" the "i"-th field |
  21.  | or an empty string if less than "i" fields are present.          |
  22.  |                                                                  |
  23.  | N.B.: NO CHECKS ARE DONE. Please be sure that "i" is positive    |
  24.  | and that "token" is large enough before calling GetTokBlk().     |
  25.  *------------------------------------------------------------------*/
  26.  
  27. /**
  28.  | First: skip leading whitespaces (or until the end of the string).
  29. **/
  30.  
  31.   while (*string && isspace(*string)) string++;
  32.  
  33. /**
  34.  | Second: skip i-1 fields and their trailing whitespaces,
  35.  | or until the end of the string.
  36. **/
  37.  
  38.   while (--i) {
  39.     while (*string && !isspace(*string)) string++;
  40.     while (*string &&  isspace(*string)) string++;
  41.   }
  42.  
  43. /**
  44.  | Third: copy this field to the output, until the next whitespace
  45.  | or the end of the string; terminate the output string and return.
  46. **/
  47.  
  48.   while (*string && !isspace(*string)) *token++ = *string++;
  49.   *token = '\0';
  50. }
  51.  
  52. void GetTokDel(
  53.   char *string,     /* String to be parsed */
  54.   char *delim,      /* Delimiters between fields */
  55.   int i,            /* Field number (1, 2, ...) */
  56.   char *token       /* Output */
  57. ){
  58.  
  59. /*---------------------------------------------------------------*
  60.  | This procedure examines a character string, to be splitted    |
  61.  | in fields separated with characters from a given set of       |
  62.  | delimiters; then returns in "token" the "i"-th field, without |
  63.  | leading and trailing whitespaces (but including whitespaces   |
  64.  | bounded by other characters); or an empty string if the field |
  65.  | contains only whitespaces, or if less than "i" fields are     |
  66.  | found.                                                        |
  67.  |                                                               |
  68.  | N.B.: NO CHECKS ARE DONE. Please be sure that "i" is positive |
  69.  | and that "token" is large enough before calling GetTokDel().  |
  70.  *---------------------------------------------------------------*/
  71.  
  72.   char *pc = token;     /* Pointer to the end of the returned string */
  73.  
  74. /**
  75.  | First: skip characters until i-1 delimiters are found, or
  76.  | until the end of the string. "string" points to the first
  77.  | character in the needed field, or to the terminating '\0'.
  78. **/
  79.  
  80.   while (--i) while (*string && !strchr(delim, *string++)) ;
  81.  
  82. /**
  83.  | Second: skip leading whitespaces.
  84. **/
  85.  
  86.   while (*string && isspace(*string)) string++;
  87.  
  88. /**
  89.  | Third: copy all to "token", until the end of the string or
  90.  | the next delimiter. Take care to have a pointer to the last
  91.  | non-whitespace character in output (plus one).
  92. **/
  93.  
  94.   while (*string && !strchr(delim, *string)) {
  95.     *token = *string++;
  96.     if (!isspace(*token++)) pc = token;
  97.   }
  98.  
  99. /**
  100.  | Fourth: terminate the output string, and return.
  101. **/
  102.  
  103.   *pc = '\0';
  104. }
  105.