⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 getpars.c

📁 地震波正演和显示模块
💻 C
📖 第 1 页 / 共 2 页
字号:
	for (i=0,nname=0; i<nargs; ++i)
		if (!strcmp(name,argtbl[i].name)) ++nname;
	return nname;
}

/* return number of values in n'th occurrence 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 occurrence of parameter name */
int countparval (char *name)
{
	return countnparval(0,name);
}



/*
 * Return the index of the n'th occurrence of a parameter name, 
 * except if n==0, return the index of the last occurrence.
 * Return -1 if the specified occurrence does not exist.
 */
static int getparindex (int n, char *name)
{
	int i;
	if (n==0) {
		for (i=nargs-1; i>=0; --i)
			if (!strcmp(name,argtbl[i].name)) break;
		return i;
	} else {
		for (i=0; i<nargs; ++i)
			if (!strcmp(name,argtbl[i].name))
				if (--n==0) break;
		if (i<nargs)
			return i;
		else
			return -1;
	}
}

/* Initialize getpar */

static void getparinit (void)
{
	static char *pfname;	/* name of parameter file		*/
	FILE *pffd=NULL;	/* file id of parameter file		*/
	size_t pflen;		/* length of parameter file in bytes	*/ 
	static size_t pfargc;	/* arg count from parameter file	*/
	int parfile;		/* parfile existence flag		*/
	int argstrlen=0;
	char *pargstr;		/* storage for parameter file args	*/
	size_t nread=0;		/* bytes fread				*/
	int i, j;		/* counters				*/
        int start = TRUE;
        int debug = FALSE;
        int quote = FALSE;


	tabled = TRUE;		/* remember table is built		*/


	/* Check if xargc was initiated */

	if(!xargc)
		printf("%s: xargc=%d -- not initiated in main", __FILE__, xargc);

	/* Space needed for command lines */

	for (i = 1, argstrlen = 0; i < xargc; i++) {
		argstrlen += strlen(xargv[i]) + 1;
	}

	/* Get parfile name if there is one */

	if ((pfname = getpfname())) {
		parfile = TRUE;
	} else {
		parfile = FALSE;
	}

	if (parfile) {
	 	pffd = fopen(pfname, "r");

		/* Get the length */
		fseek(pffd, 0, SEEK_END);

		pflen = (off_t) ftello(pffd);

		rewind(pffd);
		argstrlen += pflen;
	} else {
		pflen = 0;
	}

/*--------------------------------------------------------------------*\
   Allocate space for command line and parameter file. The pointer
   table could be as large as the string buffer, but no larger.

   The parser logic has been completely rewritten to prevent bad
   input from crashing the program.

\*--------------------------------------------------------------------*/

	argstr = (char *) calloc(argstrlen+1, 1);
	targv = (char **) calloc(argstrlen+1, 1);

	if (parfile) {
		/* Read the parfile */

		nread = fread(argstr, 1, pflen, pffd);
  		if (nread != pflen) {
  	 	    printf("%s: fread only %d bytes out of %d from %s",
  					__FILE__,  nread, pflen, pfname);
		}
		fclose(pffd);


	} else {
		pfargc = 0;
	}
    
    
        /* force input to valid 7 bit ASCII */
    
        for( i=0; i<nread; i++ ){
            argstr[i] &= 0x7F;
        }
    
        /* tokenize the input */
    
        j = 0;
    
        for( i=0; i<nread; i++ ){
    
            /* look for start of token */
    
            if( start ){
    
                if( isgraph( argstr[i] ) ){
                    targv[j] = &(argstr[i]);
                    start = !start;
                    j++;
    
                }else{
                    argstr[i] = 0;
    
                }
    
            /* terminate token */
    
            }else if( !quote && isspace( argstr[i] ) ){
                argstr[i] = 0;
                start = !start;
    
            }
    
            /* toggle quote semaphore */
    
            if( argstr[i] == '\'' || argstr[i] == '\"' ){
                quote = !quote;
    
            }
    
        }
    
        /* display all tokens */
    
        if( debug ){
    
            i=0;
            while( targv[i] != 0 ){
                if( strlen( targv[i] ) ){
                    fprintf( stderr ,"%d -> %s\n" ,i ,targv[i] );
                }
                i++;
        
            }
        }
    
        /* discard non-parameter tokens */
    
        i=0;
        targc=0;
        while( targv[i] != 0 ){
            if( strchr( targv[i] ,'=' ) ){
                targv[targc] = targv[i];
                targc++;
            }
            i++;
        }

	/* Copy command line arguments */

	for (j = 1, pargstr = argstr + pflen + 1; j < xargc; j++) {
		strcpy(pargstr,xargv[j]);
		targv[targc++] = pargstr;
		pargstr += strlen(xargv[j]) + 1;
	}

	/* Allocate space for the pointer table */

	argtbl = (pointer_table*) calloc(targc, sizeof(pointer_table));

	/* Tabulate targv */

	tabulate(targc, targv);
	
	return;
}
#define PFNAME "par="
/* Get name of parameter file */
static char *getpfname (void)
{
	int i;
	size_t pfnamelen;

	pfnamelen = strlen(PFNAME);
	for (i = xargc-1 ; i > 0 ; i--) {
		if(!strncmp(PFNAME, xargv[i], pfnamelen)
		    && strlen(xargv[i]) != pfnamelen) {
			return xargv[i] + pfnamelen;
		}	
	}
	return NULL;
}

#define iswhite(c)	((c) == ' ' || (c) == '\t' || (c) == '\n')

/* 
 * Replace the whites by (possibly multiple) nulls.  If we see a non-white
 * and the previous char is a null, this signals the start of a string
 * and we bump the count.  This routine returns a count of the strings.
 */
static size_t white2null (char *str, size_t len)
{
	int i;
	size_t count = 0;
	int inquote = FALSE;

	str[0] = '\0'; /* This line added by Dave Hale, 1/30/96. */
	for (i = 1; i < len; i++) {
		if (str[i]=='"') inquote=(inquote==TRUE)?FALSE:TRUE;
		if (!inquote) {
			if (iswhite(str[i])) { /* Is this a new word ? */
				str[i] = '\0';
			} else if (!str[i-1]) { /* multiple whites */
				count++;
			}
		}
	}
	for (i = 1, inquote=FALSE; i < len; i++) {
		if (str[i]=='"') inquote=(inquote==TRUE)?FALSE:TRUE;
		if (inquote) {
			if (str[i+1]!='"') {
				str[i] = str[i+1];
			} else {
				str[i] = '\0';
				str[i+1] = '\0';
				inquote = FALSE;
			}
		}
	}
	str[len] = '\0';
	return count;
}

/* Install symbol table */
static void tabulate (size_t argc, char **argv)
{
	int i;
	char *eqptr;
        int debug=FALSE;

	for (i = 0, nargs = 0 ; i < argc; i++) {
		eqptr = strchr(argv[i], '=');
		if (eqptr) {
			argtbl[nargs].name = argv[i];
			argtbl[nargs].asciival = eqptr + 1;
			*eqptr = (char)0;

			/* Debugging dump */
                        if( debug ){
                                fprintf(stderr, 
                                "argtbl[%d]: name=%s asciival=%s\n", 
                                nargs,argtbl[nargs].name,argtbl[nargs].asciival); 

                        }
			nargs++;
		}
	}
	return;
}

/* Count characters in a string */
static int ccount (char c, char *s)
{
	int i, count;
	for (i = 0, count = 0; s[i] != 0; i++)
		if(s[i] == c) count++;
	return count;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -