Please check the -Wall -Wextra errors! U must check the return of asprintf() variants.... And then you can use the optimized append/insert that does not need to strlen()
On 4/9/10, Enlightenment SVN <[email protected]> wrote: > Log: > Adding strbuf printfs > Author: tiago > Date: 2010-04-09 08:56:20 -0700 (Fri, 09 Apr 2010) > New Revision: 47877 > > Modified: > trunk/eina/src/include/eina_strbuf.h trunk/eina/src/lib/eina_strbuf.c > > Modified: trunk/eina/src/include/eina_strbuf.h > =================================================================== > --- trunk/eina/src/include/eina_strbuf.h 2010-04-09 15:53:07 UTC (rev > 47876) > +++ trunk/eina/src/include/eina_strbuf.h 2010-04-09 15:56:20 UTC (rev > 47877) > @@ -32,12 +32,16 @@ > 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,7 +115,35 @@ > */ > #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); > EAPI char *eina_strbuf_string_steal(Eina_Strbuf *buf) EINA_MALLOC > EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(1); > > Modified: trunk/eina/src/lib/eina_strbuf.c > =================================================================== > --- trunk/eina/src/lib/eina_strbuf.c 2010-04-09 15:53:07 UTC (rev 47876) > +++ trunk/eina/src/lib/eina_strbuf.c 2010-04-09 15:56:20 UTC (rev 47877) > @@ -14,6 +14,7 @@ > #include "eina_safety_checks.h" > #include "eina_strbuf.h" > > +#include <stdio.h> > #include <stdlib.h> > #include <string.h> > > @@ -458,6 +459,65 @@ > } > > /** > + * @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 @@ > } > > /** > + * @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. > > > ------------------------------------------------------------------------------ > Download Intel® Parallel Studio Eval > Try the new software tools for yourself. Speed compiling, find bugs > proactively, and fine-tune applications for parallel performance. > See why Intel Parallel Studio got high marks during beta. > http://p.sf.net/sfu/intel-sw-dev > _______________________________________________ > enlightenment-svn mailing list > [email protected] > https://lists.sourceforge.net/lists/listinfo/enlightenment-svn > -- Sent from my mobile device Gustavo Sverzut Barbieri http://profusion.mobi embedded systems -------------------------------------- MSN: [email protected] Skype: gsbarbieri Mobile: +55 (19) 9225-2202 ------------------------------------------------------------------------------ Download Intel® Parallel Studio Eval Try the new software tools for yourself. Speed compiling, find bugs proactively, and fine-tune applications for parallel performance. See why Intel Parallel Studio got high marks during beta. http://p.sf.net/sfu/intel-sw-dev _______________________________________________ enlightenment-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/enlightenment-devel
