一.iniparser的基本概念
1 iniparser库的概念
iniparser是一个C语言库,是针对ini配置文件的开源解析器。ini文件则是一些系统或者软件的配置文件。 iniparser可以对配置文件进行解析、添加、修改、删除等操作。
二.inisparser库中的API
2.1 inisparser库的下载
git clone https://github.com/ndevilla/iniparser
下载完成之后我们进入iniparser文件夹内的src下,可以看到iniparser.c,iniparser.h和dictionnary.c和dictiona.h四个文件。我们所需要使用的API就在这些文件内。使用包含四个文件就好了。
2.2 iniparser.h头文件中的API
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对象(内存)
2.3 dictiona.h中的API
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对象
三.iniparser实例
使用iniparser来解析ini配置文件。
所用到的配置文件是自己写的配置文件mqtt_aliyun_mqtt
[mqtt_server_addr]
host =iot-06z00gyvi020tfv.mqtt.iothub.aliyuncs.com
port =1883
[user_passwd]
username =MQTT&gv9vUe6QzX6
passwd =34bde8763e13a25d2448e8594dc8a69650e05cf70c1fb8fa731ffeaeddef95
[client_id]
id =gv9vUe6QzX6.MQTT|securemode=2,signmethod=hmacsha256,timestamp=252460800000|
[sub_topic]
topic =/sys/gv9vUe6QzX6/MQTT/thing/service/property/set
[pub_topic]
topic =/sys/gv9vUe6QzX6/MQTT/thing/event/property/post
使用如下代码来解析文件中的数据:
/*********************************************************************************
* Copyright: (C) 2022 hubeiwuhan
* All rights reserved.
*
* Filename: iniparsertest.c
* Description: This file
*
* Version: 1.0.0(04/03/22)
* Author: yanp <2405204881@qq.com>
* ChangeLog: 1, Release initial version on "04/03/22 07:15:02"
*
********************************************************************************/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "iniparser.h"
#define INI_PATH "/home/pi/test/iniparser/src/mqtt_aliyun_conf.ini"
int main()
{
dictionary *ini = NULL;
const char *str=NULL;
char buff[1024];
int port
ini = iniparser_load(INI_PATH);
if( ini == NULL )
{
return -1;
}
memset(buff,0,sizeof(buff));
str=iniparser_getstring(ini, "mqtt_server_addr:host", "NULL");
strncpy(buff, str, strlen(str));
printf("host is:%s\n", buff);
str = iniparser_getstring(ini, "mqtt_server_addr:port", "NULL");
port=atoi(str);
printf("port is:%s\n", port);
str = iniparser_getstring(ini, "sub_topic:topic", "NULL");
strncpy(buff, str, strlen(str));
printf("topic is:%s\n", buff);
str = iniparser_getstring(ini, "user_passwd:username", "NULL");
strncpy(buff, str, strlen(str));
printf("user is:%s\n", buff);
str = iniparser_getstring(ini, "user_passwd:passwd", "NULL");
strncpy(buff, str, strlen(str));
printf("passwd is:%s\n", buff);
str = iniparser_getstring(ini, "client_id:id", "NULL");
strncpy(buff, str, strlen(str));
printf("client_id is:%s\n", buff);
iniparser_freedict(ini);
return 0;
}
运行结果如下