📄 getpars.c.ori
字号:
/* Copyright (c) Colorado School of Mines, 1990./* All rights reserved. *//*---------------------------------------------------------------------- * Copyright (c) Colorado School of Mines, 1989. * All rights reserved. * * This code is part of SU. SU stands for Seismic Unix, a processing line * developed at the Colorado School of Mines, partially based on Stanford * Exploration Project (SEP) software. Inquiries should be addressed to: * * Jack K. Cohen, Center for Wave Phenomena, Colorado School of Mines, * Golden, CO 80401 (jkc@dix.mines.colorado.edu) *---------------------------------------------------------------------- */ /*****************************************************************************Functions to get parameters from the command line. Numeric parameters maybe single values or arrays of int, uint, short, ushort, long, ulong, float, or double. Single character strings (type string or char *) may also begotten. (Arrays of strings may not be gotten.) The functions are:int getpar<type> (char *name, <type> *ptr); Gets values for the last occurence of a parameter, where <type> denotes one of the parameter types listed above. Returns the number of parameters gotten, zero if parameter not specified.int getnpar<type> (int n, char *name, <type> *ptr); Gets values for the n'th occurence of a parameter, where <type> denotes one of the parameter types listed above. Returns the number of parameters gotten, zero if n'th occurence not found.int countparname (char *name); Returns the number of occurences of a parameter name.int countparval (char *name); Returns the number of values in the last occurence of a parameter.int countnparval (char *name); Returns the number of values in the n'th occurence of a parameter.****************************************************************************** Example: ... 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 = ealloc1float(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.5More examples are provided in the DTEST code at the end of this file.****************************************************************************** Notes:The functions: eatoh, eatou, eatol, eatov, eatoi, eatop usedbelow are versions of atoi that check for overflow. The sourcefile for these functions is atopkge.c.****************************************************************************** Authors:Rob Clayton & Jon Claerbout, Stanford University, 1979-1985Shuki Ronen & Jack Cohen, Colorado School of Mines, 1985-1990Dave Hale, Colorado School of Mines, 05/29/90*****************************************************************************/ #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 bool tabled = false; /* true when parameters tabled *//* functions declared and used internally */static int getparindex (int n, char *name);static int getparinit(void);static int tabulate (int argc, char **argv);static char *getpfname (void);static int white2null (char *str, int len);static int ccount (char c, char *s);/* functions to get values for the last occurence 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 getpar (char *name, char *type, void *ptr){ return getnpar(0,name,type,ptr);}/* functions to get values for the n'th occurence 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 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. Therefore, no arrays of strings. */ 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; default: err("%s: invalid parameter type = %s", __FILE__,type); } while (*aval++ != ',') { if (!*aval) break; } } return nval;}/* return number of occurences of parameter name */int countparname (char *name){ int i,nname; if (xargc == 1) return 0; if (!tabled) getparinit(); for (i=0,nname=0; i<nargs; ++i) if (!strcmp(name,argtbl[i].name)) ++nname; return nname;}/* return number of values in n'th occurence of parameter name */int countnparval (int n, char *name){ int i; if (xargc == 1) return 0; if (!tabled) getparinit(); i = getparindex(n,name); if (i>=0) return ccount(',',argtbl[i].asciival) + 1; else return 0;}/* return number of values in last occurence of parameter name */int countparval (char *name){ return countnparval(0,name);}/* obsolute functions - provided here for compatibility */int igetpar (char *name, int *ptr){ return getnpar(0,name,"i",ptr);}int pgetpar (char *name, unsigned int *ptr){ return getnpar(0,name,"p",ptr);}int hgetpar (char *name, short *ptr){ return getnpar(0,name,"h",ptr);}int ugetpar (char *name, unsigned short *ptr){ return getnpar(0,name,"u",ptr);}int lgetpar (char *name, long *ptr){ return getnpar(0,name,"l",ptr);}int vgetpar (char *name, unsigned long *ptr){ return getnpar(0,name,"v",ptr);}int fgetpar (char *name, float *ptr){ return getnpar(0,name,"f",ptr);}int dgetpar (char *name, double *ptr){ return getnpar(0,name,"d",ptr);}int sgetpar (char *name, char **ptr){ return getnpar(0,name,"s",ptr);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -