diff --git a/src/htsmsg.c b/src/htsmsg.c index dab63ee0..5dd67e3a 100644 --- a/src/htsmsg.c +++ b/src/htsmsg.c @@ -316,6 +316,16 @@ htsmsg_get_s64(htsmsg_t *msg, const char *name, int64_t *s64p) return 0; } +/** + * + */ +int64_t +htsmsg_get_s64_or_default(htsmsg_t *msg, const char *name, int64_t def) +{ + int64_t s64; + return htsmsg_get_s64(msg, name, &s64) ? def : s64; +} + /** * */ @@ -363,15 +373,13 @@ htsmsg_get_u32(htsmsg_t *msg, const char *name, uint32_t *u32p) /** * */ -int +uint32_t htsmsg_get_u32_or_default(htsmsg_t *msg, const char *name, uint32_t def) { uint32_t u32; - return htsmsg_get_u32(msg, name, &u32) ? def : u32; + return htsmsg_get_u32(msg, name, &u32) ? def : u32; } - - /* * */ @@ -449,6 +457,13 @@ htsmsg_get_str(htsmsg_t *msg, const char *name) } +const char * +htsmsg_get_str_or_default(htsmsg_t *msg, const char *name, const char *def) +{ + const char *str = htsmsg_get_str(msg, name); + return str ?: def; +} + /* * */ diff --git a/src/htsmsg.h b/src/htsmsg.h index f74092ac..d7d2bb05 100644 --- a/src/htsmsg.h +++ b/src/htsmsg.h @@ -160,6 +160,15 @@ void htsmsg_add_binptr(htsmsg_t *msg, const char *name, const void *bin, */ int htsmsg_get_u32(htsmsg_t *msg, const char *name, uint32_t *u32p); +/** + * Return the field \p name as an u32. + * + * @return An unsigned 32 bit integer or def if the field can not be found + * or if conversion is not possible. + */ +uint32_t htsmsg_get_u32_or_default + (htsmsg_t *msg, const char *name, uint32_t def); + /** * Get an integer as an signed 32 bit integer. * @@ -178,6 +187,16 @@ int htsmsg_get_s32(htsmsg_t *msg, const char *name, int32_t *s32p); */ int htsmsg_get_s64(htsmsg_t *msg, const char *name, int64_t *s64p); + +/** + * Return the field \p name as an s64. + * + * @return A signed 64 bit integer or def if the field can not be found + * or if conversion is not possible. + */ +int64_t htsmsg_get_s64_or_default + (htsmsg_t *msg, const char *name, int64_t def); + /** * Get an integer as an unsigned 64 bit integer. * @@ -217,6 +236,15 @@ htsmsg_t *htsmsg_get_list(htsmsg_t *msg, const char *name); */ const char *htsmsg_get_str(htsmsg_t *msg, const char *name); +/** + * Get a field of type 'string'. No copying is done. + * + * @return def if the field can not be found or not of string type. + * Otherwise a pointer to the data is returned. + */ +const char *htsmsg_get_str_or_default + (htsmsg_t *msg, const char *name, const char *def); + /** * Get a field of type 'map'. No copying is done. * @@ -236,14 +264,6 @@ htsmsg_t *htsmsg_get_map_multi(htsmsg_t *msg, ...); */ const char *htsmsg_field_get_string(htsmsg_field_t *f); -/** - * Return the field \p name as an u32. - * - * @return An unsigned 32 bit integer or NULL if the field can not be found - * or if conversion is not possible. - */ -int htsmsg_get_u32_or_default(htsmsg_t *msg, const char *name, uint32_t def); - /** * Remove the given field called \p name from the message \p msg. */ diff --git a/src/htsmsg_xml.c b/src/htsmsg_xml.c index 0dc40332..e8272763 100644 --- a/src/htsmsg_xml.c +++ b/src/htsmsg_xml.c @@ -897,7 +897,7 @@ const char * htsmsg_xml_get_attr_str ( htsmsg_t *tag, const char *name ) { htsmsg_t *attr = htsmsg_get_map(tag, "attrib"); - if (attr) return htsmsg_get_str(tag, name); + if (attr) return htsmsg_get_str(attr, name); return NULL; }