Statistics
| Branch: | Revision:

grapes / som / ChunkBuffer / config.c @ 4809b354

History | View | Annotate | Download (1.46 KB)

1
/*
2
 *  Copyright (c) 2010 Luca Abeni
3
 *
4
 *  This is free software; see lgpl-2.1.txt
5
 */
6

    
7
#include <stdlib.h>
8
#include <string.h>
9
#include "config.h"
10

    
11
#define NAME_SIZE 32
12
#define VAL_SIZE 16
13

    
14
struct tag {
15
  char name[NAME_SIZE];
16
  char value[VAL_SIZE];
17
};
18

    
19
#define MAX_TAGS 16
20

    
21
#warning Fix config_parse
22
struct tag *config_parse(const char *cfg)
23
{
24
  struct tag *res;
25
  int i = 0;
26
  const char *p = cfg;
27

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

    
56
  res = realloc(res, sizeof(struct tag) * (i + 1));
57

    
58
  return res;
59
}
60

    
61
int config_value_int(const struct tag *cfg_values, const char *value, int *res)
62
{
63
  int i, done;
64

    
65
  i = 0; done = 0;
66
  while (!done) {
67
    if (!strcmp(cfg_values[i].name, value)) {
68
      *res = atoi(cfg_values[i].value);
69

    
70
      return 1;
71
    }
72
    if (cfg_values[i].name[0] == 0) {
73
      done = 1;
74
    } else {
75
      i++;
76
    }
77
  }
78

    
79
  return 0;
80
}