2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
31 * Load / store utilities for properties files.
34 * @author Olaf Wintermann
37 #ifndef UCX_PROPERTIES_H
38 #define UCX_PROPERTIES_H
48 * UcxProperties object for parsing properties data.
49 * Most of the fields are for internal use only. You may configure the
50 * properties parser, e.g. by changing the used delimiter or specifying
51 * up to three different characters that shall introduce comments.
55 * Input buffer (don't set manually).
56 * Automatically set by calls to ucx_properties_fill().
61 * Length of the input buffer (don't set manually).
62 * Automatically set by calls to ucx_properties_fill().
67 * Current buffer position (don't set manually).
68 * Used by ucx_properties_next().
73 * Internal temporary buffer (don't set manually).
74 * Used by ucx_properties_next().
79 * Internal temporary buffer length (don't set manually).
80 * Used by ucx_properties_next().
85 * Internal temporary buffer capacity (don't set manually).
86 * Used by ucx_properties_next().
92 * This is always 0 on success and a nonzero value on syntax errors.
93 * The value is set by ucx_properties_next().
98 * The delimiter that shall be used.
99 * This is '=' by default.
104 * The first comment character.
105 * This is '#' by default.
110 * The second comment character.
111 * This is not set by default.
116 * The third comment character.
117 * This is not set by default.
124 * Constructs a new UcxProperties object.
125 * @return a pointer to the new UcxProperties object
127 UcxProperties *ucx_properties_new();
130 * Destroys a UcxProperties object.
131 * @param prop the UcxProperties object to destroy
133 void ucx_properties_free(UcxProperties *prop);
136 * Sets the input buffer for the properties parser.
138 * After calling this function, you may parse the data by calling
139 * ucx_properties_next() until it returns 0. The function ucx_properties2map()
140 * is a convenience function that reads as much data as possible by using this
144 * @param prop the UcxProperties object
145 * @param buf a pointer to the new buffer
146 * @param len the payload length of the buffer
147 * @see ucx_properties_next()
148 * @see ucx_properties2map()
150 void ucx_properties_fill(UcxProperties *prop, char *buf, size_t len);
153 * Retrieves the next key/value-pair.
155 * This function returns a nonzero value as long as there are key/value-pairs
156 * found. If no more key/value-pairs are found, you may refill the input buffer
157 * with ucx_properties_fill().
159 * <b>Attention:</b> the sstr_t.ptr pointers of the output parameters point to
160 * memory within the input buffer of the parser and will get invalid some time.
161 * If you want long term copies of the key/value-pairs, use sstrdup() after
162 * calling this function.
164 * @param prop the UcxProperties object
165 * @param name a pointer to the sstr_t that shall contain the property name
166 * @param value a pointer to the sstr_t that shall contain the property value
167 * @return Nonzero, if a key/value-pair was successfully retrieved
168 * @see ucx_properties_fill()
170 int ucx_properties_next(UcxProperties *prop, sstr_t *name, sstr_t *value);
173 * Retrieves all available key/value-pairs and puts them into a UcxMap.
175 * This is done by successive calls to ucx_properties_next() until no more
176 * key/value-pairs can be retrieved.
178 * The memory for the map values is allocated by the map's own allocator.
180 * @param prop the UcxProperties object
181 * @param map the target map
182 * @return The UcxProperties.error code (i.e. 0 on success).
183 * @see ucx_properties_fill()
184 * @see UcxMap.allocator
186 int ucx_properties2map(UcxProperties *prop, UcxMap *map);
189 * Loads a properties file to a UcxMap.
191 * This is a convenience function that reads data from an input
192 * stream until the end of the stream is reached.
194 * @param map the map object to write the key/value-pairs to
195 * @param file the <code>FILE*</code> stream to read from
196 * @return 0 on success, or a non-zero value on error
198 * @see ucx_properties_fill()
199 * @see ucx_properties2map()
201 int ucx_properties_load(UcxMap *map, FILE *file);
204 * Stores a UcxMap to a file.
206 * The key/value-pairs are written by using the following format:
208 * <code>[key] = [value]\\n</code>
210 * @param map the map to store
211 * @param file the <code>FILE*</code> stream to write to
212 * @return 0 on success, or a non-zero value on error
214 int ucx_properties_store(UcxMap *map, FILE *file);
220 #endif /* UCX_PROPERTIES_H */