src/properties.c

Mon, 11 Nov 2024 21:42:14 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 11 Nov 2024 21:42:14 +0100
changeset 986
38fa7e41194c
parent 985
68754c7de906
permissions
-rw-r--r--

simplify cx_array_copy() - fixes #474

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>
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
32
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
33 static const int CX_PROPERTIES_FLAG_USE_STACK = 0x01;
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 '=',
924
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
37 //'\\',
923
45da884269c8 add ucx2.1 style interface for a properties parser
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 '#',
45da884269c8 add ucx2.1 style interface for a properties parser
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 '\0',
45da884269c8 add ucx2.1 style interface for a properties parser
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 '\0'
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) {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
52 if (0 == (prop->flags & CX_PROPERTIES_FLAG_USE_STACK)) {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
53 free(prop->buf);
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 prop->buf = NULL;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
56 prop->buf_capacity = prop->buf_size = 0;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
57 }
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
58
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
59 static int cx_properties_ensure_buf_capacity(CxProperties *prop, size_t cap) {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
60 if (prop->buf_capacity >= cap) {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
61 return 0;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
62 }
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
63
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
64 // not enough capacity - are we on the stack right now?
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
65 if ((prop->flags & CX_PROPERTIES_FLAG_USE_STACK) != 0) {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
66 // move to the heap
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
67 char *newbuf = malloc(cap);
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
68 if (newbuf == NULL) return 1;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
69 memcpy(newbuf, prop->buf, prop->buf_size);
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
70 prop->buf = newbuf;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
71 prop->flags &= CX_PROPERTIES_FLAG_USE_STACK;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
72 } else {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
73 // we are on the heap already, reallocate
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
74 // this is legit, because realloc() behaves like malloc() when the
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
75 // current pointer is NULL
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
76 char *newbuf = realloc(prop->buf, cap);
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
77 if (newbuf == NULL) return 1;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
78 prop->buf = newbuf;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
79 }
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
80
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
81 // store new capacity and return
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
82 prop->buf_capacity = cap;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
83 return 0;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
84 }
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 static int cx_properties_rescuen_input(CxProperties *prop, size_t len) {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
87 if (cx_properties_ensure_buf_capacity(prop, prop->buf_size + len)) {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
88 return 1;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
89 }
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
90 const char *src = prop->text + prop->text_pos;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
91 char *dest = prop->buf + prop->buf_size;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
92 memcpy(dest, src, len);
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
93 prop->buf_size += len;
928
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
94 prop->text_pos += len;
924
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
95 return 0;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
96 }
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
97
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
98 static int cx_properties_rescue_input(CxProperties *prop) {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
99 // someone fucked around with our integers, exit immediately
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
100 if (prop->text_pos > prop->text_size) return 0;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
101
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
102 // determine the bytes needed
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
103 size_t len = prop->text_size - prop->text_pos;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
104
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
105 return cx_properties_rescuen_input(prop, len);
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
106 }
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
107
972
a9a1d07a6840 add support for UCX strings in cxPropertiesFill()
Mike Becker <universe@uap-core.de>
parents: 929
diff changeset
108 int cxPropertiesFilln(
924
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
109 CxProperties *prop,
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
110 const char *buf,
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
111 size_t len
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
112 ) {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
113 if (cx_properties_rescue_input(prop)) return 1;
972
a9a1d07a6840 add support for UCX strings in cxPropertiesFill()
Mike Becker <universe@uap-core.de>
parents: 929
diff changeset
114 prop->text = buf;
a9a1d07a6840 add support for UCX strings in cxPropertiesFill()
Mike Becker <universe@uap-core.de>
parents: 929
diff changeset
115 prop->text_size = len;
a9a1d07a6840 add support for UCX strings in cxPropertiesFill()
Mike Becker <universe@uap-core.de>
parents: 929
diff changeset
116 prop->text_pos = 0;
924
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
117 return 0;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
118 }
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
119
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
120 void cxPropertiesUseStack(
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
121 CxProperties *prop,
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
122 char *buf,
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
123 size_t capacity
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
124 ) {
929
192a440b99df fix totally bugged assertion in cxPropertiesUseStack()
Mike Becker <universe@uap-core.de>
parents: 928
diff changeset
125 assert(prop->buf == NULL);
924
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
126 prop->buf = buf;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
127 prop->buf_capacity = capacity;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
128 prop->buf_size = 0;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
129 prop->flags |= CX_PROPERTIES_FLAG_USE_STACK;
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
928
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
132 CxPropertiesStatus cxPropertiesNext(
924
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
133 CxProperties *prop,
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
134 cxstring *key,
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
135 cxstring *value
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
136 ) {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
137 // check if we have a text buffer
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
138 if (prop->text == NULL) {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
139 return CX_PROPERTIES_NULL_INPUT;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
140 }
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
141 // check if we have rescued data
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
142 if (prop->buf_size > 0) {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
143 // check if we can now get a complete line
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
144 const char *buf = prop->text + prop->text_pos;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
145 size_t len = prop->text_size - prop->text_pos;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
146 cxstring str = cx_strn(buf, len);
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
147 cxstring nl = cx_strchr(str, '\n');
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
148 if(nl.length > 0) {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
149 // 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
150 // to complete the line
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
151 size_t len_until_nl = (size_t)(nl.ptr - buf) + 1;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
152
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
153 if (cx_properties_rescuen_input(prop, len_until_nl)) {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
154 return CX_PROPERTIES_BUFFER_ALLOC_FAILED;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
155 }
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
156
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
157 // the tmp buffer contains exactly one line now
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
158 // we use a trick here: we swap the buffers and recurse
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
159 const char *orig_text = prop->text;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
160 size_t orig_size = prop->text_size;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
161 prop->text = prop->buf;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
162 prop->text_size = prop->buf_size;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
163 prop->text_pos = 0;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
164 prop->buf_size = 0;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
165
928
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
166 CxPropertiesStatus result;
924
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
167 result = cxPropertiesNext(prop, key, value);
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 // restore original buffer
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
170 prop->text = orig_text;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
171 prop->text_size = orig_size;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
172
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
173 // set the position to after the newline
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
174 prop->text_pos = len_until_nl;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
175
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
176 // check the result
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
177 if (result == CX_PROPERTIES_NO_ERROR) {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
178 // reset the rescue buffer and return with the result
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
179 prop->buf_size = 0;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
180 return result;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
181 } else if (result == CX_PROPERTIES_NO_DATA) {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
182 // rescue buffer contained only blanks or comments
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
183 // reset the rescue buffer and retry with text buffer
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
184 prop->buf_size = 0;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
185 return cxPropertiesNext(prop, key, value);
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
186 } else {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
187 // CX_PROPERTIES_INCOMPLETE_DATA is not possible
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
188 // so it must have been another error
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
189 // do not reset the rescue buffer and return the error
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
190 return result;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
191 }
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
192 } else {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
193 // still not enough data
928
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
194 if (cx_properties_rescue_input(prop)) {
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
195 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
196 }
924
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
197 return CX_PROPERTIES_INCOMPLETE_DATA;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
198 }
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
199 }
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
200
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
201 char comment1 = prop->config.comment1;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
202 char comment2 = prop->config.comment2;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
203 char comment3 = prop->config.comment3;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
204 char delimiter = prop->config.delimiter;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
205
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
206 // get one line and parse it
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
207 while (prop->text_pos < prop->text_size) {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
208 const char *buf = prop->text + prop->text_pos;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
209 size_t len = prop->text_size - prop->text_pos;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
210
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
211 /*
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
212 * 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
213 * delimiter and comment chars
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
214 */
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
215 size_t delimiter_index = 0;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
216 size_t comment_index = 0;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
217 bool has_comment = false;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
218
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
219 size_t i = 0;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
220 char c = 0;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
221 for (; i < len; i++) {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
222 c = buf[i];
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
223 if (c == comment1 || c == comment2 || c == comment3) {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
224 if (comment_index == 0) {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
225 comment_index = i;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
226 has_comment = true;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
227 }
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
228 } else if (c == delimiter) {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
229 if (delimiter_index == 0 && !has_comment) {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
230 delimiter_index = i;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
231 }
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
232 } else if (c == '\n') {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
233 break;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
234 }
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 if (c != '\n') {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
238 // we don't have enough data for a line
928
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
239 if (cx_properties_rescue_input(prop)) {
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
240 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
241 }
924
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
242 return CX_PROPERTIES_INCOMPLETE_DATA;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
243 }
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
244
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
245 cxstring line = has_comment ?
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
246 cx_strn(buf, comment_index) :
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
247 cx_strn(buf, i);
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
248 // check line
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
249 if (delimiter_index == 0) {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
250 // if line is not blank ...
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
251 line = cx_strtrim(line);
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
252 // ... 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
253 if (line.length > 0) {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
254 if (line.ptr[0] == delimiter) {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
255 return CX_PROPERTIES_INVALID_EMPTY_KEY;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
256 } else {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
257 return CX_PROPERTIES_INVALID_MISSING_DELIMITER;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
258 }
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
259 }
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
260 } else {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
261 cxstring k = cx_strn(buf, delimiter_index);
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
262 cxstring val = cx_strn(
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
263 buf + delimiter_index + 1,
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
264 line.length - delimiter_index - 1);
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
265 k = cx_strtrim(k);
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
266 val = cx_strtrim(val);
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
267 if (k.length > 0) {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
268 *key = k;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
269 *value = val;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
270 prop->text_pos += i + 1;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
271 assert(prop->text_pos <= prop->text_size);
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
272 return CX_PROPERTIES_NO_ERROR;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
273 } else {
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
274 return CX_PROPERTIES_INVALID_EMPTY_KEY;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
275 }
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
276 }
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
277
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
278 prop->text_pos += i + 1;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
279 }
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
280
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
281 // when we come to this point, all data must have been read
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
282 assert(prop->text_pos == prop->text_size);
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
283 return CX_PROPERTIES_NO_DATA;
3c90dfc35f06 add implementation for the properties parser
Mike Becker <universe@uap-core.de>
parents: 923
diff changeset
284 }
928
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
285
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
286 static int cx_properties_sink_map(
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 972
diff changeset
287 cx_attr_unused CxProperties *prop,
928
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
288 CxPropertiesSink *sink,
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
289 cxstring key,
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
290 cxstring value
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
291 ) {
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
292 CxMap *map = sink->sink;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
293 CxAllocator *alloc = sink->data;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
294 cxmutstr v = cx_strdup_a(alloc, value);
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
295 int r = cx_map_put_cxstr(map, key, v.ptr);
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
296 if (r != 0) cx_strfree_a(alloc, &v);
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
297 return r;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
298 }
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
299
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
300 CxPropertiesSink cxPropertiesMapSink(CxMap *map) {
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
301 CxPropertiesSink sink;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
302 sink.sink = map;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
303 sink.data = cxDefaultAllocator;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
304 sink.sink_func = cx_properties_sink_map;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
305 return sink;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
306 }
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
307
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
308 static int cx_properties_read_string(
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
309 CxProperties *prop,
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
310 CxPropertiesSource *src,
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
311 cxstring *target
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
312 ) {
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
313 if (prop->text == src->src) {
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
314 // when the input buffer already contains the string
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
315 // we have nothing more to provide
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
316 target->length = 0;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
317 } else {
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
318 target->ptr = src->src;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
319 target->length = src->data_size;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
320 }
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
321 return 0;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
322 }
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 static int cx_properties_read_file(
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 972
diff changeset
325 cx_attr_unused CxProperties *prop,
928
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
326 CxPropertiesSource *src,
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
327 cxstring *target
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
328 ) {
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
329 target->ptr = src->data_ptr;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
330 target->length = fread(src->data_ptr, 1, src->data_size, src->src);
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
331 return ferror(src->src);
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
332 }
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
333
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
334 static int cx_properties_read_init_file(
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 972
diff changeset
335 cx_attr_unused CxProperties *prop,
928
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
336 CxPropertiesSource *src
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
337 ) {
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
338 src->data_ptr = malloc(src->data_size);
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
339 if (src->data_ptr == NULL) return 1;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
340 return 0;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
341 }
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
342
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
343 static void cx_properties_read_clean_file(
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 972
diff changeset
344 cx_attr_unused CxProperties *prop,
928
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
345 CxPropertiesSource *src
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
346 ) {
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
347 free(src->data_ptr);
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
348 }
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
349
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
350 CxPropertiesSource cxPropertiesStringSource(cxstring str) {
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
351 CxPropertiesSource src;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
352 src.src = (void*) str.ptr;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
353 src.data_size = str.length;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
354 src.read_func = cx_properties_read_string;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
355 src.read_init_func = NULL;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
356 src.read_clean_func = NULL;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
357 return src;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
358 }
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
359
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
360 CxPropertiesSource cxPropertiesCstrnSource(const char *str, size_t len) {
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
361 CxPropertiesSource src;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
362 src.src = (void*) str;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
363 src.data_size = len;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
364 src.read_func = cx_properties_read_string;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
365 src.read_init_func = NULL;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
366 src.read_clean_func = NULL;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
367 return src;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
368 }
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
369
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
370 CxPropertiesSource cxPropertiesCstrSource(const char *str) {
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
371 CxPropertiesSource src;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
372 src.src = (void*) str;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
373 src.data_size = strlen(str);
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
374 src.read_func = cx_properties_read_string;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
375 src.read_init_func = NULL;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
376 src.read_clean_func = NULL;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
377 return src;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
378 }
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
379
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
380 CxPropertiesSource cxPropertiesFileSource(FILE *file, size_t chunk_size) {
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
381 CxPropertiesSource src;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
382 src.src = file;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
383 src.data_size = chunk_size;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
384 src.read_func = cx_properties_read_file;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
385 src.read_init_func = cx_properties_read_init_file;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
386 src.read_clean_func = cx_properties_read_clean_file;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
387 return src;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
388 }
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
389
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
390 CxPropertiesStatus cxPropertiesLoad(
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
391 CxProperties *prop,
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
392 CxPropertiesSink sink,
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
393 CxPropertiesSource source
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
394 ) {
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
395 assert(source.read_func != NULL);
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
396 assert(sink.sink_func != NULL);
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
397
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
398 // initialize reader
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
399 if (source.read_init_func != NULL) {
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
400 if (source.read_init_func(prop, &source)) {
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
401 return CX_PROPERTIES_READ_INIT_FAILED;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
402 }
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
403 }
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
404
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
405 // transfer the data from the source to the sink
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
406 CxPropertiesStatus status;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
407 bool found = false;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
408 while (true) {
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
409 // read input
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
410 cxstring input;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
411 if (source.read_func(prop, &source, &input)) {
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
412 status = CX_PROPERTIES_READ_FAILED;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
413 break;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
414 }
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
415
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
416 // no more data - break
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
417 if (input.length == 0) {
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
418 status = found ? CX_PROPERTIES_NO_ERROR : CX_PROPERTIES_NO_DATA;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
419 break;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
420 }
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
421
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
422 // set the input buffer and read the k/v-pairs
972
a9a1d07a6840 add support for UCX strings in cxPropertiesFill()
Mike Becker <universe@uap-core.de>
parents: 929
diff changeset
423 prop->text = input.ptr;
a9a1d07a6840 add support for UCX strings in cxPropertiesFill()
Mike Becker <universe@uap-core.de>
parents: 929
diff changeset
424 prop->text_size = input.length;
a9a1d07a6840 add support for UCX strings in cxPropertiesFill()
Mike Becker <universe@uap-core.de>
parents: 929
diff changeset
425 prop->text_pos = 0;
928
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
426
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
427 CxPropertiesStatus kv_status;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
428 do {
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
429 cxstring key, value;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
430 kv_status = cxPropertiesNext(prop, &key, &value);
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
431 if (kv_status == CX_PROPERTIES_NO_ERROR) {
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
432 found = true;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
433 if (sink.sink_func(prop, &sink, key, value)) {
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
434 kv_status = CX_PROPERTIES_SINK_FAILED;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
435 }
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
436 }
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
437 } while (kv_status == CX_PROPERTIES_NO_ERROR);
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
438
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
439 if (kv_status > CX_PROPERTIES_OK) {
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
440 status = kv_status;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
441 break;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
442 }
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
443 }
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
444
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
445 if (source.read_clean_func != NULL) {
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
446 source.read_clean_func(prop, &source);
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
447 }
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
448
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
449 return status;
d2d42cb1d59e add source/sink facility for properties - fixes #430
Mike Becker <universe@uap-core.de>
parents: 924
diff changeset
450 }

mercurial