1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "uconfig.h"
struct uconfig_pair_s {
char *key;
char *value;
struct uconfig_pair_s *next;
};
struct uconfig_s {
struct uconfig_pair_s *head;
struct uconfig_pair_s *tail;
};
/* Create a new Uconfig pair. KEY and VALUE are copied to create the new
* pair. */
static struct uconfig_pair_s *uconfig_pair_new(const char *key, const char *value)
{
struct uconfig_pair_s *pair = malloc(sizeof(struct uconfig_pair_s));
pair->key = strdup(key);
pair->value = strdup(value);
pair->next = NULL;
return pair;
}
static void uconfig_add_tail(struct uconfig_s *uconfig,
struct uconfig_pair_s *pair)
{
if (!uconfig->head) {
/* first pair in the config handle */
uconfig->head = pair;
uconfig->tail = pair;
}
else {
uconfig->tail->next = pair;
uconfig->tail = pair;
}
}
static int char_is_blank(int c)
{
return c == ' ' || c == '\t' || c == '\n';
}
/* Remove all the leading and trailing blank characters from STR. STR is
* modified in-place. */
static char *trim(char *str)
{
int len = strlen(str);
while ((len > 0) && char_is_blank(*str)) {
str++;
len--;
}
while ((len > 0) && char_is_blank(str[len - 1])) {
str[len - 1] = '\0';
len--;
}
if (len == 0) {
fprintf(stderr,
"%s: warning: returning empty string\n",
__func__);
}
return str;
}
static int uconfig_parse_line(struct uconfig_s *uconfig,
const char *file,
int line_number,
char *line)
{
char *sep_ptr = strchr(line, '=');
if (sep_ptr == NULL) {
fprintf(stderr,
"%s: %s:%d: no '=' character found\n",
__func__, file, line_number);
return -1;
}
*sep_ptr = '\0';
char *key = trim(line);
char *value = trim(sep_ptr + 1);
struct uconfig_pair_s *pair = uconfig_pair_new(key, value);
uconfig_add_tail(uconfig, pair);
return 0;
}
struct uconfig_s *uconfig_new(const char*file)
{
FILE *input = fopen(file, "r");
struct uconfig_s *uconfig = NULL;
ssize_t line_len;
size_t line_size = 128;
char *line = NULL;
int line_number = 1;
int rc;
if (input == NULL) {
fprintf(stderr, "%s: fopen: %s: ", __func__, file);
perror("");
goto err;
}
uconfig = calloc(1, sizeof(struct uconfig_s));
line = malloc(line_size);
while ((line_len = getline(&line, &line_size, input)) != -1) {
rc = uconfig_parse_line(uconfig, file, line_number, line);
if (rc == -1)
goto err;
line_number++;
}
if (ferror(input)) {
fprintf(stderr, "%s: getline: %s: ", __func__, file);
perror("");
goto err;
}
free(line);
fclose(input);
return uconfig;
err:
if (input)
fclose(input);
if (line)
free(line);
if (uconfig)
uconfig_destroy(uconfig);
return NULL;
}
char *uconfig_get(const struct uconfig_s *uconfig, const char *key)
{
struct uconfig_pair_s *pair = uconfig->head;
while (pair) {
if (strcmp(pair->key, key) == 0)
return pair->value;
pair = pair->next;
}
return NULL;
}
static void uconfig_destroy_pairs(struct uconfig_pair_s *pair)
{
if (pair->next)
uconfig_destroy_pairs(pair->next);
free(pair->key);
free(pair->value);
free(pair);
}
void uconfig_destroy(struct uconfig_s *uconfig)
{
uconfig_destroy_pairs(uconfig->head);
}
|