home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / FIND#.CMM < prev    next >
Text File  |  1994-10-03  |  3KB  |  66 lines

  1. // Find#.cmm  CEnvi program to find the first number in a program's output.
  2. // ver.1      FIND_TEMP_FILE will be assumed to be set to temporary file.
  3.  
  4. #define  DEFAULT_INDEX  1
  5.  
  6. main(ArgCount,ArgStrings)
  7. {
  8.    Index = DEFAULT_INDEX
  9.    if ( ArgCount != 1
  10.      && ( ArgCount != 2  ||  0 == (Index = atoi(ArgStrings[1])) ) )
  11.       Instructions()
  12.    else {
  13.       HowManyFound = 0; // this will keep a list of how many numbers found so far
  14.       while ( (input = gets()) ) {
  15.          // get the next number in this line
  16.          while ( (input = strpbrk(input,"-0123456789")) ) {
  17.             if ( '-' == input[0] && !isdigit(input[1]) ) {
  18.                // this was a negative sign, but no numbers follow
  19.                input++
  20.             } else {
  21.                // convert these digits to a number
  22.                NumberList[HowManyFound] = atol(input)
  23.                if ( ++HowManyFound == Index ) {
  24.                      NUMBER = NumberList[HowManyFound-1]
  25.                      ESet(FIND_TEMP_FILE)
  26.                      return(NUMBER = NumberList[HowManyFound-1])
  27.                }
  28.                // skip past all the digits text
  29.                input += 1 + strspn(input+1,"0123456789")
  30.             }
  31.          }
  32.       }
  33.       if ( Index < 0 ) {
  34.          // if enough numbers have been found, then find this offset from the end
  35.          if ( -Index <= HowManyFound ) {
  36.             NUMBER = NumberList[HowManyFound+Index]
  37.             ESet(FIND_TEMP_FILE)
  38.             return(NUMBER)
  39.          }
  40.       }
  41.    }
  42.    // if did not return earlier, then number was not found
  43.    undefine(NUMBER)
  44.    ESet(FIND_TEMP_FILE)
  45.    return(0)
  46. }
  47.  
  48. Instructions()
  49. {
  50.    printf("\a\n")
  51.    printf("Find#.cmm - Read input for numbers. Set the NUMBER environment variable to the\n")
  52.    printf("            number found and return ERRORLEVEL to number found. 0 if not found.\n")
  53.    printf("\n")
  54.    printf("USAGE: CEnvi2 Find#.cmm [Index]\n")
  55.    printf("\n")
  56.    printf("Where Index tells which found number to return, where 1 is the first number\n")
  57.    printf("found, 2 is the seconds number, and so on; -1 is the last number found, -2 is\n")
  58.    printf("the second to last number, and so on.  If Index is not supplied then the\n")
  59.    printf("default is 1.  If the number is not found then NUMBER is cleared.\n")
  60.    printf("\n")
  61.    printf("Example: To find how many bytes are used the current directory:\n")
  62.    printf("             dir | CEnvi2 Find#.cmm -2\n")
  63.    printf("\n")
  64. }
  65.  
  66.