embedded-framework/utils/FileIo/src/file_io.c
2023-09-04 00:50:55 +08:00

109 lines
2.1 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "file_io.h"
void _write_config_file(char *cfgname)
{
}
void __read_config_file(char *cfgName)
{
}
/**
*@brief 从给定字符串中提取key和value对。
*处理字符串data从中取出第一个key和value并存入key和value
*@param data 要解析的字符串
*@param key 保存提取到的KEY串
*@param value 保存提取到的value串
*@param maxlen key和value能容忍的最大字符串长度
*@return 处理完字符串后,指针指向的位置
*
*/
char *sysfunc_get_key_value(char *data, char *key, char *value, int maxlen)
{
int i = 0;
char *temp = data;
while(*temp && *temp == ';')
temp++;
while(*temp && *temp != ';' && *temp != '='
&& i < maxlen)
{
key[i++] = *temp++;
}
key[i] = '\0';
if (*temp == ';' || *temp == '\0')
value[0] = '\0';
else if(*temp == '=')
{
i = 0;
temp++;
while(*temp && *temp != ';'
&& i < maxlen)
{
value[i++] = *temp++;
}
value[i] = 0;
}
return temp;
}
//返回参数长度
U32 ParseParam(char *pParam, int nMaxLen, char *pBuffer)
{
int nLen = 0;
while(pBuffer && *pBuffer && *pBuffer != ';')
{
*pParam++ = *pBuffer++;
nLen++;
}
return nLen;
}
//从acData中查找key的值保存在value中
static char *__get_value(char *acData, char *key, char *value, int nMaxLen)
{
char *pItem, *pValue;
U32 nRead;
char acBuff[256]={0};
U32 nOffset = 0;
int nCh = 0;
char *out_ptr = NULL;
while ((nRead = ParseParam(acBuff, 64, acData+nOffset)) > 0)
{
acBuff[nRead] = 0; //把分号去掉
nOffset += (nRead+1);
if (strncmp(acBuff, "[CH1]", 5) == 0)
{
nCh = 0;
}
else if (strncmp(acBuff, "[CH2]", 5) == 0)
{
nCh = 1;
}
else if (strncmp(acBuff, "[CH3]", 5) == 0)
{
nCh = 2;
}
else if (strncmp(acBuff, "[ALL]", 5) == 0)
{
nCh = 0;
}
else
{
pItem = strtok_r(acBuff, "=", &out_ptr);
pValue = strtok_r(NULL, "\r", &out_ptr);
//Printf("pItem: %s, pValue: %s\n", pItem, pValue);
if (pValue == NULL)
pValue = "";
if (pItem != NULL && strcmp(pItem, key) == 0)
{
strncpy(value, pValue, nMaxLen);
return value;
}
}
}
return NULL;
}