目录
源码gitHub下载地址:GitHub链接
inipaeser库的使用方法
inipaeser配置文档的基本书写格式
使用ini配置文件实例
inipaeser.h中的基本API
dictionary.h中的基本API
C解析实例
最近,再利用树莓派实现4Gppp拨号时,自己在写参数解析的函数时发现有太多的参数需要输入,如串口的波特率、设备名、奇偶校验、停止位等等......如果所有参数都需要我们一一输入然后在一一解析实在是太麻烦了,百度发现可以使用inipaeser库来解析出我们写入配置文件的所有信息,进而解决了这个棘手的问题。
1.编译生成动态链接库
a.先解压
tar -zxvf iniparser-3.1.tar.gz
cd tar -zxvf iniparser
b.编译
make
拷贝src下的头文件dictionary.h和iniparser.h以及压缩包目录下的静态库libiniparser.a和动态库libiniparser.so.1到目标文件系统的对应目录下。
2.直接编译源码使用
复制src下的dictionary.h,iniparser.h,dictionary.h和iniparser.h到项目目录,将文件加入到项目Makefile编译目标里。
;其中';'表示注释,第一[xxx],其中xxx为该配置文档的第一部分,第二个[xxx],为其第二部分,一次类推。
[section0] ;第一部分
name0=val0 ;定义第一个变量name0,其值为val0,不管是什么类型的值,都使用‘=’给其赋值
name1=val1
[section1] ;第二部分
name0=value0
name1=value1
;创建第一部分,与串口相关的配置
[comport]
device_name = /dev/ttyUSB2
nspeed = 115200
nstop = 1
nbits = 8
nevent = N
;创建第一部分,与ppp拨号相关的配置
[ppp]
ping_ipaddr = 4.2.2.2
;创建第一部分,与mosquitto相关的配置
[mosquitto]
host = a1qhZLMC
password = 6B16C7526688A90274603
client_id = 666888|securemode=3|
port = 1883
sub_topic = /sys/a1qhZLMCMvl
dev_name = /dev/ttyUSB3
user_name = Switch
keepalive = 60
int iniparser_getnsec(dictionary * d); //获取dictionary对象的section个数
char * iniparser_getsecname(dictionary * d, int n); //获取dictionary对象的第n个section的名字
void iniparser_dump_ini(dictionary * d, FILE * f); //保存dictionary对象到file
void iniparser_dumpsection_ini(dictionary * d, char * s, FILE * f); //保存dictionary对象一个section到file
void iniparser_dump(dictionary * d, FILE * f); //保存dictionary对象到file
int iniparser_getsecnkeys(dictionary * d, char * s); //获取dictionary对象某个section下的key个数
char ** iniparser_getseckeys(dictionary * d, char * s); //获取dictionary对象某个section下所有的key
char * iniparser_getstring(dictionary * d, const char * key, char * def); //返回dictionary对象的section:key对应的字串值
int iniparser_getint(dictionary * d, const char * key, int notfound); //返回idictionary对象的section:key对应的整形值
double iniparser_getdouble(dictionary * d, const char * key, double notfound); //返回dictionary对象的section:key对应的双浮点值
int iniparser_getboolean(dictionary * d, const char * key, int notfound); //返回dictionary对象的section:key对应的布尔值
int iniparser_set(dictionary * ini, const char * entry, const char * val); //设置dictionary对象的某个section:key的值
void iniparser_unset(dictionary * ini, const char * entry); //删除dictionary对象中某个section:key
int iniparser_find_entry(dictionary * ini, const char * entry) ; //判断dictionary对象中是否存在某个section:key
dictionary * iniparser_load(const char * ininame); //解析dictionary对象并返回(分配内存)dictionary对象
void iniparser_freedict(dictionary * d); //释放dictionary对象(内存)
unsigned dictionary_hash(const char * key); //计算关键词的hash值
dictionary * dictionary_new(int size); //创建dictionary对象
void dictionary_del(dictionary * vd); //删除dictionary对象
char * dictionary_get(dictionary * d, const char * key, char * def); //获取dictionary对象的key值
int dictionary_set(dictionary * vd, const char * key, const char * val); //设置dictionary对象的key值
void dictionary_unset(dictionary * d, const char * key); //删除dictionary对象的key值
void dictionary_dump(dictionary * d, FILE * out); //保存dictionary对象
[comport]
device_name = /dev/ttyUSB2
nspeed = 115200
nstop = 1
nbits = 8
nevent = N
[ppp]
ping_ipaddr = 4.2.2.2
[mosquitto]
host = a1qhZLMCMvl.iot-as-mqtt.cn-shanghai.aliyuncs.com
password = 6B16C7526688A902746032C03374C6B80998B7D5
client_id = 666888|securemode=3,signmethod=hmacsha1|
port = 1883
sub_topic = /sys/a1qhZLMCMvl/Switch/thing/event/property/post
dev_name = /dev/ttyUSB3
user_name = Switch&a1qhZLMCMvl
keepalive =
/*********************************************************************************
* Copyright: (C) 2020 ysn
* All rights reserved.
*
* Filename: tets.c
* Description: This file
*
* Version: 1.0.0(11/08/20)
* Author: tianjincheng <[email protected]>
* ChangeLog: 1, Release initial version on "11/08/20 07:38:08"
*
********************************************************************************/
#include
#include
#include "iniparser.h"
#include "dictionary.h"
#define INI_PATH "init_param.conf"
int iniparser_save(dictionary * d, const char *inipath);
int main(void)
{
dictionary *ini;
ini = iniparser_load(INI_PATH);//parser the file
printf("%s:\n",iniparser_getsecname(ini,0));//get section name
int n = iniparser_getint(ini,"comport:nSpeed",-1);
printf("speed : %d\n",n);
const char *str = iniparser_getstring(ini,"comport:device_name","null");
printf("device_name : %s\n",str);
printf("\n%s:\n",iniparser_getsecname(ini,1));
const char *ip = iniparser_getstring(ini,"ppp:ping_ipaddr","null");
printf("ping_ipaddr : %s\n",ip);
printf("\n%s:\n",iniparser_getsecname(ini,2));
iniparser_set(ini, "mosquitto:keepalive", "60");
iniparser_save(ini, INI_PATH);
iniparser_freedict(ini);//free dirctionary object
return 0;
}
int iniparser_save(dictionary * d, const char *inipath) //自己实现的ini配置文档保存函数
{
int ret = 0;
FILE *fp = NULL;
if (inipath == NULL || d == NULL) {
ret = -1;
printf("saveConfig error:%d from (filepath == NULL || head == NULL)\n",ret);
return ret;
}
fp = fopen(inipath,"w");
if (fp == NULL) {
ret = -2;
printf("saveConfig:open file error:%d from %s\n",ret,inipath);
return ret;
}
iniparser_dump_ini(d,fp);
fclose(fp);
return 0;
}