Sun, 14 Jul 2013 17:11:34 +0200
added properties load/store functions
olaf@108 | 1 | /* |
olaf@108 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
olaf@108 | 3 | * |
olaf@108 | 4 | * Copyright 2013 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 | |
olaf@108 | 29 | #include <stdio.h> |
olaf@108 | 30 | #include <stdlib.h> |
olaf@108 | 31 | #include <string.h> |
olaf@108 | 32 | |
olaf@108 | 33 | #include "properties.h" |
olaf@108 | 34 | |
olaf@108 | 35 | UcxPropParser *ucx_prop_new() { |
olaf@108 | 36 | UcxPropParser *parser = (UcxPropParser*)malloc( |
olaf@108 | 37 | sizeof(UcxPropParser)); |
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@108 | 57 | void ucx_prop_free(UcxPropParser *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@108 | 64 | void ucx_prop_fill(UcxPropParser *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@108 | 70 | static void parser_tmp_append(UcxPropParser *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@108 | 80 | int ucx_prop_parse(UcxPropParser *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@108 | 100 | // run parse with the tmp buffer as main buffer |
olaf@108 | 101 | int ret = ucx_prop_parse(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@108 | 114 | return ret ? ret : ucx_prop_parse(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@109 | 179 | sstr_t v = sstrn(buf+delimiter_index+1, i-delimiter_index-1); |
olaf@109 | 180 | n = sstrtrim(n); |
olaf@109 | 181 | v = sstrtrim(v); |
olaf@109 | 182 | if(n.length != 0 || v.length != 0) { |
olaf@109 | 183 | *name = n; |
olaf@109 | 184 | *value = v; |
olaf@109 | 185 | parser->pos += i + 1; |
olaf@109 | 186 | return 1; |
olaf@109 | 187 | } else { |
olaf@109 | 188 | parser->error = 1; |
olaf@109 | 189 | } |
olaf@108 | 190 | } |
olaf@108 | 191 | |
olaf@108 | 192 | parser->pos += i + 1; |
olaf@108 | 193 | } |
olaf@108 | 194 | |
olaf@109 | 195 | return 0; |
olaf@108 | 196 | } |
olaf@108 | 197 | |
olaf@109 | 198 | int ucx_prop_parse2map(UcxPropParser *parser, UcxMap *map) { |
olaf@109 | 199 | sstr_t name; |
olaf@109 | 200 | sstr_t value; |
olaf@109 | 201 | while(ucx_prop_parse(parser, &name, &value)) { |
olaf@109 | 202 | name = sstrdup_alloc(map->allocator, name); |
olaf@109 | 203 | if(!name.ptr) { |
olaf@109 | 204 | return 1; |
olaf@109 | 205 | } |
olaf@109 | 206 | value = sstrdup_alloc(map->allocator, value); |
olaf@109 | 207 | if(!value.ptr) { |
olaf@109 | 208 | map->allocator->free(map->allocator->pool, value.ptr); |
olaf@109 | 209 | return 1; |
olaf@109 | 210 | } |
olaf@109 | 211 | if(ucx_map_sstr_put(map, name, value.ptr)) { |
olaf@109 | 212 | map->allocator->free(map->allocator->pool, name.ptr); |
olaf@109 | 213 | map->allocator->free(map->allocator->pool, value.ptr); |
olaf@109 | 214 | return 1; |
olaf@109 | 215 | } |
olaf@109 | 216 | } |
olaf@109 | 217 | if(parser->error) { |
olaf@109 | 218 | return 1; |
olaf@109 | 219 | } else { |
olaf@109 | 220 | return 0; |
olaf@109 | 221 | } |
olaf@109 | 222 | } |
olaf@109 | 223 | |
olaf@109 | 224 | int ucx_properties_load(UcxMap *map, FILE *file) { |
olaf@109 | 225 | UcxPropParser *parser = ucx_prop_new(); |
olaf@109 | 226 | if(!parser || !map || !file) { |
olaf@109 | 227 | return 1; |
olaf@109 | 228 | } |
olaf@109 | 229 | |
olaf@109 | 230 | int error = 0; |
olaf@109 | 231 | size_t r; |
olaf@109 | 232 | char buf[1024]; |
olaf@109 | 233 | while((r = fread(buf, 1, 1024, file)) != 0) { |
olaf@109 | 234 | ucx_prop_fill(parser, buf, r); |
olaf@109 | 235 | if(ucx_prop_parse2map(parser, map)) { |
olaf@109 | 236 | error = 1; |
olaf@109 | 237 | break; |
olaf@109 | 238 | } |
olaf@109 | 239 | } |
olaf@109 | 240 | |
olaf@109 | 241 | ucx_prop_free(parser); |
olaf@109 | 242 | return error; |
olaf@109 | 243 | } |
olaf@109 | 244 | |
olaf@109 | 245 | int ucx_properties_store(UcxMap *map, FILE *file) { |
olaf@109 | 246 | UcxMapIterator iter = ucx_map_iterator(map); |
olaf@109 | 247 | char *k, *v; |
olaf@109 | 248 | sstr_t key, value; |
olaf@109 | 249 | size_t written; |
olaf@109 | 250 | |
olaf@109 | 251 | UCX_MAP_FOREACH(v, iter) { |
olaf@109 | 252 | k = (char*) iter.cur->key.data; |
olaf@109 | 253 | key = sstrn(k, iter.cur->key.len); |
olaf@109 | 254 | value = sstr(v); |
olaf@109 | 255 | |
olaf@109 | 256 | written = 0; |
olaf@109 | 257 | written += fwrite(key.ptr, 1, key.length, file); |
olaf@109 | 258 | written += fwrite(" = ", 1, 3, file); |
olaf@109 | 259 | written += fwrite(value.ptr, 1, value.length, file); |
olaf@109 | 260 | written += fwrite("\n", 1, 1, file); |
olaf@109 | 261 | |
olaf@109 | 262 | if (written != key.length + value.length + 4) return 1; |
olaf@109 | 263 | } |
olaf@109 | 264 | |
olaf@109 | 265 | return 0; |
olaf@109 | 266 | } |
olaf@109 | 267 |