示例#1
0
void settings_init(const char *path)
{

    config_t *cfg;

    cfg = cfg_open(path);

    settings_parse(cfg);

    cfg_close(cfg);

}
示例#2
0
文件: settings.c 项目: knudje/pilight
int settings_read(void) {
    FILE *fp;
    char *content;
    size_t bytes;
    JsonNode *root;
    struct stat st;

    /* Read JSON config file */
    if(!(fp = fopen(settingsfile, "rb"))) {
        logprintf(LOG_ERR, "cannot read settings file: %s", settingsfile);
        return EXIT_FAILURE;
    }

    fstat(fileno(fp), &st);
    bytes = (size_t)st.st_size;

    if(!(content = calloc(bytes+1, sizeof(char)))) {
        logprintf(LOG_ERR, "out of memory");
        fclose(fp);
        return EXIT_FAILURE;
    }

    if(fread(content, sizeof(char), bytes, fp) == -1) {
        logprintf(LOG_ERR, "cannot read settings file: %s", settingsfile);
    }
    fclose(fp);

    /* Validate JSON and turn into JSON object */
    if(json_validate(content) == false) {
        logprintf(LOG_ERR, "settings are not in a valid json format", content);
        sfree((void *)&content);
        return EXIT_FAILURE;
    }

    root = json_decode(content);

    if(settings_parse(root) != 0) {
        sfree((void *)&content);
        return EXIT_FAILURE;
    }
    char *output = json_stringify(root, "\t");
    settings_write(output);
    json_delete(root);
    sfree((void *)&output);
    sfree((void *)&content);
    return EXIT_SUCCESS;
}