📄 getpars.c
字号:
/*********************** self documentation **********************/
/*****************************************************************************
GETPARS - Functions to GET PARameterS from the command line. Numeric
parameters may be single values or arrays of int, uint,
short, ushort, long, ulong, float, or double. Single character
strings (type string or char *) may also be gotten.
Arrays of strings, delimited by, but not containing
commas are permitted.
The functions are:
initargs Makes command line args available to subroutines (re-entrant).
Every par program starts with this call!
getparint get integers
getparuint get unsigned integers
getparshort get short integers
getparushort get unsigned short integers
getparlong get long integers
getparulong get unsigned long integers
getparfloat get float
getpardouble get double
getparstring get a single string
getparstringarray get string array (fields delimited by commas)
getpar get parameter by type
getnparint get n'th occurrence of integer
getnparuint get n'th occurrence of unsigned int
getnparshort get n'th occurrence of short integer
getnparushort get n'th occurrence of unsigned short int
getnparlong get n'th occurrence of long integer
getnparulong get n'th occurrence of unsigned long int
getnparfloat get n'th occurrence of float integer
getnpardouble get n'th occurrence of double integer
getnparstring get n'th occurrence of string integer
getnparstringarray get n'th occurrence of string integer array
getnpar get n'th occurrence by type
countparname return the number of times a parameter names is used
countparval return the number of values in the last occurrence
of a parameter
countnparval return the number of values in the n'th occurrence
of a parameter
getPar Promax compatible version of getpar
******************************************************************************
Function Prototypes:
void initargs (int argc, char **argv);
int getparint (char *name, int *p);
int getparuint (char *name, unsigned int *p);
int getparshort (char *name, short *p);
int getparushort (char *name, unsigned short *p);
int getparlong (char *name, long *p);
int getparulong (char *name, unsigned long *p);
int getparfloat (char *name, float *p);
int getpardouble (char *name, double *p);
int getparstring (char *name, char **p);
int getparstringarray (char *name, char **p);
int getnparint (int n, char *name, int *p);
int getnparuint (int n, char *name, unsigned int *p);
int getnparshort (int n, char *name, short *p);
int getnparushort (int n, char *name, unsigned short *p);
int getnparlong (int n, char *name, long *p);
int getnparulong (int n, char *name, unsigned long *p);
int getnparfloat (int n, char *name, float *p);
int getnpardouble (int n, char *name, double *p);
int getnparstring (int n, char *name, char **p);
int getnparstringarray (int n, char *name, char **p);
int getnpar (int n, char *name, char *type, void *ptr);
int countparname (char *name);
int countparval (char *name);
int countnparval (int n, char *name);
void getPar(char *name, char *type, void *ptr);
******************************************************************************
Notes:
Here are some usage examples:
... if integer n not specified, then default to zero.
if (!getparint("n", &n)) n = 0;
... if array of floats vx is specified, then
if (nx=countparval("vx")) {
... allocate space for array
vx = alloc1float(nx);
... and get the floats
getparfloat("vx",vx);
}
The command line for the above examples might look like:
progname n=35 vx=3.21,4,9.5
Every par program starts with this call!
More examples are provided in the DTEST code at the end of this file.
The functions: eatoh, eatou, eatol, eatov, eatoi, eatop used
below are versions of atoi that check for overflow. The source
file for these functions is atopkge.c.
*****************************************************************************/
/**************** end self doc ********************************/
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "../include/par.h"
/* parameter table */
typedef struct {
char *name; /* external name of parameter */
char *asciival; /* ascii value of parameter */
} pointer_table;
/* global variables declared and used internally */
static pointer_table *argtbl; /* parameter table */
static int nargs; /* number of args that parse */
static int tabled = FALSE; /* true when parameters tabled */
static size_t targc; /* total number of args */
static char **targv; /* pointer to arg strings */
static char *argstr; /* storage for command line */
/* functions declared and used internally */
static int getparindex (int n, char *name);
static void getparinit(void);
static void tabulate (size_t argc, char **argv);
static char *getpfname (void);
static size_t white2null (char *str, size_t len);
static int ccount (char c, char *s);
/* make command line args available to subroutines -- re-entrant version */
void initargs(int argc, char **argv)
{
xargc = argc; xargv = argv;
if(tabled==TRUE){
free(argstr);
free(targv);
free(argtbl);
}
tabled = FALSE;
return;
}
/* functions to get values for the last occurrence of a parameter name */
int getparint (char *name, int *ptr)
{
return getnpar(0,name,"i",ptr);
}
int getparuint (char *name, unsigned int *ptr)
{
return getnpar(0,name,"p",ptr);
}
int getparshort (char *name, short *ptr)
{
return getnpar(0,name,"h",ptr);
}
int getparushort (char *name, unsigned short *ptr)
{
return getnpar(0,name,"u",ptr);
}
int getparlong (char *name, long *ptr)
{
return getnpar(0,name,"l",ptr);
}
int getparulong (char *name, unsigned long *ptr)
{
return getnpar(0,name,"v",ptr);
}
int getparfloat (char *name, float *ptr)
{
return getnpar(0,name,"f",ptr);
}
int getpardouble (char *name, double *ptr)
{
return getnpar(0,name,"d",ptr);
}
int getparstring (char *name, char **ptr)
{
return getnpar(0,name,"s",ptr);
}
int getparstringarray (char *name, char **ptr)
{
return getnpar(0,name,"a",ptr);
}
int getpar (char *name, char *type, void *ptr)
{
return getnpar(0,name,type,ptr);
}
/* functions to get values for the n'th occurrence of a parameter name */
int getnparint (int n, char *name, int *ptr)
{
return getnpar(n,name,"i",ptr);
}
int getnparuint (int n, char *name, unsigned int *ptr)
{
return getnpar(n,name,"p",ptr);
}
int getnparshort (int n, char *name, short *ptr)
{
return getnpar(n,name,"h",ptr);
}
int getnparushort (int n, char *name, unsigned short *ptr)
{
return getnpar(n,name,"u",ptr);
}
int getnparlong (int n, char *name, long *ptr)
{
return getnpar(n,name,"l",ptr);
}
int getnparulong (int n, char *name, unsigned long *ptr)
{
return getnpar(n,name,"v",ptr);
}
int getnparfloat (int n, char *name, float *ptr)
{
return getnpar(n,name,"f",ptr);
}
int getnpardouble (int n, char *name, double *ptr)
{
return getnpar(n,name,"d",ptr);
}
int getnparstring (int n, char *name, char **ptr)
{
return getnpar(n,name,"s",ptr);
}
int getnparstringarray (int n, char *name, char **ptr)
{
return getnpar(n,name,"a",ptr);
}
int getnpar (int n, char *name, char *type, void *ptr)
{
int i; /* index of name in symbol table */
int nval; /* number of parameter values found */
char *aval; /* ascii field of symbol */
if (xargc == 1) return 0;
if (!tabled) getparinit();/* Tabulate command line and parfile */
i = getparindex(n,name);/* Get parameter index */
if (i < 0) return 0; /* Not there */
/*
* handle string type as a special case, since a string
* may contain commas.
*/
if (type[0]=='s') {
*((char**)ptr) = argtbl[i].asciival;
return 1;
}
/* convert vector of ascii values to numeric values */
for (nval=0,aval=argtbl[i].asciival; *aval; nval++) {
switch (type[0]) {
case 'i':
*(int*)ptr = eatoi(aval);
ptr = (int*)ptr+1;
break;
case 'p':
*(unsigned int*)ptr = eatop(aval);
ptr = (unsigned int*)ptr+1;
break;
case 'h':
*(short*)ptr = eatoh(aval);
ptr = (short*)ptr+1;
break;
case 'u':
*(unsigned short*)ptr = eatou(aval);
ptr = (unsigned short*)ptr+1;
break;
case 'l':
*(long*)ptr = eatol(aval);
ptr = (long*)ptr+1;
break;
case 'v':
*(unsigned long*)ptr = eatov(aval);
ptr = (unsigned long*)ptr+1;
break;
case 'f':
*(float*)ptr = eatof(aval);
ptr = (float*)ptr+1;
break;
case 'd':
*(double*)ptr = eatod(aval);
ptr = (double*)ptr+1;
break;
case 'a':
{ char *tmpstr="";
tmpstr = calloc(strlen(aval)+1,1);
strchop(aval,tmpstr);
*(char**)ptr = tmpstr;
ptr=(char **)ptr + 1;
}
break;
default:
printf("%s: invalid parameter type = %s",
__FILE__,type);
}
while (*aval++ != ',') {
if (!*aval) break;
}
}
return nval;
}
/* Promax compatible version of getnpar */
void getPar(char *name, char *type, void *ptr)
{
(void) getnpar(0,name,type,ptr);
return;
}
/* return number of occurrences of parameter name */
int countparname (char *name)
{
int i,nname;
if (xargc == 1) return 0;
if (!tabled) getparinit();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -