92 typedef struct cx_properties_config_s CxPropertiesConfig; |
92 typedef struct cx_properties_config_s CxPropertiesConfig; |
93 |
93 |
94 /** |
94 /** |
95 * Default properties configuration. |
95 * Default properties configuration. |
96 */ |
96 */ |
97 cx_attr_export |
97 CX_EXPORT extern const CxPropertiesConfig cx_properties_config_default; |
98 extern const CxPropertiesConfig cx_properties_config_default; |
|
99 |
98 |
100 /** |
99 /** |
101 * Status codes for the properties interface. |
100 * Status codes for the properties interface. |
102 */ |
101 */ |
103 enum cx_properties_status { |
102 enum cx_properties_status { |
208 * @param key the key |
207 * @param key the key |
209 * @param value the value |
208 * @param value the value |
210 * @retval zero success |
209 * @retval zero success |
211 * @retval non-zero sinking the k/v-pair failed |
210 * @retval non-zero sinking the k/v-pair failed |
212 */ |
211 */ |
213 cx_attr_nonnull |
|
214 typedef int(*cx_properties_sink_func)( |
212 typedef int(*cx_properties_sink_func)( |
215 CxProperties *prop, |
213 CxProperties *prop, |
216 CxPropertiesSink *sink, |
214 CxPropertiesSink *sink, |
217 cxstring key, |
215 cxstring key, |
218 cxstring value |
216 cxstring value |
255 * @param src the source |
253 * @param src the source |
256 * @param target a string buffer where the read data shall be stored |
254 * @param target a string buffer where the read data shall be stored |
257 * @retval zero success |
255 * @retval zero success |
258 * @retval non-zero reading the data failed |
256 * @retval non-zero reading the data failed |
259 */ |
257 */ |
260 cx_attr_nonnull |
|
261 typedef int(*cx_properties_read_func)( |
258 typedef int(*cx_properties_read_func)( |
262 CxProperties *prop, |
259 CxProperties *prop, |
263 CxPropertiesSource *src, |
260 CxPropertiesSource *src, |
264 cxstring *target |
261 cxstring *target |
265 ); |
262 ); |
270 * @param prop the properties interface that wants to read from the source |
267 * @param prop the properties interface that wants to read from the source |
271 * @param src the source |
268 * @param src the source |
272 * @retval zero initialization was successful |
269 * @retval zero initialization was successful |
273 * @retval non-zero otherwise |
270 * @retval non-zero otherwise |
274 */ |
271 */ |
275 cx_attr_nonnull |
|
276 typedef int(*cx_properties_read_init_func)( |
272 typedef int(*cx_properties_read_init_func)( |
277 CxProperties *prop, |
273 CxProperties *prop, |
278 CxPropertiesSource *src |
274 CxPropertiesSource *src |
279 ); |
275 ); |
280 |
276 |
282 * A function that cleans memory initialized by the read_init_func. |
278 * A function that cleans memory initialized by the read_init_func. |
283 * |
279 * |
284 * @param prop the properties interface that wants to read from the source |
280 * @param prop the properties interface that wants to read from the source |
285 * @param src the source |
281 * @param src the source |
286 */ |
282 */ |
287 cx_attr_nonnull |
|
288 typedef void(*cx_properties_read_clean_func)( |
283 typedef void(*cx_properties_read_clean_func)( |
289 CxProperties *prop, |
284 CxProperties *prop, |
290 CxPropertiesSource *src |
285 CxPropertiesSource *src |
291 ); |
286 ); |
292 |
287 |
329 * @param prop the properties interface |
324 * @param prop the properties interface |
330 * @param config the properties configuration |
325 * @param config the properties configuration |
331 * @see cxPropertiesInitDefault() |
326 * @see cxPropertiesInitDefault() |
332 */ |
327 */ |
333 cx_attr_nonnull |
328 cx_attr_nonnull |
334 cx_attr_export |
329 CX_EXPORT void cxPropertiesInit(CxProperties *prop, CxPropertiesConfig config); |
335 void cxPropertiesInit(CxProperties *prop, CxPropertiesConfig config); |
|
336 |
330 |
337 /** |
331 /** |
338 * Destroys the properties interface. |
332 * Destroys the properties interface. |
339 * |
333 * |
340 * @note Even when you are certain that you did not use the interface in a |
334 * @note Even when you are certain that you did not use the interface in a |
344 * add a call to this function. |
338 * add a call to this function. |
345 * |
339 * |
346 * @param prop the properties interface |
340 * @param prop the properties interface |
347 */ |
341 */ |
348 cx_attr_nonnull |
342 cx_attr_nonnull |
349 cx_attr_export |
343 CX_EXPORT void cxPropertiesDestroy(CxProperties *prop); |
350 void cxPropertiesDestroy(CxProperties *prop); |
|
351 |
344 |
352 /** |
345 /** |
353 * Destroys and re-initializes the properties interface. |
346 * Destroys and re-initializes the properties interface. |
354 * |
347 * |
355 * You might want to use this to reset the parser after |
348 * You might want to use this to reset the parser after |
356 * encountering a syntax error. |
349 * encountering a syntax error. |
357 * |
350 * |
358 * @param prop the properties interface |
351 * @param prop the properties interface |
359 */ |
352 */ |
360 cx_attr_nonnull |
353 cx_attr_nonnull |
361 static inline void cxPropertiesReset(CxProperties *prop) { |
354 CX_EXPORT void cxPropertiesReset(CxProperties *prop); |
362 CxPropertiesConfig config = prop->config; |
|
363 cxPropertiesDestroy(prop); |
|
364 cxPropertiesInit(prop, config); |
|
365 } |
|
366 |
355 |
367 /** |
356 /** |
368 * Initialize a properties parser with the default configuration. |
357 * Initialize a properties parser with the default configuration. |
369 * |
358 * |
370 * @param prop (@c CxProperties*) the properties interface |
359 * @param prop (@c CxProperties*) the properties interface |
371 * @see cxPropertiesInit() |
360 * @see cxPropertiesInit() |
372 */ |
361 */ |
373 #define cxPropertiesInitDefault(prop) \ |
362 #define cxPropertiesInitDefault(prop) \ |
374 cxPropertiesInit(prop, cx_properties_config_default) |
363 cxPropertiesInit(prop, cx_properties_config_default) |
375 |
364 |
376 /** |
365 /** |
377 * Fills the input buffer with data. |
366 * Fills the input buffer with data. |
378 * |
367 * |
379 * After calling this function, you can parse the data by calling |
368 * After calling this function, you can parse the data by calling |
392 * @param len the length of the data |
381 * @param len the length of the data |
393 * @retval zero success |
382 * @retval zero success |
394 * @retval non-zero a memory allocation was necessary but failed |
383 * @retval non-zero a memory allocation was necessary but failed |
395 * @see cxPropertiesFill() |
384 * @see cxPropertiesFill() |
396 */ |
385 */ |
397 cx_attr_nonnull |
386 cx_attr_nonnull cx_attr_access_r(2, 3) |
398 cx_attr_access_r(2, 3) |
387 CX_EXPORT int cxPropertiesFilln(CxProperties *prop, const char *buf, size_t len); |
399 cx_attr_export |
|
400 int cxPropertiesFilln( |
|
401 CxProperties *prop, |
|
402 const char *buf, |
|
403 size_t len |
|
404 ); |
|
405 |
388 |
406 /** |
389 /** |
407 * Internal function, do not use. |
390 * Internal function, do not use. |
408 * |
391 * |
409 * @param prop the properties interface |
392 * @param prop the properties interface |
410 * @param str the text to fill in |
393 * @param str the text to fill in |
411 * @retval zero success |
394 * @retval zero success |
412 * @retval non-zero a memory allocation was necessary but failed |
395 * @retval non-zero a memory allocation was necessary but failed |
413 */ |
396 */ |
414 cx_attr_nonnull |
397 cx_attr_nonnull |
415 static inline int cx_properties_fill( |
398 CX_INLINE int cx_properties_fill(CxProperties *prop, cxstring str) { |
416 CxProperties *prop, |
|
417 cxstring str |
|
418 ) { |
|
419 return cxPropertiesFilln(prop, str.ptr, str.length); |
399 return cxPropertiesFilln(prop, str.ptr, str.length); |
420 } |
400 } |
421 |
401 |
422 /** |
402 /** |
423 * Fills the input buffer with data. |
403 * Fills the input buffer with data. |
447 * @param prop the properties interface |
427 * @param prop the properties interface |
448 * @param buf a pointer to stack memory |
428 * @param buf a pointer to stack memory |
449 * @param capacity the capacity of the stack memory |
429 * @param capacity the capacity of the stack memory |
450 */ |
430 */ |
451 cx_attr_nonnull |
431 cx_attr_nonnull |
452 cx_attr_export |
432 CX_EXPORT void cxPropertiesUseStack(CxProperties *prop, char *buf, size_t capacity); |
453 void cxPropertiesUseStack( |
|
454 CxProperties *prop, |
|
455 char *buf, |
|
456 size_t capacity |
|
457 ); |
|
458 |
433 |
459 /** |
434 /** |
460 * Retrieves the next key/value-pair. |
435 * Retrieves the next key/value-pair. |
461 * |
436 * |
462 * This function returns zero as long as there are key/value-pairs found. |
437 * This function returns zero as long as there are key/value-pairs found. |
484 * @retval CX_PROPERTIES_NULL_INPUT the input buffer was never filled |
459 * @retval CX_PROPERTIES_NULL_INPUT the input buffer was never filled |
485 * @retval CX_PROPERTIES_INVALID_EMPTY_KEY the properties data contains an illegal empty key |
460 * @retval CX_PROPERTIES_INVALID_EMPTY_KEY the properties data contains an illegal empty key |
486 * @retval CX_PROPERTIES_INVALID_MISSING_DELIMITER the properties data contains a line without delimiter |
461 * @retval CX_PROPERTIES_INVALID_MISSING_DELIMITER the properties data contains a line without delimiter |
487 * @retval CX_PROPERTIES_BUFFER_ALLOC_FAILED an internal allocation was necessary but failed |
462 * @retval CX_PROPERTIES_BUFFER_ALLOC_FAILED an internal allocation was necessary but failed |
488 */ |
463 */ |
489 cx_attr_nonnull |
464 cx_attr_nonnull cx_attr_nodiscard |
490 cx_attr_nodiscard |
465 CX_EXPORT CxPropertiesStatus cxPropertiesNext(CxProperties *prop, cxstring *key, cxstring *value); |
491 cx_attr_export |
|
492 CxPropertiesStatus cxPropertiesNext( |
|
493 CxProperties *prop, |
|
494 cxstring *key, |
|
495 cxstring *value |
|
496 ); |
|
497 |
466 |
498 /** |
467 /** |
499 * Creates a properties sink for an UCX map. |
468 * Creates a properties sink for an UCX map. |
500 * |
469 * |
501 * The values stored in the map will be pointers to freshly allocated, |
470 * The values stored in the map will be pointers to freshly allocated, |
507 * |
476 * |
508 * @param map the map that shall consume the k/v-pairs. |
477 * @param map the map that shall consume the k/v-pairs. |
509 * @return the sink |
478 * @return the sink |
510 * @see cxPropertiesLoad() |
479 * @see cxPropertiesLoad() |
511 */ |
480 */ |
512 cx_attr_nonnull |
481 cx_attr_nonnull cx_attr_nodiscard |
513 cx_attr_nodiscard |
482 CX_EXPORT CxPropertiesSink cxPropertiesMapSink(CxMap *map); |
514 cx_attr_export |
|
515 CxPropertiesSink cxPropertiesMapSink(CxMap *map); |
|
516 |
483 |
517 /** |
484 /** |
518 * Creates a properties source based on an UCX string. |
485 * Creates a properties source based on an UCX string. |
519 * |
486 * |
520 * @param str the string |
487 * @param str the string |
521 * @return the properties source |
488 * @return the properties source |
522 * @see cxPropertiesLoad() |
489 * @see cxPropertiesLoad() |
523 */ |
490 */ |
524 cx_attr_nodiscard |
491 cx_attr_nodiscard |
525 cx_attr_export |
492 CX_EXPORT CxPropertiesSource cxPropertiesStringSource(cxstring str); |
526 CxPropertiesSource cxPropertiesStringSource(cxstring str); |
|
527 |
493 |
528 /** |
494 /** |
529 * Creates a properties source based on C string with the specified length. |
495 * Creates a properties source based on C string with the specified length. |
530 * |
496 * |
531 * @param str the string |
497 * @param str the string |
532 * @param len the length |
498 * @param len the length |
533 * @return the properties source |
499 * @return the properties source |
534 * @see cxPropertiesLoad() |
500 * @see cxPropertiesLoad() |
535 */ |
501 */ |
536 cx_attr_nonnull |
502 cx_attr_nonnull cx_attr_nodiscard cx_attr_access_r(1, 2) |
537 cx_attr_nodiscard |
503 CX_EXPORT CxPropertiesSource cxPropertiesCstrnSource(const char *str, size_t len); |
538 cx_attr_access_r(1, 2) |
|
539 cx_attr_export |
|
540 CxPropertiesSource cxPropertiesCstrnSource(const char *str, size_t len); |
|
541 |
504 |
542 /** |
505 /** |
543 * Creates a properties source based on a C string. |
506 * Creates a properties source based on a C string. |
544 * |
507 * |
545 * The length will be determined with strlen(), so the string MUST be |
508 * The length will be determined with strlen(), so the string MUST be |
547 * |
510 * |
548 * @param str the string |
511 * @param str the string |
549 * @return the properties source |
512 * @return the properties source |
550 * @see cxPropertiesLoad() |
513 * @see cxPropertiesLoad() |
551 */ |
514 */ |
552 cx_attr_nonnull |
515 cx_attr_nonnull cx_attr_nodiscard cx_attr_cstr_arg(1) |
553 cx_attr_nodiscard |
516 CX_EXPORT CxPropertiesSource cxPropertiesCstrSource(const char *str); |
554 cx_attr_cstr_arg(1) |
|
555 cx_attr_export |
|
556 CxPropertiesSource cxPropertiesCstrSource(const char *str); |
|
557 |
517 |
558 /** |
518 /** |
559 * Creates a properties source based on an FILE. |
519 * Creates a properties source based on an FILE. |
560 * |
520 * |
561 * @param file the file |
521 * @param file the file |
562 * @param chunk_size how many bytes may be read in one operation |
522 * @param chunk_size how many bytes may be read in one operation |
563 * |
523 * |
564 * @return the properties source |
524 * @return the properties source |
565 * @see cxPropertiesLoad() |
525 * @see cxPropertiesLoad() |
566 */ |
526 */ |
567 cx_attr_nonnull |
527 cx_attr_nonnull cx_attr_nodiscard cx_attr_access_r(1) |
568 cx_attr_nodiscard |
528 CX_EXPORT CxPropertiesSource cxPropertiesFileSource(FILE *file, size_t chunk_size); |
569 cx_attr_access_r(1) |
|
570 cx_attr_export |
|
571 CxPropertiesSource cxPropertiesFileSource(FILE *file, size_t chunk_size); |
|
572 |
529 |
573 |
530 |
574 /** |
531 /** |
575 * Loads properties data from a source and transfers it to a sink. |
532 * Loads properties data from a source and transfers it to a sink. |
576 * |
533 * |
598 * @retval CX_PROPERTIES_INVALID_EMPTY_KEY the properties data contains an illegal empty key |
555 * @retval CX_PROPERTIES_INVALID_EMPTY_KEY the properties data contains an illegal empty key |
599 * @retval CX_PROPERTIES_INVALID_MISSING_DELIMITER the properties data contains a line without delimiter |
556 * @retval CX_PROPERTIES_INVALID_MISSING_DELIMITER the properties data contains a line without delimiter |
600 * @retval CX_PROPERTIES_BUFFER_ALLOC_FAILED an internal allocation was necessary but failed |
557 * @retval CX_PROPERTIES_BUFFER_ALLOC_FAILED an internal allocation was necessary but failed |
601 */ |
558 */ |
602 cx_attr_nonnull |
559 cx_attr_nonnull |
603 cx_attr_export |
560 CX_EXPORT CxPropertiesStatus cxPropertiesLoad(CxProperties *prop, |
604 CxPropertiesStatus cxPropertiesLoad( |
561 CxPropertiesSink sink, CxPropertiesSource source); |
605 CxProperties *prop, |
|
606 CxPropertiesSink sink, |
|
607 CxPropertiesSource source |
|
608 ); |
|
609 |
562 |
610 #ifdef __cplusplus |
563 #ifdef __cplusplus |
611 } // extern "C" |
564 } // extern "C" |
612 #endif |
565 #endif |
613 |
566 |