olaf@108: /* olaf@108: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. olaf@108: * olaf@108: * Copyright 2013 Olaf Wintermann. All rights reserved. olaf@108: * olaf@108: * Redistribution and use in source and binary forms, with or without olaf@108: * modification, are permitted provided that the following conditions are met: olaf@108: * olaf@108: * 1. Redistributions of source code must retain the above copyright olaf@108: * notice, this list of conditions and the following disclaimer. olaf@108: * olaf@108: * 2. Redistributions in binary form must reproduce the above copyright olaf@108: * notice, this list of conditions and the following disclaimer in the olaf@108: * documentation and/or other materials provided with the distribution. olaf@108: * olaf@108: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" olaf@108: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE olaf@108: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE olaf@108: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE olaf@108: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR olaf@108: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF olaf@108: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS olaf@108: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN olaf@108: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) olaf@108: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE olaf@108: * POSSIBILITY OF SUCH DAMAGE. olaf@108: */ olaf@108: olaf@108: #include olaf@108: #include olaf@108: #include olaf@108: olaf@108: #include "properties.h" olaf@108: olaf@108: UcxPropParser *ucx_prop_new() { olaf@108: UcxPropParser *parser = (UcxPropParser*)malloc( olaf@108: sizeof(UcxPropParser)); olaf@108: if(!parser) { olaf@108: return NULL; olaf@108: } olaf@108: olaf@108: parser->buffer = NULL; olaf@108: parser->buflen = 0; olaf@108: parser->pos = 0; olaf@108: parser->tmp = NULL; olaf@108: parser->tmplen = 0; olaf@108: parser->tmpcap = 0; olaf@108: parser->delimiter = '='; olaf@108: parser->comment1 = '#'; olaf@108: parser->comment2 = 0; olaf@108: parser->comment3 = 0; olaf@108: olaf@108: return parser; olaf@108: } olaf@108: olaf@108: void ucx_prop_free(UcxPropParser *parser) { olaf@108: if(parser->tmp) { olaf@108: free(parser->tmp); olaf@108: } olaf@108: free(parser); olaf@108: } olaf@108: olaf@108: void ucx_prop_fill(UcxPropParser *parser, char *buf, size_t len) { olaf@108: parser->buffer = buf; olaf@108: parser->buflen = len; olaf@108: parser->pos = 0; olaf@108: } olaf@108: olaf@108: static void parser_tmp_append(UcxPropParser *parser, char *buf, size_t len) { olaf@108: if(parser->tmpcap - parser->tmplen < len) { olaf@108: size_t newcap = parser->tmpcap + len + 64; olaf@108: parser->tmp = (char*)realloc(parser->tmp, newcap); olaf@108: parser->tmpcap = newcap; olaf@108: } olaf@108: memcpy(parser->tmp + parser->tmplen, buf, len); olaf@108: parser->tmplen += len; olaf@108: } olaf@108: olaf@108: int ucx_prop_parse(UcxPropParser *parser, sstr_t *name, sstr_t *value) { olaf@108: if(parser->tmplen > 0) { olaf@108: char *buf = parser->buffer + parser->pos; olaf@108: size_t len = parser->buflen - parser->pos; olaf@108: sstr_t str = sstrn(buf, len); olaf@108: sstr_t nl = sstrchr(str, '\n'); olaf@108: if(nl.ptr) { olaf@108: size_t newlen = (size_t)(nl.ptr - buf) + 1; olaf@108: parser_tmp_append(parser, buf, newlen); olaf@108: // the tmp buffer contains exactly one line now olaf@108: olaf@108: char *orig_buf = parser->buffer; olaf@108: size_t orig_len = parser->buflen; olaf@108: olaf@108: parser->buffer = parser->tmp; olaf@108: parser->buflen = parser->tmplen; olaf@108: parser->pos = 0; olaf@108: parser->tmp = NULL; olaf@108: parser->tmpcap = 0; olaf@108: parser->tmplen = 0; olaf@108: // run parse with the tmp buffer as main buffer olaf@108: int ret = ucx_prop_parse(parser, name, value); olaf@108: olaf@108: // restore original buffer olaf@108: parser->tmp = parser->buffer; olaf@108: parser->buffer = orig_buf; olaf@108: parser->buflen = orig_len; olaf@108: parser->pos = newlen; olaf@108: olaf@108: /* olaf@108: * if ret == 0 the tmp buffer contained just space or comment olaf@108: * we parse again with the original buffer to get a name/value olaf@108: * or a new tmp buffer olaf@108: */ olaf@108: return ret ? ret : ucx_prop_parse(parser, name, value); olaf@108: } else { olaf@108: parser_tmp_append(parser, buf, len); olaf@108: return 0; olaf@108: } olaf@108: } else if(parser->tmp) { olaf@108: free(parser->tmp); olaf@108: parser->tmp = NULL; olaf@108: } olaf@108: olaf@108: char comment1 = parser->comment1; olaf@108: char comment2 = parser->comment2; olaf@108: char comment3 = parser->comment3; olaf@108: char delimiter = parser->delimiter; olaf@108: olaf@108: // get one line and parse it olaf@108: while(1) { olaf@108: if(parser->pos >= parser->buflen) { olaf@108: return 0; olaf@108: } olaf@108: char *buf = parser->buffer + parser->pos; olaf@108: size_t len = parser->buflen - parser->pos; olaf@108: olaf@108: /* olaf@108: * First we check if we have at least one line. We also get indices of olaf@108: * delimiter and comment chars olaf@108: */ olaf@108: size_t delimiter_index = 0; olaf@108: size_t comment_index = 0; olaf@108: int has_comment = 0; olaf@108: olaf@108: size_t i = 0; olaf@108: char c = 0; olaf@108: for(;itmpcap = len + 128; olaf@108: parser->tmp = (char*)malloc(parser->tmpcap); olaf@108: parser->tmplen = len; olaf@108: memcpy(parser->tmp, buf, len); olaf@108: return 0; olaf@108: } olaf@108: olaf@108: sstr_t line = has_comment ? sstrn(buf, comment_index) : sstrn(buf, i); olaf@108: // check line olaf@108: if(delimiter_index == 0) { olaf@108: line = sstrtrim(line); olaf@108: if(line.length != 0) { olaf@108: // syntax error olaf@108: // TODO olaf@108: } olaf@108: parser->pos += i + 1; olaf@108: continue; olaf@108: } olaf@108: olaf@108: sstr_t n = sstrn(buf, delimiter_index); olaf@108: sstr_t v = sstrn(buf + delimiter_index + 1, i - delimiter_index - 1); olaf@108: n = sstrtrim(n); olaf@108: v = sstrtrim(v); olaf@108: if(n.length == 0 || v.length == 0) { olaf@108: // syntax error olaf@108: // TODO olaf@108: parser->pos += i + 1; olaf@108: continue; olaf@108: } olaf@108: olaf@108: *name = n; olaf@108: *value = v; olaf@108: olaf@108: parser->pos += i + 1; olaf@108: break; olaf@108: } olaf@108: olaf@108: return 1; olaf@108: } olaf@108: