Statistics
| Branch: | Revision:

grapes / src / grapes_config.c @ 176b8de8

History | View | Annotate | Download (2.93 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 <stdio.h>
10
#include "grapes_config.h"
11

    
12
#define NAME_SIZE 32
13
#define VAL_SIZE 64
14

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

    
20
#define MAX_TAGS 16
21

    
22
struct tag *grapes_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
      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
      memcpy(res[i].name, p, p1 - p);
47
      p = strchr(p1, ',');
48
      if (p == NULL) {
49
        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
      }
56
      memcpy(res[i++].value, p1 + 1, p - p1 - 1);
57
      if (*p) p++;
58
    } else {
59
      p = NULL;
60
    }
61
  }
62
  memset(res[i].name, 0, NAME_SIZE);
63
  memset(res[i].value, 0, VAL_SIZE);
64

    
65
  res = realloc(res, sizeof(struct tag) * (i + 1));
66

    
67
  return res;
68
}
69

    
70
const char *grapes_config_value_str(const struct tag *cfg_values, const char *value)
71
{
72
  int i, done;
73

    
74
  i = 0; done = 0;
75
  while (!done) {
76
    if (!strcmp(cfg_values[i].name, value)) {
77
      return cfg_values[i].value;
78
    }
79
    if (cfg_values[i].name[0] == 0) {
80
      done = 1;
81
    } else {
82
      i++;
83
    }
84
  }
85

    
86
  return NULL;
87
}
88

    
89
int grapes_config_value_int(const struct tag *cfg_values, const char *value, int *res)
90
{
91
  const char *str_res;
92

    
93
  str_res = grapes_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
}
102

    
103
int grapes_config_value_double(const struct tag *cfg_values, const char *value, double *res)
104
{
105
  const char *str_res;
106

    
107
  str_res = grapes_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
}
116

    
117
const char *grapes_config_value_str_default(const struct tag *cfg_values, const char *value, const char *default_value)
118
{
119
  const char *res;
120

    
121
  res = grapes_config_value_str(cfg_values, value);
122
  return res ? res : default_value;
123
}
124

    
125
int grapes_config_value_int_default(const struct tag *cfg_values, const char *value, int *res, int default_value)
126
{
127
  int r;
128

    
129
  r = grapes_config_value_int(cfg_values, value, res);
130
  if (!r) {
131
    *res = default_value;
132
  }
133
  return r;
134
}
135

    
136
int grapes_config_value_double_default(const struct tag *cfg_values, const char *value, double *res, double default_value)
137
{
138
  int r;
139

    
140
  r = grapes_config_value_double(cfg_values, value, res);
141
  if (!r) {
142
    *res = default_value;
143
  }
144
  return r;
145
}