src/server/ucx/properties.c

Mon, 09 Sep 2013 12:15:54 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 09 Sep 2013 12:15:54 +0200
changeset 93
95b77e842db3
parent 88
73b3485e96f1
permissions
-rw-r--r--

fixed solaris build

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

mercurial