aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVadim Yanitskiy <vyanitskiy@sysmocom.de>2021-06-07 16:30:28 +0200
committerVadim Yanitskiy <vyanitskiy@sysmocom.de>2021-10-13 14:03:26 +0300
commitc549719088ebd71677626e14329cd0c1aeef5622 (patch)
treec63d597e782fc4ef4ad1ab6850ef5204913b2be3
parent69cc4b613b7ca2b7f8b70f30d31069949ac952ce (diff)
utils: introduce osmo_talloc_replace_string_fmt()
-rw-r--r--include/osmocom/core/utils.h2
-rw-r--r--src/utils.c21
2 files changed, 23 insertions, 0 deletions
diff --git a/include/osmocom/core/utils.h b/include/osmocom/core/utils.h
index 85a8cb31..1fdb0eb8 100644
--- a/include/osmocom/core/utils.h
+++ b/include/osmocom/core/utils.h
@@ -117,6 +117,8 @@ static inline void osmo_talloc_replace_string(void *ctx, char **dst, const char
*dst = talloc_strdup(ctx, newstr);
}
+void osmo_talloc_replace_string_fmt(void *ctx, char **dst, const char *fmt, ...);
+
/*! Append to a string and re-/allocate if necessary.
* \param[in] ctx Talloc context to use for initial allocation.
* \param[in,out] dest char* to re-/allocate and append to.
diff --git a/src/utils.c b/src/utils.c
index c180595a..626dcb48 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -1493,4 +1493,25 @@ int osmo_str_to_int(int *result, const char *str, int base, int min_val, int max
return rc;
}
+/*! Replace a string using talloc and release its prior content (if any).
+ * This is a format string capable equivalent of osmo_talloc_replace_string().
+ * \param[in] ctx Talloc context to use for allocation.
+ * \param[out] dst Pointer to string, will be updated with ptr to new string.
+ * \param[in] fmt Format string that will be copied to newly allocated string. */
+void osmo_talloc_replace_string_fmt(void *ctx, char **dst, const char *fmt, ...)
+{
+ char *name = NULL;
+
+ if (fmt != NULL) {
+ va_list ap;
+
+ va_start(ap, fmt);
+ name = talloc_vasprintf(ctx, fmt, ap);
+ va_end(ap);
+ }
+
+ talloc_free(*dst);
+ *dst = name;
+}
+
/*! @} */