olaf@108: /* olaf@108: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. olaf@108: * universe@259: * Copyright 2017 Mike Becker, 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: universe@251: #include "ucx/properties.h" universe@251: olaf@108: #include olaf@108: #include olaf@108: #include olaf@108: olaf@110: UcxProperties *ucx_properties_new() { olaf@110: UcxProperties *parser = (UcxProperties*)malloc( olaf@110: sizeof(UcxProperties)); 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@109: parser->error = 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@110: void ucx_properties_free(UcxProperties *parser) { olaf@108: if(parser->tmp) { olaf@108: free(parser->tmp); olaf@108: } olaf@108: free(parser); olaf@108: } olaf@108: olaf@110: void ucx_properties_fill(UcxProperties *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@110: static void parser_tmp_append(UcxProperties *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@110: int ucx_properties_next(UcxProperties *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@111: // run ucx_properties_next with the tmp buffer as main buffer olaf@110: int ret = ucx_properties_next(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@109: * if ret == 0 the tmp buffer contained just space or a 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@110: return ret ? ret : ucx_properties_next(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@109: while(parser->pos < parser->buflen) { 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@109: parser->error = 1; olaf@108: } olaf@109: } else { olaf@109: sstr_t n = sstrn(buf, delimiter_index); olaf@117: sstr_t v = sstrn( olaf@117: buf + delimiter_index + 1, olaf@117: line.length - delimiter_index - 1); olaf@109: n = sstrtrim(n); olaf@109: v = sstrtrim(v); olaf@109: if(n.length != 0 || v.length != 0) { olaf@109: *name = n; olaf@109: *value = v; olaf@109: parser->pos += i + 1; olaf@109: return 1; olaf@109: } else { olaf@109: parser->error = 1; olaf@109: } olaf@108: } olaf@108: olaf@108: parser->pos += i + 1; olaf@108: } olaf@108: olaf@109: return 0; olaf@108: } olaf@108: olaf@110: int ucx_properties2map(UcxProperties *parser, UcxMap *map) { olaf@109: sstr_t name; olaf@109: sstr_t value; olaf@110: while(ucx_properties_next(parser, &name, &value)) { universe@125: value = sstrdup_a(map->allocator, value); olaf@109: if(!value.ptr) { olaf@109: return 1; olaf@109: } olaf@109: if(ucx_map_sstr_put(map, name, value.ptr)) { universe@173: alfree(map->allocator, value.ptr); olaf@109: return 1; olaf@109: } olaf@109: } universe@130: if (parser->error) { universe@130: return parser->error; olaf@109: } else { olaf@109: return 0; olaf@109: } olaf@109: } olaf@109: universe@152: // buffer size is documented - change doc, when you change bufsize! universe@152: #define UCX_PROPLOAD_BUFSIZE 1024 olaf@109: int ucx_properties_load(UcxMap *map, FILE *file) { olaf@110: UcxProperties *parser = ucx_properties_new(); universe@130: if(!(parser && map && file)) { olaf@109: return 1; olaf@109: } olaf@109: olaf@109: int error = 0; olaf@109: size_t r; universe@152: char buf[UCX_PROPLOAD_BUFSIZE]; universe@152: while((r = fread(buf, 1, UCX_PROPLOAD_BUFSIZE, file)) != 0) { olaf@110: ucx_properties_fill(parser, buf, r); universe@130: error = ucx_properties2map(parser, map); universe@130: if (error) { olaf@109: break; olaf@109: } olaf@109: } olaf@110: ucx_properties_free(parser); olaf@109: return error; olaf@109: } olaf@109: olaf@109: int ucx_properties_store(UcxMap *map, FILE *file) { olaf@109: UcxMapIterator iter = ucx_map_iterator(map); universe@152: void *v; olaf@112: sstr_t value; olaf@109: size_t written; olaf@109: olaf@111: UCX_MAP_FOREACH(k, v, iter) { universe@152: value = sstr((char*)v); olaf@109: olaf@109: written = 0; olaf@112: written += fwrite(k.data, 1, k.len, file); olaf@109: written += fwrite(" = ", 1, 3, file); olaf@109: written += fwrite(value.ptr, 1, value.length, file); olaf@109: written += fwrite("\n", 1, 1, file); olaf@109: universe@130: if (written != k.len + value.length + 4) { universe@130: return 1; universe@130: } olaf@109: } olaf@109: olaf@109: return 0; olaf@109: } olaf@109: