1

我正在尝试从一组中读取布尔设置。我正在使用以下代码,但config_lookup_bool总是返回 CONFIG_FALSE。据我了解,它应该将值写入send_keys并返回 CONFIG_TRUE 。

代码:

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
}

配置文件:

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

我的代码或我的想法有什么错误吗?

4

2 回答 2

0

感谢您的输入。问题是我在“”中有真/假,因此它被解析为字符串。应该是

settings :
{
  send_keys = true;
  start_apps = false;
  sync_clocks = false;
  pc_clock_is_origin = true;
  calibration_start_time = 0L;
};
于 2021-02-26T16:26:26.783 回答
0

这里有一个config_lookup_bool 示例,其中包含配置文件和代码:(使用此示例与您所拥有的进行比较。)

配置文件内容:

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

读取和处理它的源...

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;
}
于 2021-02-25T22:19:46.443 回答