目录
概述:
INI file是配置文件,保存的是数据,主要是系统或者软件的配置信息。
Iniparser则是对INI file的解析或者操作(get,set,delete 等等)。
下面分别就INI file的文件格式和Iniparser提供的APIs进行说明。
INI file
INI文件则是一些系统或者软件的配置文件。
主要是由“properties”和“section”组成的文本文件。
Properties(Keys)
INI文件的最基本组成单元就是key或者叫property。
每个key都有一个名称(name)和对应的值(value),名称和值之间用等号(=)关联,名称出现在等号的左边。
name=value
Sections
Keys可以被归类为一组(这种分类没有特殊的要求)这组名的定义要独立一行,并用中括号([,])括起来。
在section声明下的keys都会和该section关联起来,
一个section的作用域会在下一个section声明的地方结束,如果没有下一个section的声明,那么该section的结束地方就是该文件末尾。section是不可以嵌套的。
[section]
最终是用section:key来定位一个key的,所以不同section下的key的名称是可以相同的。
Case insensitivity
section和property的名称命名是大小写无关的,因为iniparser里面处理名称的时候,都会统一改成小写。
Comments
注释是以分号开头的
; comment line
Iniparser
iniParser: stand-alone ini parser library in ANSI C
可以通过该网站访问iniparser的主页http://ndevilla.free.fr/iniparser/
你也可以通过github下载source code tree
git clone http://github.com/ndevilla/iniparser.git
iniparser简介:
iniparser是针对INI文件的解析器,既然称为解析器,就是对INI文件里数据组织形式的解析。
INI file之前介绍过,我觉得INI file里面的保存的数据最重要的就是key了,而key有两部分组成,名称(name)和值(value).而key是归类到section里面的,
所以需要用section:key来定位一个key.
iniparser提供给用户的,用来操作INI file的APIs都在iniparser.h文件里声明了。
dictionary.h里面声明了一些直接解析ini file的APIs,
iniparser.h里面声明了一些提供用户操作的APIs,
需要说明的是iniparser.h里面的APIs是对dictionary.h里面APIs的再次封装,以提供用户友好性。
├── src
├── dictionary.c
├── dictionary.h
├── iniparser.c
└── iniparser.h
iniparser.h里面的APIs:
Functions | ||
int | iniparser_getnsec (dictionary *d) | |
| Get number of sections in a dictionary. | |
char * | iniparser_getsecname (dictionary *d, int n) | |
| Get name for section n in a dictionary. | |
void | iniparser_dump_ini (dictionary *d, FILE *f) | |
| Save a dictionary to a loadable ini file. | |
void | iniparser_dumpsection_ini (dictionary *d, char *s, FILE *f) | |
| Save a dictionary section to a loadable ini file. | |
void | iniparser_dump (dictionary *d, FILE *f) | |
| Dump a dictionary to an opened file pointer. | |
int | iniparser_getsecnkeys (dictionary *d, char *s) | |
| Get the number of keys in a section of a dictionary. | |
char ** | iniparser_getseckeys (dictionary *d, char *s) | |
| Get the number of keys in a section of a dictionary. | |
char * | iniparser_getstring (dictionary *d, const char *key, char *def) | |
| Get the string associated to a key. | |
int | iniparser_getint (dictionary *d, const char *key, int notfound) | |
| Get the string associated to a key, convert to an int. | |
double | iniparser_getdouble (dictionary *d, const char *key, double notfound) | |
| Get the string associated to a key, convert to a double. | |
int | iniparser_getboolean (dictionary *d, const char *key, int notfound) | |
| Get the string associated to a key, convert to a boolean. | |
int | iniparser_set (dictionary *ini, const char *entry, const char *val) | |
| Set an entry in a dictionary. | |
void | iniparser_unset (dictionary *ini, const char *entry) | |
| Delete an entry in a dictionary. | |
int | iniparser_find_entry (dictionary *ini, const char *entry) | |
| Finds out if a given entry exists in a dictionary. | |
dictionary * | iniparser_load (const char *ininame) | |
| Parse an ini file and return an allocated dictionary object. | |
void | iniparser_freedict (dictionary *d) | |
| Free all memory associated to an ini dictionary. |
参考资料:
实例
1、生成shared library
$ gcc -fPIC -c dictionary.c iniparser.c
$ gcc -shared -o libiniparser.so dictionary.o iniparser.o -lc
在该目录下,生成了libiniparser.so共享库,
以后应用程序想要调用该共享库里面的APIs,只需引用iniparser.h头文件,并链接该共享库就可以了。
2、编辑INI文件
$ vim switch.ini
#This is the ini file of switch to save configure
[mirror]
mport = 15 ;
sports = 0x0 ;
[igmp]
dports = 0x1 ;
mport = 4 ;
[flooding]
mulricast = 0xffff ;
unicast = 0xffff ;
3、编写调用iniparser APIs的应用程序
$ vim test.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "iniparser.h"
int main()
{
int status;
int tmp;
dictionary * ini;
ini = iniparser_load("switch.ini");
if (ini==NULL)
{
fprintf(stderr, "cannot parse file: %s\n", "switch.ini");
return -1;
}
iniparser_dump(ini, stderr);
/* Get attributes */
printf("Mirror:\n");
tmp = iniparser_getint(ini, "mirror:mport", -1);
printf("mport: [%x]\n", tmp);
printf("igmp:\n");
tmp = iniparser_getint(ini, "igmp:mport", -1);
printf("mport: [%x]\n", tmp);
printf("flooding:\n");
tmp = iniparser_getint(ini, "flooding:multicast", -1);
printf("multicast: [%x]\n", tmp);
printf("flooding:\n");
tmp = iniparser_getint(ini, "flooding:unicast", -1);
printf("unicast: [%x]\n", tmp);
iniparser_freedict(ini);
return 0;
}
4、编译,链接生成可执行文件
$ gcc test.c -o test -L. -liniparser
$ ls
dictionary.h iniparser.h libiniparser.so switch.ini test test.c
$ ./test
[mirror]=UNDEF
[mirror:mport]=[15]
[mirror:sports]=[0x0]
[igmp]=UNDEF
[igmp:dports]=[0x1]
[igmp:mport]=[4]
[flooding]=UNDEF
[flooding:mulricast]=[0xffff]
[flooding:unicast]=[0xffff]
Mirror:mport: [f]
igmp:mport: [4]
flooding:multicast: [ffffffff] /*这里需要注意的是数据格式*/
flooding:unicast: [ffff]
从输出结果来看,该应用程序通过iniparser library提供的APIs,将INI file里面的配置信息解析出来了。
另一个ini开源库 minIni
GitHub - compuphase/minIni: A small and portable INI file library with read/write support