home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d5xx / d523 / bmake.lha / BMake / source.lzh / parsing.c < prev    next >
C/C++ Source or Header  |  1991-06-08  |  2KB  |  141 lines

  1. /*    Parsing.c
  2.  *    (c) Copyright 1991 by Ben Eng, All Rights Reserved
  3.  *
  4.  */
  5.  
  6. #include <string.h>
  7. #include <ctype.h>
  8.  
  9. /*    Parse the first word from the source string into the dest string
  10.  *
  11.  *    return: pointer to next whitespace past parsed argument
  12.  */
  13.  
  14. char *
  15. parse_strtok( char *dest, char *source, int len, int (*isdelim)( int ))
  16. {
  17.     register char *s, *d;
  18.     int i = 0;
  19.  
  20.     s = source;
  21.     d=dest;
  22.     while( i < len && *s ) {
  23.         if( (*isdelim)( *s )) break;
  24.         *d++ = *s++;
  25.         i++;
  26.     }
  27.     *d=(char)0;
  28.     return s;
  29. }
  30.  
  31.  
  32. static int
  33. iswhite( int ch )
  34. {
  35.     return( isspace( ch ));    /* macro */
  36. }
  37.  
  38. /*    Parse the first word from the source string into the dest string
  39.  *
  40.  *    return: pointer to next whitespace past parsed argument
  41.  */
  42. char *
  43. parse_str( char *dest, char *source, int len )
  44. {
  45.     while( isspace( *source )) source++;    /* skip whitespaces */
  46.     return( parse_strtok( dest, source, len - 1, iswhite ));
  47. }
  48.  
  49. /*    Counts the number of words in the string */
  50. int
  51. count_args( unsigned char *string )
  52. {
  53.     int count, sflag, prev;
  54.     register unsigned char *a = string;
  55.  
  56.     prev = 1;
  57.     count = 0;
  58.     for( ; *a; a++ ) {
  59.         sflag = isspace( *a );
  60.         if( !sflag && prev ) count++;
  61.         prev = sflag;
  62.     }
  63.     return count;
  64. }
  65.  
  66. /*    finds the position of a particular word in a string */
  67. /*    the first word is numbered 1 */
  68. /*    find_word( string, count_args( string )) == last_word */
  69. char *
  70. find_word( char *string, int word )
  71. {
  72.     int count, sflag, prev;
  73.     register char *a = string;
  74.  
  75.     prev = 1;
  76.     count = 0;
  77.     for( ; *a; a++ ) {
  78.         sflag = isspace( *a );
  79.         if( !sflag && prev ) if( ++count == word ) return( a );
  80.         prev = sflag;
  81.     }
  82.     return( NULL ); /* not found */
  83. }
  84.  
  85. void
  86. strip_trailspace( char *line )
  87. {
  88.     char *cptr;
  89.  
  90.     cptr = line + strlen( line ) - 1;
  91.  
  92.     while( cptr >= line && isspace( *cptr )) {
  93.         *cptr-- = (char)0;
  94.     }
  95. }
  96.  
  97. int
  98. isnotsuf( int ch )
  99. {
  100.     if( !ch || isspace( ch ) || ch == '.' ) return( 1 );
  101.     return( 0 );
  102. }
  103.  
  104. int
  105. isemptyline( char *line )
  106. {
  107.     while( *line ) {
  108.         if( !isspace( *line )) return( 0 );
  109.         line++;
  110.     }
  111.     return( 1 );
  112. }
  113.  
  114. /*    locate the next instance of a token that is not escaped by a backslash
  115.  */
  116. char *
  117. find_token( char *line, int tok )
  118. {
  119.     char *delim = strchr( line, tok );
  120.     while( delim && *(delim-1) == '\\' ) {
  121.         delim = strchr( delim + 1, tok );
  122.     }
  123.     return( delim );
  124. }
  125.  
  126. void
  127. shift_string_left( char *string, int shift )
  128. {
  129.     register char *d, *s;
  130.     char *end = string + strlen( string );
  131.  
  132.     if( shift > 0 ) {
  133.         d = string;
  134.         s = string + shift;
  135.         while( s <= end ) {
  136.             *d++ = *s++;
  137.         }
  138.     }
  139. }
  140.  
  141.