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: */ universe@130: /** universe@130: * @file properties.h universe@130: * universe@130: * Load / store utilities for properties files. universe@130: * universe@130: * @author Mike Becker universe@130: * @author Olaf Wintermann universe@130: */ olaf@108: olaf@120: #ifndef UCX_PROPERTIES_H olaf@120: #define UCX_PROPERTIES_H olaf@108: universe@259: #include "ucx.h" universe@259: #include "map.h" olaf@108: olaf@108: #ifdef __cplusplus olaf@108: extern "C" { olaf@108: #endif olaf@108: universe@133: /** universe@133: * UcxProperties object for parsing properties data. universe@133: * Most of the fields are for internal use only. You may configure the universe@133: * properties parser, e.g. by changing the used delimiter or specifying universe@133: * up to three different characters that shall introduce comments. universe@133: */ olaf@108: typedef struct { universe@133: /** universe@133: * Input buffer (don't set manually). universe@133: * Automatically set by calls to ucx_properties_fill(). universe@133: */ olaf@108: char *buffer; universe@146: universe@133: /** universe@133: * Length of the input buffer (don't set manually). universe@133: * Automatically set by calls to ucx_properties_fill(). universe@133: */ olaf@108: size_t buflen; universe@146: universe@133: /** universe@133: * Current buffer position (don't set manually). universe@133: * Used by ucx_properties_next(). universe@133: */ olaf@108: size_t pos; universe@146: universe@133: /** universe@133: * Internal temporary buffer (don't set manually). universe@133: * Used by ucx_properties_next(). universe@133: */ olaf@108: char *tmp; universe@146: universe@133: /** universe@133: * Internal temporary buffer length (don't set manually). universe@133: * Used by ucx_properties_next(). universe@133: */ olaf@108: size_t tmplen; universe@146: universe@133: /** universe@133: * Internal temporary buffer capacity (don't set manually). universe@133: * Used by ucx_properties_next(). universe@133: */ olaf@108: size_t tmpcap; universe@146: universe@133: /** universe@133: * Parser error code. universe@133: * This is always 0 on success and a nonzero value on syntax errors. universe@133: * The value is set by ucx_properties_next(). universe@133: */ olaf@109: int error; universe@146: universe@133: /** universe@133: * The delimiter that shall be used. universe@133: * This is '=' by default. universe@133: */ olaf@108: char delimiter; universe@146: universe@133: /** universe@133: * The first comment character. universe@133: * This is '#' by default. universe@133: */ olaf@108: char comment1; universe@146: universe@133: /** universe@133: * The second comment character. universe@133: * This is not set by default. universe@133: */ olaf@108: char comment2; universe@146: universe@133: /** universe@133: * The third comment character. universe@133: * This is not set by default. universe@133: */ olaf@108: char comment3; olaf@110: } UcxProperties; olaf@108: olaf@108: universe@130: /** universe@130: * Constructs a new UcxProperties object. universe@133: * @return a pointer to the new UcxProperties object universe@130: */ olaf@110: UcxProperties *ucx_properties_new(); universe@146: universe@130: /** universe@225: * Destroys a UcxProperties object. universe@133: * @param prop the UcxProperties object to destroy universe@130: */ universe@130: void ucx_properties_free(UcxProperties *prop); universe@146: universe@130: /** universe@133: * Sets the input buffer for the properties parser. universe@133: * universe@133: * After calling this function, you may parse the data by calling universe@133: * ucx_properties_next() until it returns 0. The function ucx_properties2map() universe@215: * is a convenience function that reads as much data as possible by using this universe@215: * function. universe@133: * universe@133: * universe@133: * @param prop the UcxProperties object universe@133: * @param buf a pointer to the new buffer universe@133: * @param len the payload length of the buffer universe@133: * @see ucx_properties_next() universe@133: * @see ucx_properties2map() universe@130: */ universe@130: void ucx_properties_fill(UcxProperties *prop, char *buf, size_t len); universe@146: universe@133: /** universe@133: * Retrieves the next key/value-pair. universe@133: * universe@133: * This function returns a nonzero value as long as there are key/value-pairs universe@133: * found. If no more key/value-pairs are found, you may refill the input buffer universe@133: * with ucx_properties_fill(). universe@133: * universe@133: * Attention: the sstr_t.ptr pointers of the output parameters point to universe@133: * memory within the input buffer of the parser and will get invalid some time. universe@133: * If you want long term copies of the key/value-pairs, use sstrdup() after universe@133: * calling this function. universe@133: * universe@133: * @param prop the UcxProperties object universe@133: * @param name a pointer to the sstr_t that shall contain the property name universe@133: * @param value a pointer to the sstr_t that shall contain the property value universe@133: * @return Nonzero, if a key/value-pair was successfully retrieved universe@133: * @see ucx_properties_fill() universe@133: */ universe@130: int ucx_properties_next(UcxProperties *prop, sstr_t *name, sstr_t *value); universe@146: universe@133: /** universe@225: * Retrieves all available key/value-pairs and puts them into a UcxMap. universe@133: * universe@133: * This is done by successive calls to ucx_properties_next() until no more universe@133: * key/value-pairs can be retrieved. universe@133: * universe@133: * @param prop the UcxProperties object universe@133: * @param map the target map universe@133: * @return The UcxProperties.error code (i.e. 0 on success). universe@133: * @see ucx_properties_fill() universe@133: */ universe@130: int ucx_properties2map(UcxProperties *prop, UcxMap *map); olaf@109: universe@130: /** universe@225: * Loads a properties file to a UcxMap. universe@130: * universe@215: * This is a convenience function that reads data from an input universe@130: * stream until the end of the stream is reached. universe@130: * universe@133: * @param map the map object to write the key/value-pairs to universe@133: * @param file the FILE* stream to read from universe@130: * @return 0 on success, or a non-zero value on error universe@130: * universe@130: * @see ucx_properties_fill() universe@130: * @see ucx_properties2map() universe@130: */ olaf@109: int ucx_properties_load(UcxMap *map, FILE *file); universe@146: universe@130: /** universe@225: * Stores a UcxMap to a file. universe@130: * universe@130: * The key/value-pairs are written by using the following format: universe@130: * universe@130: * [key] = [value]\\n universe@130: * universe@133: * @param map the map to store universe@133: * @param file the FILE* stream to write to universe@130: * @return 0 on success, or a non-zero value on error universe@130: */ olaf@109: int ucx_properties_store(UcxMap *map, FILE *file); olaf@108: olaf@108: #ifdef __cplusplus olaf@108: } olaf@108: #endif olaf@108: olaf@120: #endif /* UCX_PROPERTIES_H */ olaf@108: