src/cx/properties.h

Sat, 12 Oct 2024 19:34:19 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 12 Oct 2024 19:34:19 +0200
changeset 924
3c90dfc35f06
parent 923
45da884269c8
permissions
-rw-r--r--

add implementation for the properties parser

relates to #429

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2024 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
 * \brief Interface for parsing data from properties files.
 * \author Mike Becker
 * \author Olaf Wintermann
 * \copyright 2-Clause BSD License
 */

#ifndef UCX_PROPERTIES
#define UCX_PROPERTIES

#include "common.h"
#include "string.h"
#include "array_list.h"

struct cx_properties_config_s {
    /**
     * The key/value delimiter that shall be used.
     * This is '=' by default.
     */
    char delimiter;

    /**
     * The character, when appearing at the end of a line, continues that line.
     * This is '\' by default.
     */
    // char continuation; // TODO: line continuation in properties

    /**
     * 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;
};

/**
 * Typedef for the properties config.
 */
typedef struct cx_properties_config_s CxPropertiesConfig;

/**
 * Default properties configuration.
 */
extern const CxPropertiesConfig cx_properties_config_default;

/**
 * Status codes for the properties interface.
 */
enum cx_properties_status {
    /**
     * Everything is fine.
     */
    CX_PROPERTIES_NO_ERROR,
    /**
     * The input buffer does not contain more data.
     */
    CX_PROPERTIES_NO_DATA,
    /**
     * The input ends unexpectedly.
     *
     * This either happens when the last line does not terminate with a line
     * break, or when the input ends with a parsed key but no value.
     */
    CX_PROPERTIES_INCOMPLETE_DATA,
    /**
     * Input buffer is \c NULL.
     */
    CX_PROPERTIES_NULL_INPUT,
    /**
     * The line contains a delimiter, but no key.
     */
    CX_PROPERTIES_INVALID_EMPTY_KEY,
    /**
     * The line contains data, but no delimiter.
     */
    CX_PROPERTIES_INVALID_MISSING_DELIMITER,
    /**
     * More internal buffer was needed, but could not be allocated.
     */
    CX_PROPERTIES_BUFFER_ALLOC_FAILED,
};

/**
 * Interface for working with properties data.
 */
struct cx_properties_s {
    /**
     * The configuration.
     */
    CxPropertiesConfig config;

    /**
     * The text buffer.
     */
    const char *text;

    /**
     * Size of the text buffer.
     */
    size_t text_size;

    /**
     * Position in the text buffer.
     */
    size_t text_pos;

    /**
     * Temporary internal buffer.
     */
    char *buf;

    /**
     * Size of the internal buffer.
     */
    size_t buf_size;

    /**
     * Capacity of the internal buffer.
     */
    size_t buf_capacity;

    /**
     * Internal flags.
     */
    int flags;
};

/**
 * Typedef for the properties interface.
 */
typedef struct cx_properties_s CxProperties;


/**
 * Initialize a properties interface.
 *
 * @param prop the properties interface
 * @param config the properties configuration
 * @see cxPropertiesInitDefault()
 */
__attribute__((__nonnull__))
void cxPropertiesInit(CxProperties *prop, CxPropertiesConfig config);

/**
 * Destroys the properties interface.
 *
 * \note Even when you are certain that you did not use the interface in a
 * way that caused a memory allocation, you should call this function anyway.
 * Future versions of the library might add features that need additional memory
 * and you really don't want to search the entire code where you might need
 * add call to this function.
 *
 * @param prop the properties interface
 */
__attribute__((__nonnull__))
void cxPropertiesDestroy(CxProperties *prop);

/**
 * Initialize a properties parser with the default configuration.
 *
 * @param prop the properties interface
 * @see cxPropertiesInit()
 */
#define cxPropertiesInitDefault(prop) \
    cxPropertiesInit(prop, cx_properties_config_default)

/**
 * Sets an input buffer.
 *
 * After calling this function, you can parse the data by calling
 * cxPropertiesNext() until the status is #CX_PROPERTIES_NO_DATA.
 *
 * @param prop the properties interface
 * @param buf a pointer to data
 * @param len the length of the data
 */
__attribute__((__nonnull__))
void cxPropertiesInput(
        CxProperties *prop,
        const char *buf,
        size_t len
);

/**
 * Sets a new input buffer after copying the current unprocessed data
 * to a temporary buffer.
 *
 * This temporary buffer is allocated on the heap, unless you specified
 * a buffer on the stack with #cxPropertiesUseStack().
 * In that case, the stack buffer is used, until the capacity is not sufficient
 * anymore.
 *
 * When this function is called without any unprocessed data that needs to be
 * copied, it behaves exactly as #cxPropertiesInput().
 *
 * @param prop the properties interface
 * @param buf a pointer to data
 * @param len the length of the data
 * @return non-zero when a memory allocation was necessary but failed
 */
__attribute__((__nonnull__))
int cxPropertiesFill(
        CxProperties *prop,
        const char *buf,
        size_t len
);

/**
 * Specifies stack memory that shall be used by #cxPropertiesFill().
 *
 * @param prop the properties interface
 * @param buf a pointer to stack memory
 * @param capacity the capacity of the stack memory
 */
void cxPropertiesUseStack(
        CxProperties *prop,
        char *buf,
        size_t capacity
);

/**
 * Retrieves the next key/value-pair.
 *
 * This function returns zero as long as there are key/value-pairs found.
 * If no more key/value-pairs are found, #CX_PROPERTIES_NO_DATA is returned.
 *
 * When an incomplete line is encountered, #CX_PROPERTIES_INCOMPLETE_DATA is
 * returned and the position of the input buffer will be the start of the
 * affected line. You can then add more data with #cxPropertiesFill().
 *
 * \attention The returned strings will point into a buffer that might not be
 * available later. It is strongly recommended to copy the strings for further
 * use.
 *
 * @param prop the properties interface
 * @param key 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 the status code as defined above
 * @see cxPropertiesFill()
 */
enum cx_properties_status cxPropertiesNext(
        CxProperties *prop,
        cxstring *key,
        cxstring *value
);

#endif // UCX_PROPERTIES

mercurial