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