Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | ||
3 | * | ||
4 | * Copyright 2024 Mike Becker, Olaf Wintermann All rights reserved. | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions are met: | ||
8 | * | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
26 | * POSSIBILITY OF SUCH DAMAGE. | ||
27 | */ | ||
28 | |||
29 | #include "cx/properties.h" | ||
30 | |||
31 | #include <assert.h> | ||
32 | |||
33 | const CxPropertiesConfig cx_properties_config_default = { | ||
34 | '=', | ||
35 | '#', | ||
36 | '\0', | ||
37 | '\0', | ||
38 | '\\', | ||
39 | }; | ||
40 | |||
41 | 13 | void cxPropertiesInit( | |
42 | CxProperties *prop, | ||
43 | CxPropertiesConfig config | ||
44 | ) { | ||
45 | 13 | memset(prop, 0, sizeof(CxProperties)); | |
46 | 13 | prop->config = config; | |
47 | 13 | } | |
48 | |||
49 | 13 | void cxPropertiesDestroy(CxProperties *prop) { | |
50 | 13 | cxBufferDestroy(&prop->input); | |
51 | 13 | cxBufferDestroy(&prop->buffer); | |
52 | 13 | } | |
53 | |||
54 | 58 | int cxPropertiesFilln( | |
55 | CxProperties *prop, | ||
56 | const char *buf, | ||
57 | size_t len | ||
58 | ) { | ||
59 |
2/2✓ Branch 1 taken 55 times.
✓ Branch 2 taken 3 times.
|
58 | if (cxBufferEof(&prop->input)) { |
60 | // destroy a possible previously initialized buffer | ||
61 | 55 | cxBufferDestroy(&prop->input); | |
62 | 55 | cxBufferInit(&prop->input, (void*) buf, len, | |
63 | NULL, CX_BUFFER_COPY_ON_WRITE | CX_BUFFER_AUTO_EXTEND); | ||
64 | 55 | prop->input.size = len; | |
65 | } else { | ||
66 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if (cxBufferAppend(buf, 1, len, &prop->input) < len) return -1; |
67 | } | ||
68 | 58 | return 0; | |
69 | } | ||
70 | |||
71 | 3 | void cxPropertiesUseStack( | |
72 | CxProperties *prop, | ||
73 | char *buf, | ||
74 | size_t capacity | ||
75 | ) { | ||
76 | 3 | cxBufferInit(&prop->buffer, buf, capacity, NULL, CX_BUFFER_COPY_ON_EXTEND); | |
77 | 3 | } | |
78 | |||
79 | 112 | CxPropertiesStatus cxPropertiesNext( | |
80 | CxProperties *prop, | ||
81 | cxstring *key, | ||
82 | cxstring *value | ||
83 | ) { | ||
84 | // check if we have a text buffer | ||
85 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 111 times.
|
112 | if (prop->input.space == NULL) { |
86 | 1 | return CX_PROPERTIES_NULL_INPUT; | |
87 | } | ||
88 | |||
89 | // a pointer to the buffer we want to read from | ||
90 | 111 | CxBuffer *current_buffer = &prop->input; | |
91 | |||
92 | // check if we have rescued data | ||
93 |
2/2✓ Branch 1 taken 24 times.
✓ Branch 2 taken 87 times.
|
111 | if (!cxBufferEof(&prop->buffer)) { |
94 | // check if we can now get a complete line | ||
95 | 24 | cxstring input = cx_strn(prop->input.space + prop->input.pos, | |
96 | 24 | prop->input.size - prop->input.pos); | |
97 | 24 | cxstring nl = cx_strchr(input, '\n'); | |
98 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 15 times.
|
24 | if (nl.length > 0) { |
99 | // we add as much data to the rescue buffer as we need | ||
100 | // to complete the line | ||
101 | 9 | size_t len_until_nl = (size_t)(nl.ptr - input.ptr) + 1; | |
102 | |||
103 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
|
9 | if (cxBufferAppend(input.ptr, 1, |
104 | len_until_nl, &prop->buffer) < len_until_nl) { | ||
105 | 15 | return CX_PROPERTIES_BUFFER_ALLOC_FAILED; | |
106 | } | ||
107 | |||
108 | // advance the position in the input buffer | ||
109 | 9 | prop->input.pos += len_until_nl; | |
110 | |||
111 | // we now want to read from the rescue buffer | ||
112 | 9 | current_buffer = &prop->buffer; | |
113 | } else { | ||
114 | // still not enough data, copy input buffer to internal buffer | ||
115 | 15 | if (cxBufferAppend(input.ptr, 1, | |
116 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
|
15 | input.length, &prop->buffer) < input.length) { |
117 | ✗ | return CX_PROPERTIES_BUFFER_ALLOC_FAILED; | |
118 | } | ||
119 | // reset the input buffer (make way for a re-fill) | ||
120 | 15 | cxBufferReset(&prop->input); | |
121 | 15 | return CX_PROPERTIES_INCOMPLETE_DATA; | |
122 | } | ||
123 | } | ||
124 | |||
125 | 96 | char comment1 = prop->config.comment1; | |
126 | 96 | char comment2 = prop->config.comment2; | |
127 | 96 | char comment3 = prop->config.comment3; | |
128 | 96 | char delimiter = prop->config.delimiter; | |
129 | |||
130 | // get one line and parse it | ||
131 |
2/2✓ Branch 1 taken 143 times.
✓ Branch 2 taken 24 times.
|
167 | while (!cxBufferEof(current_buffer)) { |
132 | 143 | const char *buf = current_buffer->space + current_buffer->pos; | |
133 | 143 | size_t len = current_buffer->size - current_buffer->pos; | |
134 | |||
135 | /* | ||
136 | * First we check if we have at least one line. We also get indices of | ||
137 | * delimiter and comment chars | ||
138 | */ | ||
139 | 143 | size_t delimiter_index = 0; | |
140 | 143 | size_t comment_index = 0; | |
141 | 143 | bool has_comment = false; | |
142 | |||
143 | 143 | size_t i = 0; | |
144 | 143 | char c = 0; | |
145 |
2/2✓ Branch 0 taken 7031 times.
✓ Branch 1 taken 9 times.
|
7040 | for (; i < len; i++) { |
146 | 7031 | c = buf[i]; | |
147 |
4/6✓ Branch 0 taken 6991 times.
✓ Branch 1 taken 40 times.
✓ Branch 2 taken 6991 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 6991 times.
|
7031 | if (c == comment1 || c == comment2 || c == comment3) { |
148 |
1/2✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
|
40 | if (comment_index == 0) { |
149 | 40 | comment_index = i; | |
150 | 40 | has_comment = true; | |
151 | } | ||
152 |
2/2✓ Branch 0 taken 75 times.
✓ Branch 1 taken 6916 times.
|
6991 | } else if (c == delimiter) { |
153 |
3/4✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 66 times.
✓ Branch 3 taken 9 times.
|
75 | if (delimiter_index == 0 && !has_comment) { |
154 | 66 | delimiter_index = i; | |
155 | } | ||
156 |
2/2✓ Branch 0 taken 134 times.
✓ Branch 1 taken 6782 times.
|
6916 | } else if (c == '\n') { |
157 | 134 | break; | |
158 | } | ||
159 | } | ||
160 | |||
161 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 134 times.
|
143 | if (c != '\n') { |
162 | // we don't have enough data for a line, use the rescue buffer | ||
163 | assert(current_buffer != &prop->buffer); | ||
164 | // make sure that the rescue buffer does not already contain something | ||
165 | assert(cxBufferEof(&prop->buffer)); | ||
166 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
|
9 | if (prop->buffer.space == NULL) { |
167 | // initialize a rescue buffer, if the user did not provide one | ||
168 | 3 | cxBufferInit(&prop->buffer, NULL, 256, NULL, CX_BUFFER_AUTO_EXTEND); | |
169 | } else { | ||
170 | // from a previous rescue there might be already read data | ||
171 | // reset the buffer to avoid unnecessary buffer extension | ||
172 | 6 | cxBufferReset(&prop->buffer); | |
173 | } | ||
174 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
|
9 | if (cxBufferAppend(buf, 1, len, &prop->buffer) < len) { |
175 | 72 | return CX_PROPERTIES_BUFFER_ALLOC_FAILED; | |
176 | } | ||
177 | // reset the input buffer (make way for a re-fill) | ||
178 | 9 | cxBufferReset(&prop->input); | |
179 | 9 | return CX_PROPERTIES_INCOMPLETE_DATA; | |
180 | } | ||
181 | |||
182 | cxstring line = has_comment ? | ||
183 |
2/2✓ Branch 0 taken 39 times.
✓ Branch 1 taken 95 times.
|
134 | cx_strn(buf, comment_index) : |
184 | 95 | cx_strn(buf, i); | |
185 | // check line | ||
186 |
2/2✓ Branch 0 taken 73 times.
✓ Branch 1 taken 61 times.
|
134 | if (delimiter_index == 0) { |
187 | // if line is not blank ... | ||
188 | 73 | line = cx_strtrim(line); | |
189 | // ... either no delimiter found, or key is empty | ||
190 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 71 times.
|
73 | if (line.length > 0) { |
191 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (line.ptr[0] == delimiter) { |
192 | 1 | return CX_PROPERTIES_INVALID_EMPTY_KEY; | |
193 | } else { | ||
194 | 1 | return CX_PROPERTIES_INVALID_MISSING_DELIMITER; | |
195 | } | ||
196 | } else { | ||
197 | // skip blank line | ||
198 | // if it was the rescue buffer, return to the original buffer | ||
199 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 70 times.
|
71 | if (current_buffer == &prop->buffer) { |
200 | // assert that the rescue buffer really does not contain more data | ||
201 | assert(current_buffer->pos + i + 1 == current_buffer->size); | ||
202 | // reset the rescue buffer, but don't destroy it! | ||
203 | 1 | cxBufferReset(&prop->buffer); | |
204 | // continue with the input buffer | ||
205 | 1 | current_buffer = &prop->input; | |
206 | } else { | ||
207 | // if it was the input buffer already, just advance the position | ||
208 | 70 | current_buffer->pos += i + 1; | |
209 | } | ||
210 | 71 | continue; | |
211 | } | ||
212 | } else { | ||
213 | 61 | cxstring k = cx_strn(buf, delimiter_index); | |
214 | 61 | cxstring val = cx_strn( | |
215 | 61 | buf + delimiter_index + 1, | |
216 | 61 | line.length - delimiter_index - 1); | |
217 | 61 | k = cx_strtrim(k); | |
218 | 61 | val = cx_strtrim(val); | |
219 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 1 times.
|
61 | if (k.length > 0) { |
220 | 60 | *key = k; | |
221 | 60 | *value = val; | |
222 | 60 | current_buffer->pos += i + 1; | |
223 | assert(current_buffer->pos <= current_buffer->size); | ||
224 | 60 | return CX_PROPERTIES_NO_ERROR; | |
225 | } else { | ||
226 | 1 | return CX_PROPERTIES_INVALID_EMPTY_KEY; | |
227 | } | ||
228 | } | ||
229 | } | ||
230 | |||
231 | // when we come to this point, all data must have been read | ||
232 | assert(cxBufferEof(&prop->buffer)); | ||
233 | assert(cxBufferEof(&prop->input)); | ||
234 | |||
235 | 24 | return CX_PROPERTIES_NO_DATA; | |
236 | } | ||
237 | |||
238 | 12 | static int cx_properties_sink_map( | |
239 | cx_attr_unused CxProperties *prop, | ||
240 | CxPropertiesSink *sink, | ||
241 | cxstring key, | ||
242 | cxstring value | ||
243 | ) { | ||
244 | 12 | CxMap *map = sink->sink; | |
245 | 12 | CxAllocator *alloc = sink->data; | |
246 | 12 | cxmutstr v = cx_strdup_a(alloc, value); | |
247 | 12 | int r = cx_map_put_cxstr(map, key, v.ptr); | |
248 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (r != 0) cx_strfree_a(alloc, &v); |
249 | 12 | return r; | |
250 | } | ||
251 | |||
252 | 3 | CxPropertiesSink cxPropertiesMapSink(CxMap *map) { | |
253 | CxPropertiesSink sink; | ||
254 | 3 | sink.sink = map; | |
255 | 3 | sink.data = (void*) cxDefaultAllocator; | |
256 | 3 | sink.sink_func = cx_properties_sink_map; | |
257 | 3 | return sink; | |
258 | } | ||
259 | |||
260 | 9 | static int cx_properties_read_string( | |
261 | CxProperties *prop, | ||
262 | CxPropertiesSource *src, | ||
263 | cxstring *target | ||
264 | ) { | ||
265 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5 times.
|
9 | if (prop->input.space == src->src) { |
266 | // when the input buffer already contains the string | ||
267 | // we have nothing more to provide | ||
268 | 4 | target->length = 0; | |
269 | } else { | ||
270 | 5 | target->ptr = src->src; | |
271 | 5 | target->length = src->data_size; | |
272 | } | ||
273 | 9 | return 0; | |
274 | } | ||
275 | |||
276 | 7 | static int cx_properties_read_file( | |
277 | cx_attr_unused CxProperties *prop, | ||
278 | CxPropertiesSource *src, | ||
279 | cxstring *target | ||
280 | ) { | ||
281 | 7 | target->ptr = src->data_ptr; | |
282 | 7 | target->length = fread(src->data_ptr, 1, src->data_size, src->src); | |
283 | 7 | return ferror((FILE*)src->src); | |
284 | } | ||
285 | |||
286 | 1 | static int cx_properties_read_init_file( | |
287 | cx_attr_unused CxProperties *prop, | ||
288 | CxPropertiesSource *src | ||
289 | ) { | ||
290 | 1 | src->data_ptr = malloc(src->data_size); | |
291 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (src->data_ptr == NULL) return 1; |
292 | 1 | return 0; | |
293 | } | ||
294 | |||
295 | 1 | static void cx_properties_read_clean_file( | |
296 | cx_attr_unused CxProperties *prop, | ||
297 | CxPropertiesSource *src | ||
298 | ) { | ||
299 | 1 | free(src->data_ptr); | |
300 | 1 | } | |
301 | |||
302 | 1 | CxPropertiesSource cxPropertiesStringSource(cxstring str) { | |
303 | CxPropertiesSource src; | ||
304 | 1 | src.src = (void*) str.ptr; | |
305 | 1 | src.data_size = str.length; | |
306 | 1 | src.data_ptr = NULL; | |
307 | 1 | src.read_func = cx_properties_read_string; | |
308 | 1 | src.read_init_func = NULL; | |
309 | 1 | src.read_clean_func = NULL; | |
310 | 1 | return src; | |
311 | } | ||
312 | |||
313 | 1 | CxPropertiesSource cxPropertiesCstrnSource(const char *str, size_t len) { | |
314 | CxPropertiesSource src; | ||
315 | 1 | src.src = (void*) str; | |
316 | 1 | src.data_size = len; | |
317 | 1 | src.data_ptr = NULL; | |
318 | 1 | src.read_func = cx_properties_read_string; | |
319 | 1 | src.read_init_func = NULL; | |
320 | 1 | src.read_clean_func = NULL; | |
321 | 1 | return src; | |
322 | } | ||
323 | |||
324 | 3 | CxPropertiesSource cxPropertiesCstrSource(const char *str) { | |
325 | CxPropertiesSource src; | ||
326 | 3 | src.src = (void*) str; | |
327 | 3 | src.data_size = strlen(str); | |
328 | 3 | src.data_ptr = NULL; | |
329 | 3 | src.read_func = cx_properties_read_string; | |
330 | 3 | src.read_init_func = NULL; | |
331 | 3 | src.read_clean_func = NULL; | |
332 | 3 | return src; | |
333 | } | ||
334 | |||
335 | 1 | CxPropertiesSource cxPropertiesFileSource(FILE *file, size_t chunk_size) { | |
336 | CxPropertiesSource src; | ||
337 | 1 | src.src = file; | |
338 | 1 | src.data_size = chunk_size; | |
339 | 1 | src.data_ptr = NULL; | |
340 | 1 | src.read_func = cx_properties_read_file; | |
341 | 1 | src.read_init_func = cx_properties_read_init_file; | |
342 | 1 | src.read_clean_func = cx_properties_read_clean_file; | |
343 | 1 | return src; | |
344 | } | ||
345 | |||
346 | 6 | CxPropertiesStatus cxPropertiesLoad( | |
347 | CxProperties *prop, | ||
348 | CxPropertiesSink sink, | ||
349 | CxPropertiesSource source | ||
350 | ) { | ||
351 | assert(source.read_func != NULL); | ||
352 | assert(sink.sink_func != NULL); | ||
353 | |||
354 | // initialize reader | ||
355 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5 times.
|
6 | if (source.read_init_func != NULL) { |
356 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (source.read_init_func(prop, &source)) { |
357 | ✗ | return CX_PROPERTIES_READ_INIT_FAILED; | |
358 | } | ||
359 | } | ||
360 | |||
361 | // transfer the data from the source to the sink | ||
362 | CxPropertiesStatus status; | ||
363 | 6 | CxPropertiesStatus kv_status = CX_PROPERTIES_NO_DATA; | |
364 | 6 | bool found = false; | |
365 | 10 | while (true) { | |
366 | // read input | ||
367 | cxstring input; | ||
368 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
|
16 | if (source.read_func(prop, &source, &input)) { |
369 | ✗ | status = CX_PROPERTIES_READ_FAILED; | |
370 | ✗ | break; | |
371 | } | ||
372 | |||
373 | // no more data - break | ||
374 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 11 times.
|
16 | if (input.length == 0) { |
375 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
|
5 | if (found) { |
376 | // something was found, check the last kv_status | ||
377 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
|
4 | if (kv_status == CX_PROPERTIES_INCOMPLETE_DATA) { |
378 | 1 | status = CX_PROPERTIES_INCOMPLETE_DATA; | |
379 | } else { | ||
380 | 3 | status = CX_PROPERTIES_NO_ERROR; | |
381 | } | ||
382 | } else { | ||
383 | // nothing found | ||
384 | 1 | status = CX_PROPERTIES_NO_DATA; | |
385 | } | ||
386 | 5 | break; | |
387 | } | ||
388 | |||
389 | // set the input buffer and read the k/v-pairs | ||
390 | 11 | cxPropertiesFill(prop, input); | |
391 | |||
392 | do { | ||
393 | cxstring key, value; | ||
394 | 23 | kv_status = cxPropertiesNext(prop, &key, &value); | |
395 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 11 times.
|
23 | if (kv_status == CX_PROPERTIES_NO_ERROR) { |
396 | 12 | found = true; | |
397 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
|
12 | if (sink.sink_func(prop, &sink, key, value)) { |
398 | ✗ | kv_status = CX_PROPERTIES_SINK_FAILED; | |
399 | } | ||
400 | } | ||
401 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 11 times.
|
23 | } while (kv_status == CX_PROPERTIES_NO_ERROR); |
402 | |||
403 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 10 times.
|
11 | if (kv_status > CX_PROPERTIES_OK) { |
404 | 1 | status = kv_status; | |
405 | 1 | break; | |
406 | } | ||
407 | } | ||
408 | |||
409 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5 times.
|
6 | if (source.read_clean_func != NULL) { |
410 | 1 | source.read_clean_func(prop, &source); | |
411 | } | ||
412 | |||
413 | 6 | return status; | |
414 | } | ||
415 |