Question

I'vm trying to write app that will work with libconfig. So I've the next part of code:

#include <stdio.h>
#include <stdlib.h>
#include <libconfig.h>


int main(int argc, char **argv)
{
    if(argc > 1)
    {
        char *config_file_path = argv[1];
        config_t cfg;

        config_init(&cfg);
        printf("loading config: %s...\n", config_file_path);

        if (!config_read_file(&cfg, config_file_path))
        {
            fprintf(stderr, "%s:%d - %s\n", config_file_path,
                config_error_line(&cfg), config_error_text(&cfg));
            config_destroy(&cfg);
            exit(EXIT_FAILURE);
        }

        long int port_number;
        config_lookup_int(&cfg, "server_app.port", &port_number); 

        printf("port: %ld\n", port_number);

        config_destroy(&cfg);
    }
    else
    {
        fprintf(stderr, "Wrong number of arguments. Usage: %s <cfg_file>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    return EXIT_SUCCESS;
}

And config files for it:

server.cfg

server_app:
{
    port = 9034;
    mail_queue_dir = "mail_queue";

@include "quote.cfg"

    logs:
    {
        error_log = "logs/error.log";
    };

    connection_address = "127.0.0.1";
    max_worker_number = 10;
    number_of_pending_connections = 10;
};

quote.cfg

# file: quote.cfg
quote = "dsf";

After compiling I try to run the main() with server.cfg in params, but I always get "syntax error" on @include's line. Any ideas?

Was it helpful?

Solution

I tried your configuration with libconfig-1.4.9 and it works:

./cfg server.cfg 
loading config: server.cfg...
port: 9034

You seem to be using an old version, that has a bug in parsing @include, judging by this change log, you should upgrade to libconfig-1.4.3 or newer:

----- version 1.4.3 ------

2010-02-13 Mark Lindner * lib/scanner.l - bugfix for matching @include with preceding whitespace

.

OTHER TIPS

@include "quote.cfg"

should be

#include "quote.cfg"

@include is not part of C syntax.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top