INI file and Iniparser
来源:互联网 发布:盛世网络剧全集 编辑:程序博客网 时间:2024/04/29 15:50
1、概述:
INI file是配置文件,保存的是数据,主要是系统或者软件的配置信息。
Iniparser则是对INI file的解析或者操作(get,set,delete 等等)。
下面分别就INI file的文件格式和Iniparser提供的APIs进行说明。
2、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
3、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文件里声明了。
├── src
├── dictionary.c
├── dictionary.h
├── iniparser.c
└── iniparser.h
dictionary.h里面声明了一些直接解析ini file的APIs,
iniparser.h里面声明了一些提供用户操作的APIs,
需要说明的是iniparser.h里面的APIs是对dictionary.h里面APIs的再次封装,以提供用户友好性。
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.
参考资料:
INI file from wiki
实例:
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 $ lsdictionary.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里面的配置信息解析出来了。
from: http://blog.csdn.net/l0605020112/article/details/9985435
- INI file and Iniparser
- INI file and Iniparser
- INI file and Iniparser
- 使用iniparser 处理INI文件
- IniParser+win7解析配置文件INI
- ini文件解析c库(iniparser)
- Unity中解析ini配置文件----INIParser
- ini文件解析c库(iniparser)
- iniparser
- C语言ini形式配置文件解析库——iniparser
- C/C++跨平台INI文件解析库:iniparser
- Configure file like format of ini and read data from ini file or write data to ini file
- INI file
- INI file 详解。
- Perl read INI file
- Read/Write ini file
- Domino - NOTES.INI file
- c# workwith .ini file
- LeetCode:Search Insert Position
- 学习过程:Red5(二)
- materialized view
- JAVA学习-多线程
- V4L2(video 4 linux 2)
- INI file and Iniparser
- hdu2546 饭卡
- Java 里的正则表达式
- Effective Objective-C 2.0 编写高质量iOS与OS X代码 理解“类对象”的用意
- 两种解决win7系统的电脑出现开机黑屏的方法
- 多态的定义及举例
- 学写网站需求分析
- 常见数据库的分页实现方案
- TF-IDF模型的概率解释