src/cx/properties.h

changeset 1038
8cab3d8e0af4
parent 1036
6d29532a76c3
equal deleted inserted replaced
1037:83620ba72cc1 1038:8cab3d8e0af4
365 cxPropertiesInit(prop, cx_properties_config_default) 365 cxPropertiesInit(prop, cx_properties_config_default)
366 366
367 /** 367 /**
368 * Fills the input buffer with data. 368 * Fills the input buffer with data.
369 * 369 *
370 * Currently unprocessed data is copied to a temporary buffer.
371 * This temporary buffer is allocated on the heap, unless you specified
372 * a buffer on the stack with #cxPropertiesUseStack().
373 * In that case, the stack buffer is used, until the capacity is not sufficient
374 * anymore.
375 *
376 * After calling this function, you can parse the data by calling 370 * After calling this function, you can parse the data by calling
377 * cxPropertiesNext() until the status is #CX_PROPERTIES_NO_DATA. 371 * cxPropertiesNext().
378 * 372 *
379 * @param prop the properties interface 373 * @attention The properties interface tries to avoid allocations.
380 * @param buf a pointer to data 374 * When you use this function and cxPropertiesNext() interleaving,
375 * no allocations are performed. However, you must not free the
376 * pointer to the data in that case. When you invoke the fill
377 * function more than once before calling cxPropertiesNext(),
378 * the additional data is appended - inevitably leading to
379 * an allocation of a new buffer and copying the previous contents.
380 *
381 * @param prop the properties interface
382 * @param buf a pointer to the data
381 * @param len the length of the data 383 * @param len the length of the data
382 * @return non-zero when a memory allocation was necessary but failed 384 * @return non-zero when a memory allocation was necessary but failed
383 */ 385 */
384 cx_attr_nonnull 386 cx_attr_nonnull
385 cx_attr_access_r(2, 3) 387 cx_attr_access_r(2, 3)
389 size_t len 391 size_t len
390 ); 392 );
391 393
392 #ifdef __cplusplus 394 #ifdef __cplusplus
393 } // extern "C" 395 } // extern "C"
394 /**
395 * Fills the input buffer with a string.
396 *
397 * Currently unprocessed data is copied to a temporary buffer.
398 * This temporary buffer is allocated on the heap, unless you specified
399 * a buffer on the stack with #cxPropertiesUseStack().
400 * In that case, the stack buffer is used, until the capacity is not sufficient
401 * anymore.
402 *
403 * @param prop the properties interface
404 * @param str the string
405 * @return non-zero when a memory allocation was necessary but failed
406 */
407 cx_attr_nonnull 396 cx_attr_nonnull
408 static inline int cxPropertiesFill( 397 static inline int cxPropertiesFill(
409 CxProperties *prop, 398 CxProperties *prop,
410 cxstring str 399 cxstring str
411 ) { 400 ) {
412 return cxPropertiesFilln(prop, str.ptr, str.length); 401 return cxPropertiesFilln(prop, str.ptr, str.length);
413 } 402 }
414 403
415 /**
416 * Fills the input buffer with a string.
417 *
418 * Currently unprocessed data is copied to a temporary buffer.
419 * This temporary buffer is allocated on the heap, unless you specified
420 * a buffer on the stack with #cxPropertiesUseStack().
421 * In that case, the stack buffer is used, until the capacity is not sufficient
422 * anymore.
423 *
424 * @param prop the properties interface
425 * @param str the string
426 * @return non-zero when a memory allocation was necessary but failed
427 */
428 cx_attr_nonnull 404 cx_attr_nonnull
429 static inline int cxPropertiesFill( 405 static inline int cxPropertiesFill(
430 CxProperties *prop, 406 CxProperties *prop,
431 cxmutstr str 407 cxmutstr str
432 ) { 408 ) {
433 return cxPropertiesFilln(prop, str.ptr, str.length); 409 return cxPropertiesFilln(prop, str.ptr, str.length);
434 } 410 }
435 411
436 /**
437 * Fills the input buffer with a string.
438 *
439 * Currently unprocessed data is copied to a temporary buffer.
440 * This temporary buffer is allocated on the heap, unless you specified
441 * a buffer on the stack with #cxPropertiesUseStack().
442 * In that case, the stack buffer is used, until the capacity is not sufficient
443 * anymore.
444 *
445 * @param prop the properties interface
446 * @param str the string
447 * @return non-zero when a memory allocation was necessary but failed
448 */
449 cx_attr_nonnull 412 cx_attr_nonnull
450 cx_attr_cstr_arg(2) 413 cx_attr_cstr_arg(2)
451 static inline int cxPropertiesFill( 414 static inline int cxPropertiesFill(
452 CxProperties *prop, 415 CxProperties *prop,
453 const char *str 416 const char *str
456 } 419 }
457 420
458 extern "C" { 421 extern "C" {
459 #else // __cplusplus 422 #else // __cplusplus
460 /** 423 /**
461 * Fills the input buffer with a string. 424 * Fills the input buffer with data.
462 * 425 *
463 * Currently unprocessed data is copied to a temporary buffer. 426 * After calling this function, you can parse the data by calling
464 * This temporary buffer is allocated on the heap, unless you specified 427 * cxPropertiesNext().
465 * a buffer on the stack with #cxPropertiesUseStack(). 428 *
466 * In that case, the stack buffer is used, until the capacity is not sufficient 429 * @attention The properties interface tries to avoid allocations.
467 * anymore. 430 * When you use this function and cxPropertiesNext() interleaving,
468 * 431 * no allocations are performed. However, you must not free the
469 * @param prop the properties interface 432 * pointer to the data in that case. When you invoke the fill
470 * @param str the string 433 * function more than once before calling cxPropertiesNext(),
434 * the additional data is appended - inevitably leading to
435 * an allocation of a new buffer and copying the previous contents.
436 *
437 * @param prop the properties interface
438 * @param str the text to fill in
471 * @return non-zero when a memory allocation was necessary but failed 439 * @return non-zero when a memory allocation was necessary but failed
472 */ 440 */
473 #define cxPropertiesFill(prop, str) _Generic((str), \ 441 #define cxPropertiesFill(prop, str) _Generic((str), \
474 cxstring: cx_properties_fill_cxstr, \ 442 cxstring: cx_properties_fill_cxstr, \
475 cxmutstr: cx_properties_fill_mutstr, \ 443 cxmutstr: cx_properties_fill_mutstr, \
476 char*: cx_properties_fill_str, \ 444 char*: cx_properties_fill_str, \
477 const char*: cx_properties_fill_str) \ 445 const char*: cx_properties_fill_str) \
478 (prop, str) 446 (prop, str)
479 447
480 /** 448 /**
481 * Implementation of cxPropertiesFill() for cxstring. 449 * @copydoc cxPropertiesFill()
482 *
483 * @param prop the properties interface
484 * @param str the string
485 * @return non-zero when a memory allocation was necessary but failed
486 */ 450 */
487 cx_attr_nonnull 451 cx_attr_nonnull
488 static inline int cx_properties_fill_cxstr( 452 static inline int cx_properties_fill_cxstr(
489 CxProperties *prop, 453 CxProperties *prop,
490 cxstring str 454 cxstring str
491 ) { 455 ) {
492 return cxPropertiesFilln(prop, str.ptr, str.length); 456 return cxPropertiesFilln(prop, str.ptr, str.length);
493 } 457 }
494 458
495 /** 459 /**
496 * Implementation of cxPropertiesFill() for cxmutstr. 460 * @copydoc cxPropertiesFill()
497 *
498 * @param prop the properties interface
499 * @param str the string
500 * @return non-zero when a memory allocation was necessary but failed
501 */ 461 */
502 cx_attr_nonnull 462 cx_attr_nonnull
503 static inline int cx_properties_fill_mutstr( 463 static inline int cx_properties_fill_mutstr(
504 CxProperties *prop, 464 CxProperties *prop,
505 cxmutstr str 465 cxmutstr str
506 ) { 466 ) {
507 return cxPropertiesFilln(prop, str.ptr, str.length); 467 return cxPropertiesFilln(prop, str.ptr, str.length);
508 } 468 }
509 469
510 /** 470 /**
511 * Implementation of cxPropertiesFill() for zero terminated C strings. 471 * @copydoc cxPropertiesFill()
512 *
513 * @param prop the properties interface
514 * @param str the string
515 * @return non-zero when a memory allocation was necessary but failed
516 */ 472 */
517 cx_attr_nonnull 473 cx_attr_nonnull
518 cx_attr_cstr_arg(2) 474 cx_attr_cstr_arg(2)
519 static inline int cx_properties_fill_str( 475 static inline int cx_properties_fill_str(
520 CxProperties *prop, 476 CxProperties *prop,
543 * 499 *
544 * This function returns zero as long as there are key/value-pairs found. 500 * This function returns zero as long as there are key/value-pairs found.
545 * If no more key/value-pairs are found, #CX_PROPERTIES_NO_DATA is returned. 501 * If no more key/value-pairs are found, #CX_PROPERTIES_NO_DATA is returned.
546 * 502 *
547 * When an incomplete line is encountered, #CX_PROPERTIES_INCOMPLETE_DATA is 503 * When an incomplete line is encountered, #CX_PROPERTIES_INCOMPLETE_DATA is
548 * returned and the position of the input buffer will be the start of the 504 * returned, and you can add more data with #cxPropertiesFill().
549 * affected line. You can then add more data with #cxPropertiesFill(). 505 *
506 * \remark The incomplete line will be stored in an internal buffer, which is
507 * allocated on the heap, by default. If you want to avoid allocations,
508 * you can specify sufficient space with cxPropertiesUseStack() after
509 * initialization with cxPropertiesInit().
550 * 510 *
551 * \attention The returned strings will point into a buffer that might not be 511 * \attention The returned strings will point into a buffer that might not be
552 * available later. It is strongly recommended to copy the strings for further 512 * available later. It is strongly recommended to copy the strings for further
553 * use. 513 * use.
554 * 514 *

mercurial