Statistics
| Branch: | Revision:

grapes / som / ChunkBuffer / config.c @ 8ab58ec7

History | View | Annotate | Download (1.42 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
        res[i++].value[p - p1] = 0;
48
        p++;
49
      }
50
    } else {
51
      p = NULL;
52
    }
53
  }
54

    
55
  res = realloc(res, sizeof(struct tag) * i);
56

    
57
  return res;
58
}
59

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

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

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

    
78
  return 0;
79
}