Newer
Older
int retcode;
struct nsprintf info;
info.buffer = buffer;
info.length = 0;
info.max = maxlength;
retcode = dprintf_formatf(&info, addbyter, format, ap_save);
if(info.max) {
/* we terminate this with a zero byte */
if(info.max == info.length)
/* we're at maximum, scrap the last letter */
info.buffer[-1] = 0;
else
info.buffer[0] = 0;
}
int curl_msnprintf(char *buffer, size_t maxlength, const char *format, ...)
va_list ap_save; /* argument pointer */
va_start(ap_save, format);
retcode = curl_mvsnprintf(buffer, maxlength, format, ap_save);
va_end(ap_save);
return retcode;
}
/* fputc() look-alike */
static int alloc_addbyter(int output, FILE *data)
{
struct asprintf *infop=(struct asprintf *)data;
if(!infop->buffer) {
infop->buffer=(char *)malloc(32);
Daniel Stenberg
committed
if(!infop->buffer) {
infop->fail = TRUE;
Daniel Stenberg
committed
}
infop->alloc = 32;
infop->len =0;
}
else if(infop->len+1 >= infop->alloc) {
char *newptr;
newptr = (char *)realloc(infop->buffer, infop->alloc*2);
if(!newptr) {
Daniel Stenberg
committed
infop->fail = TRUE;
return -1;
}
infop->buffer = newptr;
infop->alloc *= 2;
}
infop->buffer[ infop->len ] = (char)output;
infop->len++;
return output; /* fputc() returns like this on success */
}
char *curl_maprintf(const char *format, ...)
{
va_list ap_save; /* argument pointer */
int retcode;
struct asprintf info;
info.buffer = NULL;
info.len = 0;
info.alloc = 0;
Daniel Stenberg
committed
info.fail = FALSE;
va_start(ap_save, format);
retcode = dprintf_formatf(&info, alloc_addbyter, format, ap_save);
va_end(ap_save);
Daniel Stenberg
committed
if((-1 == retcode) || info.fail) {
if(info.alloc)
free(info.buffer);
return NULL;
}
if(info.alloc) {
info.buffer[info.len] = 0; /* we terminate this with a zero byte */
return info.buffer;
}
else
return strdup("");
char *curl_mvaprintf(const char *format, va_list ap_save)
{
int retcode;
struct asprintf info;
info.buffer = NULL;
info.len = 0;
info.alloc = 0;
Daniel Stenberg
committed
info.fail = FALSE;
retcode = dprintf_formatf(&info, alloc_addbyter, format, ap_save);
Daniel Stenberg
committed
if((-1 == retcode) || info.fail) {
if(info.alloc)
free(info.buffer);
return NULL;
}
if(info.alloc) {
info.buffer[info.len] = 0; /* we terminate this with a zero byte */
return info.buffer;
}
else
return strdup("");
}
static int storebuffer(int output, FILE *data)
{
char **buffer = (char **)data;
**buffer = (char)output;
(*buffer)++;
return output; /* act like fputc() ! */
}
int curl_msprintf(char *buffer, const char *format, ...)
{
va_list ap_save; /* argument pointer */
int retcode;
va_start(ap_save, format);
retcode = dprintf_formatf(&buffer, storebuffer, format, ap_save);
va_end(ap_save);
*buffer=0; /* we terminate this with a zero byte */
return retcode;
}
#ifndef WIN32 /* not needed on win32 */
#endif
int curl_mprintf(const char *format, ...)
{
int retcode;
va_list ap_save; /* argument pointer */
va_start(ap_save, format);
retcode = dprintf_formatf(stdout, fputc, format, ap_save);
va_end(ap_save);
return retcode;
}
int curl_mfprintf(FILE *whereto, const char *format, ...)
{
int retcode;
va_list ap_save; /* argument pointer */
va_start(ap_save, format);
retcode = dprintf_formatf(whereto, fputc, format, ap_save);
va_end(ap_save);
return retcode;
}
int curl_mvsprintf(char *buffer, const char *format, va_list ap_save)
{
int retcode;
retcode = dprintf_formatf(&buffer, storebuffer, format, ap_save);
*buffer=0; /* we terminate this with a zero byte */
return retcode;
}
int curl_mvprintf(const char *format, va_list ap_save)
{
return dprintf_formatf(stdout, fputc, format, ap_save);
}
int curl_mvfprintf(FILE *whereto, const char *format, va_list ap_save)
{
return dprintf_formatf(whereto, fputc, format, ap_save);
}
#ifdef DPRINTF_DEBUG
int main()
{
char buffer[129];
char *ptr;
#ifdef ENABLE_64BIT
long long one=99;
long long two=100;
long long test = 0x1000000000LL;
curl_mprintf("%lld %lld %lld\n", one, two, test);
ptr=curl_maprintf("test this then baby %s%s%s%s%s%s %d %d %d loser baby get a hit in yer face now!", "", "pretty long string pretty long string pretty long string pretty long string pretty long string", "/", "/", "/", "pretty long string", 1998, 1999, 2001);
puts(ptr);
memset(ptr, 55, strlen(ptr)+1);
free(ptr);
#if 1
curl_mprintf(buffer, "%s %s %d", "daniel", "stenberg", 19988);
curl_mfprintf(stderr, "%s %#08x\n", "dummy", 65);
printf("%s %#08x\n", "dummy", 65);
{
double tryout = 3.14156592;
curl_mprintf(buffer, "%.2g %G %f %e %E", tryout, tryout, tryout, tryout, tryout);