1

I'm trying to read a bool setting from a group. I'm using the following code, but config_lookup_bool always returns CONFIG_FALSE. As far as I understood it should write the value into send_keys and return CONFIG_TRUE instead.

Code:

int send_keys;
config_t cfg;

config_init(&cfg);
config_read_file(&cfg, "config.cfg")

if (config_lookup_bool(&cfg, "settings.send_keys", &send_keys))
{
    // do something here
}

config.cfg:

settings :
{
  send_keys = "true";
  start_apps = "false";
  sync_clocks = "false";
  pc_clock_is_origin = "true";
  calibration_start_time = 0L;
};

Is there any mistake in my code or my thoughts?

pikim
  • 11
  • 1
  • In prototype for this function: , `int config_lookup_bool (const config_t * config, const char * path, int * value)` The 2nd argument denotes a path. Is `"settings.send_keys"` a path? Are you sure about that argument? maybe use `.\\"config.cfg"` or if Linux `./"config.cfg"` – ryyker Feb 25 '21 at 22:11
  • I assume `settings.send_keys` to be a path. I took it from [example1.c](https://github.com/hyperrealm/libconfig/blob/master/examples/c/example1.c) and the corresponding [example.cfg](https://github.com/hyperrealm/libconfig/blob/master/examples/c/example.cfg). My `config.cfg` itself can be read. There are some more settings which I read at another place without problems. – pikim Feb 25 '21 at 22:18

2 Answers2

0

There is a config_lookup_bool example here that includes the config file and the code: (use this example to compare against what you have.)

config file contents:

# authenticator
    
name = "JP";
enabled = false;
length = 186;
    
ldap = {
    host = "ldap.example.com";
        base = "ou=usr,o=example.com";  /* adapt this */
        retries = [10, 15, 20, 60]; // Use more than 2
};

Source to read and process it...

int main(int argc, char **argv)
{
    config_t cfg, *cf;
    const config_setting_t *retries;
    const char *base = NULL;
    int count, n, enabled;

    cf = &cfg;
    config_init(cf);

    if (!config_read_file(cf, "ldap.cfg")) {
        fprintf(stderr, "%s:%d - %s\n",
            config_error_file(cf),
            config_error_line(cf),
            config_error_text(cf));
        config_destroy(cf);
        return(EXIT_FAILURE);
    }

    if (config_lookup_bool(cf, "enabled", &enabled))
        printf("Enabled: %s\n", enabled ? "Yep" : "Nope");
    else 
        printf("Enabled is not defined\n");

    if (config_lookup_string(cf, "ldap.base", &base))
        printf("Host: %s\n", base);

    retries = config_lookup(cf, "ldap.retries");
    count = config_setting_length(retries);

    printf("I have %d retries:\n", count);
    for (n = 0; n < count; n++) {
        printf("\t#%d. %d\n", n + 1,
            config_setting_get_int_elem(retries, n));
    }

    config_destroy(cf);
    return 0;
}
ryyker
  • 22,849
  • 3
  • 43
  • 87
0

Thanks for your input. The problem was that I had true/false in "" and therefore it was parsed as string. It should have been

settings :
{
  send_keys = true;
  start_apps = false;
  sync_clocks = false;
  pc_clock_is_origin = true;
  calibration_start_time = 0L;
};
pikim
  • 11
  • 1