2017-08-25 60 views
2

我從來沒有真正使用過配置文件,但我有興趣使用一個作爲我正在編寫的工程代碼的參數文件。使用libconfig導入數組或列表設置

我不是一個專業的程序員,我花了兩天的時間試圖弄清楚如何使用libconfig導入一個簡單的列表或數組設置,我似乎無法得到任何地方。從example here粗略地工作,我能夠成功導入標設置,如

(importtest.cfg)

mynumber = 1; 

使用的主要功能像

Config cfg; 

    // check for I/O and parse errors 
    try 
    { 
     cfg.readFile("importtest.cfg"); 
    } 
    catch(const FileIOException &fioex) 
    { 
     std::cerr << "I/O error while reading config file." << std::endl; 
     return(EXIT_FAILURE); 
    } 
    catch(const ParseException &pex) 
    { 
     std::cerr << "Parse error at " << pex.getFile() << " : line " << pex.getLine() << " - " << pex.getError() << std::endl; 
     return(EXIT_FAILURE); 
    } 

    // look for 'mynumber' 
    try 
    { 
     int number = cfg.lookup("mynumber"); 
     std::cout << "your number is " << number << std::endl; 
    } 
    catch(const SettingNotFoundException &nfex) 
    { 
     std::cerr << "No 'mynumber' setting in configuration file." << std::endl; 
    } 

我得到的預期輸出

your number is 1 

但我無法導入一個簡單的列表設置,s例如

mylist = (1,2,3,4); 

以類似的方式。我已經嘗試了許多解決方案(如創建根設置),但並不真正瞭解其中的任何解決方案。

任何幫助,非常感謝。

回答

0

我花了一段時間才弄明白這一點。這就是我終於做到了,基於https://github.com/hyperrealm/libconfig/blob/master/examples/c%2B%2B/example1.cpp

example_simple.cfg文件:

albums = 
    (
    { 
     title  = "one title"; 
     year  = 2017; 
     songs  = ["first song", "second song"]; 
    }, 
    { 
     title  = "another title"; 
     year  = 2015; 
     songs  = ["first song", "second song", "third song"]; 
    } 
); 

與libconfig C瞭解它++ API:

#include <iostream> 
#include <iomanip> 
#include <cstdlib> 
#include <vector> 
#include <libconfig.h++> 

using namespace std; 
using namespace libconfig; 

int main(int argc, char **argv) 
{ 
    Config cfg; 

    // Read the file. If there is an error, report it and exit. 
    try 
    { 
    cfg.readFile("../example_simple.cfg"); 
    } 
    catch(const FileIOException &fioex) 
    { 
    std::cerr << "I/O error while reading file." << std::endl; 
    return(EXIT_FAILURE); 
    } 
    catch(const ParseException &pex) 
    { 
    std::cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine() 
       << " - " << pex.getError() << std::endl; 
    return(EXIT_FAILURE); 
    } 

    const Setting& root = cfg.getRoot(); 
    try 
    { 
    const Setting &albums = root["albums"]; 
    for(int i = 0; i < albums.getLength(); ++i) 
    { 
     const Setting &album = albums[i]; 
     string my_title; 
     int my_year; 
     vector<string> my_songs; 

     album.lookupValue("title", my_title); 
     cout << "title: " << my_title << endl; 
     album.lookupValue("year", my_year); 
     cout << "year: " << my_year << endl; 

     const Setting &songs_settings = album.lookup("songs"); 
     vector<string> songs; 
     for (int n = 0; n < songs_settings.getLength(); ++n) 
     { 
      my_songs.push_back(songs_settings[i]); 
      cout << "song number " << n << ": " << my_songs[n] << endl; 
     } 
    } 
    } 
    catch(const SettingNotFoundException &nfex) 
    { 
    // Ignore. 
    } 

    return(EXIT_SUCCESS); 
}