GCC Code Coverage Report


Directory: ./
File: properties.c
Date: 2025-11-30 14:18:36
Exec Total Coverage
Lines: 184 184 100.0%
Functions: 17 17 100.0%
Branches: 66 78 84.6%

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