【MQTT学习5】Linux下iniparser库的下载和使用

4 篇文章 3 订阅

1、iniparser库的概念

iniparser是一个C语言库,是针对ini配置文件的开源解析器。ini文件则是一些系统或者软件的配置文件。 iniparser可以对配置文件进行解析、添加、修改、删除等操作。

2、iniparser的安装

1、下载iniparser

wget https://codeload.github.com/ndevilla/iniparser/tar.gz/refs/tags/v4.1 -O iniparserv4.1.tar.gz

2、解压

tar -zxvf iniparserv4.1.tar.gz  

3、进入目录

cd iniparser-4.1/

4、在该目录下我们可以查看到src文件夹

liruiyan@cloud-ubuntu18:~/iniparser/iniparser-4.1$ ls
AUTHORS  doc  example  FAQ-en.md  FAQ-zhcn.md  html  INSTALL  LICENSE  Makefile  README.md  src  test

进入src文件夹可以看到我们所需要的主要代码

liruiyan@cloud-ubuntu18:~/iniparser/iniparser-4.1$ cd src/
liruiyan@cloud-ubuntu18:~/iniparser/iniparser-4.1/src$ ls
dictionary.c  dictionary.h  dictionary.o  iniparser.c  iniparser.h  iniparser.o

3、API(应用编程程序接口)

dictionary.h里面声明了一些直接解析ini file的API,iniparser.h头文件里面声明了一些提供用户操作的API。iniparser.h里面的API是对dictionary.h里面API的再次封装,以提供用户友好性。

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对象(内存)  

dictionary.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对象 

4、使用iniparser库实现对ini配置文件的修改

4.1ini配置文件
ini配置文件基本组成单元是key每个key都有一个对应的[name]和[value],如:start = 192.168.0.0
多个key也可以被归类为一组section:
如:
[ipaddrpool]
start =192.168.0.0
end =192.169.1.100
其中[ipaddpool]就是组名,组成员有“start”、“end”.
每个组下的key是唯一不能重复的,但不同组下可以存在相同的key.
注释以分号(;)开头
如: ;地址头

编写配置文件:

vim config.ini
[ipaddrpool]
start                          = 192.168.1.0
end                            = 192.168.1.100

[filepath]
leasefile                      = /var/dhcplease/dhcpd.leases

[network]
interface                      = en1

[opt]
dns1                           = 8.8.8.8
dns2                           = 8.8.8.8
subnet                         = 255.255.255.0
router                         = 192.168.3.1
domain                         = local
lease                          = 864
t1                             = 432
t2                             = 756

尝试编写iniparser程序对ini文件进行修改:

*********************************************************************************
 *      Copyright:  (C) 2021 Mierya<2477326997@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  test.c
 *    Description:  This file is used to modify the ini configuration file through the iniparser library
 *                 
 *        Version:  1.0.0(07/10/2021)
 *         Author:  Mierya <2477326997@qq.com>
 *      ChangeLog:  1, Release initial version on "07/10/2021 06:49:18 PM"
 *                 
 ********************************************************************************/
#include <stdio.h>
#include "iniparser.h"
#include "dictionary.h"

#define PATH "/home/liruiyan/iniparser/iniparser-4.1/src/config.ini"

int main (int argc, char **argv)
{
    FILE  *fp = NULL  ;
    dictionary *ini= NULL;
    /* 解析dictionary对象并返回(分配内存)dictionary对象*/
    ini = iniparser_load(PATH);

    if( ini ==NULL)
    {
        printf("iniparser  failure\n");
        return -1;
    }

    /* 设置dictionary对象的某个section:key的值 */
    iniparser_set(ini, "ipaddrpool:start", "192.168.0.0");
    iniparser_set(ini, "ipaddrpool:end", "192.168.1.1");

    /* 返回dictionary对象的section,key对应的字串值 */
    printf("%s\n", iniparser_getstring(ini, "ipaddrpool:start", "null"));
    printf("%s\n", iniparser_getstring(ini, "ipaddrpool:end", "null"));
    printf("%s\n", iniparser_getstring(ini, "filepath:leasefile", "null"));
    printf("%s\n", iniparser_getstring(ini, "network:interface", "null"));
    printf("%s\n", iniparser_getstring(ini, "opt:dns1", "null"));
    printf("%s\n", iniparser_getstring(ini, "opt:dns2", "null"));
    printf("%s\n", iniparser_getstring(ini, "opt:subnet", "null"));
    printf("%s\n", iniparser_getstring(ini, "opt:router", "null"));
    printf("%s\n", iniparser_getstring(ini, "opt:domain", "null"));
     printf("%s\n", iniparser_getstring(ini, "opt:lease", "null"));
    printf("%s\n", iniparser_getstring(ini, "opt:t1", "null"));
    printf("%s\n", iniparser_getstring(ini, "opt:t2", "null"));
    
    fp = fopen(PATH, "w");
    if( fp == NULL ) {
        printf("stone:fopen error!\n");
        exit(-1);
    }
    
    /* 保存dictionary对象 */
    iniparser_dumpsection_ini(ini, "ipaddrpool", fp);
    iniparser_dumpsection_ini(ini, "filepath", fp);
    iniparser_dumpsection_ini(ini, "network", fp);
    iniparser_dumpsection_ini(ini, "opt", fp);
    fclose(fp);
    /* 释放dictionary对象(内存)*/
    iniparser_freedict(ini);
    return 0;
}

运行结果:
在这里插入图片描述
以上代码是修改地址池里面的ip,修改之后打开config_ini配置文件,可以看到配置文件里面的ip已经被修改。
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mierya0707

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值