aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJorge Zapata <jorgeluis.zapata@gmail.com>2013-09-23 21:13:18 +0200
committerCedric Bail <cedric.bail@samsung.com>2013-09-24 12:20:27 +0900
commitb5fce696c743c50ea0a049c4f879756b5ed231d4 (patch)
tree2475e05d3bec05f834289754b2cdc5fd5c38e2da
parenteina: add a substraction in rectangles and more helpers (diff)
downloadefl-b5fce696c743c50ea0a049c4f879756b5ed231d4.tar.gz
eina: add functions to alloc strings from a printf fmt
-rw-r--r--src/lib/eina/eina_str.c41
-rw-r--r--src/lib/eina/eina_str.h33
2 files changed, 74 insertions, 0 deletions
diff --git a/src/lib/eina/eina_str.c b/src/lib/eina/eina_str.c
index 11fef3c7c7..283476b29a 100644
--- a/src/lib/eina/eina_str.c
+++ b/src/lib/eina/eina_str.c
@@ -652,3 +652,44 @@ eina_str_toupper(char **str)
for (p = *str; (*p); p++)
*p = toupper((unsigned char)(*p));
}
+
+EAPI size_t
+eina_str_vprintf_length(const char *format, va_list args)
+{
+ char c;
+ size_t len;
+
+ len = vsnprintf(&c, 1, format, args) + 1;
+ return len;
+}
+
+EAPI char *
+eina_str_vprintf_dup(const char *format, va_list args)
+{
+ size_t length;
+ char *ret;
+ va_list copy;
+
+ /* be sure to use a copy or the printf implementation will
+ * step into the args
+ */
+ va_copy(copy, args);
+ length = eina_str_vprintf_length(format, copy);
+ va_end(copy);
+
+ ret = calloc(length, sizeof(char));
+ vsprintf(ret, format, args);
+ return ret;
+}
+
+EAPI char *
+eina_str_printf_dup(const char *format, ...)
+{
+ char *ret;
+ va_list args;
+
+ va_start(args, format);
+ ret = eina_str_vprintf_dup(format, args);
+ va_end(args);
+ return ret;
+}
diff --git a/src/lib/eina/eina_str.h b/src/lib/eina/eina_str.h
index dae592bac6..1d8edae88f 100644
--- a/src/lib/eina/eina_str.h
+++ b/src/lib/eina/eina_str.h
@@ -345,6 +345,39 @@ static inline size_t eina_str_join(char *dst, size_t size, char sep, const char
static inline size_t eina_strlen_bounded(const char *str, size_t maxlen) EINA_PURE EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(1);
+/**
+ * @brief Gets the length needed for a string based on a printf format and args
+ *
+ * @param format The printf format
+ * @param args The printf args
+ * @return The length needed for such format and args
+ * @since 1.7.9
+ */
+EAPI size_t eina_str_vprintf_length(const char *format, va_list args);
+
+/**
+ * @brief Gets a newly allocated string that represents the printf format and args
+ *
+ * @param format The printf format
+ * @param args The printf args
+ * @return A newly allocated string
+ *
+ * @see eina_str_dup_printf
+ * @since 1.7.9
+ */
+EAPI char * eina_str_vprintf_dup(const char *format, va_list args);
+
+/**
+ * @brief Gets a newly allocated string that represents the printf format and args
+ *
+ * @param format The printf format
+ * @return A newly allocated string
+ *
+ * @see eina_str_dup_vprintf
+ * @since 1.7.9
+ */
+EAPI char * eina_str_printf_dup(const char *format, ...);
+
#include "eina_inline_str.x"
/**