Re: [E-devel] [EGIT] [core/efl] master 04/04: eina: add functions to alloc strings from a printf fmt
Hello Lucas, On Wed, Sep 25, 2013 at 6:21 AM, Lucas De Marchi lucas.demar...@profusion.mobi wrote: On Tue, Sep 24, 2013 at 11:03 AM, Lucas De Marchi lucas.demar...@profusion.mobi wrote: On Tue, Sep 24, 2013 at 3:29 AM, Cedric BAIL cedric.b...@free.fr wrote: On Tue, Sep 24, 2013 at 2:14 PM, Lucas De Marchi lucas.demar...@profusion.mobi wrote: On Tue, Sep 24, 2013 at 12:20 AM, Jorge Zapata jorgeluis.zap...@gmail.com wrote: cedric pushed a commit to branch master. http://git.enlightenment.org/core/efl.git/commit/?id=b5fce696c743c50ea0a049c4f879756b5ed231d4 commit b5fce696c743c50ea0a049c4f879756b5ed231d4 Author: Jorge Zapata jorgeluis.zap...@gmail.com Date: Mon Sep 23 21:13:18 2013 +0200 eina: add functions to alloc strings from a printf fmt --- src/lib/eina/eina_str.c | 41 + src/lib/eina/eina_str.h | 33 + 2 files changed, 74 insertions(+) diff --git a/src/lib/eina/eina_str.c b/src/lib/eina/eina_str.c index 11fef3c..283476b 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; this is wrong if 1) vsnprintf returns an error (for example if there's a wrong format string) 2) format is - return value value should be 0, but will be 1 If you change the return value to ssize_t it's better and simpler if you do: return vsnprintf(NULL, 0, format, args); If you prefer to ignore the error and continue with an unsigned type, then you should check the return value for 0 before returning. +} + +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)); malloc instead? you are replacing all the chars below +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; +} why these instead of plain (v)asprintf? These functions are GNU extensions, not in C or POSIX., straight from the man page... sure... and they are available everywhere. I just committed some fixes and related changes, please take a look: devs/lucas/fix-eina-newfuncs Why is there only eina_str_vprintf_length() and not eina_str_printf_length()? Initially that function was only for local purposes and thus not exported, but then I thought it might be useful in some cases, indeed the not vargs version should be included too As I said above IMO we could remove the check for asprintf since it's in general available on everywhere... we use asprintf() in some other places already and the only platform that doesn't have it have the impl in evil_printf.h. I still think there isn't much point in adding these APIs otherwise let's at least let their implementation correct. You are correct, I created this functions because windows does not have an implementation for it, I was not aware that evil already had a similar implementation, thanks for pointing it out. From my POV given that evil already provides the missing functionality, I'm ok to just remove this new API Lucas De Marchi -- October Webinars: Code for Performance Free Intel webinars can help you accelerate application performance. Explore tips for MPI, OpenMP, advanced profiling, and more. Get the most from the latest Intel processors and coprocessors. See abstracts and register http://pubads.g.doubleclick.net/gampad/clk?id=60133471iu=/4140/ostg.clktrk ___ enlightenment-devel mailing list enlightenment-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/enlightenment-devel -- October Webinars: Code for Performance Free Intel webinars can help you accelerate application performance. Explore tips for MPI, OpenMP, advanced profiling, and more. Get the most from the latest Intel processors and coprocessors. See abstracts and register http://pubads.g.doubleclick.net/gampad/clk?id=60133471iu=/4140/ostg.clktrk ___ enlightenment-devel mailing
Re: [E-devel] [EGIT] [core/efl] master 04/04: eina: add functions to alloc strings from a printf fmt
On Tue, Sep 24, 2013 at 2:14 PM, Lucas De Marchi lucas.demar...@profusion.mobi wrote: On Tue, Sep 24, 2013 at 12:20 AM, Jorge Zapata jorgeluis.zap...@gmail.com wrote: cedric pushed a commit to branch master. http://git.enlightenment.org/core/efl.git/commit/?id=b5fce696c743c50ea0a049c4f879756b5ed231d4 commit b5fce696c743c50ea0a049c4f879756b5ed231d4 Author: Jorge Zapata jorgeluis.zap...@gmail.com Date: Mon Sep 23 21:13:18 2013 +0200 eina: add functions to alloc strings from a printf fmt --- src/lib/eina/eina_str.c | 41 + src/lib/eina/eina_str.h | 33 + 2 files changed, 74 insertions(+) diff --git a/src/lib/eina/eina_str.c b/src/lib/eina/eina_str.c index 11fef3c..283476b 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; this is wrong if 1) vsnprintf returns an error (for example if there's a wrong format string) 2) format is - return value value should be 0, but will be 1 If you change the return value to ssize_t it's better and simpler if you do: return vsnprintf(NULL, 0, format, args); If you prefer to ignore the error and continue with an unsigned type, then you should check the return value for 0 before returning. +} + +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)); malloc instead? you are replacing all the chars below +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; +} why these instead of plain (v)asprintf? These functions are GNU extensions, not in C or POSIX., straight from the man page... -- Cedric BAIL -- October Webinars: Code for Performance Free Intel webinars can help you accelerate application performance. Explore tips for MPI, OpenMP, advanced profiling, and more. Get the most from the latest Intel processors and coprocessors. See abstracts and register http://pubads.g.doubleclick.net/gampad/clk?id=60133471iu=/4140/ostg.clktrk ___ enlightenment-devel mailing list enlightenment-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/enlightenment-devel
Re: [E-devel] [EGIT] [core/efl] master 04/04: eina: add functions to alloc strings from a printf fmt
On Tue, Sep 24, 2013 at 3:29 AM, Cedric BAIL cedric.b...@free.fr wrote: On Tue, Sep 24, 2013 at 2:14 PM, Lucas De Marchi lucas.demar...@profusion.mobi wrote: On Tue, Sep 24, 2013 at 12:20 AM, Jorge Zapata jorgeluis.zap...@gmail.com wrote: cedric pushed a commit to branch master. http://git.enlightenment.org/core/efl.git/commit/?id=b5fce696c743c50ea0a049c4f879756b5ed231d4 commit b5fce696c743c50ea0a049c4f879756b5ed231d4 Author: Jorge Zapata jorgeluis.zap...@gmail.com Date: Mon Sep 23 21:13:18 2013 +0200 eina: add functions to alloc strings from a printf fmt --- src/lib/eina/eina_str.c | 41 + src/lib/eina/eina_str.h | 33 + 2 files changed, 74 insertions(+) diff --git a/src/lib/eina/eina_str.c b/src/lib/eina/eina_str.c index 11fef3c..283476b 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; this is wrong if 1) vsnprintf returns an error (for example if there's a wrong format string) 2) format is - return value value should be 0, but will be 1 If you change the return value to ssize_t it's better and simpler if you do: return vsnprintf(NULL, 0, format, args); If you prefer to ignore the error and continue with an unsigned type, then you should check the return value for 0 before returning. +} + +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)); malloc instead? you are replacing all the chars below +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; +} why these instead of plain (v)asprintf? These functions are GNU extensions, not in C or POSIX., straight from the man page... sure... and they are available everywhere. Lucas De Marchi -- October Webinars: Code for Performance Free Intel webinars can help you accelerate application performance. Explore tips for MPI, OpenMP, advanced profiling, and more. Get the most from the latest Intel processors and coprocessors. See abstracts and register http://pubads.g.doubleclick.net/gampad/clk?id=60133471iu=/4140/ostg.clktrk ___ enlightenment-devel mailing list enlightenment-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/enlightenment-devel
Re: [E-devel] [EGIT] [core/efl] master 04/04: eina: add functions to alloc strings from a printf fmt
On Tue, Sep 24, 2013 at 11:03 AM, Lucas De Marchi lucas.demar...@profusion.mobi wrote: On Tue, Sep 24, 2013 at 3:29 AM, Cedric BAIL cedric.b...@free.fr wrote: On Tue, Sep 24, 2013 at 2:14 PM, Lucas De Marchi lucas.demar...@profusion.mobi wrote: On Tue, Sep 24, 2013 at 12:20 AM, Jorge Zapata jorgeluis.zap...@gmail.com wrote: cedric pushed a commit to branch master. http://git.enlightenment.org/core/efl.git/commit/?id=b5fce696c743c50ea0a049c4f879756b5ed231d4 commit b5fce696c743c50ea0a049c4f879756b5ed231d4 Author: Jorge Zapata jorgeluis.zap...@gmail.com Date: Mon Sep 23 21:13:18 2013 +0200 eina: add functions to alloc strings from a printf fmt --- src/lib/eina/eina_str.c | 41 + src/lib/eina/eina_str.h | 33 + 2 files changed, 74 insertions(+) diff --git a/src/lib/eina/eina_str.c b/src/lib/eina/eina_str.c index 11fef3c..283476b 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; this is wrong if 1) vsnprintf returns an error (for example if there's a wrong format string) 2) format is - return value value should be 0, but will be 1 If you change the return value to ssize_t it's better and simpler if you do: return vsnprintf(NULL, 0, format, args); If you prefer to ignore the error and continue with an unsigned type, then you should check the return value for 0 before returning. +} + +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)); malloc instead? you are replacing all the chars below +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; +} why these instead of plain (v)asprintf? These functions are GNU extensions, not in C or POSIX., straight from the man page... sure... and they are available everywhere. I just committed some fixes and related changes, please take a look: devs/lucas/fix-eina-newfuncs Why is there only eina_str_vprintf_length() and not eina_str_printf_length()? As I said above IMO we could remove the check for asprintf since it's in general available on everywhere... we use asprintf() in some other places already and the only platform that doesn't have it have the impl in evil_printf.h. I still think there isn't much point in adding these APIs otherwise let's at least let their implementation correct. Lucas De Marchi -- October Webinars: Code for Performance Free Intel webinars can help you accelerate application performance. Explore tips for MPI, OpenMP, advanced profiling, and more. Get the most from the latest Intel processors and coprocessors. See abstracts and register http://pubads.g.doubleclick.net/gampad/clk?id=60133471iu=/4140/ostg.clktrk ___ enlightenment-devel mailing list enlightenment-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/enlightenment-devel
Re: [E-devel] [EGIT] [core/efl] master 04/04: eina: add functions to alloc strings from a printf fmt
s/@since 1.7.9/@since 1.8 On Tue, Sep 24, 2013 at 12:20 AM, Jorge Zapata jorgeluis.zap...@gmail.comwrote: cedric pushed a commit to branch master. http://git.enlightenment.org/core/efl.git/commit/?id=b5fce696c743c50ea0a049c4f879756b5ed231d4 commit b5fce696c743c50ea0a049c4f879756b5ed231d4 Author: Jorge Zapata jorgeluis.zap...@gmail.com Date: Mon Sep 23 21:13:18 2013 +0200 eina: add functions to alloc strings from a printf fmt --- src/lib/eina/eina_str.c | 41 + src/lib/eina/eina_str.h | 33 + 2 files changed, 74 insertions(+) diff --git a/src/lib/eina/eina_str.c b/src/lib/eina/eina_str.c index 11fef3c..283476b 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 dae592b..1d8edae 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 /** -- -- October Webinars: Code for Performance Free Intel webinars can help you accelerate application performance. Explore tips for MPI, OpenMP, advanced profiling, and more. Get the most from the latest Intel processors and coprocessors. See abstracts and register http://pubads.g.doubleclick.net/gampad/clk?id=60133471iu=/4140/ostg.clktrk ___ enlightenment-devel mailing list enlightenment-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/enlightenment-devel
Re: [E-devel] [EGIT] [core/efl] master 04/04: eina: add functions to alloc strings from a printf fmt
On Tue, Sep 24, 2013 at 12:20 AM, Jorge Zapata jorgeluis.zap...@gmail.com wrote: cedric pushed a commit to branch master. http://git.enlightenment.org/core/efl.git/commit/?id=b5fce696c743c50ea0a049c4f879756b5ed231d4 commit b5fce696c743c50ea0a049c4f879756b5ed231d4 Author: Jorge Zapata jorgeluis.zap...@gmail.com Date: Mon Sep 23 21:13:18 2013 +0200 eina: add functions to alloc strings from a printf fmt --- src/lib/eina/eina_str.c | 41 + src/lib/eina/eina_str.h | 33 + 2 files changed, 74 insertions(+) diff --git a/src/lib/eina/eina_str.c b/src/lib/eina/eina_str.c index 11fef3c..283476b 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; this is wrong if 1) vsnprintf returns an error (for example if there's a wrong format string) 2) format is - return value value should be 0, but will be 1 If you change the return value to ssize_t it's better and simpler if you do: return vsnprintf(NULL, 0, format, args); If you prefer to ignore the error and continue with an unsigned type, then you should check the return value for 0 before returning. +} + +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)); malloc instead? you are replacing all the chars below +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; +} why these instead of plain (v)asprintf? Lucas De Marchi -- October Webinars: Code for Performance Free Intel webinars can help you accelerate application performance. Explore tips for MPI, OpenMP, advanced profiling, and more. Get the most from the latest Intel processors and coprocessors. See abstracts and register http://pubads.g.doubleclick.net/gampad/clk?id=60133471iu=/4140/ostg.clktrk ___ enlightenment-devel mailing list enlightenment-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/enlightenment-devel