$ sudo aptitude install libconfig8 libconfig8-dev libconfig++ libconfig++-dev
#include <libconfig.h++>
#include <libconfig.hh>
#include <libconfig.h>
# Example application configuration file version = "1.0"; application: { window: { title = "My Application"; size = { w = 640; h = 480; }; pos = { x = 350; y = 250; }; }; list = ( ( "abc", 123, true ), 1.234, ( /* an empty list */) ); books = ( { title = "Treasure Island"; author = "Robert Louis Stevenson"; price = 29.95; qty = 5; }, { title = "Snow Crash"; author = "Neal Stephenson"; price = 9.99; qty = 8; } ); misc: { pi = 3.141592654; bigint = 9223372036854775807L; columns = [ "Last Name", "First Name", "MI" ]; bitmask = 0x1FC3; }; };
name = value;
name : value
{ settings... }
[ value, value ... ]
( value, value ... )
3578934 897893450934L
3.1415
" "
. " " " "
" " /* */ " " // " "
. # file: quote.cfg quote = "Criticism may not be agreeable, but it is necessary." " It fulfils the same function as pain in the human" " body. It calls attention to an unhealthy state of" " things.\n" "\t--Winston Churchill";
# file: test.cfg info: { name = "Winston Churchill"; @include "quote.cfg" country = "UK"; };
#include <stdio.h> #include <stdlib.h> #include <libconfig.h> /* 'example.cfg' */ int main(int argc, char **argv) { /* . */ config_t cfg; config_setting_t *setting; const char *str; config_init(&cfg); /* */ /* . , */ if(! config_read_file(&cfg, "example.cfg")) { fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); config_destroy(&cfg); return(EXIT_FAILURE); } /* "name". */ if(config_lookup_string(&cfg, "name", &str)) printf("Store name: %s\n\n", str); else fprintf(stderr, "No 'name' setting in configuration file.\n"); /* . */ setting = config_lookup(&cfg, "inventory.books"); if(setting != NULL) { int count = config_setting_length(setting); int i; printf("%-30s %-30s %-6s %s\n", "TITLE", "AUTHOR", "PRICE", "QTY"); for(i = 0; i < count; ++i) { config_setting_t *book = config_setting_get_elem(setting, i); /* , . */ const char *title, *author; double price; int qty; if(!(config_setting_lookup_string(book, "title", &title) && config_setting_lookup_string(book, "author", &author) && config_setting_lookup_float(book, "price", &price) && config_setting_lookup_int(book, "qty", &qty))) continue; printf("%-30s %-30s $%6.2f %3d\n", title, author, price, qty); } putchar('\n'); } /* . */ setting = config_lookup(&cfg, "inventory.movies"); if(setting != NULL) { unsigned int count = config_setting_length(setting); unsigned int i; printf("%-30s %-10s %-6s %s\n", "TITLE", "MEDIA", "PRICE", "QTY"); for(i = 0; i < count; ++i) { config_setting_t *movie = config_setting_get_elem(setting, i); /* , . */ const char *title, *media; double price; int qty; if(!(config_setting_lookup_string(movie, "title", &title) && config_setting_lookup_string(movie, "media", &media) && config_setting_lookup_float(movie, "price", &price) && config_setting_lookup_int(movie, "qty", &qty))) continue; printf("%-30s %-10s $%6.2f %3d\n", title, media, price, qty); } putchar('\n'); } config_destroy(&cfg); /* , */ return(EXIT_SUCCESS); }
#include <iostream> #include <iomanip> #include <cstdlib> #include <libconfig.h++> using namespace std; using namespace libconfig; // , 'example.cfg' int main(int argc, char **argv) { Config cfg; // . // ++ , try { cfg.readFile("example.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); } // . try { string name = cfg.lookup("name"); cout << "Store name: " << name << endl << endl; } catch(const SettingNotFoundException &nfex) { cerr << "No 'name' setting in configuration file." << endl; } const Setting& root = cfg.getRoot(); // . try { const Setting &books = root["inventory"]["books"]; int count = books.getLength(); cout << setw(30) << left << "TITLE" << " " << setw(30) << left << "AUTHOR" << " " << setw(6) << left << "PRICE" << " " << "QTY" << endl; for(int i = 0; i < count; ++i) { const Setting &book = books[i]; // , . string title, author; double price; int qty; if(!(book.lookupValue("title", title) && book.lookupValue("author", author) && book.lookupValue("price", price) && book.lookupValue("qty", qty))) continue; cout << setw(30) << left << title << " " << setw(30) << left << author << " " << '$' << setw(6) << right << price << " " << qty << endl; } cout << endl; } catch(const SettingNotFoundException &nfex) { // Ignore. } // . try { const Setting &movies = root["inventory"]["movies"]; int count = movies.getLength(); cout << setw(30) << left << "TITLE" << " " << setw(10) << left << "MEDIA" << " " << setw(6) << left << "PRICE" << " " << "QTY" << endl; for(int i = 0; i < count; ++i) { const Setting &movie = movies[i]; // , . string title, media; double price; int qty; if(!(movie.lookupValue("title", title) && movie.lookupValue("media", media) && movie.lookupValue("price", price) && movie.lookupValue("qty", qty))) continue; cout << setw(30) << left << title << " " << setw(10) << left << media << " " << '$' << setw(6) << right << price << " " << qty << endl; } cout << endl; } catch(const SettingNotFoundException &nfex) { // Ignore. } return(EXIT_SUCCESS); }
$ apt-get source libconfig++
Source: https://habr.com/ru/post/148948/
All Articles