#include "paramRead.h"
#include "common.h"
/*
读取字符类型的值
*/
int GetIniKeyString(char *dst, const char *title, const char *key, const char *filename)
{
FILE * fp = NULL;
int flag = 0;
char sTitle[32], *wTmp;
static char sLine[1024];
sprintf(sTitle, "[%s]", title);
if (NULL == (fp = fopen(filename, "r")))
{
// fclose(fp);
perror("fopen");
return -1;
}
else
{
while (NULL != fgets(sLine, 1024, fp))
{
// 这是注释行
if (0 == strncmp("//", sLine, 2))
continue;
if ('#' == sLine[0])
continue;
wTmp = strchr(sLine, '=');
if ((NULL != wTmp) && (1 == flag))
{
if (0 == strncmp(key, sLine, strlen(key)))
{ // 长度依文件读取的为准
if (sLine[strlen(sLine) - 2] == '\r')
{
sLine[strlen(sLine) - 2] = '\0';
}
else
{
sLine[strlen(sLine) - 1] = '\0';
}
fclose(fp);
strcpy(dst, wTmp + 1);
return 0;
}
}
else
{
if (0 == strncmp(sTitle, sLine, strlen(sTitle)))
{ // 长度依文件读取的为准
flag = 1; // 找到标题位置
}
}
}
}
fclose(fp);
return -1;
}
/*
读取数组类型的值
*/
int GetIniKeyFloatArray(const char *title, const char *key, float *array, int arraySize, const char *filename)
{
FILE * fp = NULL;
int flag = 0;
char sTitle[32];
static char sLine[1024];
char * p;
int i = 0;
sprintf(sTitle, "[%s]", title);
if (NULL == (fp = fopen(filename, "r")))
{
// fclose(fp);
perror("fopen");
return -1;
}
else
{
while (NULL != fgets(sLine, 1024, fp))
{
// 这是注释行
if (0 == strncmp("//", sLine, 2))
continue;
if ('#' == sLine[0])
continue;
if ((1 == flag) && 0 == strncmp(sLine, key, strlen(key))) //找到键值位置
{
p = strtok(sLine + strlen(key) + 1, ",");
while (i < arraySize)
{
if (p != NULL && *p != '\n' && *p != '\r')
{
array[i] = atof(p);
p = strtok(NULL, ",");
i++;
}
else
{
if (NULL == fgets(sLine, 1024, fp))
{
fclose(fp);
return -1;
}
else
p = strtok(sLine, ",");
}
}
fclose(fp);
return 0;
}
else
{
if (0 == strncmp(sTitle, sLine, strlen(sTitle)))
{ // 长度依文件读取的为准
flag = 1; // 找到标题位置
}
}
}
}
fclose(fp);
return -1;
}
/*
读取整数类型的值
*/
long long GetIniKeyInt(const char *title, const char *key, const char *filename)
{
char str[1024];
if (!GetIniKeyString(str, title, key, filename))
return strtoll(str, NULL, 0);
else
return -1;
}
/*
读取浮点型的值
*/
double GetIniKeyFloat(const char *title, const char *key, const char *filename)
{
char str[1024];
if (!GetIniKeyString(str, title, key, filename))
return atof(str);
else
return -1;
}
配置文件如下:
[SITE]
SiteId=1
SiteName=boer
#单位 秒 [1,5)
TransmissionInterval=2
[GPS]
#使用本文件参数 0,必须使用本文件的 ; 1,必须使用原始数据的 ; 2,灵活切换,默认使用原始数据中的,如果原始数据的信息不可用,就用本文件的
UseFile=2
#设备所在经度(至少精确到小数后六位)
longitude=112.994963
#设备所在纬度(至少精确到小数后六位)
latitude=28.233651
#经纬度平滑值 0=固定经纬度,(0,1)=平滑值,1=不平滑
#SmoothValue=1
#0,使用本文件的角度 ; 1,使用原始数据的角度。(未修正磁偏角)
UseSystemAzimuth=1
#系统方位角
SystemAzimuth=0
#使用时区 0,使用本文件的时区; 1,使用本地获取的时区
UseTimeZone=1
#使用的本文件时区(当UseTimeZone=0时生效)
TimeZone=8
#0,不使用磁偏角;1,使用本文件磁偏角;2,根据经纬度(精确到度)自动计算磁偏角
UseMaDe=2
#使用的本文件磁偏角(当UseMade=1时生效)(偏西为负,偏东为正(-180,180))
MagneticDeclination=-3.75300
[DEVICE]
#0表示平行形态,1表示五角星形态
AntennaShape=1
[ANGLE]
#幅度估计距离的系数,越大则距离越大
miu=6.5
#天线间距
d2450=0.06
#初始相位,2450M
initPhase2450=-0.9,-0.9,-0.9,-0.9,-0.9
#天线间距
d5800=0.025
#初始相位,5800M
initPhase5800=-0.9,-0.9,-0.9,-0.9,-0.9
修改配置文件内容代码如下:
int update_gps(const char *fileName,const char*key,const char*data)
{
FILE *fp = fopen(fileName,"r+");
if(fp<0)
{
return 0;
}
FILE *fp2 = fopen("temp.ini","w+");
if(fp2<0)
{
return 0;
}
char *file_buf = (char*)malloc(4096);
char check_buf[1024]={0};
while(fgets(check_buf,1024,fp) != NULL)
{
if(strncmp(check_buf,key,strlen(key)) == 0)
{
memset(check_buf,0,1024);
sprintf(check_buf,"%s=%s\n",key,data);
}
fprintf(fp2,"%s",check_buf);
}
fclose(fp);
fclose(fp2);
//memset(check_buf,0,1024);
//sprintf(check_buf,"rm -r %s", fileName);
//system(check_buf);
memset(check_buf,0,1024);
sprintf(check_buf,"mv temp.ini %s", fileName);
system(check_buf);
return 0;
}
用法如下:
int main()
{
double ret = GetIniKeyFloat("GPS","longitude",CHECK_GPS_INI);
printf("longitude = %f\n",ret);
update_gps(CHECK_GPS_INI,"longitude","100.0011111");
ret = GetIniKeyFloat("GPS","longitude",CHECK_GPS_INI);
printf("longitude = %f\n",ret);
}
结果如下:
longitude = 112.994963
longitude = 100.001111