📄 parse.c
字号:
if (!done)
i--;
}
/* Scan until we find '%' */
/* This is first mandatory parameter */
format2 = &format2[i];
while((format2[0] != '\0')&&(format2[0] != '%'))
format2 = &format2[1];
/* Get count of the pointer reference */
while (tmp != format2)
{
if (tmp[0] == '%')
(*pos)++;
tmp = &tmp[1];
}
return(format2);
}
static void QccParsePrintUsage(const char *format)
{
QccString program_name;
QccString usage_string;
QccStringMakeNull(program_name);
QccStringMakeNull(usage_string);
QccGetProgramName(program_name);
QccPrintQccPackVersion(stderr);
sprintf(usage_string, "Usage: %s ", program_name);
while (format[0] != '\0')
{
/* Skip to label */
if (format[0] == '%')
{
while ((format[0] != ':')&&(format[0] != ']')&&
(format[0] != '[')&&(format[0] != ' ')&&
(format[0] != '\n')&&(format[0] != '\t')&&
(format[0] != '\r'))
format = &format[1];
if (format[0] == ':')
format = &format[1];
}
/* Print labels and switches */
if (format[0] != '\0')
{
sprintf(usage_string, "%s%c", usage_string, format[0]);
format = &format[1];
}
}
QccErrorAddMessage(usage_string);
}
int QccParseParametersVA(int argc, char *argv[], const char *format,
va_list ap)
{
int done, switch_done;
const char *format2;
const char *pnt;
char fmt[QCCPARSESTRINGLEN];
int cindex, i, pos;
int multiple_arguments;
int return_value = 0;
void **parse_pointer = NULL;
char *parse_type = NULL;
if ((parse_pointer =
(void **)calloc(QCCPARSEMAXNUMARGUMENTS, sizeof(void *))) == NULL)
{
QccErrorAddMessage("(QccParseParameters): Error allocating memory");
goto Error;
}
if ((parse_type =
(char *)calloc(QCCPARSEMAXNUMARGUMENTS, sizeof(char))) == NULL)
{
QccErrorAddMessage("(QccParseParameters): Error allocating memory");
goto Error;
}
/* Get pointers */
i = 0;
pnt = &format[0];
while ((i < QCCPARSEMAXNUMARGUMENTS) && (pnt[0] != '\0'))
{
if (pnt[0] == '%')
{
parse_type[i] = QccParseGetType(pnt, &multiple_arguments);
if (multiple_arguments)
switch (parse_type[i])
{
case 'f':
parse_pointer[i++] = (void *) va_arg(ap, int *);
parse_pointer[i] = (void *) va_arg(ap, float **);
*((float **)parse_pointer[i++]) = NULL;
break;
case 'u':
parse_pointer[i++] = (void *) va_arg(ap, int *);
parse_pointer[i] = (void *) va_arg(ap, unsigned int **);
*((unsigned int **)parse_pointer[i++]) = NULL;
break;
case 's':
parse_pointer[i++] = (void *) va_arg(ap, int *);
parse_pointer[i] = (void *) va_arg(ap, char ***);
*((unsigned char ***)parse_pointer[i++]) = NULL;
break;
case 'd':
parse_pointer[i++] = (void *) va_arg(ap, int *);
parse_pointer[i] = (void *) va_arg(ap, int **);
*((int **)parse_pointer[i++]) = NULL;
break;
}
else
switch (parse_type[i])
{
case 'f':
parse_pointer[i++] = (void *) va_arg(ap, float *);
break;
case 'u':
parse_pointer[i++] = (void *) va_arg(ap, unsigned int *);
break;
case 's':
parse_pointer[i++] = (void *) va_arg(ap, char *);
break;
case 'd':
parse_pointer[i++] = (void *) va_arg(ap, int *);
break;
default:
goto QccParseErrorInUsageStringReturn;
}
}
pnt = &pnt[1];
}
if (i >= QCCPARSEMAXNUMARGUMENTS)
{
QccErrorAddMessage("(QccParseParameters): Too many arguments");
goto QccParseErrorInUsageStringReturn;
}
/* Process all switches first */
cindex = 1;
switch_done = 0;
while ((cindex < argc)&&!switch_done)
{
/* If not a switch then done */
if (argv[cindex][0] != '-')
switch_done = 1;
else
{
/* Find switch in prototype */
pnt = QccParseFindPosition(format, argv[cindex], &pos);
if (pnt == NULL)
goto QccParseParseErrorReturn;
cindex++;
done = 0;
while(!done)
{
/* Pointer reference */
if (pnt[0] == '%')
{
/* Pointer flag, no associated value */
if (pnt[1] == ':')
{
/* Set the flag */
*(int *)parse_pointer[pos++] = 1;
pnt = &pnt[2];
}
else
{
if (cindex >= argc)
goto QccParseParseErrorReturn;
/* Get format for argument */
pnt = QccParseGetFormat(fmt, pnt, &multiple_arguments);
/* Get value of argument */
if (!QccParseReadParameter(argc, argv,
fmt, pos++, &cindex, 0,
parse_pointer,
parse_type))
goto QccParseParseErrorReturn;
cindex++;
}
}
else
{
/* Keep searching till end of switch's parameters */
if ((pnt[0] == '\0')||(pnt[0] == ']')||(pnt[0] == '['))
done = 1;
else
pnt = &pnt[1];
}
}
}
}
/* Now do all mandatory parameters */
format2 = QccParseStartOfMandatoryParameters(format, &pos);
/* Loop till end of string or out of arguments */
while ((format2[0] != '\0')&&(cindex < argc))
{
/* Pointer reference */
if (format2[0] == '%')
{
/* Get format for argument */
format2 = QccParseGetFormat(fmt, format2, &multiple_arguments);
/* Get value of argument */
if (!QccParseReadParameter(argc, argv,
fmt, pos++, &cindex, multiple_arguments,
parse_pointer, parse_type))
goto QccParseParseErrorReturn;
}
cindex++;
/* Loop to next pointer reference */
while((format2[0] != '\0')&&(format2[0] != '%'))
format2 = &format2[1];
}
/* Check to see if all mandatory paramters have been processed */
if ((format2[0] != '\0') || (cindex < argc))
goto QccParseParseErrorReturn;
/* Normal error-free return */
return_value = 0;
goto Return;
QccParseParseErrorReturn:
QccParsePrintUsage(format);
goto Error;
QccParseErrorInUsageStringReturn:
QccErrorAddMessage("(QccParseParameters): Error in usage string");
goto Error;
Error:
return_value = 1;
Return:
if (parse_pointer != NULL)
free(parse_pointer);
if (parse_type != NULL)
free(parse_type);
return(return_value);
}
int QccParseParameters(int argc, char *argv[], const char *format, ...)
{
int return_value;
va_list ap;
va_start(ap, format);
return_value = QccParseParametersVA(argc, argv, format, ap);
va_end(ap);
return(return_value);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -