option_list.c
Go to the documentation of this file.
00001 #include <stdlib.h>
00002 #include <stdio.h>
00003 #include <string.h>
00004 #include "option_list.h"
00005 #include "utils.h"
00006 
00007 list *read_data_cfg(char *filename)
00008 {
00009     FILE *file = fopen(filename, "r");
00010     if(file == 0) file_error(filename);
00011     char *line;
00012     int nu = 0;
00013     list *options = make_list();
00014     while((line=fgetl(file)) != 0){
00015         ++ nu;
00016         strip(line);
00017         switch(line[0]){
00018             case '\0':
00019             case '#':
00020             case ';':
00021                 free(line);
00022                 break;
00023             default:
00024                 if(!read_option(line, options)){
00025                     fprintf(stderr, "Config file error line %d, could parse: %s\n", nu, line);
00026                     free(line);
00027                 }
00028                 break;
00029         }
00030     }
00031     fclose(file);
00032     return options;
00033 }
00034 
00035 int read_option(char *s, list *options)
00036 {
00037     size_t i;
00038     size_t len = strlen(s);
00039     char *val = 0;
00040     for(i = 0; i < len; ++i){
00041         if(s[i] == '='){
00042             s[i] = '\0';
00043             val = s+i+1;
00044             break;
00045         }
00046     }
00047     if(i == len-1) return 0;
00048     char *key = s;
00049     option_insert(options, key, val);
00050     return 1;
00051 }
00052 
00053 void option_insert(list *l, char *key, char *val)
00054 {
00055     kvp *p = malloc(sizeof(kvp));
00056     p->key = key;
00057     p->val = val;
00058     p->used = 0;
00059     list_insert(l, p);
00060 }
00061 
00062 void option_unused(list *l)
00063 {
00064     node *n = l->front;
00065     while(n){
00066         kvp *p = (kvp *)n->val;
00067         if(!p->used){
00068             fprintf(stderr, "Unused field: '%s = %s'\n", p->key, p->val);
00069         }
00070         n = n->next;
00071     }
00072 }
00073 
00074 char *option_find(list *l, char *key)
00075 {
00076     node *n = l->front;
00077     while(n){
00078         kvp *p = (kvp *)n->val;
00079         if(strcmp(p->key, key) == 0){
00080             p->used = 1;
00081             return p->val;
00082         }
00083         n = n->next;
00084     }
00085     return 0;
00086 }
00087 char *option_find_str(list *l, char *key, char *def)
00088 {
00089     char *v = option_find(l, key);
00090     if(v) return v;
00091     if(def) fprintf(stderr, "%s: Using default '%s'\n", key, def);
00092     return def;
00093 }
00094 
00095 int option_find_int(list *l, char *key, int def)
00096 {
00097     char *v = option_find(l, key);
00098     if(v) return atoi(v);
00099     fprintf(stderr, "%s: Using default '%d'\n", key, def);
00100     return def;
00101 }
00102 
00103 int option_find_int_quiet(list *l, char *key, int def)
00104 {
00105     char *v = option_find(l, key);
00106     if(v) return atoi(v);
00107     return def;
00108 }
00109 
00110 float option_find_float_quiet(list *l, char *key, float def)
00111 {
00112     char *v = option_find(l, key);
00113     if(v) return atof(v);
00114     return def;
00115 }
00116 
00117 float option_find_float(list *l, char *key, float def)
00118 {
00119     char *v = option_find(l, key);
00120     if(v) return atof(v);
00121     fprintf(stderr, "%s: Using default '%lf'\n", key, def);
00122     return def;
00123 }


rail_object_detector
Author(s):
autogenerated on Sat Jun 8 2019 20:26:30