-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdhdb_ini.c
More file actions
168 lines (144 loc) · 3.86 KB
/
dhdb_ini.c
File metadata and controls
168 lines (144 loc) · 3.86 KB
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
162
163
164
165
166
167
168
/*
* dhdb - Multi-format dynamic and hierarchical database for C
* Copyright (c) 2015 Tommi M. Leino <tleino@me.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "dhdb_ini.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#ifndef __USE_BSD
#define __USE_BSD
#endif
#include <string.h>
static void _parse_section(dhdb_t *, const char *, dhdb_t **);
static void _parse_line(dhdb_t *, const char *, dhdb_t **);
static void _print_value(dhdb_t *, char *);
static const char* _serialize(dhdb_t *, char *, int);
dhdb_t*
dhdb_create_from_ini(const char *str)
{
dhdb_t *s, *current_section;
char *line;
int i, begin;
s = dhdb_create();
current_section = 0;
begin = 0;
for (i = 0; i < strlen(str); i++) {
if (str[i] == '\n') {
line = strdup(&str[begin]);
line[i - begin] = 0;
if ( line[i-begin-1] == '\r' )
line[i-begin-1] = 0;
_parse_line(s, line, ¤t_section);
free(line);
begin = i + 1;
}
}
return s;
}
const char*
dhdb_to_ini(dhdb_t *s)
{
static char out[8192];
assert(s);
out[0] = 0;
_serialize(s, out, 0);
return out;
}
static void
_parse_section(dhdb_t *s, const char *line, dhdb_t **current_section)
{
char *section;
section = strdup(&line[1]);
section[strlen(section)-1] = 0;
if (dhdb_by(s, section) == NULL)
dhdb_set_obj(s, section, dhdb_create());
*current_section = dhdb_by(s, section);
free(section);
}
static void
_parse_line(dhdb_t *s, const char *line, dhdb_t **current_section)
{
int i;
char *p, *field, *value, *value_tmp;
if (line[0] == '[')
return _parse_section(s, line, current_section);
if (line[0] == ';' || line[0] == '#')
return;
p = strchr(line, '=');
if (!p) return;
field = strdup(line);
field[p - line] = 0;
value = strdup(&line[p - line + 1]);
if ( value[0] == '"' && value[strlen(value)-1] == '"' ) {
value[strlen(value)-1] = 0;
value_tmp = strdup(&value[1]);
free(value);
value = strdup(value_tmp);
free(value_tmp);
}
if (*current_section)
dhdb_set_obj_str(*current_section, field, value);
else
dhdb_set_obj_str(s, field, value);
free(field);
free(value);
}
static void
_print_value(dhdb_t *s, char *out)
{
char val[8192];
val[0] = 0;
if (dhdb_type(s) == DHDB_VALUE_NUMBER) {
if ((int) dhdb_num(s) == dhdb_num(s))
snprintf(val, sizeof(val), "%ld",
(long int) dhdb_num(s));
else
snprintf(val, sizeof(val), "%.8f", dhdb_num(s));
}
else if (dhdb_type(s) == DHDB_VALUE_STRING)
snprintf(val, sizeof(val), "%s", dhdb_str(s));
else if (dhdb_type(s) == DHDB_VALUE_BOOL)
snprintf(val, sizeof(val), "%s",
dhdb_num(s) ? "true" : "false");
else if (dhdb_type(s) == DHDB_VALUE_NULL)
snprintf(val, sizeof(val), "null");
strcat(out, val);
}
static const char*
_serialize(dhdb_t *s, char *out, int level)
{
char val[8192];
const char *name;
dhdb_t *n;
val[0] = 0;
name = dhdb_name(s);
if (level == 1 && name && dhdb_type(s) == DHDB_VALUE_OBJECT) {
strcat(out, "[");
strcat(out, dhdb_name(s));
strcat(out, "]\n");
} else if (name && dhdb_type(s) != DHDB_VALUE_OBJECT) {
strcat(out, dhdb_name(s));
strcat(out, "=");
_print_value(s, out);
strcat(out, "\n");
}
n = dhdb_first(s);
while (n) {
_serialize(n, out, level + 1);
n = dhdb_next(n);
}
return out;
}