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

📄 getarg.c

📁 一款最完整的工业组态软源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
	    case 'o':				      /* Get octal integers. */
		ScanRes	= sscanf(*((*argv)++), "%o",
					 (int *) Parameters[(*ParamCount)++]);
		break;
	    case 'D':				/* Get signed long integers. */
		ScanRes	= sscanf(*((*argv)++), "%ld",
					(long *) Parameters[(*ParamCount)++]);
		break;
	    case 'U':			      /* Get unsigned long integers. */
		ScanRes	= sscanf(*((*argv)++), "%lu",
			       (unsigned long *) Parameters[(*ParamCount)++]);
		break;
	    case 'X':				   /* Get hex long integers. */
		ScanRes	= sscanf(*((*argv)++), "%lx",
					(long *) Parameters[(*ParamCount)++]);
		break;
	    case 'O':				 /* Get octal long integers. */
		ScanRes	= sscanf(*((*argv)++), "%lo",
					(long *) Parameters[(*ParamCount)++]);
		break;
	    case 'f':					/* Get float number. */
		ScanRes	= sscanf(*((*argv)++), "%f",
				       (float *) Parameters[(*ParamCount)++]);
	    case 'F':				 /* Get double float number. */
		ScanRes	= sscanf(*((*argv)++), "%lf",
				      (double *) Parameters[(*ParamCount)++]);
		break;
	    case 's':					  /* It as a string. */
		ScanRes	= 1;				     /* Allways O.K. */
		GAByteCopy((char *) Parameters[(*ParamCount)++],
					(char *) ((*argv)++), sizeof(char *));
		break;
	    case '*':			     /* Get few parameters into one: */
		ScanRes	= GAGetMultiParmeters(Parameters, ParamCount,
						  &CtrlStrCopy[i], argc, argv);
		if ((ScanRes ==	0) && (CtrlStrCopy[i] == '!')) {
		    GAErrorToken = Option;
		    return CMD_ERR_WildEmpty;
		}
		break;
	    default:
		ScanRes = 0;               /* Make optimizer warning silent. */
	}
	/* If reading fails and	this number is a must (!) then error: */
	if ((ScanRes ==	0) && (CtrlStrCopy[i] == '!')) {
	    GAErrorToken = Option;
	    return CMD_ERR_NumRead;
	}
	if (CtrlStrCopy[i+1] !=	'*') {
	    (*argc)--;	     /* Everything is OK - update to next parameter: */
	    i += 2;			 /* Skip to next parameter (if any). */
	}
	else
	    i += 3;				       /* Skip the '*' also! */
    }

    return ARG_OK;
}

