Use of iniparser library

Use of iniparser library

The function of the iniparser library is to allow us to process the ini file in the c file using the method in the iniparser library.

1. Download the source code on github
git clone https://github.com/ndevilla/iniparser

The code we want to use is in /iniparser/src
Insert picture description here

2. File example
  • File ini.conf
;地址池
[ipaddrpool]
start                          = 192.168.1.1
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
  • File test.c
#include <stdio.h>
#include <stdlib.h>
#include "iniparser.h"
#include "dictionary.h"
#define  INI_PATH "./init.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;
}
2. Compilation method

Enter make in the iniparser folder. This will generate a static library (libiniparser.a) and a dynamic library (libiniparser.so.1), copy these two files directly to the project, and put them in the etc directory.
Insert picture description here
Run the command

gcc test.c -I etc/ -L etc/ -liniparser

1、添加头文件
  -I: This is the introduction of the header file, and the address of the subsequent joint file, that is, the introduction of /etc/iniparser.h and /etc/dictionary.h.
2、添加库文件
  -L: Can not find in the standard library, you need to find the option in the specified directory to add a new directory to the GCC library file search path. That is, look for the directories of libiniparser.so.1 and libiniparser.a.
3、动态库与静态库
  -l: The iniparser directly followed by no spaces after it refers to the two files libiniparser.so.1 and libiniparser.a, omitting the previous lib and the following so and a.

3. Introduction to header files
  • 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中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对象 

Guess you like

Origin blog.csdn.net/qq_45125250/article/details/110359440