src/cx/properties.h

changeset 1675
36c0fb2b60b2
parent 1609
a767bf59cad3
equal deleted inserted replaced
1674:8b0f162ac88e 1675:36c0fb2b60b2
39 #include "common.h" 39 #include "common.h"
40 #include "string.h" 40 #include "string.h"
41 #include "map.h" 41 #include "map.h"
42 #include "buffer.h" 42 #include "buffer.h"
43 43
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47
48 /** 44 /**
49 * Configures the expected characters for the properties parser. 45 * Configures the expected characters for the properties parser.
50 */ 46 */
51 struct cx_properties_config_s { 47 struct cx_properties_config_s {
52 /** 48 /**
182 * 178 *
183 * @param prop the properties interface 179 * @param prop the properties interface
184 * @param config the properties configuration 180 * @param config the properties configuration
185 * @see cxPropertiesInitDefault() 181 * @see cxPropertiesInitDefault()
186 */ 182 */
187 cx_attr_nonnull 183 CX_EXTERN CX_NONNULL
188 CX_EXPORT void cxPropertiesInit(CxProperties *prop, CxPropertiesConfig config); 184 void cxPropertiesInit(CxProperties *prop, CxPropertiesConfig config);
189 185
190 /** 186 /**
191 * Destroys the properties interface. 187 * Destroys the properties interface.
192 * 188 *
193 * @note Even when you are certain that you did not use the interface in a 189 * @note Even when you are certain that you did not use the interface in a
196 * and you really don't want to search the entire code where you might need to 192 * and you really don't want to search the entire code where you might need to
197 * add a call to this function. 193 * add a call to this function.
198 * 194 *
199 * @param prop the properties interface 195 * @param prop the properties interface
200 */ 196 */
201 cx_attr_nonnull 197 CX_EXTERN CX_NONNULL
202 CX_EXPORT void cxPropertiesDestroy(CxProperties *prop); 198 void cxPropertiesDestroy(CxProperties *prop);
203 199
204 /** 200 /**
205 * Destroys and re-initializes the properties interface. 201 * Destroys and re-initializes the properties interface.
206 * 202 *
207 * You might want to use this to reset the parser after 203 * You might want to use this to reset the parser after
208 * encountering a syntax error. 204 * encountering a syntax error.
209 * 205 *
210 * @param prop the properties interface 206 * @param prop the properties interface
211 */ 207 */
212 cx_attr_nonnull 208 CX_EXTERN CX_NONNULL
213 CX_EXPORT void cxPropertiesReset(CxProperties *prop); 209 void cxPropertiesReset(CxProperties *prop);
214 210
215 /** 211 /**
216 * Initialize a properties parser with the default configuration. 212 * Initialize a properties parser with the default configuration.
217 * 213 *
218 * @param prop (@c CxProperties*) the properties interface 214 * @param prop (@c CxProperties*) the properties interface
240 * @param len the length of the data 236 * @param len the length of the data
241 * @retval zero success 237 * @retval zero success
242 * @retval non-zero a memory allocation was necessary but failed 238 * @retval non-zero a memory allocation was necessary but failed
243 * @see cxPropertiesFill() 239 * @see cxPropertiesFill()
244 */ 240 */
245 cx_attr_nonnull cx_attr_access_r(2, 3) 241 CX_EXTERN CX_NONNULL CX_ACCESS_R(2, 3)
246 CX_EXPORT int cxPropertiesFilln(CxProperties *prop, const char *buf, size_t len); 242 int cxPropertiesFilln(CxProperties *prop, const char *buf, size_t len);
247 243
248 /** 244 /**
249 * Internal function, do not use. 245 * Internal function, do not use.
250 * 246 *
251 * @param prop the properties interface 247 * @param prop the properties interface
252 * @param str the text to fill in 248 * @param str the text to fill in
253 * @retval zero success 249 * @retval zero success
254 * @retval non-zero a memory allocation was necessary but failed 250 * @retval non-zero a memory allocation was necessary but failed
255 */ 251 */
256 cx_attr_nonnull 252 CX_NONNULL CX_INLINE
257 CX_INLINE int cx_properties_fill(CxProperties *prop, cxstring str) { 253 int cx_properties_fill(CxProperties *prop, cxstring str) {
258 return cxPropertiesFilln(prop, str.ptr, str.length); 254 return cxPropertiesFilln(prop, str.ptr, str.length);
259 } 255 }
260 256
261 /** 257 /**
262 * Fills the input buffer with data. 258 * Fills the input buffer with data.
285 * 281 *
286 * @param prop the properties interface 282 * @param prop the properties interface
287 * @param buf a pointer to stack memory 283 * @param buf a pointer to stack memory
288 * @param capacity the capacity of the stack memory 284 * @param capacity the capacity of the stack memory
289 */ 285 */
290 cx_attr_nonnull 286 CX_EXTERN CX_NONNULL
291 CX_EXPORT void cxPropertiesUseStack(CxProperties *prop, char *buf, size_t capacity); 287 void cxPropertiesUseStack(CxProperties *prop, char *buf, size_t capacity);
292 288
293 /** 289 /**
294 * Retrieves the next key/value-pair. 290 * Retrieves the next key/value-pair.
295 * 291 *
296 * This function returns zero as long as there are key/value-pairs found. 292 * This function returns zero as long as there are key/value-pairs found.
318 * @retval CX_PROPERTIES_NULL_INPUT the input buffer was never filled 314 * @retval CX_PROPERTIES_NULL_INPUT the input buffer was never filled
319 * @retval CX_PROPERTIES_INVALID_EMPTY_KEY the properties data contains an illegal empty key 315 * @retval CX_PROPERTIES_INVALID_EMPTY_KEY the properties data contains an illegal empty key
320 * @retval CX_PROPERTIES_INVALID_MISSING_DELIMITER the properties data contains a line without delimiter 316 * @retval CX_PROPERTIES_INVALID_MISSING_DELIMITER the properties data contains a line without delimiter
321 * @retval CX_PROPERTIES_BUFFER_ALLOC_FAILED an internal allocation was necessary but failed 317 * @retval CX_PROPERTIES_BUFFER_ALLOC_FAILED an internal allocation was necessary but failed
322 */ 318 */
323 cx_attr_nonnull cx_attr_nodiscard 319 CX_EXTERN CX_NONNULL CX_NODISCARD
324 CX_EXPORT CxPropertiesStatus cxPropertiesNext(CxProperties *prop, cxstring *key, cxstring *value); 320 CxPropertiesStatus cxPropertiesNext(CxProperties *prop, cxstring *key, cxstring *value);
325 321
326 /** 322 /**
327 * The size of the stack memory that `cxPropertiesLoad()` will reserve with `cxPropertiesUseStack()`. 323 * The size of the stack memory that `cxPropertiesLoad()` will reserve with `cxPropertiesUseStack()`.
328 */ 324 */
329 CX_EXPORT extern const unsigned cx_properties_load_buf_size; 325 CX_EXPORT extern const unsigned cx_properties_load_buf_size;
340 * @param filename the file name 336 * @param filename the file name
341 * @param target the target map 337 * @param target the target map
342 * @param config the parser config 338 * @param config the parser config
343 * @return status code 339 * @return status code
344 */ 340 */
345 cx_attr_nonnull_arg(3) 341 CX_EXTERN CX_NONNULL_ARG(3)
346 CX_EXPORT CxPropertiesStatus cx_properties_load(const CxAllocator *allocator, 342 CxPropertiesStatus cx_properties_load(const CxAllocator *allocator,
347 cxstring filename, CxMap *target, CxPropertiesConfig config); 343 cxstring filename, CxMap *target, CxPropertiesConfig config);
348 344
349 /** 345 /**
350 * Loads properties from a file and inserts them into a map. 346 * Loads properties from a file and inserts them into a map.
351 * 347 *
399 * @see cxPropertiesLoad() 395 * @see cxPropertiesLoad()
400 */ 396 */
401 #define cxPropertiesLoadDefault(allocator, filename, target) \ 397 #define cxPropertiesLoadDefault(allocator, filename, target) \
402 cx_properties_load(allocator, cx_strcast(filename), target, cx_properties_config_default) 398 cx_properties_load(allocator, cx_strcast(filename), target, cx_properties_config_default)
403 399
404
405 #ifdef __cplusplus
406 } // extern "C"
407 #endif
408
409 #endif // UCX_PROPERTIES_H 400 #endif // UCX_PROPERTIES_H

mercurial