Fatfs对ini文件的逐行解析,结果存在预定的buffer里。也可以显示在enwin等界面上。
希望大家给出改进建议!
/**********************************************************
ini解析函数,可滤除所有空格 TAB
滤除空行
丢弃缺少"="的行
丢弃缺少"]"的类
ZZY 2015.01
**********************************************************/
[mw_shl_code=c,true]#include "ff.h"
#include "string.h"
#include "stdio.h"
#include "Fs_Operate.h"
#include "GUI_Creat.h"
FATFS fs; //必须在函数外面,栈过小[/mw_shl_code]
[mw_shl_code=c,true]/****************************************[/mw_shl_code]
[mw_shl_code=c,true]ini解析函数 去除所有空格 TAB 空行[/mw_shl_code]
[mw_shl_code=c,true]去除无效行[/mw_shl_code]
[mw_shl_code=c,true]by ZZY 2015.01[/mw_shl_code]
[mw_shl_code=c,true]****************************************/[/mw_shl_code]
[mw_shl_code=c,true]void ReadFileData(char* File_Des) //输入文件名
{
FRESULT result;
//FATFS fs;
FIL file;
DIR DirInf;
//uint32_t bw;
result = f_mount(0, fs[0]);
if (result != FR_OK)
{
//printf("?????????????§°?(%d)\r\n", result);
}
result = f_opendir(&DirInf, "/");
if (result != FR_OK)
{
//printf("?ò???ù?????§°?(%d)\r\n", result);
return;
}
result = f_open(&file,File_Des, FA_OPEN_EXISTING | FA_READ);
if (result != FR_OK)
{
printf("Not a File \r\n");
return;
}
while (1)
{ //char* p = 0;
char Print_buf1[20]; //文件名
char Print_buf[30];
char buf[70]; // > 30+20+1+1+??????
unsigned char i,j;
unsigned char Valid_Flag = 0;
if(f_gets(buf, sizeof(buf), &file)){
printf("%s", buf);
//f_lseek(&file, file.fptr - strlen(buf));
memset(Print_buf,0,sizeof(Print_buf));
memset(Print_buf1,0,sizeof(Print_buf1));
if(buf[0] == ';'){
printf(" >注释\r\n*");
}
else if(buf[0] == '\r' || buf[0] == '\n'){
printf(" >空行\r\n*");//????
}
else if(buf[0] == '['){
for(i=1,j=0; j<sizeof(Print_buf); i++){
if(buf != ' ' && buf != ' '){
if(buf != ']' && buf != '\r' && buf != '\n' && buf != '\0')
Print_buf[j++] = buf;
else{
if(buf == ']') Valid_Flag = 1; //有效
break;
}
}
}
//strncpy(Print_buf, &(buf[1]), strlen(buf)-4);// []\r\n
if(Valid_Flag){
printf(" >节-值为:%s", Print_buf);
}
else printf(" >节-值无效");
printf("\r\n*");
}
else{
for(i=0,j=0; j<(sizeof(Print_buf1)); i++){
if(buf != ' ' && buf != ' '){
if(buf != '=' && buf != '\r' && buf != '\n' && buf != '\0')
Print_buf1[j++] = buf;
else{
if(buf == '=') Valid_Flag = 1; //有效
break;
}
}
}
for(i++,j=0; j<sizeof(Print_buf); i++){
//if(i >= sizeof(buf)) break;
if(buf != ' ' && buf != ' '){
if(buf != '\r' && buf != '\n' && buf != '\0'){
Print_buf[j++] = buf;
}
else break;
}
}
// p = strchr (buf,'=');
// strncpy(Print_buf1,buf,p-buf);
// strncpy(Print_buf, ++p, strlen(p)-2);// \r\n
if(Valid_Flag )
printf(" >参数 %s 值:%s", Print_buf1,Print_buf);
else
printf(" >参数-值无效");
printf("\r\n*");
}
//if(!i--) break;
}
else break;
}
f_close(&file);
f_mount(0, 0);
}
/**********************************************************
打印到MULTIEDIT中
ZZY 2015.1
**********************************************************/
void ReadText(char* File_Des,WM_MESSAGE * pMsg)
{
FRESULT result;
//FATFS fs;
FIL file;
DIR DirInf;
WM_HWIN hWin = pMsg->hWin; //?°????±ú
char buf[70]; // > 30+20+1+1+??????
result = f_mount(0, fs[0]);
if (result != FR_OK)
{ printf("?????????????§°?(%d)\r\n", result);
}
result = f_opendir(&DirInf, "/");
if (result != FR_OK)
{ printf("?ò???ù?????§°?(%d)\r\n", result); return;
}
result = f_open(&file,File_Des, FA_OPEN_EXISTING | FA_READ);
if (result != FR_OK)
{ printf("Not a File \r\n"); return;
}
while (f_gets(buf, sizeof(buf), &file)){
printf("%s", buf);
MULTIEDIT_AddText(WM_GetDialogItem(hWin,GUI_ID_MULTIEDIT0),buf);
//f_lseek(&file, file.fptr - strlen(buf));
}
f_close(&file);
f_mount(0, 0);
}
[/mw_shl_code]
|