【iniparser】C语言配置文件解析库 — iniparser 的使用

文章目录

  • O、简介
  • 一、API
  • 二、使用
  • 三、 一些问题

O、简介

C语言配置文件解析库 — iniparser可以方便的对配置文件进行解析、添加、修改、删除等操作。

  • github地址
  • iniparser官网
  • 我的CSDN下载(v4.1-20180802)

配置文件格式:

  • 注释:iniparser会无视注释,注释以英文分号开头(;这是配置文件注释)
  • 注释结构:配置文件包含两个基本单元 section 和 key。其中 section 相当于一组 key 的名字,section下面包含 0或多个key,每个 key 是配置文件的最小单元。
;section注释
[setion1]
key11 = value11 
key12 = value12 

[setion2]
key21 = value21 
key22 = value22
...

注意: iniparser 的一条原则是 section 和 key 大小写无关,写入的字符串全部小写化,取出的字符串也全部小写化。

一、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);  //打印整个配置文件信息(f赋值为stdout则输出到终端)
 
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对象

二、使用

1、准备

  1. 下载源码,提取 src 中 4个源文件(dictionary.h、dictionary.c、iniparser.h、iniparser.c),测试中添加到新建的 iniparser 目录中;
  2. 由于 iniparser 是 dictionary 的再次封装,使用时引用 iniparser.h 即可;
  3. 创建配置测试文件(test_parser.ini);
;test_parser.ini

[it]
macbook                        = apple
xiaomi                         = MI

[browser]
chrome                         = google
firefox                        = mozilla
ie                             = microsoft

[server]
ip                             = 127.0.0.1
port                           = 10010

[testUpper]
upper                          = Upper

2、解析、取值、修改、删除等基本操作

// name: ini_parser.c
// date: 2018-08-02

#include 
#include 

#include "iniparser/iniparser.h"

void main(){

	dictionary *dict = NULL;

	printf("---------------------------------load-------------------------------\r\n");
	dict = iniparser_load("./test_parser.ini");
	if(NULL == dict){
		printf("iniparser load ini file failed, please check it...\r\n");
		exit(0);
	}

	iniparser_dump(dict, stdout);

	printf("---------------------------------read-------------------------------\r\n");
	printf("it_macbook: %s\r\n", iniparser_getstring(dict, "it:macbook", ""));

	printf("server_port: %d\r\n", iniparser_getint(dict, "server:port", 0));

	printf("---------------------------------find-------------------------------\r\n");
	if(iniparser_find_entry(dict, "server:ip")){
		printf("find entry server:ip, the value is: %s\r\n", iniparser_getstring(dict, "server:ip", ""));
	}else{
		printf("the entry server:ip not exist...\r\n");
    }
    
	if(iniparser_find_entry(dict, "it:golang")){
		printf("find entry it:golang, the value is: %s\r\n", iniparser_getstring(dict, "it:golang", ""));
	}else{
		printf("the entry it:golang not exist...\r\n");
	}
	
	printf("---------------------------------set-------------------------------\r\n");
	if(!iniparser_set(dict, "it:golang", "google")){
		printf("set it:golang entry OK.\r\n");
	}else{
		printf("set it:golang entry failed.\r\n");
    }
    
	if(iniparser_find_entry(dict, "it:golang")){
		printf("find entry it:golang, the value is: %s\r\n", iniparser_getstring(dict, "it:golang", ""));
    }else{
		printf("the entry it:golang not exist...\r\n");
    }

	printf("---------------------------------unset-------------------------------\r\n");
	iniparser_unset(dict, "it:golang");
	
	if(iniparser_find_entry(dict, "it:golang")){
		printf("find entry it:golang, the value is: %s\r\n", iniparser_getstring(dict, "it:golang", ""));
	}else{
		printf("the entry it:golang not exist...\r\n");
    }
    
	printf("---------------------------------dump ini-------------------------------\r\n");
	if(!iniparser_set(dict, "it:golang", "google")){
		printf("set it:golang entry OK.\r\n");
	}else{
		printf("set it:golang entry failed.\r\n");
	}
	
	FILE *ini_file = fopen("./test_parser.ini", "w");
	iniparser_dump_ini(dict, ini_file);
	fclose(ini_file);

	iniparser_dump(dict, stdout);

	iniparser_freedict(dict);
	
	printf("---------------------------------END-------------------------------\r\n");
}