/***************************************************************************
* Routine to get few parameters	into one pointer such that the returned	   *
* pointer actually points on a block of	pointers to the	parameters...	   *
* For example *F means a pointer to pointers on	floats.			   *
* Returns number of parameters actually	read.				   *
* This routine assumes that all	pointers (on any kind of scalar) has the   *
* same size (and the union below is totally ovelapped bteween dif. arrays) *
***************************************************************************/
static int GAGetMultiParmeters(int *Parameters[], int *ParamCount,
	char *CtrlStrCopy, int *argc, char ***argv)
{
    int	i = 0, ScanRes,	NumOfPrm = 0, **Pmain, **Ptemp;
    union TmpArray {    /* Save here the temporary data before copying it to */
	int    *IntArray[MAX_PARAM];	      /* the returned pointer block. */
	long   *LngArray[MAX_PARAM];
	float  *FltArray[MAX_PARAM];
	double *DblArray[MAX_PARAM];
	char   *ChrArray[MAX_PARAM];
    } TmpArray;

    do {
	switch(CtrlStrCopy[2]) {    /* CtrlStr == '!*?' or '%*?' where ? is. */
	    case 'd':			   /* Format to read the parameters: */
		TmpArray.IntArray[NumOfPrm] = (int *) MyMalloc(sizeof(int));
		ScanRes	= sscanf(*((*argv)++), "%d",
				       (int *) TmpArray.IntArray[NumOfPrm++]);
		break;
	    case 'u':
		TmpArray.IntArray[NumOfPrm] = (int *) MyMalloc(sizeof(int));
		ScanRes	= sscanf(*((*argv)++), "%u",
			      (unsigned	int *) TmpArray.IntArray[NumOfPrm++]);
		break;
	    case 'o':
		TmpArray.IntArray[NumOfPrm] = (int *) MyMalloc(sizeof(int));
		ScanRes	= sscanf(*((*argv)++), "%o",
				       (int *) TmpArray.IntArray[NumOfPrm++]);
		break;
	    case 'x':
		TmpArray.IntArray[NumOfPrm] = (int *) MyMalloc(sizeof(int));
		ScanRes	= sscanf(*((*argv)++), "%x",
				       (int *) TmpArray.IntArray[NumOfPrm++]);
		break;
	    case 'D':
		TmpArray.LngArray[NumOfPrm] = (long *) MyMalloc(sizeof(long));
		ScanRes	= sscanf(*((*argv)++), "%ld",
				      (long *) TmpArray.IntArray[NumOfPrm++]);
		break;
	    case 'U':
		TmpArray.LngArray[NumOfPrm] = (long *) MyMalloc(sizeof(long));
		ScanRes	= sscanf(*((*argv)++), "%lu",
			     (unsigned long *) TmpArray.IntArray[NumOfPrm++]);
		break;
	    case 'O':
		TmpArray.LngArray[NumOfPrm] = (long *) MyMalloc(sizeof(long));
		ScanRes	= sscanf(*((*argv)++), "%lo",
				      (long *) TmpArray.IntArray[NumOfPrm++]);
		break;
	    case 'X':
		TmpArray.LngArray[NumOfPrm] = (long *) MyMalloc(sizeof(long));
		ScanRes	= sscanf(*((*argv)++), "%lx",
				      (long *) TmpArray.IntArray[NumOfPrm++]);
		break;
	    case 'f':
		TmpArray.FltArray[NumOfPrm] = (float *)	MyMalloc(sizeof(float));
		ScanRes	= sscanf(*((*argv)++), "%f",
				     (float *) TmpArray.LngArray[NumOfPrm++]);
		break;
	    case 'F':
		TmpArray.DblArray[NumOfPrm] =
					   (double *) MyMalloc(sizeof(double));
		ScanRes	= sscanf(*((*argv)++), "%lf",
				    (double *) TmpArray.LngArray[NumOfPrm++]);
		break;
	    case 's':
		while ((*argc) && ((**argv)[0] != '-'))	{
		    TmpArray.ChrArray[NumOfPrm++] = *((*argv)++);
		    (*argc)--;
		}
		ScanRes	= 0;		       /* Force quit from do - loop. */
		NumOfPrm++;	    /* Updated again immediately after loop! */
		(*argv)++;					       /* "" */
		break;
	    default:
		ScanRes = 0;               /* Make optimizer warning silent. */
	}
	(*argc)--;
    }
    while (ScanRes == 1);		  /* Exactly one parameter was read. */
    (*argv)--; NumOfPrm--; (*argc)++;

    /* Now allocate the	block with the exact size, and set it: */
    Ptemp = Pmain = (int **) MyMalloc((unsigned) (NumOfPrm+1) *	sizeof(int *));
    /* And here	we use the assumption that all pointers	are the	same: */
    for (i = 0; i < NumOfPrm; i++)
	*Ptemp++ = TmpArray.IntArray[i];
    *Ptemp = NULL;		       /* Close the block with NULL pointer. */

    /* That it save the	number of parameters read as first parameter to	*/
    /* return and the pointer to the block as second, and return:	*/
    *Parameters[(*ParamCount)++] = NumOfPrm;
    GAByteCopy((char *)	Parameters[(*ParamCount)++], (char *) &Pmain,
							     sizeof(char *));
    return NumOfPrm;
}

/***************************************************************************
* Routine to scan the CtrlStr, upto Max	and count the number of	parameters *
* to that point:							   *
* 1. Each option is counted as one parameter - boolean variable	(int)	   *
* 2. Within an option, each %? or !? is	counted	once - pointer to something*
* 3. Within an option, %*? or !*? is counted twice - one for item count	   *
*    and one for pointer to block pointers.				   *
* Note ALL variables are passed	by address and so of fixed size	(address). *
***************************************************************************/
static void GASetParamCount(char *CtrlStr, int Max, int *ParamCount)
{
    int	i;

    *ParamCount	= 0;
    for (i = 0; i < Max; i++) if (ISCTRLCHAR(CtrlStr[i])) {
	if (CtrlStr[i+1] == '*')
	    *ParamCount += 2;
	else
	    (*ParamCount)++;
    }
}

/***************************************************************************
* Routine to copy exactly n bytes from Src to Dst. Note system library	   *
* routine strncpy should do the	same, but it stops on NULL char	!	   *
***************************************************************************/
static void GAByteCopy(char *Dst, char *Src, unsigned n)
{
    while (n--)	*(Dst++) = *(Src++);
}

/***************************************************************************
* Routine to check if more option (i.e.	first char == '-') exists in the   *
* given	list argc, argv:						   *
***************************************************************************/
static int GAOptionExists(int argc, char **argv)
{
    while (argc--)
	if ((*argv++)[0] == '-') return TRUE;
    return FALSE;
}

