AlexeyAB
2018-02-08 cd8a3dcb4ca42f22ad8f46a95e00977c92be6bbd
src/option_list.c
@@ -2,12 +2,53 @@
#include <stdio.h>
#include <string.h>
#include "option_list.h"
#include "utils.h"
typedef struct{
    char *key;
    char *val;
    int used;
} kvp;
list *read_data_cfg(char *filename)
{
    FILE *file = fopen(filename, "r");
    if(file == 0) file_error(filename);
    char *line;
    int nu = 0;
    list *options = make_list();
    while((line=fgetl(file)) != 0){
        ++ nu;
        strip(line);
        switch(line[0]){
            case '\0':
            case '#':
            case ';':
                free(line);
                break;
            default:
                if(!read_option(line, options)){
                    fprintf(stderr, "Config file error line %d, could parse: %s\n", nu, line);
                    free(line);
                }
                break;
        }
    }
    fclose(file);
    return options;
}
int read_option(char *s, list *options)
{
    size_t i;
    size_t len = strlen(s);
    char *val = 0;
    for(i = 0; i < len; ++i){
        if(s[i] == '='){
            s[i] = '\0';
            val = s+i+1;
            break;
        }
    }
    if(i == len-1) return 0;
    char *key = s;
    option_insert(options, key, val);
    return 1;
}
void option_insert(list *l, char *key, char *val)
{
@@ -47,7 +88,7 @@
{
    char *v = option_find(l, key);
    if(v) return v;
    fprintf(stderr, "%s: Using default '%s'\n", key, def);
    if(def) fprintf(stderr, "%s: Using default '%s'\n", key, def);
    return def;
}
@@ -59,6 +100,20 @@
    return def;
}
int option_find_int_quiet(list *l, char *key, int def)
{
    char *v = option_find(l, key);
    if(v) return atoi(v);
    return def;
}
float option_find_float_quiet(list *l, char *key, float def)
{
    char *v = option_find(l, key);
    if(v) return atof(v);
    return def;
}
float option_find_float(list *l, char *key, float def)
{
    char *v = option_find(l, key);