www.digitalmars.com         C & C++   DMDScript  

D - va_list / rolling your own printf / OutBuffer

reply "Sean L. Palmer" <seanpalmer earthlink.net> writes:
I'm trying to make my own variant of printf (one that prints into a dynamic
array of char) and am having problems with the OutBuffer class.

char[] Fmt(char[] fmtp, ...)
{
  va_list ap;
  ap = cast(va_list)&fmtp;
  ap += fmtp.size;
  printf(fmtp,*(int*)ap); // this works fine
  OutBuffer buf;
  buf.vprintf(fmtp, ap);  // this doesn't work, causes program termination
errorlevel 128
  return buf.toString();  // but if I don't put something into buf, this
crashes!
}

...

printf(Fmt("This is %dth test\n",45)); // should print This is 45th test,
but it just bombs

Am I using OutBuffer wrongly?  Misunderstanding D's va_list?  or what?

If I leave out buf.vprintf(fmtp, ap), I get a crash.  If I leave it in, I
get the exit(128).

Sean
May 27 2002
next sibling parent "Sean L. Palmer" <seanpalmer earthlink.net> writes:
I got this to work but I think it's a sucky kludge as I can't predict how
many characters I'll need:

import c.stdio;

char[] Fmt(char[] fmtp, ...)
{
  va_list ap;
  ap = cast(va_list)&fmtp;
  ap += fmtp.size;
  char[] buf;
  buf.length = 1024;
  vsprintf(buf, fmtp, ap);
  buf.length = strlen(buf);
  return buf;
}

"Sean L. Palmer" <seanpalmer earthlink.net> wrote in message
news:acus8q$gna$1 digitaldaemon.com...
 I'm trying to make my own variant of printf (one that prints into a
dynamic
 array of char) and am having problems with the OutBuffer class.

 char[] Fmt(char[] fmtp, ...)
 {
   va_list ap;
   ap = cast(va_list)&fmtp;
   ap += fmtp.size;
   printf(fmtp,*(int*)ap); // this works fine
   OutBuffer buf;
   buf.vprintf(fmtp, ap);  // this doesn't work, causes program termination
 errorlevel 128
   return buf.toString();  // but if I don't put something into buf, this
 crashes!
 }

 ...

 printf(Fmt("This is %dth test\n",45)); // should print This is 45th test,
 but it just bombs

 Am I using OutBuffer wrongly?  Misunderstanding D's va_list?  or what?

 If I leave out buf.vprintf(fmtp, ap), I get a crash.  If I leave it in, I
 get the exit(128).

 Sean
May 27 2002
prev sibling parent "Pavel Minayev" <evilone omen.ru> writes:
"Sean L. Palmer" <seanpalmer earthlink.net> wrote in message
news:acus8q$gna$1 digitaldaemon.com...

 printf(Fmt("This is %dth test\n",45)); // should print This is 45th test,
 but it just bombs

 Am I using OutBuffer wrongly?  Misunderstanding D's va_list?  or what?
OutBuffer's buggy. You've got to wait till Walter fixes it... or write your own.
May 27 2002