Skip to content
mprintf.c 26.4 KiB
Newer Older
Daniel Stenberg's avatar
Daniel Stenberg committed
  int retcode;
  va_list ap_save; /* argument pointer */
  va_start(ap_save, format);
  retcode = curl_mvsnprintf(buffer, maxlength, format, ap_save);
  va_end(ap_save);
Daniel Stenberg's avatar
Daniel Stenberg committed
  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);
    if(!infop->buffer)
      return -1; /* fail */
    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) {
      return -1;
    }
    infop->buffer = newptr;
    infop->alloc *= 2;
  }

  infop->buffer[ infop->len ] = output;

  infop->len++;

  return output; /* fputc() returns like this on success */
}

char *curl_maprintf(const char *format, ...)
Daniel Stenberg's avatar
Daniel Stenberg committed
{
  va_list ap_save; /* argument pointer */
  int retcode;
  struct asprintf info;

  info.buffer = NULL;
  info.len = 0;
  info.alloc = 0;

  va_start(ap_save, format);
  retcode = dprintf_formatf(&info, alloc_addbyter, format, ap_save);
  va_end(ap_save);
  if(-1 == retcode) {
    if(info.alloc)
      free(info.buffer);
    return NULL;
  }
  if(info.alloc) {
Daniel Stenberg's avatar
Daniel Stenberg committed
    info.buffer[info.len] = 0; /* we terminate this with a zero byte */
    return info.buffer;
  }
  else
char *curl_mvaprintf(const char *format, va_list ap_save)
Daniel Stenberg's avatar
Daniel Stenberg committed
{
  int retcode;
  struct asprintf info;

  info.buffer = NULL;
  info.len = 0;
  info.alloc = 0;

  retcode = dprintf_formatf(&info, alloc_addbyter, format, ap_save);
  if(-1 == retcode) {
    if(info.alloc)
      free(info.buffer);
    return NULL;
  }

  if(info.alloc) {
Daniel Stenberg's avatar
Daniel Stenberg committed
    info.buffer[info.len] = 0; /* we terminate this with a zero byte */
    return info.buffer;
  }
  else
Daniel Stenberg's avatar
Daniel Stenberg committed
}

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, ...)
Daniel Stenberg's avatar
Daniel Stenberg committed
{
  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 */
Daniel Stenberg's avatar
Daniel Stenberg committed
extern int fputc(int, FILE *);
Daniel Stenberg's avatar
Daniel Stenberg committed

int curl_mprintf(const char *format, ...)
Daniel Stenberg's avatar
Daniel Stenberg committed
{
  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, ...)
Daniel Stenberg's avatar
Daniel Stenberg committed
{
  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)
Daniel Stenberg's avatar
Daniel Stenberg committed
{
  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)
Daniel Stenberg's avatar
Daniel Stenberg committed
{
  return dprintf_formatf(stdout, fputc, format, ap_save);
}

int curl_mvfprintf(FILE *whereto, const char *format, va_list ap_save)
Daniel Stenberg's avatar
Daniel Stenberg committed
{
  return dprintf_formatf(whereto, fputc, format, ap_save);
}

#ifdef DPRINTF_DEBUG
int main()
{
  char buffer[129];
  char *ptr;
#ifdef HAVE_LONGLONG
Daniel Stenberg's avatar
Daniel Stenberg committed
  long long hullo;
  dprintf("%3$12s %1$s %2$qd %4$d\n", "daniel", hullo, "stenberg", 65);
#endif

  mprintf("%3d %5d\n", 10, 1998);
  
  ptr=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
  mprintf(buffer, "%s %s %d", "daniel", "stenberg", 19988);
  puts(buffer);

  mfprintf(stderr, "%s %#08x\n", "dummy", 65);

  printf("%s %#08x\n", "dummy", 65);
  {
    double tryout = 3.14156592;
    mprintf(buffer, "%.2g %G %f %e %E", tryout, tryout, tryout, tryout, tryout);
    puts(buffer);
    printf("%.2g %G %f %e %E\n", tryout, tryout, tryout, tryout, tryout);
  }
#endif

  return 0;
}

#endif