ucx/properties.h

Fri, 09 Aug 2013 10:24:02 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 09 Aug 2013 10:24:02 +0200
changeset 133
0a70e0d36949
parent 130
633f15ce2ee4
child 146
aa376dba1ba8
permissions
-rw-r--r--

finished documentation of UcxProperties

olaf@108 1 /*
olaf@108 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
olaf@108 3 *
olaf@108 4 * Copyright 2013 Olaf Wintermann. All rights reserved.
olaf@108 5 *
olaf@108 6 * Redistribution and use in source and binary forms, with or without
olaf@108 7 * modification, are permitted provided that the following conditions are met:
olaf@108 8 *
olaf@108 9 * 1. Redistributions of source code must retain the above copyright
olaf@108 10 * notice, this list of conditions and the following disclaimer.
olaf@108 11 *
olaf@108 12 * 2. Redistributions in binary form must reproduce the above copyright
olaf@108 13 * notice, this list of conditions and the following disclaimer in the
olaf@108 14 * documentation and/or other materials provided with the distribution.
olaf@108 15 *
olaf@108 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
olaf@108 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
olaf@108 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
olaf@108 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
olaf@108 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
olaf@108 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
olaf@108 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
olaf@108 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
olaf@108 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
olaf@108 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
olaf@108 26 * POSSIBILITY OF SUCH DAMAGE.
olaf@108 27 */
universe@130 28 /**
universe@130 29 * @file properties.h
universe@130 30 *
universe@130 31 * Load / store utilities for properties files.
universe@130 32 *
universe@130 33 * @author Mike Becker
universe@130 34 * @author Olaf Wintermann
universe@130 35 */
olaf@108 36
olaf@120 37 #ifndef UCX_PROPERTIES_H
olaf@120 38 #define UCX_PROPERTIES_H
olaf@108 39
olaf@108 40 #include "ucx.h"
olaf@108 41 #include "map.h"
olaf@108 42
olaf@108 43 #ifdef __cplusplus
olaf@108 44 extern "C" {
olaf@108 45 #endif
olaf@108 46
universe@133 47 /**
universe@133 48 * UcxProperties object for parsing properties data.
universe@133 49 * Most of the fields are for internal use only. You may configure the
universe@133 50 * properties parser, e.g. by changing the used delimiter or specifying
universe@133 51 * up to three different characters that shall introduce comments.
universe@133 52 */
olaf@108 53 typedef struct {
universe@133 54 /**
universe@133 55 * Input buffer (don't set manually).
universe@133 56 * Automatically set by calls to ucx_properties_fill().
universe@133 57 */
olaf@108 58 char *buffer;
universe@133 59 /**
universe@133 60 * Length of the input buffer (don't set manually).
universe@133 61 * Automatically set by calls to ucx_properties_fill().
universe@133 62 */
olaf@108 63 size_t buflen;
universe@133 64 /**
universe@133 65 * Current buffer position (don't set manually).
universe@133 66 * Used by ucx_properties_next().
universe@133 67 */
olaf@108 68 size_t pos;
universe@133 69 /**
universe@133 70 * Internal temporary buffer (don't set manually).
universe@133 71 * Used by ucx_properties_next().
universe@133 72 */
olaf@108 73 char *tmp;
universe@133 74 /**
universe@133 75 * Internal temporary buffer length (don't set manually).
universe@133 76 * Used by ucx_properties_next().
universe@133 77 */
olaf@108 78 size_t tmplen;
universe@133 79 /**
universe@133 80 * Internal temporary buffer capacity (don't set manually).
universe@133 81 * Used by ucx_properties_next().
universe@133 82 */
olaf@108 83 size_t tmpcap;
universe@133 84 /**
universe@133 85 * Parser error code.
universe@133 86 * This is always 0 on success and a nonzero value on syntax errors.
universe@133 87 * The value is set by ucx_properties_next().
universe@133 88 */
olaf@109 89 int error;
universe@133 90 /**
universe@133 91 * The delimiter that shall be used.
universe@133 92 * This is '=' by default.
universe@133 93 */
olaf@108 94 char delimiter;
universe@133 95 /**
universe@133 96 * The first comment character.
universe@133 97 * This is '#' by default.
universe@133 98 */
olaf@108 99 char comment1;
universe@133 100 /**
universe@133 101 * The second comment character.
universe@133 102 * This is not set by default.
universe@133 103 */
olaf@108 104 char comment2;
universe@133 105 /**
universe@133 106 * The third comment character.
universe@133 107 * This is not set by default.
universe@133 108 */
olaf@108 109 char comment3;
olaf@110 110 } UcxProperties;
olaf@108 111
olaf@108 112
universe@130 113 /**
universe@130 114 * Constructs a new UcxProperties object.
universe@133 115 * @return a pointer to the new UcxProperties object
universe@130 116 */
olaf@110 117 UcxProperties *ucx_properties_new();
universe@130 118 /**
universe@130 119 * Destroys an UcxProperties object.
universe@133 120 * @param prop the UcxProperties object to destroy
universe@130 121 */
universe@130 122 void ucx_properties_free(UcxProperties *prop);
universe@130 123 /**
universe@133 124 * Sets the input buffer for the properties parser.
universe@133 125 *
universe@133 126 * After calling this function, you may parse the data by calling
universe@133 127 * ucx_properties_next() until it returns 0. The function ucx_properties2map()
universe@133 128 * is a convenience function that performs these successive calls of
universe@133 129 * ucx_properties_next() within a while loop and puts the properties to a map.
universe@133 130 *
universe@133 131 *
universe@133 132 * @param prop the UcxProperties object
universe@133 133 * @param buf a pointer to the new buffer
universe@133 134 * @param len the payload length of the buffer
universe@133 135 * @see ucx_properties_next()
universe@133 136 * @see ucx_properties2map()
universe@130 137 */
universe@130 138 void ucx_properties_fill(UcxProperties *prop, char *buf, size_t len);
universe@133 139 /**
universe@133 140 * Retrieves the next key/value-pair.
universe@133 141 *
universe@133 142 * This function returns a nonzero value as long as there are key/value-pairs
universe@133 143 * found. If no more key/value-pairs are found, you may refill the input buffer
universe@133 144 * with ucx_properties_fill().
universe@133 145 *
universe@133 146 * <b>Attention:</b> the sstr_t.ptr pointers of the output parameters point to
universe@133 147 * memory within the input buffer of the parser and will get invalid some time.
universe@133 148 * If you want long term copies of the key/value-pairs, use sstrdup() after
universe@133 149 * calling this function.
universe@133 150 *
universe@133 151 * @param prop the UcxProperties object
universe@133 152 * @param name a pointer to the sstr_t that shall contain the property name
universe@133 153 * @param value a pointer to the sstr_t that shall contain the property value
universe@133 154 * @return Nonzero, if a key/value-pair was successfully retrieved
universe@133 155 * @see ucx_properties_fill()
universe@133 156 */
universe@130 157 int ucx_properties_next(UcxProperties *prop, sstr_t *name, sstr_t *value);
universe@133 158 /**
universe@133 159 * Retrieves all available key/value-pairs and puts them into an UcxMap.
universe@133 160 *
universe@133 161 * This is done by successive calls to ucx_properties_next() until no more
universe@133 162 * key/value-pairs can be retrieved.
universe@133 163 *
universe@133 164 * @param prop the UcxProperties object
universe@133 165 * @param map the target map
universe@133 166 * @return The UcxProperties.error code (i.e. 0 on success).
universe@133 167 * @see ucx_properties_fill()
universe@133 168 */
universe@130 169 int ucx_properties2map(UcxProperties *prop, UcxMap *map);
olaf@109 170
universe@130 171 /**
universe@130 172 * Loads a properties file to an UcxMap.
universe@130 173 *
universe@130 174 * This is a convenience function that reads chunks of 1 KB from an input
universe@130 175 * stream until the end of the stream is reached.
universe@130 176 *
universe@130 177 * An UcxProperties object is implicitly created and destroyed.
universe@130 178 *
universe@133 179 * @param map the map object to write the key/value-pairs to
universe@133 180 * @param file the <code>FILE*</code> stream to read from
universe@130 181 * @return 0 on success, or a non-zero value on error
universe@130 182 *
universe@130 183 * @see ucx_properties_fill()
universe@130 184 * @see ucx_properties2map()
universe@130 185 */
olaf@109 186 int ucx_properties_load(UcxMap *map, FILE *file);
universe@130 187 /**
universe@130 188 * Stores an UcxMap to a file.
universe@130 189 *
universe@130 190 * The key/value-pairs are written by using the following format:
universe@130 191 *
universe@130 192 * <code>[key] = [value]\\n</code>
universe@130 193 *
universe@133 194 * @param map the map to store
universe@133 195 * @param file the <code>FILE*</code> stream to write to
universe@130 196 * @return 0 on success, or a non-zero value on error
universe@130 197 */
olaf@109 198 int ucx_properties_store(UcxMap *map, FILE *file);
olaf@108 199
olaf@108 200 #ifdef __cplusplus
olaf@108 201 }
olaf@108 202 #endif
olaf@108 203
olaf@120 204 #endif /* UCX_PROPERTIES_H */
olaf@108 205

mercurial