STM32系列单片机读取配置文件
周杰伦所有的歌STM32系列单⽚机读取配置⽂件
/*
* ini.c
*
*  Created on: 2021年3⽉2⽇
*      Author: ubuntu
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sysdefines.h"
#define SECTION_MAX_LEN 256
#define STRVALUE_MAX_LEN 256
#define LINE_CONTENT_MAX_LEN 256
//read value from .ini
void IniReadValue(char* section, char* key, char* val, const char* file)
{
FILE* fp;
int i = 0;
int lineContentLen = 0;
int position = 0;
char lineContent[LINE_CONTENT_MAX_LEN];
char bFoundSection = 0;
char bFoundKey = 0;
fp = fopen(file, "r");
if(fp == NULL)
{
printf("%s: Opent file %s failed.\n", __FILE__, file);
return;
}
while(feof(fp) == 0)
{
memset(lineContent, 0, LINE_CONTENT_MAX_LEN);
fgets(lineContent, LINE_CONTENT_MAX_LEN, fp);
if((lineContent[0] == ';') || (lineContent[0] == '\0') || (lineContent[0] == '\r') || (lineContent[0] == '\n'))
{
continue;
}
//check section
if(strncmp(lineContent, section, strlen(section)) == 0)
{
bFoundSection = 1;
//printf("Found section = %s\n", lineContent);
while(feof(fp) == 0)
{
memset(lineContent, 0, LINE_CONTENT_MAX_LEN);
fgets(lineContent, LINE_CONTENT_MAX_LEN, fp);
//check key
if(strncmp(lineContent, key, strlen(key)) == 0)
{
bFoundKey = 1;
lineContentLen = strlen(lineContent);
//find value
for(i = strlen(key); i < lineContentLen; i++)
{
if(lineContent[i] == '=')
{
position = i + 1;
break;
}
}
}
if(i >= lineContentLen) break;
strncpy(val, lineContent + position, strlen(lineContent + position));
lineContentLen = strlen(val);
for(i = 0; i < lineContentLen; i++)
{
if((lineContent[i] == '\0') || (lineContent[i] == '\r') || (lineContent[i] == '\n'))                        {
val[i] = '\0';
break;
}
}
}
else if(lineContent[0] == '[')
{
break;
}
}
break;
}
}
if(!bFoundSection)
{
printf("No section = %s\n", section);
}
else if(!bFoundKey){printf("No key = %s\n", key);}
fclose(fp);
}
FRESULT readStringValue(const char* section, char* key, char* val, const char* file) {
char sect[SECTION_MAX_LEN];
//    printf("section = %s, key = %s, file = %s\n", section, key, file);
if (section == NULL || key == NULL || val == NULL || file == NULL)
{
儒林外史人物形象分析
printf("%s: input parameter(s) is NULL!\n", __func__);
return ERROR_BE;
}
memset(sect, 0, SECTION_MAX_LEN);
sprintf(sect, "[%s]", section);
//printf("\n");
IniReadValue(sect, key, val, file);
return ERROR_OK;
}
uint32_t readIntValue(const char* section, char* key, const char* file)
{
char strValue[STRVALUE_MAX_LEN];
memset(strValue, '\0', STRVALUE_MAX_LEN);
if(readStringValue(section, key, strValue, file) != ERROR_OK)
{
printf("%s: error", __func__);
return 0;
}
return(atoi(strValue));
}
void IniWriteValue(const char* section, char* key, char* val, const char* file)空调变频是什么
{
FILE* fp;
int err = 0;
char lineContent[LINE_CONTENT_MAX_LEN];
char strWrite[LINE_CONTENT_MAX_LEN];
uint8_t bFoundSection = 0;
uint8_t bFoundSection = 0;
uint8_t bFoundKey = 0;
memset(lineContent, '\0', LINE_CONTENT_MAX_LEN);
memset(strWrite, '\0', LINE_CONTENT_MAX_LEN);
sprintf(strWrite, "%s=%s\n", key, val);
fp = fopen(file, "r+");
if(fp == NULL)
{
printf("%s: Opent file %s failed.\n", __FILE__, file);
return;
}
while(feof(fp) == 0)
{
memset(lineContent, 0, LINE_CONTENT_MAX_LEN);
fgets(lineContent, LINE_CONTENT_MAX_LEN, fp);
if((lineContent[0] == ';') || (lineContent[0] == '\0') || (lineContent[0] == '\r') || (lineContent[0] == '\n'))        {
continue;
}
//check section
if(strncmp(lineContent, section, strlen(section)) == 0)
{
bFoundSection = 1;
while(feof(fp) == 0)
{
memset(lineContent, 0, LINE_CONTENT_MAX_LEN);
fgets(lineContent, LINE_CONTENT_MAX_LEN, fp);
//check key
if(strncmp(lineContent, key, strlen(key)) == 0)
{
bFoundKey = 1;
printf("%s: %s=%s\n", __func__, key, val);
fseek(fp, (0-strlen(lineContent)),SEEK_CUR);
err = fputs(strWrite, fp);
if(err < 0){printf("%s err.\n", __func__);}
break;
}
else if(lineContent[0] == '[')
{
break;
}
无极剑圣天赋}
break;
}
}
if(!bFoundSection){printf("No section = %s\n", section);}
else if(!bFoundKey){printf("No key = %s\n", key);}各省省会
fclose(fp);
}
FRESULT writeStringVlaue(const char* section, char* key, char* val, const char* file)
{
char sect[SECTION_MAX_LEN];
//printf("section = %s, key = %s, file = %s\n", section, key, file);
if (section == NULL || key == NULL || val == NULL || file == NULL)
{
printf("%s: input parameter(s) is NULL!\n", __func__);
return ERROR_BE;
}
memset(sect, '\0', SECTION_MAX_LEN);
sprintf(sect, "[%s]", section);
IniWriteValue(sect, key, val, file);
return ERROR_OK;
}
FRESULT writeIntValue(const char* section, char* key, int val, const char* file) {
char strValue[STRVALUE_MAX_LEN];数九从什么时候开始
memset(strValue, '\0', STRVALUE_MAX_LEN);
sprintf(strValue, "%-4d", val);
writeStringVlaue(section, key, strValue, file);
return ERROR_OK;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。