home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Monster Media 1994 #1
/
monster.zip
/
monster
/
OS2
/
CENV2_19.ZIP
/
FIND#.CMM
< prev
next >
Wrap
Text File
|
1994-03-08
|
3KB
|
66 lines
// Find#.cmm CEnvi program to find the first number in a program's output.
// ver.1 FIND_TEMP_FILE will be assumed to be set to temporary file.
#define DEFAULT_INDEX 1
main(ArgCount,ArgStrings)
{
Index = DEFAULT_INDEX
if ( ArgCount != 1
&& ( ArgCount != 2 || 0 == (Index = atoi(ArgStrings[1])) ) )
Instructions()
else {
HowManyFound = 0; // this will keep a list of how many numbers found so far
while ( (input = gets()) ) {
// get the next number in this line
while ( (input = strpbrk(input,"-0123456789")) ) {
if ( '-' == input[0] && !isdigit(input[1]) ) {
// this was a negative sign, but no numbers follow
input++
} else {
// convert these digits to a number
NumberList[HowManyFound] = atol(input)
if ( ++HowManyFound == Index ) {
NUMBER = NumberList[HowManyFound-1]
ESet(FIND_TEMP_FILE)
return(NUMBER = NumberList[HowManyFound-1])
}
// skip past all the digits text
input += 1 + strspn(input+1,"0123456789")
}
}
}
if ( Index < 0 ) {
// if enough numbers have been found, then find this offset from the end
if ( -Index <= HowManyFound ) {
NUMBER = NumberList[HowManyFound+Index]
ESet(FIND_TEMP_FILE)
return(NUMBER)
}
}
}
// if did not return earlier, then number was not found
undefine(NUMBER)
ESet(FIND_TEMP_FILE)
return(0)
}
Instructions()
{
printf("\a\n")
printf("Find#.cmm - Read input for numbers. Set the NUMBER environment variable to the\n")
printf(" number found and return ERRORLEVEL to number found. 0 if not found.\n")
printf("\n")
printf("USAGE: CEnvi Find#.cmm [Index]\n")
printf("\n")
printf("Where Index tells which found number to return, where 1 is the first number\n")
printf("found, 2 is the seconds number, and so on; -1 is the last number found, -2 is\n")
printf("the second to last number, and so on. If Index is not supplied then the\n")
printf("default is 1. If the number is not found then NUMBER is cleared.\n")
printf("\n")
printf("Example: To find how many bytes are used the current directory:\n")
printf(" dir | cenvi Find#.cmm -2\n")
printf("\n")
}