src/properties.c

Sun, 29 Dec 2019 16:04:34 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 29 Dec 2019 16:04:34 +0100
changeset 382
d746888af4e5
parent 259
2f5dea574a75
permissions
-rw-r--r--

adds more sstrreplace tests

108
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
259
2f5dea574a75 modules documentation
Mike Becker <universe@uap-core.de>
parents: 251
diff changeset
4 * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
108
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
d2b1e67b2b48 new properties parser
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
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
251
fae240d633fc changes source directory structure in preperation for autotools rollout
Mike Becker <universe@uap-core.de>
parents: 250
diff changeset
29 #include "ucx/properties.h"
fae240d633fc changes source directory structure in preperation for autotools rollout
Mike Becker <universe@uap-core.de>
parents: 250
diff changeset
30
108
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include <stdio.h>
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 #include <stdlib.h>
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 #include <string.h>
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34
110
1cf71e56f01e renamed properties functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 109
diff changeset
35 UcxProperties *ucx_properties_new() {
1cf71e56f01e renamed properties functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 109
diff changeset
36 UcxProperties *parser = (UcxProperties*)malloc(
1cf71e56f01e renamed properties functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 109
diff changeset
37 sizeof(UcxProperties));
108
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 if(!parser) {
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 return NULL;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 }
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 parser->buffer = NULL;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 parser->buflen = 0;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 parser->pos = 0;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 parser->tmp = NULL;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 parser->tmplen = 0;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 parser->tmpcap = 0;
109
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
48 parser->error = 0;
108
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 parser->delimiter = '=';
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 parser->comment1 = '#';
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 parser->comment2 = 0;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 parser->comment3 = 0;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 return parser;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 }
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56
110
1cf71e56f01e renamed properties functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 109
diff changeset
57 void ucx_properties_free(UcxProperties *parser) {
108
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 if(parser->tmp) {
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 free(parser->tmp);
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 }
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 free(parser);
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 }
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63
110
1cf71e56f01e renamed properties functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 109
diff changeset
64 void ucx_properties_fill(UcxProperties *parser, char *buf, size_t len) {
108
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 parser->buffer = buf;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 parser->buflen = len;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 parser->pos = 0;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 }
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69
110
1cf71e56f01e renamed properties functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 109
diff changeset
70 static void parser_tmp_append(UcxProperties *parser, char *buf, size_t len) {
108
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 if(parser->tmpcap - parser->tmplen < len) {
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 size_t newcap = parser->tmpcap + len + 64;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 parser->tmp = (char*)realloc(parser->tmp, newcap);
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 parser->tmpcap = newcap;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 }
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 memcpy(parser->tmp + parser->tmplen, buf, len);
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 parser->tmplen += len;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 }
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79
110
1cf71e56f01e renamed properties functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 109
diff changeset
80 int ucx_properties_next(UcxProperties *parser, sstr_t *name, sstr_t *value) {
108
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 if(parser->tmplen > 0) {
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 char *buf = parser->buffer + parser->pos;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 size_t len = parser->buflen - parser->pos;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 sstr_t str = sstrn(buf, len);
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 sstr_t nl = sstrchr(str, '\n');
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 if(nl.ptr) {
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 size_t newlen = (size_t)(nl.ptr - buf) + 1;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 parser_tmp_append(parser, buf, newlen);
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 // the tmp buffer contains exactly one line now
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 char *orig_buf = parser->buffer;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 size_t orig_len = parser->buflen;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 parser->buffer = parser->tmp;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 parser->buflen = parser->tmplen;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 parser->pos = 0;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 parser->tmp = NULL;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 parser->tmpcap = 0;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 parser->tmplen = 0;
111
c8c59d7f4536 new map foreach macro
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 110
diff changeset
100 // run ucx_properties_next with the tmp buffer as main buffer
110
1cf71e56f01e renamed properties functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 109
diff changeset
101 int ret = ucx_properties_next(parser, name, value);
108
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 // restore original buffer
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 parser->tmp = parser->buffer;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 parser->buffer = orig_buf;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 parser->buflen = orig_len;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 parser->pos = newlen;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 /*
109
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
110 * if ret == 0 the tmp buffer contained just space or a comment
108
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 * we parse again with the original buffer to get a name/value
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 * or a new tmp buffer
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113 */
110
1cf71e56f01e renamed properties functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 109
diff changeset
114 return ret ? ret : ucx_properties_next(parser, name, value);
108
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 } else {
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 parser_tmp_append(parser, buf, len);
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 return 0;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 }
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 } else if(parser->tmp) {
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120 free(parser->tmp);
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 parser->tmp = NULL;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 }
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 char comment1 = parser->comment1;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125 char comment2 = parser->comment2;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126 char comment3 = parser->comment3;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127 char delimiter = parser->delimiter;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
128
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129 // get one line and parse it
109
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
130 while(parser->pos < parser->buflen) {
108
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
131 char *buf = parser->buffer + parser->pos;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132 size_t len = parser->buflen - parser->pos;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134 /*
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135 * First we check if we have at least one line. We also get indices of
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136 * delimiter and comment chars
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
137 */
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138 size_t delimiter_index = 0;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139 size_t comment_index = 0;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140 int has_comment = 0;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142 size_t i = 0;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143 char c = 0;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
144 for(;i<len;i++) {
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145 c = buf[i];
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146 if(c == comment1 || c == comment2 || c == comment3) {
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 if(comment_index == 0) {
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148 comment_index = i;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149 has_comment = 1;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 }
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151 } else if(c == delimiter) {
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 if(delimiter_index == 0 && !has_comment) {
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153 delimiter_index = i;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154 }
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155 } else if(c == '\n') {
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156 break;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157 }
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158 }
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 if(c != '\n') {
109
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
161 // we don't have enough data for a line
108
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 // store remaining bytes in temporary buffer for next round
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163 parser->tmpcap = len + 128;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164 parser->tmp = (char*)malloc(parser->tmpcap);
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165 parser->tmplen = len;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
166 memcpy(parser->tmp, buf, len);
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167 return 0;
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 }
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170 sstr_t line = has_comment ? sstrn(buf, comment_index) : sstrn(buf, i);
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171 // check line
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
172 if(delimiter_index == 0) {
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
173 line = sstrtrim(line);
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174 if(line.length != 0) {
109
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
175 parser->error = 1;
108
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176 }
109
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
177 } else {
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
178 sstr_t n = sstrn(buf, delimiter_index);
117
ec0ae0c8854e fixed properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 116
diff changeset
179 sstr_t v = sstrn(
ec0ae0c8854e fixed properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 116
diff changeset
180 buf + delimiter_index + 1,
ec0ae0c8854e fixed properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 116
diff changeset
181 line.length - delimiter_index - 1);
109
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
182 n = sstrtrim(n);
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
183 v = sstrtrim(v);
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
184 if(n.length != 0 || v.length != 0) {
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
185 *name = n;
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
186 *value = v;
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
187 parser->pos += i + 1;
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
188 return 1;
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
189 } else {
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
190 parser->error = 1;
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
191 }
108
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
192 }
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
193
109
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
194 parser->pos += i + 1;
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
195 }
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
196
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
197 return 0;
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
198 }
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
199
110
1cf71e56f01e renamed properties functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 109
diff changeset
200 int ucx_properties2map(UcxProperties *parser, UcxMap *map) {
109
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
201 sstr_t name;
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
202 sstr_t value;
110
1cf71e56f01e renamed properties functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 109
diff changeset
203 while(ucx_properties_next(parser, &name, &value)) {
125
fca8efb122de changed suffix for allocator aware functions + added allocator aware functions for UcxList
Mike Becker <universe@uap-core.de>
parents: 117
diff changeset
204 value = sstrdup_a(map->allocator, value);
109
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
205 if(!value.ptr) {
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
206 return 1;
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
207 }
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
208 if(ucx_map_sstr_put(map, name, value.ptr)) {
173
31a8682fffb7 fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
209 alfree(map->allocator, value.ptr);
109
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
210 return 1;
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
211 }
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
212 }
130
633f15ce2ee4 started documentation of UcxProperties + some fixes
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
213 if (parser->error) {
633f15ce2ee4 started documentation of UcxProperties + some fixes
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
214 return parser->error;
109
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
215 } else {
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
216 return 0;
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
217 }
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
218 }
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
219
152
3238f65db163 minor code fixes + added clang support + improved build system
Mike Becker <universe@uap-core.de>
parents: 130
diff changeset
220 // buffer size is documented - change doc, when you change bufsize!
3238f65db163 minor code fixes + added clang support + improved build system
Mike Becker <universe@uap-core.de>
parents: 130
diff changeset
221 #define UCX_PROPLOAD_BUFSIZE 1024
109
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
222 int ucx_properties_load(UcxMap *map, FILE *file) {
110
1cf71e56f01e renamed properties functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 109
diff changeset
223 UcxProperties *parser = ucx_properties_new();
130
633f15ce2ee4 started documentation of UcxProperties + some fixes
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
224 if(!(parser && map && file)) {
109
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
225 return 1;
108
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
226 }
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
227
109
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
228 int error = 0;
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
229 size_t r;
152
3238f65db163 minor code fixes + added clang support + improved build system
Mike Becker <universe@uap-core.de>
parents: 130
diff changeset
230 char buf[UCX_PROPLOAD_BUFSIZE];
3238f65db163 minor code fixes + added clang support + improved build system
Mike Becker <universe@uap-core.de>
parents: 130
diff changeset
231 while((r = fread(buf, 1, UCX_PROPLOAD_BUFSIZE, file)) != 0) {
110
1cf71e56f01e renamed properties functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 109
diff changeset
232 ucx_properties_fill(parser, buf, r);
130
633f15ce2ee4 started documentation of UcxProperties + some fixes
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
233 error = ucx_properties2map(parser, map);
633f15ce2ee4 started documentation of UcxProperties + some fixes
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
234 if (error) {
109
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
235 break;
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
236 }
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
237 }
110
1cf71e56f01e renamed properties functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 109
diff changeset
238 ucx_properties_free(parser);
109
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
239 return error;
108
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
240 }
d2b1e67b2b48 new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
241
109
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
242 int ucx_properties_store(UcxMap *map, FILE *file) {
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
243 UcxMapIterator iter = ucx_map_iterator(map);
152
3238f65db163 minor code fixes + added clang support + improved build system
Mike Becker <universe@uap-core.de>
parents: 130
diff changeset
244 void *v;
112
6384016df2a3 removed map load/store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 111
diff changeset
245 sstr_t value;
109
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
246 size_t written;
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
247
111
c8c59d7f4536 new map foreach macro
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 110
diff changeset
248 UCX_MAP_FOREACH(k, v, iter) {
152
3238f65db163 minor code fixes + added clang support + improved build system
Mike Becker <universe@uap-core.de>
parents: 130
diff changeset
249 value = sstr((char*)v);
109
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
250
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
251 written = 0;
112
6384016df2a3 removed map load/store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 111
diff changeset
252 written += fwrite(k.data, 1, k.len, file);
109
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
253 written += fwrite(" = ", 1, 3, file);
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
254 written += fwrite(value.ptr, 1, value.length, file);
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
255 written += fwrite("\n", 1, 1, file);
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
256
130
633f15ce2ee4 started documentation of UcxProperties + some fixes
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
257 if (written != k.len + value.length + 4) {
633f15ce2ee4 started documentation of UcxProperties + some fixes
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
258 return 1;
633f15ce2ee4 started documentation of UcxProperties + some fixes
Mike Becker <universe@uap-core.de>
parents: 125
diff changeset
259 }
109
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
260 }
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
261
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
262 return 0;
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
263 }
75cb6590358b added properties load/store functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
264

mercurial