Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

importing an array or list setting using libconfig

I've never really worked with configuration files before, but I'm interested in using one as a parameter file for an engineering code I'm writing.

Not being a professional programmer, I've spent two days trying to figure out how to import a simple list or array setting using libconfig and I can't seem to get anywhere. Roughly working from the example here, I'm able to successfully import scalar settings, like

(importtest.cfg)

mynumber = 1;

using a main function like

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

I get the expected output

your number is 1

But I am unable to import a simple list setting, such as

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

In a similar way. I've tried a number of solutions (like creating a root setting), but don't really understand any of them.

Any help is much appreciated.

like image 573
JMJ Avatar asked Oct 19 '25 08:10

JMJ


1 Answers

It also took me a while to figure this out. This is how I finally did it, based on https://github.com/hyperrealm/libconfig/blob/master/examples/c%2B%2B/example1.cpp:

example_simple.cfg file:

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

Read it with 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);
}
like image 134
Katu Avatar answered Oct 21 '25 22:10

Katu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!