文件解析C语言代码
//滤除字符串首尾空格,对于整行注释的,直接返回失败
app_u32 app_trimline(app_u8 *pline, app_u8 **ppheader, app_u8 **pptail)
{
app_u8* pheader = APP_NULL;
app_u8* ptail = APP_NULL;
pheader = pline;
ptail = pline + strlen((char\*)pline) - 1;
while ((' ' == (\*(pheader))) && ('\\0' != (\*(pheader))))
\{
pheader += 1;
\}
while ((' ' == (\*ptail)) && (pheader != ptail))
\{
\*ptail = '\\0';
ptail -= 1;
\}
if ('\#' == (\*pheader))
\{
//注释行
return APP\_INVALID\_U32;
\}
\*ppheader = pheader;
\*pptail = ptail;
return APP\_OK;
}
//过滤掉两端的花括号
app_u8* app_trimbrace(app_u8 *pline, app_u8 **ppheader, app_u8 **pptail)
{
app_u32 ulrunrslt = APP_OK;
app_u8* pret = APP_NULL;
app_u8* pheader = APP_NULL;
app_u8* ptail = APP_NULL;
app_u32 ullen = 0;
ullen = strlen(pline);
ulrunrslt = vos\_mallocmemory((ullen+1), vos\_mem\_type\_static, (app\_void\*\*)(&pret));
if (APP\_OK != ulrunrslt)
\{
return APP\_NULL;
\}
memcpy(pret, pline, ullen);
pheader = pret;
ptail = pret + ullen - 1;
while (((' ' == (\*(pheader))) || ('\{' == (\*(pheader)))) && ('\\0' != (\*(pheader))))
\{
pheader += 1;
\}
while (((' ' == (\*ptail)) || ('\}' == (\*(ptail))) || (10 == (\*(ptail))) || (13 == (\*(ptail))))
&& (pheader != ptail))
\{
\*ptail = '\\0';
ptail -= 1;
\}
\*ppheader = pheader;
\*pptail = ptail;
return pret;
}
//通过项目名称找到对应的值
app_u32 app_getvaluebyitemname(app_u8* pline, app_u8* pitemname,
app_u8* poutvale)
{
app_u32 ulresult = APP_OK;
app_u8* pheader = APP_NULL;
app_u8* ptail = APP_NULL;
\*poutvale = '0';
\*(poutvale + 1) = '\\0';
ulresult = app\_trimline(pline, &pheader, &ptail);
if (APP\_OK != ulresult)
\{
return APP\_INVALID\_U32;
\}
if (APP\_NULL == strstr(pheader, pitemname))
\{
return APP\_INVALID\_U32;
\}
//偏移到赋值符号=处
while (('\\0' != (\*pheader)) && ('=' != (\*pheader)))
\{
pheader += 1;
\}
if ('\\0' == (\*pheader))
\{
return APP\_INVALID\_U32;
\}
pheader += 1; //跳过赋值号=
//滤除空格
while (('\\0' != (\*pheader)) && (' ' == (\*pheader)))
\{
pheader += 1;
\}
ptail = pheader + strlen(pheader);
//滤除空格
while ((pheader != ptail) && (' ' == (\*ptail)))
\{
\*ptail = '\\0';
ptail -= 1;
\}
if (ptail <= pheader)
\{
return APP\_INVALID\_U32;
\}
memcpy(poutvale, pheader, strlen(pheader));
return APP\_OK;
}
//移动到下一个token
app_void app_movenexttoken(app_u8* puchead, app_u32 ultotallen, app_u8** ppcurpos)
{
app_u8* puctmp = *ppcurpos;
if (APP\_NULL == puctmp)
\{
return;
\}
if (ultotallen <= (((app\_u32)puctmp) - ((app\_u32)puchead)))
\{
\*ppcurpos = APP\_NULL;
return;
\}
while (('\\0' != \*puctmp) && (ultotallen > (((app\_u32)puctmp) - ((app\_u32)puchead))))
\{
puctmp += 1;
\}
if (ultotallen <= (((app\_u32)puctmp) - ((app\_u32)puchead)))
\{
\*ppcurpos = APP\_NULL;
return;
\}
if ('\\0' == \*puctmp)
\{
puctmp += 1;
\}
\*ppcurpos = puctmp;
return;
}
//根据指定的Token分离字符串
app_u8* app_splitstrbytoken(app_u8* pucstr, app_u32 ulspschar)
{
app_u32 ulrunrslt = APP_OK;
app_u32 ulloopvar = 0;
app_u32 ulstrlen = 0;
app_u8* puchead = APP_NULL;
if (APP\_NULL == pucstr)
\{
return APP\_NULL;
\}
ulstrlen = strlen(pucstr);
ulrunrslt = vos\_mallocmemory((ulstrlen+1), vos\_mem\_type\_static, (app\_void\*\*)(&puchead));
if (APP\_OK != ulrunrslt)
\{
return APP\_NULL;
\}
memcpy(puchead, pucstr, ulstrlen);
for (ulloopvar = 0; ulstrlen > ulloopvar; ulloopvar++)
\{
if (ulspschar == (\*(puchead + ulloopvar)))
\{
\*(puchead + ulloopvar) = '\\0';
\}
\}
return puchead;
}
//获取补偿关键字
app_u32 app_findcompensationkeyname(app_u8 *pkeyname, app_u8 *pline)
{
app_u32 ulrunrslt = APP_OK;
app_u8* pheader = APP_NULL;
app_u8* ptail = APP_NULL;
app_u8 auckeyname[MAX_KEYNAME_LEN] = {0};
ulrunrslt = app\_trimline(pline, &pheader, &ptail);
if (APP\_OK != ulrunrslt)
\{
return APP\_INVALID\_U32;
\}
sprintf(auckeyname, "\[%s\]", pkeyname);
if (APP\_NULL == strstr(pline, auckeyname))
\{
return APP\_INVALID\_U32;
\}
printf(" find keyname %s \\r\\n ", auckeyname);
return APP\_OK;
}
//获取补偿参数个数
app_u32 app_findcompensationnum(app_u8 *pline, app_u32 *ptotalnum)
{
app_u32 ulrunrslt = APP_OK;
app_u8* pheader = APP_NULL;
app_u8* ptail = APP_NULL;
app_u8 aucstrvalue[20] = {0};
app_u32 ulval = 0;
ulrunrslt = app\_trimline(pline, &pheader, &ptail);
if (APP\_OK != ulrunrslt)
\{
return APP\_INVALID\_U32;
\}
//解析num
ulrunrslt = app\_getvaluebyitemname(pheader, "num", (app\_u8\*)aucstrvalue);
if (APP\_OK != ulrunrslt)
\{
return APP\_INVALID\_U32;
\}
ulval = atoi((app\_u8\*)aucstrvalue);
\*ptotalnum = ulval;
printf(" find num %u \\r\\n ", (int)ulval);
return APP\_OK;
}
//解析补偿值
app_u32 app_parsecompensationvalue(app_u8 *pstrvalue, app_u32 *poutnum,
cmd_dwnmachine_compensation *pcompensation, app_u32 ulcurpos)
{
app_u32 ulrunrslt = APP_OK;
app_u8* pheader = APP_NULL;
app_u8* ptail = APP_NULL;
app_u8* ptmpval = APP_NULL;
app_u32 ultmpcount = 0;
app_u32 ulnameval = 0;
app_u32 ulnumperline = 0;
//"|"分隔使用变量
app\_u8 \*pnewvalue = APP\_NULL;
app\_u32 ultotallen = 0;
app\_u8 \*pcurpos = APP\_NULL;
app\_u8 \*ptmpheader = APP\_NULL;
app\_u8 \*ptmptail = APP\_NULL;
//","分隔使用变量
app\_u8 \*pcommavalue = APP\_NULL;
app\_u32 ulcommalen = 0;
app\_u8 \*pcommacurpos = APP\_NULL;
ulrunrslt = app\_trimline(pstrvalue, &pheader, &ptail);
if (APP\_OK != ulrunrslt)
\{
return APP\_INVALID\_U32;
\}
ultotallen = strlen(pheader);
pnewvalue = app\_splitstrbytoken(pheader, '|'); //\{0,1,20\}|\{0,2,30\} ==> \{0,1,20\} \\0 \{0,2,30\}
pcurpos = pnewvalue;
while (APP\_NULL != pcurpos)
\{
printf(" find \[|\] %s \\r\\n ", pcurpos);
ptmpheader = APP\_NULL;
ptmptail = APP\_NULL;
ptmpval = app\_trimbrace(pcurpos, &ptmpheader, &ptmptail); //\{0,1,20\} ==> \\0 0,1,20 \\0
printf(" find \[\{\}\] %s \\r\\n ", ptmpheader);
ulcommalen = strlen(ptmpheader);
pcommavalue = app\_splitstrbytoken(ptmpheader, ','); //0,1,20 ==> 0 \\0 1 \\0 20
DELETE\_MEMEORY(ptmpval);
pcommacurpos = pcommavalue;
ultmpcount = 0;
while (APP\_NULL != pcommacurpos)
\{
ulnameval = atoi(pcommacurpos);
printf(" find \[,\] %d ulcurpos = %d ulnumperline = %d\\r\\n ", (int)ulnameval,
(int)ulcurpos, (int)ulnumperline);
if (0 == ultmpcount)
\{
pcompensation->ppara\[ulcurpos+ulnumperline\].location = ulnameval;
\}
if (1 == ultmpcount)
\{
pcompensation->ppara\[ulcurpos+ulnumperline\].dir = ulnameval;
\}
if (2 == ultmpcount)
\{
pcompensation->ppara\[ulcurpos+ulnumperline\].step = ulnameval;
\}
ultmpcount += 1;
app\_movenexttoken(pcommavalue, ulcommalen, &pcommacurpos);
\}
DELETE\_MEMEORY(pcommavalue);
ulnumperline++;
app\_movenexttoken(pnewvalue, ultotallen, &pcurpos);
\}
DELETE\_MEMEORY(pnewvalue);
\*poutnum = ulnumperline;
printf(" find num per line = %d \\r\\n ", (int)ulnumperline);
return APP\_OK;
}
//获取补偿参数
app_u32 app_findcompensationvalue(app_u8 *pline, app_u32 lineno,
app_u32 *poutnum, cmd_dwnmachine_compensation *pcompensation, app_u32 curpos)
{
app_u32 ulrunrslt = APP_OK;
app_u8* pheader = APP_NULL;
app_u8* ptail = APP_NULL;
app_u8 aucstrvalue[MAX_LINE_LEN] = {0};
ulrunrslt = app\_trimline(pline, &pheader, &ptail);
if (APP\_OK != ulrunrslt)
\{
return APP\_INVALID\_U32;
\}
//第一次获取该行,需要从itemname开始查找
if (1 == lineno)
\{
//解析value
ulrunrslt = app\_getvaluebyitemname(pheader, "value", (app\_u8\*)aucstrvalue);
if (APP\_OK != ulrunrslt)
\{
return APP\_INVALID\_U32;
\}
\}
else
\{
if (('\{' != (\*pheader)) && ('|' != (\*pheader)))
\{
return APP\_INVALID\_U32;
\}
if ('|' == (\*pheader))
\{
memcpy(aucstrvalue, pheader+1, strlen(pheader)-1);
\}
else
\{
memcpy(aucstrvalue, pheader, strlen(pheader));
\}
\}
printf(" \\r\\n find \#%d\# value %s \\r\\n ", (int)lineno, aucstrvalue);
//解析value
ulrunrslt = app\_parsecompensationvalue(aucstrvalue, poutnum, pcompensation, curpos);
if (APP\_OK != ulrunrslt)
\{
return APP\_INVALID\_U32;
\}
return APP\_OK;
}
//根据关键字从文件中获取补偿参数
app_u32 app_getcompensationparabykeyfromfile(app_u8 *pkeyname, cmd_dwnmachine_compensation **ppcompensation)
{
app_u32 ulresult = APP_OK;
FILE *pFile = NULL;
app_u8 aucpath[MAX_PATH_LEN] = {0};
app_u8 aucfile[MAX_FILE_LEN] = {0};
app_u8 aucline[MAX_LINE_LEN] = {0};
app_u32 ultotalnum = 0; //总个数
app_u32 ultmpnum = 0; //解析一行得出的个数
app_u32 ulcurnum = 0; //当前解析出的总个数
app_u32 ulmemsize = 0;
app_u32 ullineno = 0; //行序号
cmd_dwnmachine_compensation *pcompensation = APP_NULL;
app_u32 ulindex = 0;
vos\_getworkpath((app\_u8\*)aucpath);
sprintf(aucfile, "%s/%s", (char\*)aucpath, "compensation.txt");
pFile = fopen((char\*)aucfile, "r");
if (APP\_NULL == pFile)
\{
return APP\_INVALID\_U32;
\}
printf(" \\r\\n find file %s \\r\\n ", aucfile);
memset((app\_u8\*)aucline, 0x00, sizeof(aucline));
while (APP\_NULL != fgets((app\_u8\*)aucline, sizeof(aucline), pFile))
\{
//查找关键字
if (APP\_OK != app\_findcompensationkeyname(pkeyname, (app\_u8\*)aucline))
\{
continue;
\}
memset((app\_u8\*)aucline, 0x00, sizeof(aucline));
if (APP\_NULL == fgets((app\_u8\*)aucline, sizeof(aucline), pFile))
\{
break;
\}
//解析num
ulresult = app\_findcompensationnum((app\_u8\*)aucline, &ultotalnum);
if (APP\_OK != ulresult)
\{
break;
\}
//构造补偿对象
ulmemsize = sizeof(cmd\_dwnmachine\_compensation) + ultotalnum\*sizeof(cmd\_compensation\_para)
- sizeof(cmd\_compensation\_para \*);
ulresult = vos\_mallocmemory(ulmemsize, vos\_mem\_type\_static, (vos\_void\*\*)&pcompensation);
if (APP\_OK != ulresult)
\{
break;
\}
pcompensation->cmd = 0x04c0;
pcompensation->type = 1;
pcompensation->paranum = ultotalnum;
if (0 == ultotalnum)
\{
break;
\}
ulmemsize = ultotalnum\*sizeof(cmd\_compensation\_para);
ulresult = vos\_mallocmemory(ulmemsize, vos\_mem\_type\_static, (vos\_void\*\*)&(pcompensation->ppara));
if (APP\_OK != ulresult)
\{
break;
\}
//解析多行value
memset((app\_u8\*)aucline, 0x00, sizeof(aucline));
while (APP\_NULL != fgets((app\_u8\*)aucline, sizeof(aucline), pFile))
\{
ullineno++;
ultmpnum = 0;
ulresult = app\_findcompensationvalue((app\_u8\*)aucline, ullineno, &ultmpnum,
pcompensation, ulcurnum);
if (APP\_OK != ulresult)
\{
DELETE\_MEMEORY(pcompensation->ppara);
DELETE\_MEMEORY(pcompensation);
break;
\}
ulcurnum += ultmpnum;
//所有参数值已获取
if (ulcurnum >= ultotalnum)
\{
break;
\}
memset((app\_u8\*)aucline, 0x00, sizeof(aucline));
\}
\}
\*ppcompensation = pcompensation;
fclose(pFile);
pFile = APP\_NULL;
\#if 1 //打印
printf(" cmd = %d \\r\\n", (int)pcompensation->cmd);
printf(" type = %d \\r\\n", (int)pcompensation->type);
printf(" paranum = %d \\r\\n", (int)pcompensation->paranum);
for (ulindex = 0; ulindex < pcompensation->paranum; ulindex++)
\{
printf(" \[%d\] location = %d \\r\\n", (int)ulindex, (int)pcompensation->ppara\[ulindex\].location);
printf(" \[%d\] dir = %d \\r\\n", (int)ulindex, (int)pcompensation->ppara\[ulindex\].dir);
printf(" \[%d\] step = %d \\r\\n", (int)ulindex, (int)pcompensation->ppara\[ulindex\].step);
\}
\#endif
return ulresult;
}
文件格式:
#反应盘复位补偿参数
[REACT_DISK_RESET]
num = 3
value = {0,1,20} | {0,2, 30}| { 51, 1, 10 }
#反应盘旋转补偿参数
[REACT_DISK_RTT]
num = 6
value = {1, 1, 10}|{2, 1, 10}|{51,1, 10}
| {11,1, 900}
| {11,1, 900}
| {11,1, 900}
还没有评论,来说两句吧...