ucx/properties.h

Tue, 24 Mar 2015 12:02:47 +0100

author
Mike Becker <universe@uap-core.de>
date
Tue, 24 Mar 2015 12:02:47 +0100
changeset 78
ca7f024dd0f9
parent 70
88092b88ec00
child 110
53895e9a4bbb
permissions
-rw-r--r--

described DavQLStatement grammar + added more features + renamed PUT to SET (for now)

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2014 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 "ucx.h"
#include "map.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 an 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 performs these successive calls of
 * ucx_properties_next() within a while loop and puts the properties to a map.
 * 
 * 
 * @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 sstr_t.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 sstr_t that shall contain the property name
 * @param value a pointer to the sstr_t 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, sstr_t *name, sstr_t *value);

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

/**
 * Loads a properties file to an UcxMap.
 * 
 * This is a convenience function that reads chunks of 1 KB from an input
 * stream until the end of the stream is reached.
 * 
 * An UcxProperties object is implicitly created and destroyed.
 * 
 * @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(UcxMap *map, FILE *file);

/**
 * Stores an 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(UcxMap *map, FILE *file);

#ifdef	__cplusplus
}
#endif

#endif	/* UCX_PROPERTIES_H */

mercurial