/**
* @Copyright (c) 2019, sutpc and/or its affiliates. All rights reserved.
* @file config.c
* @brief 从.conf扩展名的配置文件读入配置信息,转换成对应数据类型输出
* @version 1.1
* @author 周勇
* @date 2019年9月25日
*/
#include "config.h"
uint64_t GetConfigStringValue(char const* pInFileName, char* pInSectionName, char* pInKeyName, char *pOutKeyValue) {
FILE *fpConfig;
char szBuffer[150];
char *pStr1, *pStr2;
uint64_t iRetCode = 0;
memset(szBuffer, 0, sizeof(szBuffer));
if (NULL == (fpConfig = fopen(pInFileName, "r")))
return FILENAME_NOTEXIST;
while (!feof(fpConfig)) {
if (NULL == fgets(szBuffer, 150, fpConfig)) {
break;
}
pStr1 = szBuffer;
while ((' ' == *pStr1) || ('\t' == *pStr1))
pStr1++;
if ('[' == *pStr1)
{
pStr1++;
while ((' ' == *pStr1) || ('\t' == *pStr1))
pStr1++;
pStr2 = pStr1;
while ((']' != *pStr1) && ('\0' != *pStr1))
pStr1++;
if ('\0' == *pStr1)
continue;
while (' ' == *(pStr1 - 1))
pStr1--;
*pStr1 = '\0';
iRetCode = CompareString(pStr2, pInSectionName);
if (!iRetCode) {
iRetCode = GetKeyValue(fpConfig, pInKeyName, pOutKeyValue);
fclose(fpConfig);
return iRetCode;
}
}
}
fclose(fpConfig);
//printf("retnum:%lu", (long unsigned int)iRetCode);
return SECTIONNAME_NOTEXIST;
}
int CompareString(char *pInStr1, char *pInStr2) {
if (strlen(pInStr1) != strlen(pInStr2)) {
return STRING_LENNOTEQUAL;
}
/*while( toupper(*pInStr1)==toupper(*pInStr2) )*//*#include <ctype.h>*/
while (*pInStr1 == *pInStr2) {
if ('\0' == *pInStr1)
break;
pInStr1++;
pInStr2++;
}
if ('\0' == *pInStr1)
return STRING_EQUAL;
return STRING_NOTEQUAL;
}
int GetKeyValue(FILE *fpConfig, char *pInKeyName, char *pOutKeyValue) {
char szBuffer[150];
char *pStr1, *pStr2, *pStr3;
unsigned int uiLen;
int iRetCode = 0;
memset(szBuffer, 0, sizeof(szBuffer));
while (!feof(fpConfig)) {
if (NULL == fgets(szBuffer, 150, fpConfig))
break;
pStr1 = szBuffer;
while ((' ' == *pStr1) || ('\t' == *pStr1))
pStr1++;
if ('#' == *pStr1)
continue;
if (('/' == *pStr1) && ('/' == *(pStr1 + 1)))
continue;
if (('\0' == *pStr1) || (0x0d == *pStr1) || (0x0a == *pStr1))
continue;
if ('[' == *pStr1) {
pStr2 = pStr1;
while ((']' != *pStr1) && ('\0' != *pStr1))
pStr1++;
if (']' == *pStr1)
break;
pStr1 = pStr2;
}
pStr2 = pStr1;
while (('=' != *pStr1) && ('\0' != *pStr1))
pStr1++;
if ('\0' == *pStr1)
continue;
pStr3 = pStr1 + 1;
if (pStr2 == pStr1)
continue;
*pStr1 = '\0';
pStr1--;
while ((' ' == *pStr1) || ('\t' == *pStr1)) {
*pStr1 = '\0';
pStr1--;
}
iRetCode = CompareString(pStr2, pInKeyName);
if (!iRetCode) {
pStr1 = pStr3;
while ((' ' == *pStr1) || ('\t' == *pStr1))
pStr1++;
pStr3 = pStr1;
while (('\0' != *pStr1) && (0x0d != *pStr1) && (0x0a != *pStr1)) {
if (('/' == *pStr1) && ('/' == *(pStr1 + 1)))
break;
pStr1++;
}
*pStr1 = '\0';
uiLen = strlen(pStr3);
memcpy(pOutKeyValue, pStr3, uiLen);
*(pOutKeyValue + uiLen) = '\0';
return SUCCESS;
}
}
return KEYNAME_NOTEXIST;
}
uint64_t GetConfigIntValue(char const *pInFileName, char* pInSectionName, char* pInKeyName, uint64_t *pOutKeyValue) {
uint64_t iRetCode = 0;
char szKeyValue[20], *pStr;
memset(szKeyValue, 0, sizeof(szKeyValue));
iRetCode = GetConfigStringValue(pInFileName, pInSectionName, pInKeyName, szKeyValue);
if (iRetCode)
return iRetCode;
pStr = szKeyValue;
while ((' ' == *pStr) || ('\t' == *pStr))
pStr++;
if (('0' == *pStr) && (('x' == *(pStr + 1)) || ('X' == *(pStr + 1))))
sscanf(pStr + 2, "%lu", pOutKeyValue);
else
sscanf(pStr, "%lu", pOutKeyValue);
return SUCCESS;
}
uint64_t GetConfigDoubleValue(char const *pInFileName, char* pInSectionName, char* pInKeyName, double *pOutKeyValue) {
uint64_t iRetCode = 0;
char szKeyValue[20], *pStr;
double value;
memset(szKeyValue, 0, sizeof(szKeyValue));
iRetCode = GetConfigStringValue(pInFileName, pInSectionName, pInKeyName, szKeyValue);
if (iRetCode)
return iRetCode;
pStr = szKeyValue;
while ((' ' == *pStr) || ('\t' == *pStr))
pStr++;
if (('0' == *pStr) && (('x' == *(pStr + 1)) || ('X' == *(pStr + 1))))
sscanf(pStr + 2, "%lf", pOutKeyValue);
else
value = atof(pStr);
//printf("dou:%lf\n", value);
//pOutKeyValue = &value;
memcpy(pOutKeyValue, &value, 8);
return SUCCESS;
}
uint64_t GetValueFromArray(char* buf, uint8_t len) {
uint64_t ret = 0, i;
for (i = 0; i < len; i++) {
ret = ((ret << 8) + buf[i]);
}
return ret;
}
uint8_t GetXorCheckCode(uint8_t *buf, uint8_t length) {
uint8_t num = 0;
uint8_t i = 0;
for (i = 0; i < length; i++) {
num ^= buf[i];
}
return num;
}
uint8_t GetSumCheckCode(uint8_t *buf, uint8_t length) {
uint8_t num = 0;
uint8_t i = 0;
for (i = 0; i < length; i++) {
num = (num + buf[i]) % 256;
}
return num;
}
// host long 64 to network
uint64_t Hl64ton(uint64_t host) {
uint64_t ret = 0;
uint32_t high, low;
low = host & 0xFFFFFFFF;
high = (host >> 32) & 0xFFFFFFFF;
low = htonl(low);
high = htonl(high);
ret = low;
ret <<= 32;
ret |= high;
return ret;
}
//network to host long 64
uint64_t Ntohl64(uint64_t host) {
uint64_t ret = 0;
uint32_t high, low;
low = host & 0xFFFFFFFF;
high = (host >> 32) & 0xFFFFFFFF;
low = ntohl(low);
high = ntohl(high);
ret = low;
ret <<= 32;
ret |= high;
return ret;
}
void SetArrayValue(uint8_t *addr, uint32_t len, uint8_t value) {
int i = 0;
for (i = 0; i < len; ++i) {
addr[i] = value;
}
}
C语言 实现配置文件的编写与解析
需积分: 35 134 浏览量
2022-04-11
20:02:44
上传
评论
收藏 3KB RAR 举报
大朋个大
- 粉丝: 0
- 资源: 5
最新资源
- 农村信用社联合社计算机信息系统投产与变更管理办.docx
- 农村信用社联合社计算机信息系统数据管理办法.docx
- 利用SPSS作临床效度分析线上计算网站介绍-医学研究部统计谘.(医学PPT课件).ppt
- 利用Zabbix监控mysqldump定时备份数据库状态.docx
- 利用计算机解决问题的基本过程.doc
- 化工铁路通信工程总结.doc
- 北京大学网络教育软件工程作业.docx
- 医药公司(连锁店)计算机操作规程未新系统的自行按照旧制修改-新系统过制的编号加修模版.doc
- 医药公司(连锁店)计算机系统操作规程模版.doc
- 医药连锁门店计算机系统的操作和管理程序未新系统的自行按照旧制修改-新系统过制的编号加修模版.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0