C語言配置文件解析庫——iniparser

C語言配置文件解析庫——iniparser

前言:在對項目的優化時,發現Linux下沒有專門的供給C語言使用的配置文件函數,於是搜索到了iniparser庫,可以像那些面嚮對象語言一樣,使用ini文件進行參數配置。

介紹

iniparser是針對INI文件的解析器。ini文件則是一些系統或者軟件的配置文件。


基本語法

Iniparser庫的API可以對ini文件(配置文件)進行解析、設置、刪除等操作。

ini文件的最基本組成單元就是key或者叫property,每個key都有一個名稱(name)和對應的值(value):

name=value 
  • 1

而許多個Key可以被歸類爲一組,即section。組名定義要獨立一行,並用中括號括起來:

[section]
name=value
  • 1
  • 2

在section聲明下的keys都會和該section關聯起來。一個section的作用域會在下一個section聲明的地方結束,如果沒有下一個section的聲明,那麼該section的結束地方就是該文件末尾。section是不可以嵌套的。

定位一個key是用section:key來表示的,所以不同section下的key的名稱是可以相同的。

iniparser庫處理名稱的時候,會統一換成小寫,所以section和property的名稱命名是大小寫無關的。

註釋要以分號開頭:

 ;comment
  • 1

API

iniparser.h:

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對象(內存)

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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

示例

首先解壓你下載的庫文件:

tar -zxvf iniparser-3.1.tar.gz
  • 1

編譯:

cd iniparser-3.1/
make
  • 1
  • 2

可以看到src目錄下生成了六個文件,其中dictionary.h裏面聲明瞭一些直接解析ini file的API,iniparser.h裏面聲明瞭一些提供用戶操作的API。iniparser.h裏面的API是對dictionary.h裏面API的再次封裝,以提供用戶友好性。

然後拷貝src下的頭文件dictionary.h和iniparser.h以及壓縮包目錄下的靜態庫libiniparser.a和動態庫libiniparser.so.0到目標文件系統的對應目錄下。

編寫ini文件:

#ini file for example

[tcp]
;for tcp communication

port = 8000;
ip = 127.0.0.1;
family = AF_INET;

[serial port]
;for serial port communication

speed = 9600;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

測試文件:

/*************************************************************************
    
 ************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include "iniparser.h"

int main(void)
{
    dictionary *ini;
    int n = 0;
    char *str;

    ini = iniparser_load("example.ini");//parser the file
    if(ini == NULL)
    {
        fprintf(stderr,"can not open %s","example.ini");
        exit(EXIT_FAILURE);
    }

    printf("dictionary obj:\n");
    iniparser_dump(ini,stderr);//save ini to stderr

    printf("\n%s:\n",iniparser_getsecname(ini,0));//get section name
    n = iniparser_getint(ini,"tcp:port",-1);
    printf("port : %d\n",n);

    str = iniparser_getstring(ini,"tcp:ip","null");
    printf("ip : %s\n",str);

    str = iniparser_getstring(ini,"tcp:family","null");
    printf("family : %s\n",str);

    printf("\n%s:\n",iniparser_getsecname(ini,1));
    n = iniparser_getint(ini,"serial port:speed",-1);
    printf("speed : %d\n",n);

    iniparser_freedict(ini);//free dirctionary obj

    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

運行:

gcc example.c -o example -L. -liniparser
./example
  • 1
  • 2

結果:

dictionary obj:
[tcp]=UNDEF
[tcp:port]=[8000]
[tcp:ip]=[127.0.0.1]
[tcp:family]=[AF_INET]
[serial port]=UNDEF
[serial port:speed]=[9600]

tcp:
port : 8000
ip : 127.0.0.1
family : AF_INET

serial port:
speed : 9600
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

總結

這個庫對配置文件的管理還是很方便的,希望對您有幫助。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章