diff --git a/include/conf.h b/include/conf.h index cf88f1bd8..fde91ba24 100644 --- a/include/conf.h +++ b/include/conf.h @@ -124,6 +124,8 @@ int snd_config_copy(snd_config_t **dst, snd_config_t *src); int snd_config_make(snd_config_t **config, const char *key, snd_config_type_t type); +int snd_config_make_add(snd_config_t **config, char *id, + snd_config_type_t type, snd_config_t *parent); int snd_config_make_integer(snd_config_t **config, const char *key); int snd_config_make_integer64(snd_config_t **config, const char *key); int snd_config_make_real(snd_config_t **config, const char *key); diff --git a/include/global.h b/include/global.h index 71a1b12fa..893670045 100644 --- a/include/global.h +++ b/include/global.h @@ -153,6 +153,19 @@ typedef struct timeval snd_timestamp_t; /** Hi-res timestamp */ typedef struct timespec snd_htimestamp_t; +/** + * \brief Copy a C-string into a sized buffer + * \param dst Where to copy the string to + * \param src Where to copy the string from + * \param size Size of destination buffer + * \retval The source string length + * + * The result is always a valid NUL-terminated string that fits + * in the buffer (unless, of course, the buffer size is zero). + * It does not pad out the result like strncpy() does. + */ +size_t snd_strlcpy(char *dst, const char *src, size_t size); + /** \} */ #ifdef __cplusplus diff --git a/include/local.h b/include/local.h index ed6ba9366..9d9deeac6 100644 --- a/include/local.h +++ b/include/local.h @@ -236,7 +236,6 @@ int safe_strtol(const char *str, long *val); int snd_send_fd(int sock, void *data, size_t len, int fd); int snd_receive_fd(int sock, void *data, size_t len, int *fd); -size_t snd_strlcpy(char *dst, const char *src, size_t size); /* * error messages diff --git a/src/conf.c b/src/conf.c index 1bcd65c87..1a760b896 100644 --- a/src/conf.c +++ b/src/conf.c @@ -2309,6 +2309,47 @@ int snd_config_make(snd_config_t **config, const char *id, return _snd_config_make(config, &id1, type); } +/** + * \brief Creates a configuration node and add it to the parent node. + * \param[out] config The function puts the handle to the new node at + * the address specified by \a config. + * \param[in] id The id of the new node. + * \param[in] type The type of the new node. + * \param[in] parent handle for the parent node. + * \return Zero if successful, otherwise a negative error code. + * + * This functions creates a new node of the specified type. + * The new node has id \a id, which may be \c NULL. + * and adds it to the parent node. + * + * The value of the new node is zero (for numbers), or \c NULL (for + * strings and pointers), or empty (for compound nodes). + * + * \par Errors: + *
+ *
-ENOMEM
Out of memory. + *
+ */ +int snd_config_make_add(snd_config_t **config, char *id, + snd_config_type_t type, snd_config_t *parent) +{ + char *id1; + int ret; + + assert(config); + if (id) { + id1 = strdup(id); + if (!id1) + return -ENOMEM; + } else + id1 = NULL; + ret = _snd_config_make(config, &id1, type); + if (ret < 0) + return ret; + + return snd_config_add(parent, *config); +} + /** * \brief Creates an integer configuration node. * \param[out] config The function puts the handle to the new node at