 // Alternative implementation of asprintf()
 // It should be enabled for Windows.  (Linux/Mac (or better GnuCC) have it)


  // note: __WIN32__ is not standard; it's defined by #include <tcl.h>
  //       _WIN32 is the standard way
  //      see http://nadeausoftware.com/articles/2012/01/c_c_tip_how_use_compiler_predefined_macros_detect_operating_system
#ifdef _WIN32

#include "asprintf.h"   // WARNING: must be declared BEFORE <stdio.h>

#include <stdio.h>   //vsnprintf
#include <stdlib.h>  //malloc,free
#include <stdarg.h>  //va_start et al 


int asprintf(char **str, char *fmt, ...) {
	va_list argp;
	va_start(argp,fmt);
	
	char one_char[1];
	int len = vsnprintf(one_char, 1, fmt, argp);
	if( len < 1 ) {
		*str = NULL;
		return len;
	}
	va_end(argp);
	
	*str = malloc(len+1);
	if( !str ) {
		return -1;
	}
	va_start(argp, fmt);
	vsnprintf(*str, len+1, fmt, argp);
	va_end(argp);
	return len;
}

#endif