--- a/src/cx/properties.h Sat Nov 02 13:48:53 2024 +0100 +++ b/src/cx/properties.h Sat Nov 02 19:27:45 2024 +0100 @@ -42,6 +42,7 @@ #include "array_list.h" #include <stdio.h> +#include <string.h> #ifdef __cplusplus extern "C" { @@ -371,7 +372,13 @@ cxPropertiesInit(prop, cx_properties_config_default) /** - * Sets an input buffer. + * Fills the input buffer with data. + * + * Currently unprocessed data is copied to a temporary buffer. + * This temporary buffer is allocated on the heap, unless you specified + * a buffer on the stack with #cxPropertiesUseStack(). + * In that case, the stack buffer is used, until the capacity is not sufficient + * anymore. * * After calling this function, you can parse the data by calling * cxPropertiesNext() until the status is #CX_PROPERTIES_NO_DATA. @@ -379,18 +386,19 @@ * @param prop the properties interface * @param buf a pointer to data * @param len the length of the data + * @return non-zero when a memory allocation was necessary but failed */ __attribute__((__nonnull__)) -void cxPropertiesInput( +int cxPropertiesFilln( CxProperties *prop, const char *buf, size_t len ); /** - * Sets a new input buffer after copying the current unprocessed data - * to a temporary buffer. + * Fills the input buffer with a string. * + * Currently unprocessed data is copied to a temporary buffer. * This temporary buffer is allocated on the heap, unless you specified * a buffer on the stack with #cxPropertiesUseStack(). * In that case, the stack buffer is used, until the capacity is not sufficient @@ -400,16 +408,60 @@ * copied, it behaves exactly as #cxPropertiesInput(). * * @param prop the properties interface - * @param buf a pointer to data - * @param len the length of the data + * @param str the string + * @return non-zero when a memory allocation was necessary but failed + */ +#define cxPropertiesFill(prop, str) _Generic((str), \ + cxstring: cx_properties_fill_cxstr, \ + cxmutstr: cx_properties_fill_mutstr, \ + char*: cx_properties_fill_str, \ + const char*: cx_properties_fill_str) \ + (prop, str) + +/** + * Implementation of cxPropertiesFill() for cxstring. + * + * @param prop the properties interface + * @param str the string * @return non-zero when a memory allocation was necessary but failed */ __attribute__((__nonnull__)) -int cxPropertiesFill( +static inline int cx_properties_fill_cxstr( + CxProperties *prop, + cxstring str +) { + return cxPropertiesFilln(prop, str.ptr, str.length); +} + +/** + * Implementation of cxPropertiesFill() for cxmutstr. + * + * @param prop the properties interface + * @param str the string + * @return non-zero when a memory allocation was necessary but failed + */ +__attribute__((__nonnull__)) +static inline int cx_properties_fill_mutstr( CxProperties *prop, - const char *buf, - size_t len -); + cxmutstr str +) { + return cxPropertiesFilln(prop, str.ptr, str.length); +} + +/** + * Implementation of cxPropertiesFill() for zero terminated C strings. + * + * @param prop the properties interface + * @param str the string + * @return non-zero when a memory allocation was necessary but failed + */ +__attribute__((__nonnull__)) +static inline int cx_properties_fill_str( + CxProperties *prop, + const char *str +) { + return cxPropertiesFilln(prop, str, strlen(str)); +} /** * Specifies stack memory that shall be used by #cxPropertiesFill().