[PATCH] asprintf() and vasprintf() are used but missing in Win32, those are required by neither the ISO C Standard, nor by POSIX; GNU and BSD provide them as extras.

582 views
Skip to first unread message

Adam Strzelecki

unread,
Oct 12, 2009, 11:08:47 AM10/12/09
to jansso...@googlegroups.com, Adam Strzelecki
Taken from: http://lists.zerezo.com/mingw-users/msg12649.html
---
configure.ac | 2 +-
src/dump.c | 22 ++++++++++++++++++++++
2 files changed, 23 insertions(+), 1 deletions(-)

diff --git a/configure.ac b/configure.ac
index 2835ad3..3402b37 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1,4 +1,4 @@
-AC_PREREQ([2.63])
+AC_PREREQ([2.61])
AC_INIT([jansson], [1.0.3+], [pe...@digip.org])

AM_INIT_AUTOMAKE([1.10 foreign])
diff --git a/src/dump.c b/src/dump.c
index 042b0c7..b276bfe 100644
--- a/src/dump.c
+++ b/src/dump.c
@@ -13,6 +13,28 @@
#include <jansson.h>
#include "strbuffer.h"

+#ifdef __WIN32
+#include <stdarg.h>
+int vasprintf( char **sptr, char *fmt, va_list argv )
+{
+ int wanted = vsnprintf( *sptr = NULL, 0, fmt, argv );
+ if( (wanted > 0) && ((*sptr = malloc( 1 + wanted )) != NULL) )
+ return vsprintf( *sptr, fmt, argv );
+
+ return wanted;
+}
+
+int asprintf( char **sptr, char *fmt, ... )
+{
+ int retval;
+ va_list argv;
+ va_start( argv, fmt );
+ retval = vasprintf( sptr, fmt, argv );
+ va_end( argv );
+ return retval;
+}
+#endif
+
typedef int (*dump_func)(const char *buffer, int size, void *data);

struct string
--
1.6.4.2

Adam Strzelecki

unread,
Oct 12, 2009, 11:15:59 AM10/12/09
to jansso...@googlegroups.com, Adam Strzelecki
src/dump.c | 22 ++++++++++++++++++++++
1 files changed, 22 insertions(+), 0 deletions(-)

Adam Strzelecki

unread,
Oct 12, 2009, 11:17:02 AM10/12/09
to jansso...@googlegroups.com, Adam Strzelecki
---
configure.ac | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/configure.ac b/configure.ac
index 2835ad3..3402b37 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1,4 +1,4 @@
-AC_PREREQ([2.63])
+AC_PREREQ([2.61])
AC_INIT([jansson], [1.0.3+], [pe...@digip.org])

AM_INIT_AUTOMAKE([1.10 foreign])

--
1.6.4.2

Petri Lehtinen

unread,
Oct 12, 2009, 3:18:35 PM10/12/09
to jansso...@googlegroups.com
Thanks for your patches! For this one, the best would be to remove the
call to asprintf() in the first place. But this might be a good
workaround.

BTW, the subject is too long. Please use a shorter one and provide
more information in subsequent lines in commit message.

More comments in-line.

>
> Taken from: http://lists.zerezo.com/mingw-users/msg12649.html

The linked message doesn't provide a copyright notice, so I'm
reluctant to use the code as-is.

Only asprintf is used, so this is a bit overkill. How about using
snprintf directly in asprintf instead of calling (the otherwise
useless) vasprintf?

And the replacement functions should be declared static to not mess up
people's namespaces.

Petri Lehtinen

unread,
Oct 12, 2009, 3:20:29 PM10/12/09
to jansso...@googlegroups.com
The autoconf version requirement was already relaxed to 2.59 in commit
afc9c1a23a139b0ed1bba2c375ca27ebd278c642. It was merged to master from
the 1.0 maintenance branch yesterday, so you may not have noticed.

Petri Lehtinen

unread,
Oct 13, 2009, 8:45:30 AM10/13/09
to jansso...@googlegroups.com
Thanks to Adam Strzelecki for reporting.
---
I pushed this to master. Thanks for reporting.

src/dump.c | 27 +++++++++++++--------------
1 files changed, 13 insertions(+), 14 deletions(-)

diff --git a/src/dump.c b/src/dump.c
index 93717ab..dad64f8 100644
--- a/src/dump.c
+++ b/src/dump.c
@@ -13,6 +13,9 @@
#include <jansson.h>
#include "strbuffer.h"

+#define MAX_INTEGER_STR_LENGTH 100
+#define MAX_REAL_STR_LENGTH 100


+
typedef int (*dump_func)(const char *buffer, int size, void *data);

struct string

@@ -126,30 +129,26 @@ static int do_dump(const json_t *json, unsigned long flags, int depth,

case JSON_INTEGER:
{
- char *buffer;
- int size, ret;
+ char buffer[MAX_INTEGER_STR_LENGTH];
+ int size;

- size = asprintf(&buffer, "%d", json_integer_value(json));
- if(size == -1)
+ size = snprintf(buffer, MAX_INTEGER_STR_LENGTH, "%d", json_integer_value(json));
+ if(size >= MAX_INTEGER_STR_LENGTH)
return -1;

- ret = dump(buffer, size, data);
- free(buffer);
- return ret;
+ return dump(buffer, size, data);
}

case JSON_REAL:
{
- char *buffer;
- int size, ret;
+ char buffer[MAX_REAL_STR_LENGTH];
+ int size;

- size = asprintf(&buffer, "%.17f", json_real_value(json));
- if(size == -1)
+ size = snprintf(buffer, MAX_REAL_STR_LENGTH, "%0.17f", json_real_value(json));
+ if(size >= MAX_REAL_STR_LENGTH)
return -1;

- ret = dump(buffer, size, data);
- free(buffer);
- return ret;
+ return dump(buffer, size, data);
}

case JSON_STRING:
--
1.6.5

Adam Strzelecki

unread,
Oct 13, 2009, 9:01:13 AM10/13/09
to jansso...@googlegroups.com
Petri,

Thanks for the latest Jansson changes! As an extra dropping asprintf
should make Jansson even faster as you don't use anymore subsequent
allocation and free for each int/real value.

Thanks again for great small library,

Regards,
--
Adam

Reply all
Reply to author
Forward
0 new messages