Statistics
| Branch: | Revision:

streamers / config.c @ 3fcca057

History | View | Annotate | Download (3.62 KB)

1 8a49328f CsabaKiraly
#include <stdio.h>
2
#include <string.h>
3
#include <stdlib.h>
4
#include <ctype.h>
5
#include <stddef.h>
6
7
#include "config.h"
8
9
#define CONFIG_TYPE_INT    1
10
#define CONFIG_TYPE_FLOAT  2
11
#define CONFIG_TYPE_STRING 3
12
13
Config_t g_config;
14
15
typedef struct {
16
  void* entry;
17
  const char* string;
18
  unsigned int type;
19
  const char* default_value;
20
} ConfigEntry_t;
21
22
/* NOTE: Add additional entries here */
23
ConfigEntry_t config_entries[] = {
24
    { &g_config.neighborhood_target_size, "neighborhood_target_size", CONFIG_TYPE_INT, "15" },
25 cbae3c2c CsabaKiraly
    { &g_config.alto_server,        "alto_server",        CONFIG_TYPE_STRING, "" },
26 8a49328f CsabaKiraly
    { &g_config.alto_factor,        "alto_factor",        CONFIG_TYPE_FLOAT,  "0.8" },
27
    { &g_config.alto_pri_criterion, "alto_pri_criterion", CONFIG_TYPE_INT,    "1" },
28
    { &g_config.alto_sec_criterion, "alto_sec_criterion", CONFIG_TYPE_INT,    "0" },
29
    { &g_config.update_interval,    "update_interval",    CONFIG_TYPE_INT,    "60" },
30
31
    { NULL, "", 0 }        /* end marker -- should always be last */
32
};
33
34
int config_parse_entry(void *dst, const char *src, const char *string, int type) {
35
  int len = strlen(string);
36
  if (!strncmp(src, string, len) && isspace(src[len])) {
37
    /* match found */
38
39
    char* value = src+len+1;
40
    while (isspace(*value)) ++value; /* skip white spaces */
41
42
    if (type == CONFIG_TYPE_INT) *((int*)dst) = atoi(value);
43
    else if (type == CONFIG_TYPE_FLOAT) *((float*)dst) = atof(value);
44
    else if (type == CONFIG_TYPE_STRING) {
45
      if (*(char**)dst) free(*(char**)dst); /* free any already existing strings */
46
      *(char**)dst = strdup(value);
47
    }
48
    else return 0; /* other types not yet implemented */
49
    return 1;      /* correct match found */
50
  }
51
  return 0;        /* no match */
52
}
53
54
void config_init_entry(ConfigEntry_t* entry) {
55
  if (entry->type == CONFIG_TYPE_INT)         *((int*)entry->entry) = atoi(entry->default_value);
56
  else if (entry->type == CONFIG_TYPE_FLOAT)  *((float*)entry->entry) = atof(entry->default_value);
57
  else if (entry->type == CONFIG_TYPE_STRING) *(char**)(entry->entry) = strdup(entry->default_value);
58
}
59
60
void config_init() {
61
  int e = 0;
62
  while (config_entries[e].entry) {
63
    config_init_entry(&config_entries[e]);
64
    e++;
65
  }
66
}
67
68
int config_load(const char *filename) {
69
  char line[512];
70
  int len;
71
  FILE *f = NULL;
72
  int e = 0;
73
74
  f = fopen(filename, "rt");
75
  if (!f) {
76
    fprintf(stderr,"Unable to open config file.\n");
77
    return 0;
78
  }
79
  while (fgets(line, 512, f)) {
80
    char *p = line;
81
    while (isspace(*p)) ++p; /* skip white spaces */
82
83
    /* ignore comments and empty lines */
84
    if ( (*p=='#') || (*p=='\n') || (*p==0)) continue;
85
86
        /* remove newline */
87
        len = strlen(p);
88
    if (p[len-1]=='\n') p[len-1]='\0';
89
90
    while (config_entries[e].entry) {
91
      if (config_parse_entry(config_entries[e].entry, p, config_entries[e].string, config_entries[e].type)) break;
92
      e++;
93
    }
94
  }
95
  fclose(f);
96
  return 1;
97
}
98
99
void config_free() {
100
  int e = 0;
101
  while (config_entries[e].entry) {
102
    if (config_entries[e].type == CONFIG_TYPE_STRING) {
103
      free(*(char**)config_entries[e].entry);
104
        }
105
    config_entries[e].entry = NULL;
106
    config_entries[e].string = NULL;
107
    config_entries[e].type = 0;
108
    e++;
109
  }
110
}
111
112
void config_dump() {
113
  int e = 0;
114
  while (config_entries[e].entry) {
115
    fprintf(stderr,"%s ", config_entries[e].string);
116
    if (config_entries[e].type == CONFIG_TYPE_INT) fprintf(stderr,"%d", *(int*)config_entries[e].entry);
117
    else if (config_entries[e].type == CONFIG_TYPE_FLOAT) fprintf(stderr,"%f", *(float*)config_entries[e].entry);
118
    else if (config_entries[e].type == CONFIG_TYPE_STRING) fprintf(stderr,"%s", *(char**)config_entries[e].entry);
119
    fprintf(stderr,"\n");
120
    e++;
121
  }
122
}