home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / strings / c_string / strfield.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-04-09  |  2.7 KB  |  73 lines

  1.  
  2. /*f File   : strfield.c
  3.     Author : Richard A. O'Keefe.
  4.     Updated: 21 April 1984
  5.     Defines: strfield()
  6.  
  7.     strfield(src, fields, chars, blanks, tabch)
  8.         is based on the key specifications of the sort(1) command.
  9.  
  10.         tabch corresponds to 'x' in -t'x'.  If it is NUL, a field
  11.         is leading layout (spaces, tabs &c) followed by at least
  12.         one non-layout character, and is terminated by the next
  13.         layout character or NUL.  If it is not NUL, a field is
  14.         terminated by tabch or NUL.
  15.  
  16.         fields is the number of fields to skip over.  It corresponds
  17.         to m in -m.n or +m.n .  There must be at least this many
  18.         fields, and only the last may be terminated by NUL.
  19.  
  20.         chars is the number of characters to skip after the fields
  21.         have been skipped.  At least this many non-NUL characters
  22.         must remain after the fields have been skipped.  Note that
  23.         it is entirely possible for this skip to cross one or more
  24.         field boundaries.  This corresponds to n in +m.n or -m.n .
  25.         Finally, if blanks is not 0, any layout characters wi
  26.         skipped.  There need not be any.  This corresponds to the
  27.         letter b in +2.0b or -0.4b .
  28.  
  29.         The result is NullS if the source ran out of fields or ran
  30.         out of chars.  Otherwise it is a pointer to the first
  31.         character of src which was not skipped.  It is quite possible
  32.         for this character to be the terminating NUL.
  33.  
  34.     Example:
  35.         to skip to the user-id field of /etc/passwd:
  36.             user_id = strfield(line, 2, 0, 0, ':');
  37.  
  38.         to check whether "line" is at least 27 characters long:
  39.             if (strfield(line, 0, 27, 0, 0)) then-it-is;
  40.  
  41.         to select the third blank-delimited field in a line:
  42.             head = strfield(line, 2, 0, 1, 0);
  43.             tail = strfield(head, 1, 0, 0, 0);
  44.             (* the field is the tail-head characters starting at head *)
  45.  
  46.     It's not a bug, it's a feature: "layout" means any ASCII character
  47.         in the range '\1' .. ' ', including '\n', '\f' and so on.
  48. */
  49.  
  50. #include "strings.h"
  51.  
  52. char *strfield(src, fields, chars, blanks, tabch)
  53.     register char *src;
  54.     int fields, chars, blanks, tabch;
  55.     {
  56.         if (tabch <= 0) {
  57.             while (--fields >= 0) {
  58.                 while (*src <= ' ') if (!*src++) return NullS;
  59.                 while (*++src > ' ') ;
  60.             }
  61.         } else
  62.         if (fields > 0) {
  63.             do if (!*src) return NullS;
  64.             while (*src++ != tabch || --fields > 0);
  65.         }
  66.         while (--chars >= 0) if (!*src++) return NullS;
  67.         if (blanks) while (*src && *src++ <= ' ') ;
  68.         return src;
  69.     }
  70.  
  71.  
  72.  
  73.