Statistics
| Branch: | Revision:

grapes / som / ChunkBuffer / config.c @ 0940b7ec

History | View | Annotate | Download (1.25 KB)

1
#include <stdlib.h>
2
#include <string.h>
3
#include "config.h"
4

    
5
struct tag {
6
  char name[32];
7
  char value[16];
8
};
9

    
10
#define MAX_TAGS 16
11

    
12
#warning Fix config_parse
13
struct tag *config_parse(const char *cfg)
14
{
15
  struct tag *res;
16
  int i = 0;
17
  const char *p = cfg;
18

    
19
  res = malloc(sizeof(struct tag) * MAX_TAGS);
20
  if (res == NULL) {
21
    return res;
22
  }
23
  while (p && *p != 0) {
24
    char *p1 = strchr(p, '=');
25
    if (p1) {
26
      if (i % MAX_TAGS == 0) {
27
        res = realloc(res, sizeof(struct tag) * (i + MAX_TAGS));
28
      }
29
      memcpy(res[i].name, p, p1 - p);
30
      res[i].name[p1 - p + 1] = 0;
31
      p = strchr(p1, ',');
32
      if (p == NULL) {
33
        strcpy(res[i++].value, p1 + 1);
34
      } else {
35
        memcpy(res[i].value, p1 + 1, p - p1 - 1);
36
        res[i++].value[p - p1] = 0;
37
        p++;
38
      }
39
    } else {
40
      p = NULL;
41
    }
42
  }
43
  res[i++].name[0] = 0;
44

    
45
  res = realloc(res, sizeof(struct tag) * i);
46

    
47
  return res;
48
}
49

    
50
int config_value_int(const struct tag *cfg_values, const char *value, int *res)
51
{
52
  int i, done;
53

    
54
  i = 0; done = 0;
55
  while (!done) {
56
    if (!strcmp(cfg_values[i].name, value)) {
57
      *res = atoi(cfg_values[i].value);
58

    
59
      return 1;
60
    }
61
    if (cfg_values[i].name[0] == 0) {
62
      done = 1;
63
    } else {
64
      i++;
65
    }
66
  }
67

    
68
  return 0;
69
}