ucx/cx/properties.h

changeset 992
f421aef8f865
parent 870
e167cf006213
equal deleted inserted replaced
991:ab3125bd8b5f 992:f421aef8f865
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 #include <stdio.h>
45 #include <string.h>
46
47 #ifdef __cplusplus 44 #ifdef __cplusplus
48 extern "C" { 45 extern "C" {
49 #endif 46 #endif
50 47
51 /** 48 /**
77 char comment3; 74 char comment3;
78 75
79 /* 76 /*
80 * The character, when appearing at the end of a line, continues that line. 77 * The character, when appearing at the end of a line, continues that line.
81 * This is '\' by default. 78 * This is '\' by default.
82 */
83 /**
84 * Reserved for future use.
85 */ 79 */
86 char continuation; 80 char continuation;
87 }; 81 };
88 82
89 /** 83 /**
139 /** 133 /**
140 * More internal buffer was needed, but could not be allocated. 134 * More internal buffer was needed, but could not be allocated.
141 */ 135 */
142 CX_PROPERTIES_BUFFER_ALLOC_FAILED, 136 CX_PROPERTIES_BUFFER_ALLOC_FAILED,
143 /** 137 /**
144 * Initializing the properties source failed. 138 * A file operation failed.
145 * 139 * Only for cxPropertiesLoad().
146 * @see cx_properties_read_init_func 140 * It is system-specific if errno is set.
147 */ 141 */
148 CX_PROPERTIES_READ_INIT_FAILED, 142 CX_PROPERTIES_FILE_ERROR,
149 /** 143 /**
150 * Reading from a properties source failed. 144 * A map operation failed.
151 * 145 * Only for cxPropertiesLoad().
152 * @see cx_properties_read_func 146 */
153 */ 147 CX_PROPERTIES_MAP_ERROR,
154 CX_PROPERTIES_READ_FAILED,
155 /**
156 * Sinking a k/v-pair failed.
157 *
158 * @see cx_properties_sink_func
159 */
160 CX_PROPERTIES_SINK_FAILED,
161 }; 148 };
162 149
163 /** 150 /**
164 * Typedef for the properties status enum. 151 * Typedef for the properties status enum.
165 */ 152 */
187 174
188 /** 175 /**
189 * Typedef for the properties interface. 176 * Typedef for the properties interface.
190 */ 177 */
191 typedef struct cx_properties_s CxProperties; 178 typedef struct cx_properties_s CxProperties;
192
193
194 /**
195 * Typedef for a properties sink.
196 */
197 typedef struct cx_properties_sink_s CxPropertiesSink;
198
199 /**
200 * A function that consumes a k/v-pair in a sink.
201 *
202 * The sink could be a map, and the sink function would be calling
203 * a map function to store the k/v-pair.
204 *
205 * @param prop the properties interface that wants to sink a k/v-pair
206 * @param sink the sink
207 * @param key the key
208 * @param value the value
209 * @retval zero success
210 * @retval non-zero sinking the k/v-pair failed
211 */
212 typedef int(*cx_properties_sink_func)(
213 CxProperties *prop,
214 CxPropertiesSink *sink,
215 cxstring key,
216 cxstring value
217 );
218
219 /**
220 * Defines a sink for k/v-pairs.
221 */
222 struct cx_properties_sink_s {
223 /**
224 * The sink object.
225 */
226 void *sink;
227 /**
228 * Optional custom data.
229 */
230 void *data;
231 /**
232 * A function for consuming k/v-pairs into the sink.
233 */
234 cx_properties_sink_func sink_func;
235 };
236
237
238 /**
239 * Typedef for a properties source.
240 */
241 typedef struct cx_properties_source_s CxPropertiesSource;
242
243 /**
244 * A function that reads data from a source.
245 *
246 * When the source is depleted, implementations SHALL provide an empty
247 * string in the @p target and return zero.
248 * A non-zero return value is only permitted in case of an error.
249 *
250 * The meaning of the optional parameters is implementation-dependent.
251 *
252 * @param prop the properties interface that wants to read from the source
253 * @param src the source
254 * @param target a string buffer where the read data shall be stored
255 * @retval zero success
256 * @retval non-zero reading the data failed
257 */
258 typedef int(*cx_properties_read_func)(
259 CxProperties *prop,
260 CxPropertiesSource *src,
261 cxstring *target
262 );
263
264 /**
265 * A function that may initialize additional memory for the source.
266 *
267 * @param prop the properties interface that wants to read from the source
268 * @param src the source
269 * @retval zero initialization was successful
270 * @retval non-zero otherwise
271 */
272 typedef int(*cx_properties_read_init_func)(
273 CxProperties *prop,
274 CxPropertiesSource *src
275 );
276
277 /**
278 * A function that cleans memory initialized by the read_init_func.
279 *
280 * @param prop the properties interface that wants to read from the source
281 * @param src the source
282 */
283 typedef void(*cx_properties_read_clean_func)(
284 CxProperties *prop,
285 CxPropertiesSource *src
286 );
287
288 /**
289 * Defines a properties source.
290 */
291 struct cx_properties_source_s {
292 /**
293 * The source object.
294 *
295 * For example, a file stream or a string.
296 */
297 void *src;
298 /**
299 * Optional additional data pointer.
300 */
301 void *data_ptr;
302 /**
303 * Optional size information.
304 */
305 size_t data_size;
306 /**
307 * A function that reads data from the source.
308 */
309 cx_properties_read_func read_func;
310 /**
311 * Optional function that may prepare the source for reading data.
312 */
313 cx_properties_read_init_func read_init_func;
314 /**
315 * Optional function that cleans additional memory allocated by the
316 * read_init_func.
317 */
318 cx_properties_read_clean_func read_clean_func;
319 };
320 179
321 /** 180 /**
322 * Initialize a properties interface. 181 * Initialize a properties interface.
323 * 182 *
324 * @param prop the properties interface 183 * @param prop the properties interface
463 */ 322 */
464 cx_attr_nonnull cx_attr_nodiscard 323 cx_attr_nonnull cx_attr_nodiscard
465 CX_EXPORT CxPropertiesStatus cxPropertiesNext(CxProperties *prop, cxstring *key, cxstring *value); 324 CX_EXPORT CxPropertiesStatus cxPropertiesNext(CxProperties *prop, cxstring *key, cxstring *value);
466 325
467 /** 326 /**
468 * Creates a properties sink for an UCX map. 327 * The size of the stack memory that `cxPropertiesLoad()` will reserve with `cxPropertiesUseStack()`.
469 * 328 */
470 * The values stored in the map will be pointers to freshly allocated, 329 CX_EXPORT extern const unsigned cx_properties_load_buf_size;
471 * zero-terminated C strings (@c char*), which means the @p map should have been 330
472 * created with #CX_STORE_POINTERS. 331 /**
473 * 332 * The size of the stack memory that `cxPropertiesLoad()` will use to read contents from the file.
474 * The cxDefaultAllocator will be used unless you specify a custom 333 */
475 * allocator in the optional @c data field of the returned sink. 334 CX_EXPORT extern const unsigned cx_properties_load_fill_size;
476 * 335
477 * @param map the map that shall consume the k/v-pairs. 336 /**
478 * @return the sink 337 * Internal function - use cxPropertiesLoad() instead.
479 * @see cxPropertiesLoad() 338 *
480 */ 339 * @param config the parser config
481 cx_attr_nonnull cx_attr_nodiscard 340 * @param allocator the allocator for the values
482 CX_EXPORT CxPropertiesSink cxPropertiesMapSink(CxMap *map); 341 * @param filename the file name
483 342 * @param target the target map
484 /** 343 * @return status code
485 * Creates a properties source based on an UCX string. 344 */
486 * 345 cx_attr_nonnull_arg(4)
487 * @param str the string 346 CX_EXPORT CxPropertiesStatus cx_properties_load(CxPropertiesConfig config,
488 * @return the properties source 347 const CxAllocator *allocator, cxstring filename, CxMap *target);
489 * @see cxPropertiesLoad() 348
490 */ 349 /**
491 cx_attr_nodiscard 350 * Loads properties from a file and inserts them into a map.
492 CX_EXPORT CxPropertiesSource cxPropertiesStringSource(cxstring str); 351 *
493 352 * Entries are added to the map, possibly overwriting existing entries.
494 /** 353 *
495 * Creates a properties source based on C string with the specified length. 354 * The map must either store pointers of type @c char*, or elements of type cxmutstr.
496 * 355 * Any other configuration is not supported.
497 * @param str the string 356 *
498 * @param len the length 357 * @note When the parser finds an error, all successfully parsed keys before the error
499 * @return the properties source 358 * are added to the map nonetheless.
500 * @see cxPropertiesLoad() 359 *
501 */ 360 * @param config the parser config
502 cx_attr_nonnull cx_attr_nodiscard cx_attr_access_r(1, 2) 361 * @param allocator the allocator for the values that will be stored in the map
503 CX_EXPORT CxPropertiesSource cxPropertiesCstrnSource(const char *str, size_t len); 362 * @param filename (any string) the absolute or relative path to the file
504 363 * @param target (@c CxMap*) the map where the properties shall be added
505 /** 364 * @retval CX_PROPERTIES_NO_ERROR (zero) at least one key/value pair was found
506 * Creates a properties source based on a C string. 365 * @retval CX_PROPERTIES_NO_DATA the file is syntactically OK, but does not contain properties
507 * 366 * @retval CX_PROPERTIES_INCOMPLETE_DATA unexpected end of file
508 * The length will be determined with strlen(), so the string MUST be
509 * zero-terminated.
510 *
511 * @param str the string
512 * @return the properties source
513 * @see cxPropertiesLoad()
514 */
515 cx_attr_nonnull cx_attr_nodiscard cx_attr_cstr_arg(1)
516 CX_EXPORT CxPropertiesSource cxPropertiesCstrSource(const char *str);
517
518 /**
519 * Creates a properties source based on an FILE.
520 *
521 * @param file the file
522 * @param chunk_size how many bytes may be read in one operation
523 *
524 * @return the properties source
525 * @see cxPropertiesLoad()
526 */
527 cx_attr_nonnull cx_attr_nodiscard cx_attr_access_r(1)
528 CX_EXPORT CxPropertiesSource cxPropertiesFileSource(FILE *file, size_t chunk_size);
529
530
531 /**
532 * Loads properties data from a source and transfers it to a sink.
533 *
534 * This function tries to read as much data from the source as possible.
535 * When the source was completely consumed and at least on k/v-pair was found,
536 * the return value will be #CX_PROPERTIES_NO_ERROR.
537 * When the source was consumed but no k/v-pairs were found, the return value
538 * will be #CX_PROPERTIES_NO_DATA.
539 * In case the source data ends unexpectedly, the #CX_PROPERTIES_INCOMPLETE_DATA
540 * is returned. In that case you should call this function again with the same
541 * sink and either an updated source or the same source if the source is able to
542 * yield the missing data.
543 *
544 * The other result codes apply, according to their description.
545 *
546 * @param prop the properties interface
547 * @param sink the sink
548 * @param source the source
549 * @retval CX_PROPERTIES_NO_ERROR (zero) a key/value pair was found
550 * @retval CX_PROPERTIES_READ_INIT_FAILED initializing the source failed
551 * @retval CX_PROPERTIES_READ_FAILED reading from the source failed
552 * @retval CX_PROPERTIES_SINK_FAILED sinking the properties into the sink failed
553 * @retval CX_PROPERTIES_NO_DATA the source did not provide any key/value pairs
554 * @retval CX_PROPERTIES_INCOMPLETE_DATA the source did not provide enough data
555 * @retval CX_PROPERTIES_INVALID_EMPTY_KEY the properties data contains an illegal empty key 367 * @retval CX_PROPERTIES_INVALID_EMPTY_KEY the properties data contains an illegal empty key
556 * @retval CX_PROPERTIES_INVALID_MISSING_DELIMITER the properties data contains a line without delimiter 368 * @retval CX_PROPERTIES_INVALID_MISSING_DELIMITER the properties data contains a line without delimiter
557 * @retval CX_PROPERTIES_BUFFER_ALLOC_FAILED an internal allocation was necessary but failed 369 * @retval CX_PROPERTIES_BUFFER_ALLOC_FAILED an internal allocation was necessary but failed
558 */ 370 * @retval CX_PROPERTIES_FILE_ERROR a file operation failed; depending on the system @c errno might be set
559 cx_attr_nonnull 371 * @retval CX_PROPERTIES_MAP_ERROR storing a key/value pair in the map failed
560 CX_EXPORT CxPropertiesStatus cxPropertiesLoad(CxProperties *prop, 372 * @see cxPropertiesLoadDefault()
561 CxPropertiesSink sink, CxPropertiesSource source); 373 */
374 #define cxPropertiesLoad(config, allocator, filename, target) \
375 cx_properties_load(config, allocator, cx_strcast(filename), target)
376
377 /**
378 * Loads properties from a file and inserts them into a map with a default config.
379 *
380 * Entries are added to the map, possibly overwriting existing entries.
381 *
382 * The map must either store pointers of type @c char*, or elements of type cxmutstr.
383 * Any other configuration is not supported.
384 *
385 * @note When the parser finds an error, all successfully parsed keys before the error
386 * are added to the map nonetheless.
387 *
388 * @param allocator the allocator for the values that will be stored in the map
389 * @param filename (any string) the absolute or relative path to the file
390 * @param target (@c CxMap*) the map where the properties shall be added
391 * @retval CX_PROPERTIES_NO_ERROR (zero) at least one key/value pair was found
392 * @retval CX_PROPERTIES_NO_DATA the file is syntactically OK, but does not contain properties
393 * @retval CX_PROPERTIES_INCOMPLETE_DATA unexpected end of file
394 * @retval CX_PROPERTIES_INVALID_EMPTY_KEY the properties data contains an illegal empty key
395 * @retval CX_PROPERTIES_INVALID_MISSING_DELIMITER the properties data contains a line without delimiter
396 * @retval CX_PROPERTIES_BUFFER_ALLOC_FAILED an internal allocation was necessary but failed
397 * @retval CX_PROPERTIES_FILE_ERROR a file operation failed; depending on the system @c errno might be set
398 * @retval CX_PROPERTIES_MAP_ERROR storing a key/value pair in the map failed
399 * @see cxPropertiesLoad()
400 */
401 #define cxPropertiesLoadDefault(allocator, filename, target) \
402 cx_properties_load(cx_properties_config_default, allocator, cx_strcast(filename), target)
403
562 404
563 #ifdef __cplusplus 405 #ifdef __cplusplus
564 } // extern "C" 406 } // extern "C"
565 #endif 407 #endif
566 408

mercurial