/***************************************************************************
* Routine to print some	error messages,	for this module:		   *
***************************************************************************/
void GAPrintErrMsg(int Error)
{
    fprintf(stderr, "Error in command line parsing - ");
    switch (Error) {
	case 0:;
	    fprintf(stderr, "Undefined error");
	    break;
	case CMD_ERR_NotAnOpt:
	    fprintf(stderr, "None option Found");
	    break;
	case CMD_ERR_NoSuchOpt:
	    fprintf(stderr, "Undefined option Found");
	    break;
	case CMD_ERR_WildEmpty:
	    fprintf(stderr, "Empty input for '!*?' seq.");
	    break;
	case CMD_ERR_NumRead:
	    fprintf(stderr, "Failed on reading number");
	    break;
	case CMD_ERR_AllSatis:
	    fprintf(stderr, "Fail to satisfy");
	    break;
    }
    fprintf(stderr, " - '%s'.\n", GAErrorToken);
}

/***************************************************************************
* Routine to print correct format of command line allowed:		   *
***************************************************************************/
void GAPrintHowTo(char *CtrlStr)
{
    int	i = 0, SpaceFlag;

    fprintf(stderr, "Usage: ");
    /* Print program name - first word in ctrl.	str. (optional): */
    while (!(ISSPACE(CtrlStr[i])) && (!ISCTRLCHAR(CtrlStr[i+1])))
	fprintf(stderr, "%c", CtrlStr[i++]);

    while (i < (int)strlen(CtrlStr))	{
	while ((ISSPACE(CtrlStr[i])) &&	(i < (int)strlen(CtrlStr))) i++;
	switch (CtrlStr[i+1]) {
	    case '%':
		fprintf(stderr, " [-%c", CtrlStr[i++]);
		i += 2;		     /* Skip the '%-' or '!- after the char! */
		SpaceFlag = TRUE;
		while (!ISCTRLCHAR(CtrlStr[i]) && (i < (int)strlen(CtrlStr)) &&
		    (!ISSPACE(CtrlStr[i])))
		    if (SpaceFlag) {
			if (CtrlStr[i++] == SPACE_CHAR)
			    fprintf(stderr, " ");
			else
			    fprintf(stderr, " %c", CtrlStr[i-1]);
			SpaceFlag = FALSE;
		    }
		    else if (CtrlStr[i++] == SPACE_CHAR)
			fprintf(stderr, " ");
		    else
			fprintf(stderr, "%c", CtrlStr[i-1]);
		while (!ISSPACE(CtrlStr[i]) && (i < (int)strlen(CtrlStr))) {
		    if (CtrlStr[i] == '*') fprintf(stderr, "...");
		    i++;			     /* Skip the rest of it. */
		}
		fprintf(stderr, "]");
		break;
	    case '!':
		fprintf(stderr, " -%c", CtrlStr[i++]);
		i += 2;		     /* Skip the '%-' or '!- after the char! */
		SpaceFlag = TRUE;
		while (!ISCTRLCHAR(CtrlStr[i]) && (i < (int)strlen(CtrlStr)) &&
		    (!ISSPACE(CtrlStr[i])))
		    if (SpaceFlag) {
			if (CtrlStr[i++] == SPACE_CHAR)
			    fprintf(stderr, " ");
			else
			    fprintf(stderr, " %c", CtrlStr[i-1]);
			SpaceFlag = FALSE;
		    }
		    else if (CtrlStr[i++] == SPACE_CHAR)
			fprintf(stderr, " ");
		    else
		        fprintf(stderr, "%c", CtrlStr[i-1]);
		while (!ISSPACE(CtrlStr[i]) && (i < (int)strlen(CtrlStr))) {
		    if (CtrlStr[i] == '*') fprintf(stderr, "...");
		    i++;			     /* Skip the rest of it. */
		}
		break;
	    default:		       /* Not checked, but must be last one! */
		fprintf(stderr, " ");
		while (!ISSPACE(CtrlStr[i]) && (i < (int)strlen(CtrlStr)) &&
		       !ISCTRLCHAR(CtrlStr[i]))
		    fprintf(stderr, "%c", CtrlStr[i++]);
		fprintf(stderr, "\n");
		return;
	}
    }
    fprintf(stderr, "\n");
}

#ifdef	MYMALLOC

/***************************************************************************
* My Routine to	allocate dynamic memory. All program requests must call    *
* this routine (no direct call to malloc). Dies if no memory.		   *
***************************************************************************/
static char *MyMalloc(unsigned size)
{
    char *p;

    if ((p = (char *) malloc(size)) != NULL) return p;

    fprintf(stderr, "Not enough memory, exit.\n");
    exit(2);

    return NULL;				    /* Makes warning silent. */
}

#endif	/* MYMALLOC */

⌨️ 快捷键说明

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