日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁 > 物聯(lián)網(wǎng) > 嵌入式大雜燴
[導(dǎo)讀]inih 只有幾頁代碼,并且設(shè)計(jì)緊湊小巧,因此對嵌入式系統(tǒng)非常有用。

大家好,我是ZhengN。本次給大家推薦一篇關(guān)于INI文件解析器的文章:

1、簡介

開源項(xiàng)目 inih ( 1.3K star )

  • https://github.com/benhoyt/inih

inih (INI Not Invented Here 的縮寫) 是一個簡單的用 C 語言編寫的 INI 文件解析器。

INI 文件一般用于保存配置信息,它的格式很簡單:

[section1]
name1 = value1

[section2]
name2 = value2
name3 = value3
...

在 Linux 系統(tǒng)中也經(jīng)常能看到 INI 文件:

$ cat /etc/systemd/logind.conf
[Login] #NAutoVTs=6 #ReserveVT=6 #KillUserProcesses=no ...

代碼簡介

inih 只有幾頁代碼,并且設(shè)計(jì)緊湊小巧,因此對嵌入式系統(tǒng)非常有用。

它或多或少與 Python 的INI 文件的 ConfigParser 樣式兼容,包括 RFC 822樣式的多行語法和 name: value 條目。

要使用它,只需給 ini_parse() 一個 INI 文件,它將為每個解析的 name = value 對調(diào)用一個回調(diào)函數(shù),通過回調(diào)函數(shù)返回 section,name 和 value 字符串。之所以采用這種方式 ( "SAX style",SAX 是 Simple API for XML的縮寫),是因?yàn)樗诘蛢?nèi)存嵌入式系統(tǒng)上運(yùn)行良好,而且還因?yàn)樗梢宰龅?KISS ( Keep it Simple and Stupid)。

用戶也可以:

  • 調(diào)用 ini_parse_file() 直接從 FILE * 對象進(jìn)行解析;

  • 調(diào)用 ini_parse_string() 從字符串中解析數(shù)據(jù);

  • 調(diào)用 ini_parse_stream() 以自定義 fgets-style reader 函數(shù)去解析自定義數(shù)據(jù)流;

2、使用ini.h

2.1 測試?yán)?ini_example.c

定義數(shù)據(jù)結(jié)構(gòu):

#include "../ini.h" typedef struct { int version; const char* name; const char* email;
} configuration;

程序主干:

int main(int argc, char* argv[]) {
    configuration config; if (ini_parse("test.ini", handler, &config) < 0) { printf("Can't load 'test.ini'\n"); return 1;
    } printf("Config loaded from 'test.ini': version=%d, name=%s, email=%s\n",
        config.version, config.name, config.email); free((void*)config.name); free((void*)config.email); return 0;
}

用戶自定義處理函數(shù):

static int handler(void* user, const char* section, const char* name,const char* value) {
    configuration* pconfig = (configuration*)user; #define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0 if (MATCH("protocol", "version")) {
        pconfig->version = atoi(value);
    } else if (MATCH("user", "name")) {
        pconfig->name = strdup(value);
    } else if (MATCH("user", "email")) {
        pconfig->email = strdup(value);
    } else { return 0; /* unknown section/name, error */ } return 1;
}

INI 文件:

$ cat test.ini 
; Test config file for ini_example.c and INIReaderTest.cpp

[protocol]             ; Protocol configuration
version=6              ; IPv6

[user]
name = Bob Smith       ; Spaces around '=' are stripped
email = bob@smith.com  ; And comments (like this) ignored
active = true ; Test a boolean
pi = 3.14159           ; Test a floating point number

運(yùn)行效果:

$ cd inih/examples $ gcc ../ini.c ini_example.c -o ini_example $ ./ini_example Config loaded from 'test.ini': version=6, name=Bob Smith, email=bob@smith.com

2.2 更多的編譯參數(shù)

有 3 種類型的編譯參數(shù)可用:

  • Syntax options,用于配置 INI 文件語法,例如指定注釋行的字符;
#define INI_START_COMMENT_PREFIXES ";#" #define INI_ALLOW_NO_VALUE 0 ...
  • Parsing options,用于配置解析行為,例如解析錯誤時(shí)是否馬上停止解析;
#define INI_STOP_ON_FIRST_ERROR 0 #define INI_HANDLER_LINENO 0 ...
  • Memory options,內(nèi)存相關(guān)的配置,例如最大的行長度 / 使用 heap 還是 stack 來保存行數(shù)據(jù);
#define INI_USE_STACK 1 #define INI_MAX_LINE 200 ...

3、內(nèi)部實(shí)現(xiàn)

3.1 整體把握

核心代碼就是 ini.c 和 ini.h。

分解 ini.c:

4 個公共函數(shù):

int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler, int ini_parse_file(FILE* file, ini_handler handler, void* user) int ini_parse(const char* filename, ini_handler handler, void* user) int ini_parse_string(const char* string, ini_handler handler, void* user) 

5 個私有函數(shù):

static char* rstrip(char* s) static char* lskip(const char* s) static char* find_chars_or_comment(const char* s, const char* chars) static char* strncpy0(char* dest, const char* src, size_t size) static char* ini_reader_string(char* str, int num, void* stream) 

剩下的就是一些宏定義,沒有任何公共變量和全局私有變量,非常簡潔。

3.2 核心 API 的實(shí)現(xiàn)

1) ini_parse_stream() 是關(guān)鍵函數(shù),用于解析數(shù)據(jù)流:

int ini_parse_stream(ini_reader reader, void* stream,ini_handler handler, void* user) 

2) ini_parse_stream() 的參數(shù):

  • ini_reader reader:函數(shù)指針,提供了讀取一行文本的操作;
typedef char* (*ini_reader)(char* str, int num, void* stream);
  • void* stream:待解析的文本數(shù);

  • ini_handler handler:函數(shù)指針,指向用戶提供的回調(diào)函數(shù)。

typedef int (*ini_handler)(void* user, const char* section const char* name, const char* value);
  • void* user:用于保存解析后的數(shù)據(jù);

3) ini_parse_stream() 實(shí)現(xiàn)思路:

  • 讀取1行數(shù)據(jù):

  • 處理1行數(shù)據(jù):

    • 去掉行末尾的空格: rstrip();

    • 跳過行首的空格: lskip();

    • 忽略以 ;或# 開始的注釋行:

if (strchr(INI_START_COMMENT_PREFIXES, *start))
  • 判斷是否是 section:
if (*start == '[') 
        find_chars_or_comment(start + 1, "]");
  • 判斷是否是鍵值對:find_chars_or_comment(start, "=:");

  • 調(diào)用用戶指定的處理函數(shù),一般是用于保存解析到的數(shù)據(jù): handler();

更多的源碼細(xì)節(jié),請各位自行閱讀源碼吧~~~



免責(zé)聲明:本文內(nèi)容由21ic獲得授權(quán)后發(fā)布,版權(quán)歸原作者所有,本平臺僅提供信息存儲服務(wù)。文章僅代表作者個人觀點(diǎn),不代表本平臺立場,如有問題,請聯(lián)系我們,謝謝!

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請及時(shí)聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關(guān)閉