home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The C Users' Group Library 1994 August
/
wc-cdrom-cusersgrouplibrary-1994-08.iso
/
listings
/
v_10_08
/
ramey.exe
/
ARG.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-10-14
|
3KB
|
124 lines
/*
Copyright (c) Robert Ramey 1991. All Rights Reserved
*/
#include <ctype.h>
#include "arg.h"
/*********************************************************************
arg_value - get one value from input string. It may be decimal,
octal (starting with a 0), hexidecimal (starting with 0x) or
a character constant.
**********************************************************************/
char *
arg_value(cptr, value) /* return pointer to next character */
char *cptr; /* pointer to first character of string */
int *value; /* address of integer where result is placed */
{
char c;
*value = 0;
c = *cptr;
if(c == '\''){
/* value is character constant */
c = *++cptr;
if(c == '\\'){
switch(c = *++cptr){
case 'b': *value = '\b';break;
case 'f': *value = '\f';break;
case 'n': *value = '\n';break;
case 'r': *value = '\r';break;
case 't': *value = '\t';break;
case 'v': *value = '\v';break;
case '\\':*value = '\\';break;
default:
error("Invalid \\character in key");
}
}
else
*value = c & 0xff;
c = *++cptr;
if(c != '\'')
error("Invalid character constant");
else
++cptr;
}
else
if(c == '0'){
c = *++cptr;
if(c == 'x' || c == 'X'){
/* its a hexidecimal number */
while(isxdigit(c = *++cptr)){
*value = *value * 16 + isalpha(c) ?
tolower(c) - 'a' + 10 : c - '0';
}
}
else{
/* should be an octal digit in this case */
while(isodigit(c)){
*value = *value * 8 + c - '0';
c = *++cptr;
}
}
}
else{
/* it better be a decimal number */
while(isdigit(c)){
*value = *value * 10 + c - '0';
c = *++cptr;
}
}
return cptr;
}
/*********************************************************************
arg_range - parse range of values in string. 1-4 separtes to 1 and 4.
**********************************************************************/
arg_range(cptr, dest)
char *cptr; /* pointer to character string containing range spec */
RANGE *dest; /* pointer to a pair of integers to recieve results */
{
/* separate first number */
cptr = arg_value(cptr, &dest->start);
/* if only one number specified */
if(*cptr == '\0'){
/* we're done. set end equal to start */
dest->end = dest->start;
return;
}
/* if the next character is not a '-', a syntax error has been made */
if(*cptr != '-')
error("Error in collating sequence value");
/* 1- is 1 to a HUGE number */
if(*++cptr == '\0'){
dest->end = HUGE;
return;
}
/* otherwise get second number */
cptr = arg_value(cptr, &dest->end);
if(*cptr != '\0')
error("Error in collating sequence range");
return;
}
/******************************************************************
arg_find - given a set of command line arguments, return index
of first argument that matches the requested character string.
if no command line argument matches the given string, return a -1.
******************************************************************/
int
arg_find(argc, argv, arg)
int argc;
char **argv, *arg;
{
int i;
for(i = 0;i < argc;++i){
if(!strcmp(*argv, arg))
return i;
++argv;
}
return -1;
}