2017-08-25 6 views
2

私はこれまでコンフィグレーションファイルを使ったことはありませんでしたが、私が書いているエンジニアリングコードのパラメータファイルとして使うことに興味があります。libconfigを使用して配列やリストの設定をインポートする

私はプロのプログラマーではなく、libconfigを使って単純なリストや配列の設定をインポートする方法を理解するのに2日間を費やしました。大雑把example hereから作業、私は正常に私が期待される出力を得る

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; 
    } 

のような主要機能を使用して

(importtest.cfg)

mynumber = 1; 

のように、スカラー設定をインポートすることができますよ

your number is 1 

ただし、単純なリスト設定をインポートできません。 uchとして

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); 
} 
関連する問題