ucx/ucx/properties.h

changeset 157
0b33b9396851
equal deleted inserted replaced
156:62f1a55535e7 157:0b33b9396851
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
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.
15 *
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.
27 */
28 /**
29 * @file properties.h
30 *
31 * Load / store utilities for properties files.
32 *
33 * @author Mike Becker
34 * @author Olaf Wintermann
35 */
36
37 #ifndef UCX_PROPERTIES_H
38 #define UCX_PROPERTIES_H
39
40 #include "ucx.h"
41 #include "map.h"
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47 /**
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.
52 */
53 typedef struct {
54 /**
55 * Input buffer (don't set manually).
56 * Automatically set by calls to ucx_properties_fill().
57 */
58 char *buffer;
59
60 /**
61 * Length of the input buffer (don't set manually).
62 * Automatically set by calls to ucx_properties_fill().
63 */
64 size_t buflen;
65
66 /**
67 * Current buffer position (don't set manually).
68 * Used by ucx_properties_next().
69 */
70 size_t pos;
71
72 /**
73 * Internal temporary buffer (don't set manually).
74 * Used by ucx_properties_next().
75 */
76 char *tmp;
77
78 /**
79 * Internal temporary buffer length (don't set manually).
80 * Used by ucx_properties_next().
81 */
82 size_t tmplen;
83
84 /**
85 * Internal temporary buffer capacity (don't set manually).
86 * Used by ucx_properties_next().
87 */
88 size_t tmpcap;
89
90 /**
91 * Parser error code.
92 * This is always 0 on success and a nonzero value on syntax errors.
93 * The value is set by ucx_properties_next().
94 */
95 int error;
96
97 /**
98 * The delimiter that shall be used.
99 * This is '=' by default.
100 */
101 char delimiter;
102
103 /**
104 * The first comment character.
105 * This is '#' by default.
106 */
107 char comment1;
108
109 /**
110 * The second comment character.
111 * This is not set by default.
112 */
113 char comment2;
114
115 /**
116 * The third comment character.
117 * This is not set by default.
118 */
119 char comment3;
120 } UcxProperties;
121
122
123 /**
124 * Constructs a new UcxProperties object.
125 * @return a pointer to the new UcxProperties object
126 */
127 UcxProperties *ucx_properties_new();
128
129 /**
130 * Destroys a UcxProperties object.
131 * @param prop the UcxProperties object to destroy
132 */
133 void ucx_properties_free(UcxProperties *prop);
134
135 /**
136 * Sets the input buffer for the properties parser.
137 *
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
141 * function.
142 *
143 *
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()
149 */
150 void ucx_properties_fill(UcxProperties *prop, char *buf, size_t len);
151
152 /**
153 * Retrieves the next key/value-pair.
154 *
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().
158 *
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.
163 *
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()
169 */
170 int ucx_properties_next(UcxProperties *prop, sstr_t *name, sstr_t *value);
171
172 /**
173 * Retrieves all available key/value-pairs and puts them into a UcxMap.
174 *
175 * This is done by successive calls to ucx_properties_next() until no more
176 * key/value-pairs can be retrieved.
177 *
178 * The memory for the map values is allocated by the map's own allocator.
179 *
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
185 */
186 int ucx_properties2map(UcxProperties *prop, UcxMap *map);
187
188 /**
189 * Loads a properties file to a UcxMap.
190 *
191 * This is a convenience function that reads data from an input
192 * stream until the end of the stream is reached.
193 *
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
197 *
198 * @see ucx_properties_fill()
199 * @see ucx_properties2map()
200 */
201 int ucx_properties_load(UcxMap *map, FILE *file);
202
203 /**
204 * Stores a UcxMap to a file.
205 *
206 * The key/value-pairs are written by using the following format:
207 *
208 * <code>[key] = [value]\\n</code>
209 *
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
213 */
214 int ucx_properties_store(UcxMap *map, FILE *file);
215
216 #ifdef __cplusplus
217 }
218 #endif
219
220 #endif /* UCX_PROPERTIES_H */
221

mercurial