ui/common/ucx_properties.h

Thu, 14 Nov 2024 17:25:23 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 14 Nov 2024 17:25:23 +0100
changeset 83
a612adaee43d
parent 0
2483f517c562
permissions
-rw-r--r--

fix pathbar elmfunc creating an element for an empty string

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
/**
 * @file properties.h
 * 
 * Load / store utilities for properties files.
 * 
 * @author Mike Becker
 * @author Olaf Wintermann
 */

#ifndef UCX_PROPERTIES_H
#define	UCX_PROPERTIES_H

#include <cx/hash_map.h>
#include <cx/string.h>

#include <stdio.h>

#ifdef	__cplusplus
extern "C" {
#endif

/**
 * UcxProperties object for parsing properties data.
 * Most of the fields are for internal use only. You may configure the
 * properties parser, e.g. by changing the used delimiter or specifying 
 * up to three different characters that shall introduce comments.
 */
typedef struct {
    /**
     * Input buffer (don't set manually).
     * Automatically set by calls to ucx_properties_fill().
     */
    char   *buffer;
    
    /**
     * Length of the input buffer (don't set manually).
     * Automatically set by calls to ucx_properties_fill().
     */
    size_t buflen;
    
    /**
     * Current buffer position (don't set manually).
     * Used by ucx_properties_next().
     */
    size_t pos;
    
    /**
     * Internal temporary buffer (don't set manually).
     * Used by ucx_properties_next().
     */
    char   *tmp;
    
    /**
     * Internal temporary buffer length (don't set manually).
     * Used by ucx_properties_next().
     */
    size_t tmplen;
    
    /**
     * Internal temporary buffer capacity (don't set manually).
     * Used by ucx_properties_next().
     */
    size_t tmpcap;
    
    /**
     * Parser error code.
     * This is always 0 on success and a nonzero value on syntax errors.
     * The value is set by ucx_properties_next().
     */
    int    error;
    
    /**
     * The delimiter that shall be used.
     * This is '=' by default.
     */
    char   delimiter;
    
    /**
     * The first comment character.
     * This is '#' by default.
     */
    char   comment1;
    
    /**
     * The second comment character.
     * This is not set by default.
     */
    char   comment2;
    
    /**
     * The third comment character.
     * This is not set by default.
     */
    char   comment3;
} UcxProperties;


/**
 * Constructs a new UcxProperties object.
 * @return a pointer to the new UcxProperties object
 */
UcxProperties *ucx_properties_new();

/**
 * Destroys a UcxProperties object.
 * @param prop the UcxProperties object to destroy
 */
void ucx_properties_free(UcxProperties *prop);

/**
 * Sets the input buffer for the properties parser.
 * 
 * After calling this function, you may parse the data by calling
 * ucx_properties_next() until it returns 0. The function ucx_properties2map()
 * is a convenience function that reads as much data as possible by using this
 * function.
 * 
 * 
 * @param prop the UcxProperties object
 * @param buf a pointer to the new buffer
 * @param len the payload length of the buffer
 * @see ucx_properties_next()
 * @see ucx_properties2map()
 */
void ucx_properties_fill(UcxProperties *prop, char *buf, size_t len);

/**
 * Retrieves the next key/value-pair.
 * 
 * This function returns a nonzero value as long as there are key/value-pairs
 * found. If no more key/value-pairs are found, you may refill the input buffer
 * with ucx_properties_fill().
 * 
 * <b>Attention:</b> the cxstring.ptr pointers of the output parameters point to
 * memory within the input buffer of the parser and will get invalid some time.
 * If you want long term copies of the key/value-pairs, use sstrdup() after
 * calling this function.
 * 
 * @param prop the UcxProperties object
 * @param name a pointer to the cxstring that shall contain the property name
 * @param value a pointer to the cxstring that shall contain the property value
 * @return Nonzero, if a key/value-pair was successfully retrieved
 * @see ucx_properties_fill()
 */
int ucx_properties_next(UcxProperties *prop, cxstring *name, cxstring *value);

/**
 * Retrieves all available key/value-pairs and puts them into a UcxMap.
 * 
 * This is done by successive calls to ucx_properties_next() until no more
 * key/value-pairs can be retrieved.
 * 
 * The memory for the map values is allocated by the map's own allocator.
 * 
 * @param prop the UcxProperties object
 * @param map the target map
 * @return The UcxProperties.error code (i.e. 0 on success).
 * @see ucx_properties_fill()
 * @see UcxMap.allocator
 */
int ucx_properties2map(UcxProperties *prop, CxMap *map);

/**
 * Loads a properties file to a UcxMap.
 * 
 * This is a convenience function that reads data from an input
 * stream until the end of the stream is reached.
 * 
 * @param map the map object to write the key/value-pairs to
 * @param file the <code>FILE*</code> stream to read from
 * @return 0 on success, or a non-zero value on error
 * 
 * @see ucx_properties_fill()
 * @see ucx_properties2map()
 */
int ucx_properties_load(CxMap *map, FILE *file);

/**
 * Stores a UcxMap to a file.
 * 
 * The key/value-pairs are written by using the following format:
 * 
 * <code>[key] = [value]\\n</code>
 * 
 * @param map the map to store
 * @param file the <code>FILE*</code> stream to write to
 * @return 0 on success, or a non-zero value on error
 */
int ucx_properties_store(CxMap *map, FILE *file);

#ifdef	__cplusplus
}
#endif

#endif	/* UCX_PROPERTIES_H */

mercurial