# 编译
$ gcc ini_parser.c iniparser/*.c 
# 结果
$ ./a.out
---------------------------------load-------------------------------
[it]=UNDEF
[it:macbook]=[apple]
[it:xiaomi]=[MI]
[browser]=UNDEF
[browser:chrome]=[google]
[browser:firefox]=[mozilla]
[browser:ie]=[microsoft]
[server]=UNDEF
[server:ip]=[127.0.0.1]
[server:port]=[10010]
[testupper]=UNDEF
[testupper:upper]=[Upper]
---------------------------------read-------------------------------
it_macbook: apple
server_port: 10010
---------------------------------find-------------------------------
find entry server:ip, the value is: 127.0.0.1
the entry it:golang not exist...
---------------------------------set-------------------------------
set it:golang entry OK.
find entry it:golang, the value is: google
---------------------------------unset-------------------------------
the entry it:golang not exist...
---------------------------------dump ini-------------------------------
set it:golang entry OK.
[it]=UNDEF
[it:macbook]=[apple]
[it:xiaomi]=[MI]
[browser]=UNDEF
[browser:chrome]=[google]
[browser:firefox]=[mozilla]
[browser:ie]=[microsoft]
[server]=UNDEF
[server:ip]=[127.0.0.1]
[server:port]=[10010]
[testupper]=UNDEF
[testupper:upper]=[Upper]
[it:golang]=[google]
---------------------------------END-------------------------------

3、遍历、查找

// name: ini_iter.c
// date: 2018-08-02

#include 
#include 

#include "iniparser/iniparser.h"

void main(){

	int num = 0;
	char str[32] = {0};
	dictionary *dict = NULL;

	printf("---------------------------------load-------------------------------\r\n");
	dict = iniparser_load("./test_parser.ini");
	if(NULL == dict){
		printf("iniparser load ini file failed, please check it...\r\n");
		exit(0);
	}

	iniparser_dump(dict, stdout);

	printf("---------------------------------iter-------------------------------\r\n");
	num = iniparser_getnsec(dict);
	if(-1 == num){
		printf("iniparser getnsec failed...\r\n");
	}else{
		printf("section num: %d\r\n", num);
    }
    
	sprintf(str, "%s", iniparser_getsecname(dict, num-1));
	printf("the %d section name: %s\r\n", num, str);
	
	// 注意:尽量不要在ini中使用大写命名,设置或者取出的字符串全部小写化处理,不能继续使用大写做比较
	if(!strcmp(str, "testupper")){
		printf("compare with testupper.\r\n");
	}
	
	if(!strcmp(str, "testUpper")){
		printf("compare with testUpper.\r\n");
    }
    
	num = iniparser_getsecnkeys(dict, str);
	printf("num: %d\r\n", num);

	const char *keys[32];
	iniparser_getseckeys(dict, str, keys);
	printf("key: %s\r\n", keys[0]);
	printf("value: %s\r\n", iniparser_getstring(dict, keys[0], ""));

	char tmp_str[32] = {0};
	sprintf(tmp_str, "%s:%s", "server", "ip");
	printf("value: %s\r\n", iniparser_getstring(dict, tmp_str, ""));
	
	iniparser_freedict(dict);
	
	printf("---------------------------------END-------------------------------\r\n");
}
# 编译
$ gcc ini_iter.c iniparser/*.c
# 结果
$ ./a.out 
---------------------------------load-------------------------------
[it]=UNDEF
[it:macbook]=[apple]
[it:xiaomi]=[MI]
[it:golang]=[google]
[browser]=UNDEF
[browser:chrome]=[google]
[browser:firefox]=[mozilla]
[browser:ie]=[microsoft]
[server]=UNDEF
[server:ip]=[127.0.0.1]
[server:port]=[10010]
[testupper]=UNDEF
[testupper:upper]=[Upper]
---------------------------------iter-------------------------------
section num: 4
the 4 section name: testupper
compare with testupper.
num: 1
key: testupper:upper
value: Upper
value: 127.0.0.1
---------------------------------END-------------------------------

由于 iniparser 接口是 dictionary 的封装,以上已经基本涵盖所有接口用法。

三、 一些问题

  • 详见 github issue页面

—— 2018-08-02 ——

你可能感兴趣的:(●,【,开源库,】)