2013年6月7日 星期五

How to use IniHandler??

1. 作為讀取參數設定檔之用途

2. INI file format (內容嚴禁空白)
[SectionName]
key=value
[Host]
ipaddress=192.168.19.1
port=9999
[RCM3]
ipaddress=192.168.19.2
port=8888


PS: 將上述(2)內容存檔於C:\ipaddress.ini

ex:
#include “IniHandler.h”

main (void)
{
/* 建立 INI file handler */
IniHandler ini_hand;

/* 打開INI file */
ini_hand.open ("c:\\ipaddress.ini");

/* 讀取所有的Section Name */
vSectionNames_T names = ini_hand.readSectionNames ();

vSection_T section;
/* 逐一以Section Name, 讀取該Section內所有的key value */
for (int i=0; i<(int)names.size(); i++) {
    ap_log (DBUG, "get the tag %s", names[i].c_str());

    section.clear ();
    /* 帶入section name, 讀取 key-value */
    section = ini_hand.readKeyValues ((char *)names[i].c_str());

    /* 逐一印出key-value */
    for (int j=0; j<(int)section.size(); j++) {
        ap_log (DBUG, "key = %s, values = %s", section[j].key, section[j].value);
    }
}
/* 關閉 INI file */
ini_hand.close ();
}

沒有留言:

張貼留言