eina: add functions to alloc strings from a printf fmt

This commit is contained in:
Jorge Zapata 2013-09-23 21:13:18 +02:00 committed by Cedric Bail
parent b98ee971f3
commit b5fce696c7
2 changed files with 74 additions and 0 deletions

View File

@ -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;
}

View File

@ -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"
/**