文章目錄
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
1 #
2 #This is the ini file of switch to save configure
3 #
4
5 [mirror]
6 mport = 15 ;
7 sports = 0x0 ;
8
9 [igmp]
10 dports = 0x1 ;
11 mport = 4 ;
12
13 [flooding]
14 mulricast = 0xffff ;
15 unicast = 0xffff ;
3、編寫調用iniparser APIs的應用程式
$ vim test.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5
6 #include "iniparser.h"
7
8 int main()
9 {
10 int status;
11 int tmp;
12 dictionary * ini;
13 ini = iniparser_load("switch.ini");
14 if (ini==NULL){
15 fprintf(stderr, "cannot parse file: %s\n", "switch.ini");
16 return -1;
17 }
18 iniparser_dump(ini, stderr);
19
20 /* Get attributes */
21 printf("Mirror:\n");
22 tmp = iniparser_getint(ini, "mirror:mport", -1);
23 printf("mport: [%x]\n", tmp);
24
25 printf("igmp:\n");
26 tmp = iniparser_getint(ini, "igmp:mport", -1);
27 printf("mport: [%x]\n", tmp);
28
29 printf("flooding:\n");
30 tmp = iniparser_getint(ini, "flooding:multicast", -1);
31 printf("multicast: [%x]\n", tmp);
32
33 printf("flooding:\n");
34 tmp = iniparser_getint(ini, "flooding:unicast", -1);
35 printf("unicast: [%x]\n", tmp);
36
37 iniparser_freedict(ini);
38 return 0;
39 }
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裡面的配置資訊解析出來了。