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 #include "ucx_properties.h"
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 UcxProperties *ucx_properties_new() {
36 UcxProperties *parser = (UcxProperties*)malloc(
37 sizeof(UcxProperties));
38 if(!parser) {
39 return NULL;
40 }
41
42 parser->buffer =
NULL;
43 parser->buflen =
0;
44 parser->pos =
0;
45 parser->tmp =
NULL;
46 parser->tmplen =
0;
47 parser->tmpcap =
0;
48 parser->error =
0;
49 parser->delimiter =
'=';
50 parser->comment1 =
'#';
51 parser->comment2 =
0;
52 parser->comment3 =
0;
53
54 return parser;
55 }
56
57 void ucx_properties_free(UcxProperties *parser) {
58 if(parser->tmp) {
59 free(parser->tmp);
60 }
61 free(parser);
62 }
63
64 void ucx_properties_fill(UcxProperties *parser,
char *buf,
size_t len) {
65 parser->buffer = buf;
66 parser->buflen = len;
67 parser->pos =
0;
68 }
69
70 static void parser_tmp_append(UcxProperties *parser,
char *buf,
size_t len) {
71 if(parser->tmpcap - parser->tmplen < len) {
72 size_t newcap = parser->tmpcap + len +
64;
73 parser->tmp = (
char*)realloc(parser->tmp, newcap);
74 parser->tmpcap = newcap;
75 }
76 memcpy(parser->tmp + parser->tmplen, buf, len);
77 parser->tmplen += len;
78 }
79
80 int ucx_properties_next(UcxProperties *parser, cxstring *name, cxstring *value) {
81 if(parser->tmplen >
0) {
82 char *buf = parser->buffer + parser->pos;
83 size_t len = parser->buflen - parser->pos;
84 cxstring str = cx_strn(buf, len);
85 cxstring nl = cx_strchr(str,
'\n');
86 if(nl.ptr) {
87 size_t newlen = (
size_t)(nl.ptr - buf) +
1;
88 parser_tmp_append(parser, buf, newlen);
89
90
91 char *orig_buf = parser->buffer;
92 size_t orig_len = parser->buflen;
93
94 parser->buffer = parser->tmp;
95 parser->buflen = parser->tmplen;
96 parser->pos =
0;
97 parser->tmp =
NULL;
98 parser->tmpcap =
0;
99 parser->tmplen =
0;
100
101 int ret = ucx_properties_next(parser, name, value);
102
103
104 parser->tmp = parser->buffer;
105 parser->buffer = orig_buf;
106 parser->buflen = orig_len;
107 parser->pos = newlen;
108
109
110
111
112
113
114 return ret ? ret : ucx_properties_next(parser, name, value);
115 }
else {
116 parser_tmp_append(parser, buf, len);
117 return 0;
118 }
119 }
else if(parser->tmp) {
120 free(parser->tmp);
121 parser->tmp =
NULL;
122 }
123
124 char comment1 = parser->comment1;
125 char comment2 = parser->comment2;
126 char comment3 = parser->comment3;
127 char delimiter = parser->delimiter;
128
129
130 while(parser->pos < parser->buflen) {
131 char *buf = parser->buffer + parser->pos;
132 size_t len = parser->buflen - parser->pos;
133
134
135
136
137
138 size_t delimiter_index =
0;
139 size_t comment_index =
0;
140 int has_comment =
0;
141
142 size_t i =
0;
143 char c =
0;
144 for(;i<len;i++) {
145 c = buf[i];
146 if(c == comment1 || c == comment2 || c == comment3) {
147 if(comment_index ==
0) {
148 comment_index = i;
149 has_comment =
1;
150 }
151 }
else if(c == delimiter) {
152 if(delimiter_index ==
0 && !has_comment) {
153 delimiter_index = i;
154 }
155 }
else if(c ==
'\n') {
156 break;
157 }
158 }
159
160 if(c !=
'\n') {
161
162
163 parser->tmpcap = len +
128;
164 parser->tmp = (
char*)malloc(parser->tmpcap);
165 parser->tmplen = len;
166 memcpy(parser->tmp, buf, len);
167 return 0;
168 }
169
170 cxstring line = has_comment ? cx_strn(buf, comment_index) : cx_strn(buf, i);
171
172 if(delimiter_index ==
0) {
173 line = cx_strtrim(line);
174 if(line.length !=
0) {
175 parser->error =
1;
176 }
177 }
else {
178 cxstring n = cx_strn(buf, delimiter_index);
179 cxstring v = cx_strn(
180 buf + delimiter_index +
1,
181 line.length - delimiter_index -
1);
182 n = cx_strtrim(n);
183 v = cx_strtrim(v);
184 if(n.length !=
0 || v.length !=
0) {
185 *name = n;
186 *value = v;
187 parser->pos += i +
1;
188 return 1;
189 }
else {
190 parser->error =
1;
191 }
192 }
193
194 parser->pos += i +
1;
195 }
196
197 return 0;
198 }
199
200 int ucx_properties2map(UcxProperties *parser, CxMap *map) {
201 cxstring name;
202 cxstring value;
203 while(ucx_properties_next(parser, &name, &value)) {
204 cxmutstr mutvalue = cx_strdup_a(map->collection.allocator, value);
205 if(!mutvalue.ptr) {
206 return 1;
207 }
208 if(cxMapPut(map, cx_hash_key_cxstr(name), mutvalue.ptr)) {
209 cxFree(map->collection.allocator, mutvalue.ptr);
210 return 1;
211 }
212 }
213 if (parser->error) {
214 return parser->error;
215 }
else {
216 return 0;
217 }
218 }
219
220
221 #define UCX_PROPLOAD_BUFSIZE 1024
222 int ucx_properties_load(CxMap *map,
FILE *file) {
223 UcxProperties *parser = ucx_properties_new();
224 if(!(parser && map && file)) {
225 return 1;
226 }
227
228 int error =
0;
229 size_t r;
230 char buf[
UCX_PROPLOAD_BUFSIZE];
231 while((r = fread(buf,
1,
UCX_PROPLOAD_BUFSIZE, file)) !=
0) {
232 ucx_properties_fill(parser, buf, r);
233 error = ucx_properties2map(parser, map);
234 if (error) {
235 break;
236 }
237 }
238 ucx_properties_free(parser);
239 return error;
240 }
241
242 int ucx_properties_store(CxMap *map,
FILE *file) {
243 CxIterator iter = cxMapIterator(map);
244 cxstring value;
245 size_t written;
246
247 cx_foreach(CxMapEntry *, v, iter) {
248 value = cx_str(v->value);
249
250 written =
0;
251 written += fwrite(v->key->data,
1, v->key->len, file);
252 written += fwrite(
" = ",
1,
3, file);
253 written += fwrite(value.ptr,
1, value.length, file);
254 written += fwrite(
"\n",
1,
1, file);
255
256 if (written != v->key->len + value.length +
4) {
257 return 1;
258 }
259 }
260
261 return 0;
262 }
263
264