📄 parse.c
字号:
#include "spiht.h"
#include "spihtdecode.h"
#define QCCPARSEMAXNUMARGUMENTS 1024
#define QCCPARSESTRINGLEN QCCSTRINGLEN
static const char *QccParseGetFormat(char *fmt, const char *pnt,
int *multiple_arguments)
{
int i = 0;
*multiple_arguments = 0;
while ((*pnt != '\0')&&(*pnt != ':')&&(*pnt != ']')&&(*pnt != '[')&&
(*pnt != ' ')&&(*pnt != '\n')&&(*pnt != '\t')&&(*pnt != '\r'))
{
if (*pnt == '*')
*multiple_arguments = 1;
else
fmt[i++] = *pnt;
pnt = &(pnt[1]);
}
fmt[i] = '\0';
return(pnt);
}
static char QccParseGetType(const char *pnt, int *multiple_arguments)
{
char type = 'd';
*multiple_arguments = 0;
while ((*pnt != '\0')&&(*pnt != ':')&&(*pnt != ']')&&(*pnt != '[')&&
(*pnt != ' ')&&(*pnt != '\n')&&(*pnt != '\t')&&(*pnt != '\r'))
{
if (pnt[0] == '*')
*multiple_arguments = 1;
else
type = pnt[0];
pnt = &(pnt[1]);
}
switch (type)
{
/* Unsigned integer */
case 'u':
type = 'u';
break;
/* Floating point */
case 'e':
case 'g':
case 'f':
type = 'f';
break;
/* Character string */
case 's':
type = 's';
break;
/* Integer */
default:
type = 'd';
break;
}
if (*multiple_arguments)
{
if (strchr(pnt, ']') != NULL)
{
QccErrorAddMessage("(QccParseParameters): Multiple argument designation can only be used for non-optional parameters");
return(0);
}
if (strchr(pnt, ' ') != NULL)
{
QccErrorAddMessage("(QccParseParameters): Multiple argument designation must be last argument");
return(0);
}
}
return (type);
}
static char *QccParseFindPosition(const char *format, char *arg, int *pos)
{
const char *format_orig;
char *pnt;
const char *tmp;
char sw[QCCPARSESTRINGLEN];
int i, done = 0;;
format_orig = format;
do
{
sw[0] = '\0';
/* Find location of switch */
pnt = strstr(format, arg);
if (pnt != NULL)
{
i = 0;
/* Extract full switch from format prototype */
while ((pnt[i] != '\0')&&(pnt[i] != ':')&&
(pnt[i] != ']')&&(pnt[i] != '[')&&(pnt[i] != ' ')&&
(pnt[i] != '\n')&&(pnt[i] != '\t')&&(pnt[i] != '\r'))
sprintf(sw, "%s%c", sw, pnt[i++]);
if (pnt[i] == '\0')
pnt = NULL;
/* Make sure switches match exactly */
if (!strcmp(sw, arg))
done = 1;
else
format = &pnt[i];
}
}
while ((pnt != NULL)&&(!done));
/* Count number of pointer references in prototype to current pos */
*pos = 0;
tmp = format_orig;
if (pnt != NULL)
while (tmp != pnt)
{
if (tmp[0] == '%')
{
(*pos)++;
}
tmp = &tmp[1];
}
return(pnt);
}
static int QccParseReadParameter(int narg,
char *arg[],
char *fmt,
int pos,
int *cindex,
int multiple_arguments,
void **parse_pointer,
char *parse_type)
{
int val = 0;
int *num_args;
int **d_ptr;
unsigned int **u_ptr;
float **f_ptr;
QccString **s_ptr;
if (!multiple_arguments)
/* Use type to cast the pointer appropriately */
switch (parse_type[pos])
{
case 'f':
val = sscanf(arg[*cindex], fmt, (float *) parse_pointer[pos]);
break;
case 'u':
val = sscanf(arg[*cindex], fmt, (unsigned int *) parse_pointer[pos]);
break;
case 's':
QccConvertToQccString((char *)(parse_pointer[pos]),
arg[*cindex]);
val = strlen((char *) parse_pointer[pos]);
break;
default:
val = sscanf(arg[*cindex], fmt, (int *) parse_pointer[pos]);
break;
}
else
{
num_args = (int *)parse_pointer[pos];
*num_args = 1;
for ( ; *cindex < narg; (*cindex)++,
*num_args += 1)
switch (parse_type[pos])
{
case 'f':
f_ptr = (float **)parse_pointer[pos + 1];
if (*num_args == 1)
{
if ((*f_ptr = (float *)malloc(sizeof(float))) == NULL)
return(0);
}
else
if ((*f_ptr =
(float *)realloc(*f_ptr, sizeof(float)*(*num_args))) ==
NULL)
return(0);
val = sscanf(arg[*cindex], fmt, &(*f_ptr)[*num_args - 1]);
if (val != 1)
return(val);
break;
case 'u':
u_ptr = (unsigned int **)parse_pointer[pos + 1];
if (*num_args == 1)
{
if ((*u_ptr =
(unsigned int *)malloc(sizeof(unsigned int))) == NULL)
return(0);
}
else
if ((*u_ptr =
(unsigned int *)realloc(*u_ptr,
sizeof(unsigned int)*(*num_args)))
== NULL)
return(0);
val = sscanf(arg[*cindex], fmt, &(*u_ptr)[*num_args - 1]);
if (val != 1)
return(val);
break;
case 's':
s_ptr = (QccString **)parse_pointer[pos + 1];
if (*num_args == 1)
{
if ((*s_ptr = (QccString *)malloc(sizeof(QccString))) == NULL)
return(0);
}
else
if ((*s_ptr =
(QccString *)realloc(*(s_ptr),
sizeof(QccString) * (*num_args))) ==
NULL)
return(0);
QccConvertToQccString((*s_ptr)[*num_args - 1], arg[*cindex]);
val = strlen(arg[*cindex]);
if (val < 1)
return(val);
break;
case 'd':
d_ptr = (int **)parse_pointer[pos + 1];
if (*num_args == 1)
{
if ((*d_ptr = (int *)malloc(sizeof(int))) == NULL)
return(0);
}
else
if ((*d_ptr =
(int *)realloc(*d_ptr, sizeof(int)*(*num_args))) == NULL)
return(0);
val = sscanf(arg[*cindex], fmt, &((*d_ptr)[*num_args - 1]));
if (val != 1)
return(val);
break;
}
*num_args -= 1;
}
return(val);
}
static const char *QccParseStartOfMandatoryParameters(const char *format,
int *pos)
{
int i, done;
const char *format2, *tmp;
format2 = tmp = format;
*pos = 0;
/* Find first ']' from end of string */
/* This is last of optional parameters */
i = strlen(format2) - 1;
done = 0;
while ((i > 0)&&(!done))
{
done = (format2[i] == ']');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -