Statistics
| Branch: | Revision:

grapes / som / ChunkBuffer / config.c @ 2a50a20c

History | View | Annotate | Download (1.44 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
struct tag *config_parse(const char *cfg)
22
{
23
  struct tag *res;
24
  int i = 0;
25
  const char *p = cfg;
26

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

    
55
  res = realloc(res, sizeof(struct tag) * (i + 1));
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
}