Statistics
| Branch: | Revision:

grapes / som / config.c @ 56e6f951

History | View | Annotate | Download (1.65 KB)

1 8ab58ec7 Luca Abeni
/*
2
 *  Copyright (c) 2010 Luca Abeni
3
 *
4
 *  This is free software; see lgpl-2.1.txt
5
 */
6
7 9c41353a Luca Abeni
#include <stdlib.h>
8
#include <string.h>
9
#include "config.h"
10
11 f5853f90 Luca
#define NAME_SIZE 32
12
#define VAL_SIZE 16
13
14 9c41353a Luca Abeni
struct tag {
15 f5853f90 Luca
  char name[NAME_SIZE];
16
  char value[VAL_SIZE];
17 9c41353a Luca Abeni
};
18
19
#define MAX_TAGS 16
20
21
struct tag *config_parse(const char *cfg)
22 40be1074 Luca Abeni
{
23 9c41353a Luca Abeni
  struct tag *res;
24 ced82324 Luca Abeni
  int i = 0;
25 0940b7ec Luca Abeni
  const char *p = cfg;
26 9c41353a Luca Abeni
27
  res = malloc(sizeof(struct tag) * MAX_TAGS);
28
  if (res == NULL) {
29
    return res;
30
  }
31 0940b7ec Luca Abeni
  while (p && *p != 0) {
32
    char *p1 = strchr(p, '=');
33 f5853f90 Luca
    
34
    memset(res[i].name, 0, NAME_SIZE);
35
    memset(res[i].value, 0, VAL_SIZE);
36 0940b7ec Luca Abeni
    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 4809b354 Luca Abeni
        memcpy(res[i++].value, p1 + 1, p - p1 - 1);
46 0940b7ec Luca Abeni
        p++;
47
      }
48
    } else {
49
      p = NULL;
50
    }
51
  }
52 69981957 Luca Abeni
  memset(res[i].name, 0, NAME_SIZE);
53
  memset(res[i].value, 0, VAL_SIZE);
54 ced82324 Luca Abeni
55 69981957 Luca Abeni
  res = realloc(res, sizeof(struct tag) * (i + 1));
56 9c41353a Luca Abeni
57
  return res;
58
}
59
60 c4aa4579 Luca Abeni
const char *config_value_str(const struct tag *cfg_values, const char *value)
61 9c41353a Luca Abeni
{
62
  int i, done;
63
64 40be1074 Luca Abeni
  i = 0; done = 0;
65 03dddbe7 Luca Abeni
  while (!done) {
66 9c41353a Luca Abeni
    if (!strcmp(cfg_values[i].name, value)) {
67 c4aa4579 Luca Abeni
      return cfg_values[i].value;
68 40be1074 Luca Abeni
    }
69 9c41353a Luca Abeni
    if (cfg_values[i].name[0] == 0) {
70 40be1074 Luca Abeni
      done = 1;
71 9c41353a Luca Abeni
    } else {
72
      i++;
73 40be1074 Luca Abeni
    }
74
  }
75 9c41353a Luca Abeni
76 c4aa4579 Luca Abeni
  return NULL;
77
}
78
79
int config_value_int(const struct tag *cfg_values, const char *value, int *res)
80
{
81
  const char *str_res;
82
83
  str_res = config_value_str(cfg_values, value);
84
  if (str_res == NULL) {
85
    return 0;
86
  }
87
88
  *res = atoi(str_res);
89
90
  return 1;
91 40be1074 Luca Abeni
}