文件解析C语言代码

小灰灰 2022-08-06 07:07 275阅读 0赞

//滤除字符串首尾空格,对于整行注释的,直接返回失败
app_u32 app_trimline(app_u8 *pline, app_u8 **ppheader, app_u8 **pptail)
{
app_u8* pheader = APP_NULL;
app_u8* ptail = APP_NULL;

  1. pheader = pline;
  2. ptail = pline + strlen((char\*)pline) - 1;
  3. while ((' ' == (\*(pheader))) && ('\\0' != (\*(pheader))))
  4. \{
  5. pheader += 1;
  6. \}
  7. while ((' ' == (\*ptail)) && (pheader != ptail))
  8. \{
  9. \*ptail = '\\0';
  10. ptail -= 1;
  11. \}
  12. if ('\#' == (\*pheader))
  13. \{
  14. //注释行
  15. return APP\_INVALID\_U32;
  16. \}
  17. \*ppheader = pheader;
  18. \*pptail = ptail;
  19. 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;

  1. ullen = strlen(pline);
  2. ulrunrslt = vos\_mallocmemory((ullen+1), vos\_mem\_type\_static, (app\_void\*\*)(&pret));
  3. if (APP\_OK != ulrunrslt)
  4. \{
  5. return APP\_NULL;
  6. \}
  7. memcpy(pret, pline, ullen);
  8. pheader = pret;
  9. ptail = pret + ullen - 1;
  10. while (((' ' == (\*(pheader))) || ('\{' == (\*(pheader)))) && ('\\0' != (\*(pheader))))
  11. \{
  12. pheader += 1;
  13. \}
  14. while (((' ' == (\*ptail)) || ('\}' == (\*(ptail))) || (10 == (\*(ptail))) || (13 == (\*(ptail))))
  15. && (pheader != ptail))
  16. \{
  17. \*ptail = '\\0';
  18. ptail -= 1;
  19. \}
  20. \*ppheader = pheader;
  21. \*pptail = ptail;
  22. 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;

  1. \*poutvale = '0';
  2. \*(poutvale + 1) = '\\0';
  3. ulresult = app\_trimline(pline, &pheader, &ptail);
  4. if (APP\_OK != ulresult)
  5. \{
  6. return APP\_INVALID\_U32;
  7. \}
  8. if (APP\_NULL == strstr(pheader, pitemname))
  9. \{
  10. return APP\_INVALID\_U32;
  11. \}
  12. //偏移到赋值符号=处
  13. while (('\\0' != (\*pheader)) && ('=' != (\*pheader)))
  14. \{
  15. pheader += 1;
  16. \}
  17. if ('\\0' == (\*pheader))
  18. \{
  19. return APP\_INVALID\_U32;
  20. \}
  21. pheader += 1; //跳过赋值号=
  22. //滤除空格
  23. while (('\\0' != (\*pheader)) && (' ' == (\*pheader)))
  24. \{
  25. pheader += 1;
  26. \}
  27. ptail = pheader + strlen(pheader);
  28. //滤除空格
  29. while ((pheader != ptail) && (' ' == (\*ptail)))
  30. \{
  31. \*ptail = '\\0';
  32. ptail -= 1;
  33. \}
  34. if (ptail <= pheader)
  35. \{
  36. return APP\_INVALID\_U32;
  37. \}
  38. memcpy(poutvale, pheader, strlen(pheader));
  39. return APP\_OK;

}

