第一点:判断读取是否是定义的格式
标准文件格式:前面可以省略空格 ,#,' ' \t ;
主要判断两种文件格式:
第一种: [ 开头
第二种: < 开头
其他的文件格式都是错误
[zview]
device=zviewtech
[Summary]
ProtocolCount=4
int CConfig::GetFileType(const char *pszFileName)
{
if ((NULL == pszFileName) || (strlen(pszFileName) > (PATH_MAX - 1)))
{
return -1;
}
FILE *fp = fopen(pszFileName, "r+");
if (NULL == fp)
{
return 0;
}
char *current = NULL;
char szBuff[INI_LINE_LENGTH + 1 + 1];
while (1)
{
const char *ret = fgets(szBuff, INI_LINE_LENGTH, fp);
if (NULL == ret) //主要是在判断读
{
if (feof(fp)) //
{
fclose(fp);
return 0; //读取文件结束
}
else //文件IO错误
{
fclose(fp);
return -1;
}
}
current = szBuff;
while (*current == ' ' || *current == '\t')
{
current++;
}
if ((*current == ';') || (*current == '#') || (*current == '\n'))
{
continue;
}
if (*current == '[')
{
fclose(fp);
return 0;
}
else if (*current == '<')
{
fclose(fp);
return 1;
}
else
{
fclose(fp);
return -1;
}
}
fclose(fp);
return -1;
}
排错机制: 倒叙排错机制
char *pKeyName = strdup(key);
if (NULL == pKeyName)
{
PAR_ERROR("Add Key Failed, Can not Allocate Memory\n");
return IDVR_ERROR_ALLOC; //分配内存失败
}
char *pKeyValue = strdup(value);
if (NULL == pKeyValue)
{
free(pKeyName);
pKeyName = NULL;
PAR_ERROR("Add Key Failed, Can not Allocate Memory\n");
return IDVR_ERROR_ALLOC; //分配内存失败
}