Statistics
| Branch: | Revision:

grapes / src / config.c @ 8e0afb75

History | View | Annotate | Download (2.17 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 4b8e42f9 Csaba Kiraly
#include <stdio.h>
10 9c41353a Luca Abeni
#include "config.h"
11
12 f5853f90 Luca
#define NAME_SIZE 32
13 d1c5887c Csaba Kiraly
#define VAL_SIZE 64
14 f5853f90 Luca
15 9c41353a Luca Abeni
struct tag {
16 f5853f90 Luca
  char name[NAME_SIZE];
17
  char value[VAL_SIZE];
18 9c41353a Luca Abeni
};
19
20
#define MAX_TAGS 16
21
22
struct tag *config_parse(const char *cfg)
23 40be1074 Luca Abeni
{
24 9c41353a Luca Abeni
  struct tag *res;
25 ced82324 Luca Abeni
  int i = 0;
26 0940b7ec Luca Abeni
  const char *p = cfg;
27 9c41353a Luca Abeni
28
  res = malloc(sizeof(struct tag) * MAX_TAGS);
29
  if (res == NULL) {
30
    return res;
31
  }
32 0940b7ec Luca Abeni
  while (p && *p != 0) {
33
    char *p1 = strchr(p, '=');
34 f5853f90 Luca
    
35
    memset(res[i].name, 0, NAME_SIZE);
36
    memset(res[i].value, 0, VAL_SIZE);
37 0940b7ec Luca Abeni
    if (p1) {
38
      if (i % MAX_TAGS == 0) {
39
        res = realloc(res, sizeof(struct tag) * (i + MAX_TAGS));
40
      }
41 4b8e42f9 Csaba Kiraly
      if (p1 - p > NAME_SIZE - 1) {
42
        fprintf(stderr, "Error, config name too long:%s\n", p);
43
        free(res);
44
        return NULL;
45
      }
46 0940b7ec Luca Abeni
      memcpy(res[i].name, p, p1 - p);
47
      p = strchr(p1, ',');
48
      if (p == NULL) {
49 4b8e42f9 Csaba Kiraly
        p = p1 + strlen(p1);
50
      }
51
      if (p - p1 > VAL_SIZE - 1) {
52
        fprintf(stderr, "Error, config value too long:%s\n", p1);
53
        free(res);
54
        return NULL;
55 0940b7ec Luca Abeni
      }
56 4b8e42f9 Csaba Kiraly
      memcpy(res[i++].value, p1 + 1, p - p1 - 1);
57
      p++;
58 0940b7ec Luca Abeni
    } else {
59
      p = NULL;
60
    }
61
  }
62 69981957 Luca Abeni
  memset(res[i].name, 0, NAME_SIZE);
63
  memset(res[i].value, 0, VAL_SIZE);
64 ced82324 Luca Abeni
65 69981957 Luca Abeni
  res = realloc(res, sizeof(struct tag) * (i + 1));
66 9c41353a Luca Abeni
67
  return res;
68
}
69
70 8eab17c6 Luca Abeni
const char *config_value_str(const struct tag *cfg_values, const char *value)
71 9c41353a Luca Abeni
{
72
  int i, done;
73
74 40be1074 Luca Abeni
  i = 0; done = 0;
75 03dddbe7 Luca Abeni
  while (!done) {
76 9c41353a Luca Abeni
    if (!strcmp(cfg_values[i].name, value)) {
77 8eab17c6 Luca Abeni
      return cfg_values[i].value;
78 40be1074 Luca Abeni
    }
79 9c41353a Luca Abeni
    if (cfg_values[i].name[0] == 0) {
80 40be1074 Luca Abeni
      done = 1;
81 9c41353a Luca Abeni
    } else {
82
      i++;
83 40be1074 Luca Abeni
    }
84
  }
85 9c41353a Luca Abeni
86 8eab17c6 Luca Abeni
  return NULL;
87
}
88
89
int config_value_int(const struct tag *cfg_values, const char *value, int *res)
90
{
91
  const char *str_res;
92
93
  str_res = config_value_str(cfg_values, value);
94
  if (str_res == NULL) {
95
    return 0;
96
  }
97
98
  *res = atoi(str_res);
99
100
  return 1;
101 40be1074 Luca Abeni
}
102 8e0afb75 MarcoMellia
103
int config_value_double(const struct tag *cfg_values, const char *value, double *res)
104
{
105
  const char *str_res;
106
107
  str_res = config_value_str(cfg_values, value);
108
  if (str_res == NULL) {
109
    return 0;
110
  }
111
112
  *res = strtod(str_res, NULL);
113
114
  return 1;
115
}