//移动到下一个token
app_void app_movenexttoken(app_u8* puchead, app_u32 ultotallen, app_u8** ppcurpos)
{
app_u8* puctmp = *ppcurpos;

  1. if (APP\_NULL == puctmp)
  2. \{
  3. return;
  4. \}
  5. if (ultotallen <= (((app\_u32)puctmp) - ((app\_u32)puchead)))
  6. \{
  7. \*ppcurpos = APP\_NULL;
  8. return;
  9. \}
  10. while (('\\0' != \*puctmp) && (ultotallen > (((app\_u32)puctmp) - ((app\_u32)puchead))))
  11. \{
  12. puctmp += 1;
  13. \}
  14. if (ultotallen <= (((app\_u32)puctmp) - ((app\_u32)puchead)))
  15. \{
  16. \*ppcurpos = APP\_NULL;
  17. return;
  18. \}
  19. if ('\\0' == \*puctmp)
  20. \{
  21. puctmp += 1;
  22. \}
  23. \*ppcurpos = puctmp;
  24. 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;

  1. if (APP\_NULL == pucstr)
  2. \{
  3. return APP\_NULL;
  4. \}
  5. ulstrlen = strlen(pucstr);
  6. ulrunrslt = vos\_mallocmemory((ulstrlen+1), vos\_mem\_type\_static, (app\_void\*\*)(&puchead));
  7. if (APP\_OK != ulrunrslt)
  8. \{
  9. return APP\_NULL;
  10. \}
  11. memcpy(puchead, pucstr, ulstrlen);
  12. for (ulloopvar = 0; ulstrlen > ulloopvar; ulloopvar++)
  13. \{
  14. if (ulspschar == (\*(puchead + ulloopvar)))
  15. \{
  16. \*(puchead + ulloopvar) = '\\0';
  17. \}
  18. \}
  19. 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};

  1. ulrunrslt = app\_trimline(pline, &pheader, &ptail);
  2. if (APP\_OK != ulrunrslt)
  3. \{
  4. return APP\_INVALID\_U32;
  5. \}
  6. sprintf(auckeyname, "\[%s\]", pkeyname);
  7. if (APP\_NULL == strstr(pline, auckeyname))
  8. \{
  9. return APP\_INVALID\_U32;
  10. \}
  11. printf(" find keyname %s \\r\\n ", auckeyname);
  12. 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;

  1. ulrunrslt = app\_trimline(pline, &pheader, &ptail);
  2. if (APP\_OK != ulrunrslt)
  3. \{
  4. return APP\_INVALID\_U32;
  5. \}
  6. //解析num
  7. ulrunrslt = app\_getvaluebyitemname(pheader, "num", (app\_u8\*)aucstrvalue);
  8. if (APP\_OK != ulrunrslt)
  9. \{
  10. return APP\_INVALID\_U32;
  11. \}
  12. ulval = atoi((app\_u8\*)aucstrvalue);
  13. \*ptotalnum = ulval;
  14. printf(" find num %u \\r\\n ", (int)ulval);
  15. 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;

  1. //"|"分隔使用变量
  2. app\_u8 \*pnewvalue = APP\_NULL;
  3. app\_u32 ultotallen = 0;
  4. app\_u8 \*pcurpos = APP\_NULL;
  5. app\_u8 \*ptmpheader = APP\_NULL;
  6. app\_u8 \*ptmptail = APP\_NULL;
  7. //","分隔使用变量
  8. app\_u8 \*pcommavalue = APP\_NULL;
  9. app\_u32 ulcommalen = 0;
  10. app\_u8 \*pcommacurpos = APP\_NULL;
  11. ulrunrslt = app\_trimline(pstrvalue, &pheader, &ptail);
  12. if (APP\_OK != ulrunrslt)
  13. \{
  14. return APP\_INVALID\_U32;
  15. \}
  16. ultotallen = strlen(pheader);
  17. pnewvalue = app\_splitstrbytoken(pheader, '|'); //\{0,1,20\}|\{0,2,30\} ==> \{0,1,20\} \\0 \{0,2,30\}
  18. pcurpos = pnewvalue;
  19. while (APP\_NULL != pcurpos)
  20. \{
  21. printf(" find \[|\] %s \\r\\n ", pcurpos);
  22. ptmpheader = APP\_NULL;
  23. ptmptail = APP\_NULL;
  24. ptmpval = app\_trimbrace(pcurpos, &ptmpheader, &ptmptail); //\{0,1,20\} ==> \\0 0,1,20 \\0
  25. printf(" find \[\{\}\] %s \\r\\n ", ptmpheader);
  26. ulcommalen = strlen(ptmpheader);
  27. pcommavalue = app\_splitstrbytoken(ptmpheader, ','); //0,1,20 ==> 0 \\0 1 \\0 20
  28. DELETE\_MEMEORY(ptmpval);
  29. pcommacurpos = pcommavalue;
  30. ultmpcount = 0;
  31. while (APP\_NULL != pcommacurpos)
  32. \{
  33. ulnameval = atoi(pcommacurpos);
  34. printf(" find \[,\] %d ulcurpos = %d ulnumperline = %d\\r\\n ", (int)ulnameval,
  35. (int)ulcurpos, (int)ulnumperline);
  36. if (0 == ultmpcount)
  37. \{
  38. pcompensation->ppara\[ulcurpos+ulnumperline\].location = ulnameval;
  39. \}
  40. if (1 == ultmpcount)
  41. \{
  42. pcompensation->ppara\[ulcurpos+ulnumperline\].dir = ulnameval;
  43. \}
  44. if (2 == ultmpcount)
  45. \{
  46. pcompensation->ppara\[ulcurpos+ulnumperline\].step = ulnameval;
  47. \}
  48. ultmpcount += 1;
  49. app\_movenexttoken(pcommavalue, ulcommalen, &pcommacurpos);
  50. \}
  51. DELETE\_MEMEORY(pcommavalue);
  52. ulnumperline++;
  53. app\_movenexttoken(pnewvalue, ultotallen, &pcurpos);
  54. \}
  55. DELETE\_MEMEORY(pnewvalue);
  56. \*poutnum = ulnumperline;
  57. printf(" find num per line = %d \\r\\n ", (int)ulnumperline);
  58. 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};

  1. ulrunrslt = app\_trimline(pline, &pheader, &ptail);
  2. if (APP\_OK != ulrunrslt)
  3. \{
  4. return APP\_INVALID\_U32;
  5. \}
  6. //第一次获取该行,需要从itemname开始查找
  7. if (1 == lineno)
  8. \{
  9. //解析value
  10. ulrunrslt = app\_getvaluebyitemname(pheader, "value", (app\_u8\*)aucstrvalue);
  11. if (APP\_OK != ulrunrslt)
  12. \{
  13. return APP\_INVALID\_U32;
  14. \}
  15. \}
  16. else
  17. \{
  18. if (('\{' != (\*pheader)) && ('|' != (\*pheader)))
  19. \{
  20. return APP\_INVALID\_U32;
  21. \}
  22. if ('|' == (\*pheader))
  23. \{
  24. memcpy(aucstrvalue, pheader+1, strlen(pheader)-1);
  25. \}
  26. else
  27. \{
  28. memcpy(aucstrvalue, pheader, strlen(pheader));
  29. \}
  30. \}
  31. printf(" \\r\\n find \#%d\# value %s \\r\\n ", (int)lineno, aucstrvalue);
  32. //解析value
  33. ulrunrslt = app\_parsecompensationvalue(aucstrvalue, poutnum, pcompensation, curpos);
  34. if (APP\_OK != ulrunrslt)
  35. \{
  36. return APP\_INVALID\_U32;
  37. \}
  38. 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;

  1. vos\_getworkpath((app\_u8\*)aucpath);
  2. sprintf(aucfile, "%s/%s", (char\*)aucpath, "compensation.txt");
  3. pFile = fopen((char\*)aucfile, "r");
  4. if (APP\_NULL == pFile)
  5. \{
  6. return APP\_INVALID\_U32;
  7. \}
  8. printf(" \\r\\n find file %s \\r\\n ", aucfile);
  9. memset((app\_u8\*)aucline, 0x00, sizeof(aucline));
  10. while (APP\_NULL != fgets((app\_u8\*)aucline, sizeof(aucline), pFile))
  11. \{
  12. //查找关键字
  13. if (APP\_OK != app\_findcompensationkeyname(pkeyname, (app\_u8\*)aucline))
  14. \{
  15. continue;
  16. \}
  17. memset((app\_u8\*)aucline, 0x00, sizeof(aucline));
  18. if (APP\_NULL == fgets((app\_u8\*)aucline, sizeof(aucline), pFile))
  19. \{
  20. break;
  21. \}
  22. //解析num
  23. ulresult = app\_findcompensationnum((app\_u8\*)aucline, &ultotalnum);
  24. if (APP\_OK != ulresult)
  25. \{
  26. break;
  27. \}
  28. //构造补偿对象
  29. ulmemsize = sizeof(cmd\_dwnmachine\_compensation) + ultotalnum\*sizeof(cmd\_compensation\_para)
  30. - sizeof(cmd\_compensation\_para \*);
  31. ulresult = vos\_mallocmemory(ulmemsize, vos\_mem\_type\_static, (vos\_void\*\*)&pcompensation);
  32. if (APP\_OK != ulresult)
  33. \{
  34. break;
  35. \}
  36. pcompensation->cmd = 0x04c0;
  37. pcompensation->type = 1;
  38. pcompensation->paranum = ultotalnum;
  39. if (0 == ultotalnum)
  40. \{
  41. break;
  42. \}
  43. ulmemsize = ultotalnum\*sizeof(cmd\_compensation\_para);
  44. ulresult = vos\_mallocmemory(ulmemsize, vos\_mem\_type\_static, (vos\_void\*\*)&(pcompensation->ppara));
  45. if (APP\_OK != ulresult)
  46. \{
  47. break;
  48. \}
  49. //解析多行value
  50. memset((app\_u8\*)aucline, 0x00, sizeof(aucline));
  51. while (APP\_NULL != fgets((app\_u8\*)aucline, sizeof(aucline), pFile))
  52. \{
  53. ullineno++;
  54. ultmpnum = 0;
  55. ulresult = app\_findcompensationvalue((app\_u8\*)aucline, ullineno, &ultmpnum,
  56. pcompensation, ulcurnum);
  57. if (APP\_OK != ulresult)
  58. \{
  59. DELETE\_MEMEORY(pcompensation->ppara);
  60. DELETE\_MEMEORY(pcompensation);
  61. break;
  62. \}
  63. ulcurnum += ultmpnum;
  64. //所有参数值已获取
  65. if (ulcurnum >= ultotalnum)
  66. \{
  67. break;
  68. \}
  69. memset((app\_u8\*)aucline, 0x00, sizeof(aucline));
  70. \}
  71. \}
  72. \*ppcompensation = pcompensation;
  73. fclose(pFile);
  74. pFile = APP\_NULL;
  75. \#if 1 //打印
  76. printf(" cmd = %d \\r\\n", (int)pcompensation->cmd);
  77. printf(" type = %d \\r\\n", (int)pcompensation->type);
  78. printf(" paranum = %d \\r\\n", (int)pcompensation->paranum);
  79. for (ulindex = 0; ulindex < pcompensation->paranum; ulindex++)
  80. \{
  81. printf(" \[%d\] location = %d \\r\\n", (int)ulindex, (int)pcompensation->ppara\[ulindex\].location);
  82. printf(" \[%d\] dir = %d \\r\\n", (int)ulindex, (int)pcompensation->ppara\[ulindex\].dir);
  83. printf(" \[%d\] step = %d \\r\\n", (int)ulindex, (int)pcompensation->ppara\[ulindex\].step);
  84. \}
  85. \#endif
  86. 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}

发表评论

表情:
评论列表 (有 0 条评论,275人围观)

还没有评论,来说两句吧...

相关阅读

    相关 C语言如何解析配置文件

    一般服务器都是通过配置文件的解析获取关键的参数,不能够老是通过执行命令传进来,那么就有了配置文件,有了配置文件,就需要做配置文件的解析。 一般C语言格式配置文件的格式: