Fri, 12 Jul 2013 20:50:18 +0200
new properties parser
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@108 | 48 | parser->delimiter = '='; |
olaf@108 | 49 | parser->comment1 = '#'; |
olaf@108 | 50 | parser->comment2 = 0; |
olaf@108 | 51 | parser->comment3 = 0; |
olaf@108 | 52 | |
olaf@108 | 53 | return parser; |
olaf@108 | 54 | } |
olaf@108 | 55 | |
olaf@108 | 56 | void ucx_prop_free(UcxPropParser *parser) { |
olaf@108 | 57 | if(parser->tmp) { |
olaf@108 | 58 | free(parser->tmp); |
olaf@108 | 59 | } |
olaf@108 | 60 | free(parser); |
olaf@108 | 61 | } |
olaf@108 | 62 | |
olaf@108 | 63 | void ucx_prop_fill(UcxPropParser *parser, char *buf, size_t len) { |
olaf@108 | 64 | parser->buffer = buf; |
olaf@108 | 65 | parser->buflen = len; |
olaf@108 | 66 | parser->pos = 0; |
olaf@108 | 67 | } |
olaf@108 | 68 | |
olaf@108 | 69 | static void parser_tmp_append(UcxPropParser *parser, char *buf, size_t len) { |
olaf@108 | 70 | if(parser->tmpcap - parser->tmplen < len) { |
olaf@108 | 71 | size_t newcap = parser->tmpcap + len + 64; |
olaf@108 | 72 | parser->tmp = (char*)realloc(parser->tmp, newcap); |
olaf@108 | 73 | parser->tmpcap = newcap; |
olaf@108 | 74 | } |
olaf@108 | 75 | memcpy(parser->tmp + parser->tmplen, buf, len); |
olaf@108 | 76 | parser->tmplen += len; |
olaf@108 | 77 | } |
olaf@108 | 78 | |
olaf@108 | 79 | int ucx_prop_parse(UcxPropParser *parser, sstr_t *name, sstr_t *value) { |
olaf@108 | 80 | if(parser->tmplen > 0) { |
olaf@108 | 81 | char *buf = parser->buffer + parser->pos; |
olaf@108 | 82 | size_t len = parser->buflen - parser->pos; |
olaf@108 | 83 | sstr_t str = sstrn(buf, len); |
olaf@108 | 84 | sstr_t nl = sstrchr(str, '\n'); |
olaf@108 | 85 | if(nl.ptr) { |
olaf@108 | 86 | size_t newlen = (size_t)(nl.ptr - buf) + 1; |
olaf@108 | 87 | parser_tmp_append(parser, buf, newlen); |
olaf@108 | 88 | // the tmp buffer contains exactly one line now |
olaf@108 | 89 | |
olaf@108 | 90 | char *orig_buf = parser->buffer; |
olaf@108 | 91 | size_t orig_len = parser->buflen; |
olaf@108 | 92 | |
olaf@108 | 93 | parser->buffer = parser->tmp; |
olaf@108 | 94 | parser->buflen = parser->tmplen; |
olaf@108 | 95 | parser->pos = 0; |
olaf@108 | 96 | parser->tmp = NULL; |
olaf@108 | 97 | parser->tmpcap = 0; |
olaf@108 | 98 | parser->tmplen = 0; |
olaf@108 | 99 | // run parse with the tmp buffer as main buffer |
olaf@108 | 100 | int ret = ucx_prop_parse(parser, name, value); |
olaf@108 | 101 | |
olaf@108 | 102 | // restore original buffer |
olaf@108 | 103 | parser->tmp = parser->buffer; |
olaf@108 | 104 | parser->buffer = orig_buf; |
olaf@108 | 105 | parser->buflen = orig_len; |
olaf@108 | 106 | parser->pos = newlen; |
olaf@108 | 107 | |
olaf@108 | 108 | /* |
olaf@108 | 109 | * if ret == 0 the tmp buffer contained just space or comment |
olaf@108 | 110 | * we parse again with the original buffer to get a name/value |
olaf@108 | 111 | * or a new tmp buffer |
olaf@108 | 112 | */ |
olaf@108 | 113 | return ret ? ret : ucx_prop_parse(parser, name, value); |
olaf@108 | 114 | } else { |
olaf@108 | 115 | parser_tmp_append(parser, buf, len); |
olaf@108 | 116 | return 0; |
olaf@108 | 117 | } |
olaf@108 | 118 | } else if(parser->tmp) { |
olaf@108 | 119 | free(parser->tmp); |
olaf@108 | 120 | parser->tmp = NULL; |
olaf@108 | 121 | } |
olaf@108 | 122 | |
olaf@108 | 123 | char comment1 = parser->comment1; |
olaf@108 | 124 | char comment2 = parser->comment2; |
olaf@108 | 125 | char comment3 = parser->comment3; |
olaf@108 | 126 | char delimiter = parser->delimiter; |
olaf@108 | 127 | |
olaf@108 | 128 | // get one line and parse it |
olaf@108 | 129 | while(1) { |
olaf@108 | 130 | if(parser->pos >= parser->buflen) { |
olaf@108 | 131 | return 0; |
olaf@108 | 132 | } |
olaf@108 | 133 | char *buf = parser->buffer + parser->pos; |
olaf@108 | 134 | size_t len = parser->buflen - parser->pos; |
olaf@108 | 135 | |
olaf@108 | 136 | /* |
olaf@108 | 137 | * First we check if we have at least one line. We also get indices of |
olaf@108 | 138 | * delimiter and comment chars |
olaf@108 | 139 | */ |
olaf@108 | 140 | size_t delimiter_index = 0; |
olaf@108 | 141 | size_t comment_index = 0; |
olaf@108 | 142 | int has_comment = 0; |
olaf@108 | 143 | |
olaf@108 | 144 | size_t i = 0; |
olaf@108 | 145 | char c = 0; |
olaf@108 | 146 | for(;i<len;i++) { |
olaf@108 | 147 | c = buf[i]; |
olaf@108 | 148 | if(c == comment1 || c == comment2 || c == comment3) { |
olaf@108 | 149 | if(comment_index == 0) { |
olaf@108 | 150 | comment_index = i; |
olaf@108 | 151 | has_comment = 1; |
olaf@108 | 152 | } |
olaf@108 | 153 | } else if(c == delimiter) { |
olaf@108 | 154 | if(delimiter_index == 0 && !has_comment) { |
olaf@108 | 155 | delimiter_index = i; |
olaf@108 | 156 | } |
olaf@108 | 157 | } else if(c == '\n') { |
olaf@108 | 158 | break; |
olaf@108 | 159 | } |
olaf@108 | 160 | } |
olaf@108 | 161 | |
olaf@108 | 162 | if(c != '\n') { |
olaf@108 | 163 | // we have not enough data for a line |
olaf@108 | 164 | // store remaining bytes in temporary buffer for next round |
olaf@108 | 165 | parser->tmpcap = len + 128; |
olaf@108 | 166 | parser->tmp = (char*)malloc(parser->tmpcap); |
olaf@108 | 167 | parser->tmplen = len; |
olaf@108 | 168 | memcpy(parser->tmp, buf, len); |
olaf@108 | 169 | return 0; |
olaf@108 | 170 | } |
olaf@108 | 171 | |
olaf@108 | 172 | sstr_t line = has_comment ? sstrn(buf, comment_index) : sstrn(buf, i); |
olaf@108 | 173 | // check line |
olaf@108 | 174 | if(delimiter_index == 0) { |
olaf@108 | 175 | line = sstrtrim(line); |
olaf@108 | 176 | if(line.length != 0) { |
olaf@108 | 177 | // syntax error |
olaf@108 | 178 | // TODO |
olaf@108 | 179 | } |
olaf@108 | 180 | parser->pos += i + 1; |
olaf@108 | 181 | continue; |
olaf@108 | 182 | } |
olaf@108 | 183 | |
olaf@108 | 184 | sstr_t n = sstrn(buf, delimiter_index); |
olaf@108 | 185 | sstr_t v = sstrn(buf + delimiter_index + 1, i - delimiter_index - 1); |
olaf@108 | 186 | n = sstrtrim(n); |
olaf@108 | 187 | v = sstrtrim(v); |
olaf@108 | 188 | if(n.length == 0 || v.length == 0) { |
olaf@108 | 189 | // syntax error |
olaf@108 | 190 | // TODO |
olaf@108 | 191 | parser->pos += i + 1; |
olaf@108 | 192 | continue; |
olaf@108 | 193 | } |
olaf@108 | 194 | |
olaf@108 | 195 | *name = n; |
olaf@108 | 196 | *value = v; |
olaf@108 | 197 | |
olaf@108 | 198 | parser->pos += i + 1; |
olaf@108 | 199 | break; |
olaf@108 | 200 | } |
olaf@108 | 201 | |
olaf@108 | 202 | return 1; |
olaf@108 | 203 | } |
olaf@108 | 204 |