Fri, 16 Aug 2013 13:40:10 +0200
fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
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@146 | 59 | |
universe@133 | 60 | /** |
universe@133 | 61 | * Length of the input buffer (don't set manually). |
universe@133 | 62 | * Automatically set by calls to ucx_properties_fill(). |
universe@133 | 63 | */ |
olaf@108 | 64 | size_t buflen; |
universe@146 | 65 | |
universe@133 | 66 | /** |
universe@133 | 67 | * Current buffer position (don't set manually). |
universe@133 | 68 | * Used by ucx_properties_next(). |
universe@133 | 69 | */ |
olaf@108 | 70 | size_t pos; |
universe@146 | 71 | |
universe@133 | 72 | /** |
universe@133 | 73 | * Internal temporary buffer (don't set manually). |
universe@133 | 74 | * Used by ucx_properties_next(). |
universe@133 | 75 | */ |
olaf@108 | 76 | char *tmp; |
universe@146 | 77 | |
universe@133 | 78 | /** |
universe@133 | 79 | * Internal temporary buffer length (don't set manually). |
universe@133 | 80 | * Used by ucx_properties_next(). |
universe@133 | 81 | */ |
olaf@108 | 82 | size_t tmplen; |
universe@146 | 83 | |
universe@133 | 84 | /** |
universe@133 | 85 | * Internal temporary buffer capacity (don't set manually). |
universe@133 | 86 | * Used by ucx_properties_next(). |
universe@133 | 87 | */ |
olaf@108 | 88 | size_t tmpcap; |
universe@146 | 89 | |
universe@133 | 90 | /** |
universe@133 | 91 | * Parser error code. |
universe@133 | 92 | * This is always 0 on success and a nonzero value on syntax errors. |
universe@133 | 93 | * The value is set by ucx_properties_next(). |
universe@133 | 94 | */ |
olaf@109 | 95 | int error; |
universe@146 | 96 | |
universe@133 | 97 | /** |
universe@133 | 98 | * The delimiter that shall be used. |
universe@133 | 99 | * This is '=' by default. |
universe@133 | 100 | */ |
olaf@108 | 101 | char delimiter; |
universe@146 | 102 | |
universe@133 | 103 | /** |
universe@133 | 104 | * The first comment character. |
universe@133 | 105 | * This is '#' by default. |
universe@133 | 106 | */ |
olaf@108 | 107 | char comment1; |
universe@146 | 108 | |
universe@133 | 109 | /** |
universe@133 | 110 | * The second comment character. |
universe@133 | 111 | * This is not set by default. |
universe@133 | 112 | */ |
olaf@108 | 113 | char comment2; |
universe@146 | 114 | |
universe@133 | 115 | /** |
universe@133 | 116 | * The third comment character. |
universe@133 | 117 | * This is not set by default. |
universe@133 | 118 | */ |
olaf@108 | 119 | char comment3; |
olaf@110 | 120 | } UcxProperties; |
olaf@108 | 121 | |
olaf@108 | 122 | |
universe@130 | 123 | /** |
universe@130 | 124 | * Constructs a new UcxProperties object. |
universe@133 | 125 | * @return a pointer to the new UcxProperties object |
universe@130 | 126 | */ |
olaf@110 | 127 | UcxProperties *ucx_properties_new(); |
universe@146 | 128 | |
universe@130 | 129 | /** |
universe@130 | 130 | * Destroys an UcxProperties object. |
universe@133 | 131 | * @param prop the UcxProperties object to destroy |
universe@130 | 132 | */ |
universe@130 | 133 | void ucx_properties_free(UcxProperties *prop); |
universe@146 | 134 | |
universe@130 | 135 | /** |
universe@133 | 136 | * Sets the input buffer for the properties parser. |
universe@133 | 137 | * |
universe@133 | 138 | * After calling this function, you may parse the data by calling |
universe@133 | 139 | * ucx_properties_next() until it returns 0. The function ucx_properties2map() |
universe@133 | 140 | * is a convenience function that performs these successive calls of |
universe@133 | 141 | * ucx_properties_next() within a while loop and puts the properties to a map. |
universe@133 | 142 | * |
universe@133 | 143 | * |
universe@133 | 144 | * @param prop the UcxProperties object |
universe@133 | 145 | * @param buf a pointer to the new buffer |
universe@133 | 146 | * @param len the payload length of the buffer |
universe@133 | 147 | * @see ucx_properties_next() |
universe@133 | 148 | * @see ucx_properties2map() |
universe@130 | 149 | */ |
universe@130 | 150 | void ucx_properties_fill(UcxProperties *prop, char *buf, size_t len); |
universe@146 | 151 | |
universe@133 | 152 | /** |
universe@133 | 153 | * Retrieves the next key/value-pair. |
universe@133 | 154 | * |
universe@133 | 155 | * This function returns a nonzero value as long as there are key/value-pairs |
universe@133 | 156 | * found. If no more key/value-pairs are found, you may refill the input buffer |
universe@133 | 157 | * with ucx_properties_fill(). |
universe@133 | 158 | * |
universe@133 | 159 | * <b>Attention:</b> the sstr_t.ptr pointers of the output parameters point to |
universe@133 | 160 | * memory within the input buffer of the parser and will get invalid some time. |
universe@133 | 161 | * If you want long term copies of the key/value-pairs, use sstrdup() after |
universe@133 | 162 | * calling this function. |
universe@133 | 163 | * |
universe@133 | 164 | * @param prop the UcxProperties object |
universe@133 | 165 | * @param name a pointer to the sstr_t that shall contain the property name |
universe@133 | 166 | * @param value a pointer to the sstr_t that shall contain the property value |
universe@133 | 167 | * @return Nonzero, if a key/value-pair was successfully retrieved |
universe@133 | 168 | * @see ucx_properties_fill() |
universe@133 | 169 | */ |
universe@130 | 170 | int ucx_properties_next(UcxProperties *prop, sstr_t *name, sstr_t *value); |
universe@146 | 171 | |
universe@133 | 172 | /** |
universe@133 | 173 | * Retrieves all available key/value-pairs and puts them into an UcxMap. |
universe@133 | 174 | * |
universe@133 | 175 | * This is done by successive calls to ucx_properties_next() until no more |
universe@133 | 176 | * key/value-pairs can be retrieved. |
universe@133 | 177 | * |
universe@133 | 178 | * @param prop the UcxProperties object |
universe@133 | 179 | * @param map the target map |
universe@133 | 180 | * @return The UcxProperties.error code (i.e. 0 on success). |
universe@133 | 181 | * @see ucx_properties_fill() |
universe@133 | 182 | */ |
universe@130 | 183 | int ucx_properties2map(UcxProperties *prop, UcxMap *map); |
olaf@109 | 184 | |
universe@130 | 185 | /** |
universe@130 | 186 | * Loads a properties file to an UcxMap. |
universe@130 | 187 | * |
universe@130 | 188 | * This is a convenience function that reads chunks of 1 KB from an input |
universe@130 | 189 | * stream until the end of the stream is reached. |
universe@130 | 190 | * |
universe@130 | 191 | * An UcxProperties object is implicitly created and destroyed. |
universe@130 | 192 | * |
universe@133 | 193 | * @param map the map object to write the key/value-pairs to |
universe@133 | 194 | * @param file the <code>FILE*</code> stream to read from |
universe@130 | 195 | * @return 0 on success, or a non-zero value on error |
universe@130 | 196 | * |
universe@130 | 197 | * @see ucx_properties_fill() |
universe@130 | 198 | * @see ucx_properties2map() |
universe@130 | 199 | */ |
olaf@109 | 200 | int ucx_properties_load(UcxMap *map, FILE *file); |
universe@146 | 201 | |
universe@130 | 202 | /** |
universe@130 | 203 | * Stores an UcxMap to a file. |
universe@130 | 204 | * |
universe@130 | 205 | * The key/value-pairs are written by using the following format: |
universe@130 | 206 | * |
universe@130 | 207 | * <code>[key] = [value]\\n</code> |
universe@130 | 208 | * |
universe@133 | 209 | * @param map the map to store |
universe@133 | 210 | * @param file the <code>FILE*</code> stream to write to |
universe@130 | 211 | * @return 0 on success, or a non-zero value on error |
universe@130 | 212 | */ |
olaf@109 | 213 | int ucx_properties_store(UcxMap *map, FILE *file); |
olaf@108 | 214 | |
olaf@108 | 215 | #ifdef __cplusplus |
olaf@108 | 216 | } |
olaf@108 | 217 | #endif |
olaf@108 | 218 | |
olaf@120 | 219 | #endif /* UCX_PROPERTIES_H */ |
olaf@108 | 220 |