From 38935e9d3c4f5d3df1290b23728fc7ded231f2b6 Mon Sep 17 00:00:00 2001 From: tiago Date: Fri, 9 Apr 2010 15:56:20 +0000 Subject: [PATCH] Adding strbuf printfs git-svn-id: http://svn.enlightenment.org/svn/e/trunk/eina@47877 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33 --- src/include/eina_strbuf.h | 32 +++++++++++++ src/lib/eina_strbuf.c | 117 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 149 insertions(+) diff --git a/src/include/eina_strbuf.h b/src/include/eina_strbuf.h index 3883daa..a6f3282 100644 --- a/src/include/eina_strbuf.h +++ b/src/include/eina_strbuf.h @@ -32,12 +32,16 @@ EAPI Eina_Bool eina_strbuf_append_escaped(Eina_Strbuf *buf, const char *str) EIN EAPI Eina_Bool eina_strbuf_append_n(Eina_Strbuf *buf, const char *str, size_t maxlen) EINA_ARG_NONNULL(1, 2); EAPI Eina_Bool eina_strbuf_append_length(Eina_Strbuf *buf, const char *str, size_t length) EINA_ARG_NONNULL(1, 2); EAPI Eina_Bool eina_strbuf_append_char(Eina_Strbuf *buf, char c) EINA_ARG_NONNULL(1); +EAPI Eina_Bool eina_strbuf_append_printf(Eina_Strbuf *buf, const char *fmt, ...) EINA_ARG_NONNULL(1, 2) EINA_PRINTF(2, 3); +EAPI Eina_Bool eina_strbuf_append_vprintf(Eina_Strbuf *buf, const char *fmt, va_list args) EINA_ARG_NONNULL(1, 2); EAPI Eina_Bool eina_strbuf_insert(Eina_Strbuf *buf, const char *str, size_t pos) EINA_ARG_NONNULL(1, 2); EAPI Eina_Bool eina_strbuf_insert_escaped(Eina_Strbuf *buf, const char *str, size_t pos) EINA_ARG_NONNULL(1, 2); EAPI Eina_Bool eina_strbuf_insert_n(Eina_Strbuf *buf, const char *str, size_t maxlen, size_t pos) EINA_ARG_NONNULL(1, 2); EAPI Eina_Bool eina_strbuf_insert_length(Eina_Strbuf *buf, const char *str, size_t length, size_t pos) EINA_ARG_NONNULL(1, 2); EAPI Eina_Bool eina_strbuf_insert_char(Eina_Strbuf *buf, char c, size_t pos) EINA_ARG_NONNULL(1); +EAPI Eina_Bool eina_strbuf_insert_printf(Eina_Strbuf *buf, const char *fmt, size_t pos, ...) EINA_ARG_NONNULL(1, 2) EINA_PRINTF(2, 4); +EAPI Eina_Bool eina_strbuf_insert_vprintf(Eina_Strbuf *buf, const char *fmt, size_t pos, va_list args) EINA_ARG_NONNULL(1, 2); /** * @def eina_strbuf_prepend(buf, str) @@ -111,6 +115,34 @@ EAPI Eina_Bool eina_strbuf_insert_char(Eina_Strbuf *buf, char c, size_t pos) EIN */ #define eina_strbuf_prepend_char(buf, c) eina_strbuf_insert_char(buf, c, 0) +/** + * @def eina_strbuf_prepend_printf(buf, fmt, ...) + * @brief Prepend the given string to the given buffer + * + * @param buf The string buffer to prepend to. + * @param str The string to prepend. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * This macro is calling eina_strbuf_insert_printf() at position 0.If @p buf + * can't prepend it, #EINA_FALSE is returned, otherwise #EINA_TRUE is + * returned. + */ +#define eina_strbuf_prepend_printf(buf, fmt, ...) eina_strbuf_insert_printf(buf, fmt, 0, ##__VA_ARGS__) + +/** + * @def eina_strbuf_prepend_vprintf(buf, fmt, args) + * @brief Prepend the given string to the given buffer + * + * @param buf The string buffer to prepend to. + * @param fmt The string to prepend. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * This macro is calling eina_strbuf_insert_vprintf() at position 0.If @p buf + * can't prepend it, #EINA_FALSE is returned, otherwise #EINA_TRUE is + * returned. + */ +#define eina_strbuf_prepend_vprintf(buf, fmt, args) eina_strbuf_insert_vprintf(buf, fmt, 0, args) + EAPI Eina_Bool eina_strbuf_remove(Eina_Strbuf *buf, size_t start, size_t end) EINA_ARG_NONNULL(1); EAPI const char *eina_strbuf_string_get(const Eina_Strbuf *buf) EINA_ARG_NONNULL(1); diff --git a/src/lib/eina_strbuf.c b/src/lib/eina_strbuf.c index 751608b..8ac40e8 100644 --- a/src/lib/eina_strbuf.c +++ b/src/lib/eina_strbuf.c @@ -14,6 +14,7 @@ #include "eina_safety_checks.h" #include "eina_strbuf.h" +#include #include #include @@ -458,6 +459,65 @@ eina_strbuf_append_length(Eina_Strbuf *buf, const char *str, size_t length) } /** + * @brief Append a string to a buffer, reallocating as necessary. + * + * @param buf The string buffer to append to. + * @param fmt The string to append. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * @see eina_strbuf_append() + */ +EAPI Eina_Bool +eina_strbuf_append_printf(Eina_Strbuf *buf, const char *fmt, ...) +{ + va_list args; + char *str; + Eina_Bool ret; + + EINA_SAFETY_ON_NULL_RETURN_VAL(fmt, EINA_FALSE); + EINA_MAGIC_CHECK_STRBUF(buf, EINA_FALSE); + + va_start(args, fmt); + vasprintf(&str, fmt, args); + va_end(args); + + if (!str) + return EINA_FALSE; + + ret = eina_strbuf_append(buf, str); + free(str); + return ret; +} + +/** + * @brief Append a string to a buffer, reallocating as necessary. + * + * @param buf The string buffer to append to. + * @param fmt The string to append. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * @see eina_strbuf_append() + */ +EAPI Eina_Bool +eina_strbuf_append_vprintf(Eina_Strbuf *buf, const char *fmt, va_list args) +{ + char *str; + Eina_Bool ret; + + EINA_SAFETY_ON_NULL_RETURN_VAL(fmt, EINA_FALSE); + EINA_MAGIC_CHECK_STRBUF(buf, EINA_FALSE); + + vasprintf(&str, fmt, args); + + if (!str) + return EINA_FALSE; + + ret = eina_strbuf_append(buf, str); + free(str); + return ret; +} + +/** * @brief Insert a string to a buffer, reallocating as necessary. * * @param buf The string buffer to insert. @@ -632,6 +692,63 @@ eina_strbuf_insert_char(Eina_Strbuf *buf, char c, size_t pos) } /** + * @brief Insert a string to a buffer, reallocating as necessary. + * + * @param buf The string buffer to insert. + * @param fmt The string to insert. + * @param pos The position to insert the string. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + */ +EAPI Eina_Bool +eina_strbuf_insert_printf(Eina_Strbuf *buf, const char *fmt, size_t pos, ...) +{ + va_list args; + char *str; + Eina_Bool ret; + + EINA_SAFETY_ON_NULL_RETURN_VAL(fmt, EINA_FALSE); + EINA_MAGIC_CHECK_STRBUF(buf, EINA_FALSE); + + va_start(args, fmt); + vasprintf(&str, fmt, args); + va_end(args); + + if (!str) + return EINA_FALSE; + + ret = eina_strbuf_insert(buf, str, pos); + free(str); + return ret; +} + +/** + * @brief Insert a string to a buffer, reallocating as necessary. + * + * @param buf The string buffer to insert. + * @param fmt The string to insert. + * @param pos The position to insert the string. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + */ +EAPI Eina_Bool +eina_strbuf_insert_vprintf(Eina_Strbuf *buf, const char *fmt, size_t pos, va_list args) +{ + char *str; + Eina_Bool ret; + + EINA_SAFETY_ON_NULL_RETURN_VAL(fmt, EINA_FALSE); + EINA_MAGIC_CHECK_STRBUF(buf, EINA_FALSE); + + vasprintf(&str, fmt, args); + + if (!str) + return EINA_FALSE; + + ret = eina_strbuf_insert(buf, str, pos); + free(str); + return ret; +} + +/** * @brief Remove a slice of the given string buffer. * * @param buf The string buffer to remove a slice. -- 2.7.4