ucx/properties.c

Mon, 06 Jan 2025 21:18:36 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 06 Jan 2025 21:18:36 +0100
changeset 852
83fdf679df99
permissions
-rw-r--r--

update ucx

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

mercurial