src/properties.c

Sun, 07 Dec 2025 15:34:46 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 07 Dec 2025 15:34:46 +0100
changeset 1555
8972247f54e8
parent 1503
48993e0e0dba
permissions
-rw-r--r--

properties.h: removes the source/sink API and adds a new cxPropertiesLoad()

resolves #610

923
45da884269c8 add ucx2.1 style interface for a properties parser
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
45da884269c8 add ucx2.1 style interface for a properties parser
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
45da884269c8 add ucx2.1 style interface for a properties parser
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 *
45da884269c8 add ucx2.1 style interface for a properties parser
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 * Copyright 2024 Mike Becker, Olaf Wintermann All rights reserved.
45da884269c8 add ucx2.1 style interface for a properties parser
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 *
45da884269c8 add ucx2.1 style interface for a properties parser
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
45da884269c8 add ucx2.1 style interface for a properties parser
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
45da884269c8 add ucx2.1 style interface for a properties parser
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 *
45da884269c8 add ucx2.1 style interface for a properties parser
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
45da884269c8 add ucx2.1 style interface for a properties parser
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
45da884269c8 add ucx2.1 style interface for a properties parser
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 *
45da884269c8 add ucx2.1 style interface for a properties parser
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
45da884269c8 add ucx2.1 style interface for a properties parser
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
45da884269c8 add ucx2.1 style interface for a properties parser
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
45da884269c8 add ucx2.1 style interface for a properties parser
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 *
45da884269c8 add ucx2.1 style interface for a properties parser
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
45da884269c8 add ucx2.1 style interface for a properties parser
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45da884269c8 add ucx2.1 style interface for a properties parser
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45da884269c8 add ucx2.1 style interface for a properties parser
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
45da884269c8 add ucx2.1 style interface for a properties parser
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
45da884269c8 add ucx2.1 style interface for a properties parser
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
45da884269c8 add ucx2.1 style interface for a properties parser
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
45da884269c8 add ucx2.1 style interface for a properties parser
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
45da884269c8 add ucx2.1 style interface for a properties parser
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45da884269c8 add ucx2.1 style interface for a properties parser
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
45da884269c8 add ucx2.1 style interface for a properties parser
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
45da884269c8 add ucx2.1 style interface for a properties parser
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 */
45da884269c8 add ucx2.1 style interface for a properties parser
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28
45da884269c8 add ucx2.1 style interface for a properties parser
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 #include "cx/properties.h"
45da884269c8 add ucx2.1 style interface for a properties parser
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30
924
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
31 #include <assert.h>
1555
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
32 #include <stdio.h>
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
33 #include <string.h>
924
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
34
923
45da884269c8 add ucx2.1 style interface for a properties parser
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35 const CxPropertiesConfig cx_properties_config_default = {
45da884269c8 add ucx2.1 style interface for a properties parser
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36 '=',
45da884269c8 add ucx2.1 style interface for a properties parser
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37 '#',
45da884269c8 add ucx2.1 style interface for a properties parser
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 '\0',
1193
cfa44f3f5e3b last minute fixes for issue #548
Mike Becker <universe@uap-core.de>
parents: 1180
diff changeset
39 '\0',
cfa44f3f5e3b last minute fixes for issue #548
Mike Becker <universe@uap-core.de>
parents: 1180
diff changeset
40 '\\',
923
45da884269c8 add ucx2.1 style interface for a properties parser
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 };
924
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
42
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
43 void cxPropertiesInit(
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
44 CxProperties *prop,
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
45 CxPropertiesConfig config
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
46 ) {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
47 memset(prop, 0, sizeof(CxProperties));
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
48 prop->config = config;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
49 }
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
50
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
51 void cxPropertiesDestroy(CxProperties *prop) {
1031
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
52 cxBufferDestroy(&prop->input);
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
53 cxBufferDestroy(&prop->buffer);
924
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
54 }
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
55
1426
3a89b31f0724 clean up header files and adds support for comparing arbitrary strings with string.h functions
Mike Becker <universe@uap-core.de>
parents: 1408
diff changeset
56 void cxPropertiesReset(CxProperties *prop) {
3a89b31f0724 clean up header files and adds support for comparing arbitrary strings with string.h functions
Mike Becker <universe@uap-core.de>
parents: 1408
diff changeset
57 CxPropertiesConfig config = prop->config;
3a89b31f0724 clean up header files and adds support for comparing arbitrary strings with string.h functions
Mike Becker <universe@uap-core.de>
parents: 1408
diff changeset
58 cxPropertiesDestroy(prop);
3a89b31f0724 clean up header files and adds support for comparing arbitrary strings with string.h functions
Mike Becker <universe@uap-core.de>
parents: 1408
diff changeset
59 cxPropertiesInit(prop, config);
3a89b31f0724 clean up header files and adds support for comparing arbitrary strings with string.h functions
Mike Becker <universe@uap-core.de>
parents: 1408
diff changeset
60 }
3a89b31f0724 clean up header files and adds support for comparing arbitrary strings with string.h functions
Mike Becker <universe@uap-core.de>
parents: 1408
diff changeset
61
972
a9a1d07a6840 add support for UCX strings in cxPropertiesFill()
Mike Becker <universe@uap-core.de>
parents: 929
diff changeset
62 int cxPropertiesFilln(
924
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
63 CxProperties *prop,
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
64 const char *buf,
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
65 size_t len
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
66 ) {
1031
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
67 if (cxBufferEof(&prop->input)) {
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
68 // destroy a possible previously initialized buffer
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
69 cxBufferDestroy(&prop->input);
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
70 cxBufferInit(&prop->input, (void*) buf, len,
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
71 NULL, CX_BUFFER_COPY_ON_WRITE | CX_BUFFER_AUTO_EXTEND);
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
72 prop->input.size = len;
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
73 } else {
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
74 if (cxBufferAppend(buf, 1, len, &prop->input) < len) return -1;
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
75 }
924
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
76 return 0;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
77 }
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
78
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
79 void cxPropertiesUseStack(
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
80 CxProperties *prop,
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
81 char *buf,
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
82 size_t capacity
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
83 ) {
1031
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
84 cxBufferInit(&prop->buffer, buf, capacity, NULL, CX_BUFFER_COPY_ON_EXTEND);
924
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
85 }
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
86
928
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
87 CxPropertiesStatus cxPropertiesNext(
924
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
88 CxProperties *prop,
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
89 cxstring *key,
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
90 cxstring *value
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
91 ) {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
92 // check if we have a text buffer
1031
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
93 if (prop->input.space == NULL) {
924
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
94 return CX_PROPERTIES_NULL_INPUT;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
95 }
1031
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
96
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
97 // a pointer to the buffer we want to read from
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
98 CxBuffer *current_buffer = &prop->input;
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
99
924
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
100 // check if we have rescued data
1031
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
101 if (!cxBufferEof(&prop->buffer)) {
924
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
102 // check if we can now get a complete line
1031
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
103 cxstring input = cx_strn(prop->input.space + prop->input.pos,
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
104 prop->input.size - prop->input.pos);
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
105 cxstring nl = cx_strchr(input, '\n');
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
106 if (nl.length > 0) {
924
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
107 // we add as much data to the rescue buffer as we need
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
108 // to complete the line
1031
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
109 size_t len_until_nl = (size_t)(nl.ptr - input.ptr) + 1;
924
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
110
1031
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
111 if (cxBufferAppend(input.ptr, 1,
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
112 len_until_nl, &prop->buffer) < len_until_nl) {
924
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
113 return CX_PROPERTIES_BUFFER_ALLOC_FAILED;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
114 }
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
115
1031
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
116 // advance the position in the input buffer
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
117 prop->input.pos += len_until_nl;
924
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
118
1031
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
119 // we now want to read from the rescue buffer
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
120 current_buffer = &prop->buffer;
924
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
121 } else {
1031
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
122 // still not enough data, copy input buffer to internal buffer
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
123 if (cxBufferAppend(input.ptr, 1,
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
124 input.length, &prop->buffer) < input.length) {
1503
48993e0e0dba add coverage exclusions for properties.c
Mike Becker <universe@uap-core.de>
parents: 1426
diff changeset
125 return CX_PROPERTIES_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE
928
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
126 }
1031
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
127 // reset the input buffer (make way for a re-fill)
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
128 cxBufferReset(&prop->input);
924
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
129 return CX_PROPERTIES_INCOMPLETE_DATA;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
130 }
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
131 }
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
132
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
133 char comment1 = prop->config.comment1;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
134 char comment2 = prop->config.comment2;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
135 char comment3 = prop->config.comment3;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
136 char delimiter = prop->config.delimiter;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
137
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
138 // get one line and parse it
1031
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
139 while (!cxBufferEof(current_buffer)) {
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
140 const char *buf = current_buffer->space + current_buffer->pos;
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
141 size_t len = current_buffer->size - current_buffer->pos;
924
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
142
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
143 /*
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
144 * First we check if we have at least one line. We also get indices of
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
145 * delimiter and comment chars
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
146 */
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
147 size_t delimiter_index = 0;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
148 size_t comment_index = 0;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
149 bool has_comment = false;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
150
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
151 size_t i = 0;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
152 char c = 0;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
153 for (; i < len; i++) {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
154 c = buf[i];
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
155 if (c == comment1 || c == comment2 || c == comment3) {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
156 if (comment_index == 0) {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
157 comment_index = i;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
158 has_comment = true;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
159 }
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
160 } else if (c == delimiter) {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
161 if (delimiter_index == 0 && !has_comment) {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
162 delimiter_index = i;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
163 }
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
164 } else if (c == '\n') {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
165 break;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
166 }
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
167 }
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
168
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
169 if (c != '\n') {
1031
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
170 // we don't have enough data for a line, use the rescue buffer
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
171 assert(current_buffer != &prop->buffer);
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
172 // make sure that the rescue buffer does not already contain something
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
173 assert(cxBufferEof(&prop->buffer));
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
174 if (prop->buffer.space == NULL) {
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
175 // initialize a rescue buffer, if the user did not provide one
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
176 cxBufferInit(&prop->buffer, NULL, 256, NULL, CX_BUFFER_AUTO_EXTEND);
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
177 } else {
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
178 // from a previous rescue there might be already read data
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
179 // reset the buffer to avoid unnecessary buffer extension
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
180 cxBufferReset(&prop->buffer);
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
181 }
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
182 if (cxBufferAppend(buf, 1, len, &prop->buffer) < len) {
928
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
183 return CX_PROPERTIES_BUFFER_ALLOC_FAILED;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
184 }
1031
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
185 // reset the input buffer (make way for a re-fill)
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
186 cxBufferReset(&prop->input);
924
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
187 return CX_PROPERTIES_INCOMPLETE_DATA;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
188 }
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
189
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
190 cxstring line = has_comment ?
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
191 cx_strn(buf, comment_index) :
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
192 cx_strn(buf, i);
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
193 // check line
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
194 if (delimiter_index == 0) {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
195 // if line is not blank ...
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
196 line = cx_strtrim(line);
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
197 // ... either no delimiter found, or key is empty
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
198 if (line.length > 0) {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
199 if (line.ptr[0] == delimiter) {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
200 return CX_PROPERTIES_INVALID_EMPTY_KEY;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
201 } else {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
202 return CX_PROPERTIES_INVALID_MISSING_DELIMITER;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
203 }
1031
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
204 } else {
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
205 // skip blank line
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
206 // if it was the rescue buffer, return to the original buffer
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
207 if (current_buffer == &prop->buffer) {
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
208 // assert that the rescue buffer really does not contain more data
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
209 assert(current_buffer->pos + i + 1 == current_buffer->size);
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
210 // reset the rescue buffer, but don't destroy it!
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
211 cxBufferReset(&prop->buffer);
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
212 // continue with the input buffer
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
213 current_buffer = &prop->input;
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
214 } else {
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
215 // if it was the input buffer already, just advance the position
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
216 current_buffer->pos += i + 1;
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
217 }
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
218 continue;
924
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
219 }
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
220 } else {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
221 cxstring k = cx_strn(buf, delimiter_index);
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
222 cxstring val = cx_strn(
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
223 buf + delimiter_index + 1,
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
224 line.length - delimiter_index - 1);
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
225 k = cx_strtrim(k);
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
226 val = cx_strtrim(val);
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
227 if (k.length > 0) {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
228 *key = k;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
229 *value = val;
1031
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
230 current_buffer->pos += i + 1;
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
231 assert(current_buffer->pos <= current_buffer->size);
924
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
232 return CX_PROPERTIES_NO_ERROR;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
233 } else {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
234 return CX_PROPERTIES_INVALID_EMPTY_KEY;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
235 }
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
236 }
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
237 }
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
238
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
239 // when we come to this point, all data must have been read
1031
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
240 assert(cxBufferEof(&prop->buffer));
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
241 assert(cxBufferEof(&prop->input));
8a90552bba29 rework of properties parser - fixes #529 and resolves #458
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
242
924
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
243 return CX_PROPERTIES_NO_DATA;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
244 }
928
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
245
1555
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
246 #ifndef CX_PROPERTIES_LOAD_FILL_SIZE
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
247 #define CX_PROPERTIES_LOAD_FILL_SIZE 1024
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
248 #endif
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
249 const unsigned cx_properties_load_fill_size = CX_PROPERTIES_LOAD_FILL_SIZE;
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
250 #ifndef CX_PROPERTIES_LOAD_BUF_SIZE
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
251 #define CX_PROPERTIES_LOAD_BUF_SIZE 256
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
252 #endif
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
253 const unsigned cx_properties_load_buf_size = CX_PROPERTIES_LOAD_BUF_SIZE;
928
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
254
1555
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
255 CxPropertiesStatus cx_properties_load(CxPropertiesConfig config,
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
256 cxstring filename, CxMap *target) {
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
257 // sanity check for the map
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
258 const bool use_cstring = cxCollectionStoresPointers(target);
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
259 if (!use_cstring && cxCollectionElementSize(target) != sizeof(cxmutstr)) {
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
260 return CX_PROPERTIES_MAP_ERROR;
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
261 }
928
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
262
1555
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
263 // create a duplicate to guarantee zero-termination
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
264 cxmutstr fname = cx_strdup(filename);
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
265 if (fname.ptr == NULL) {
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
266 return CX_PROPERTIES_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE
928
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
267 }
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
268
1555
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
269 // open the file
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
270 FILE *f = fopen(fname.ptr, "r");
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
271 if (f == NULL) {
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
272 cx_strfree(&fname);
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
273 return CX_PROPERTIES_FILE_ERROR;
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
274 }
928
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
275
1555
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
276 // initialize the parser
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
277 char linebuf[cx_properties_load_buf_size];
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
278 char fillbuf[cx_properties_load_fill_size];
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
279 CxPropertiesStatus status;
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
280 CxProperties parser;
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
281 cxPropertiesInit(&parser, config);
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
282 cxPropertiesUseStack(&parser, linebuf, cx_properties_load_buf_size);
928
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
283
1555
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
284 // read/fill/parse loop
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
285 status = CX_PROPERTIES_NO_DATA;
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
286 while (true) {
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
287 size_t r = fread(fillbuf, 1, cx_properties_load_fill_size, f);
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
288 if (ferror(f)) {
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
289 status = CX_PROPERTIES_FILE_ERROR;
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
290 break;
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
291 }
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
292 if (r == 0) {
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
293 break;
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
294 }
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
295 if (cxPropertiesFilln(&parser, fillbuf, r)) {
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
296 status = CX_PROPERTIES_BUFFER_ALLOC_FAILED;
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
297 break;
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
298 }
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
299 cxstring key, value;
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
300 while (true) {
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
301 status = cxPropertiesNext(&parser, &key, &value);
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
302 if (status != CX_PROPERTIES_NO_ERROR) {
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
303 break;
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
304 } else {
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
305 cxmutstr v = cx_strdup(value);
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
306 if (v.ptr == NULL) {
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
307 status = CX_PROPERTIES_MAP_ERROR;
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
308 break;
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
309 }
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
310 void *mv = use_cstring ? (void*)v.ptr : &v;
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
311 if (cxMapPut(target, key, mv)) {
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
312 cx_strfree(&v);
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
313 status = CX_PROPERTIES_MAP_ERROR;
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
314 break;
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
315 }
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
316 }
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
317 }
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
318 if (status > CX_PROPERTIES_OK) {
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
319 break;
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
320 } else if (status == CX_PROPERTIES_NO_DATA) {
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
321 // we want to report this case differently in this function
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
322 status = CX_PROPERTIES_NO_ERROR;
928
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
323 }
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
324 }
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
325
1555
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
326 // cleanup and exit
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
327 fclose(f);
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
328 cxPropertiesDestroy(&parser);
8972247f54e8 properties.h: removes the source/sink API and adds a new cxPropertiesLoad()
Mike Becker <universe@uap-core.de>
parents: 1503
diff changeset
329 cx_strfree(&fname);
928
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
330 return status;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
331 }

mercurial