Statistics
| Branch: | Revision:

grapes / som / ChunkBuffer / config.c @ f5853f90

History | View | Annotate | Download (1.33 KB)

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

    
5
#define NAME_SIZE 32
6
#define VAL_SIZE 16
7

    
8
struct tag {
9
  char name[NAME_SIZE];
10
  char value[VAL_SIZE];
11
};
12

    
13
#define MAX_TAGS 16
14

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

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

    
49
  res = realloc(res, sizeof(struct tag) * i);
50

    
51
  return res;
52
}
53

    
54
int config_value_int(const struct tag *cfg_values, const char *value, int *res)
55
{
56
  int i, done;
57

    
58
  i = 0; done = 0;
59
  while (!done) {
60
    if (!strcmp(cfg_values[i].name, value)) {
61
      *res = atoi(cfg_values[i].value);
62

    
63
      return 1;
64
    }
65
    if (cfg_values[i].name[0] == 0) {
66
      done = 1;
67
    } else {
68
      i++;
69
    }
70
  }
71

    
72
  return 0;
73
}