[EMAIL PROTECTED] wrote:
An alternative in c:

You can also make a new va_list out of the old one. It's basically an array of void pointers. So you va_start, va_arg and repopulate the second array with appropriate void casted pointers. You can then vprintf on this hacked va_list.


I wasn't familiar with va_list and friends, so I've dug around a bit and come up with the following C program which wraps vprintf. It's a totally useless script for any practical purpose, but demonstrates some sort of usage regarding the va_list type.


--- start ---
#include <stdio.h>
#include <stdarg.h>

void wrap_printf(char *, ...);

int main() {
  int w = 37, x = 22, y = 11, z = 10;
  char * fmt = "%#o %#o %#o %#o\n";

  wrap_printf(fmt, w, x, y, z);
  return 0;
}

void wrap_printf(char * a, ...) {
  va_list ap;

  va_start(ap, a);
  vprintf(a, ap);
  va_end(ap);
}


--- end ---

Is that usage sane (ignoring the pointlessness of it) ? Note that there's no mention of 'va_arg'.

Because that compiled without issuing any warnings and ran as desired, I came up with the following Inline C script:

--- start ---
use warnings;
use Inline C => <<'EOC';

#include <stdio.h>
#include <stdarg.h>

void wrap_printf(SV * a, ...) {
     va_list ap;
     va_start(ap, a);
     vprintf(SvPV_nolen(a), ap);
     va_end(ap);
}

EOC

wrap_printf("%#o %#o %#o %#o\n", 37, 22, 11, 10);

--- end ---

That also compiles without warning and runs fine, except that it doesn't give the correct answer. The output I get is '044170444 06 0 0' - presumably because the values that are being printf'd are not the integer values being passed to wrap_printf() - but some other values associated with 'ap', instead.

So ... can I modify the line 'vprintf(SvPV_nolen(a), ap);' in such a way that it's the integers 37, 22, 11, and 10 that get printf'd ?

Or am I faced with the task of converting the argument list received by the Inline C wrap_printf() function to an array of char* and ints, and then somehow passing that array to vprintf ? (This seems to be what your original reply is suggesting. Is that what I need to concentrate on achieving ?)

Thanks R.

Cheers,
Rob



Reply via email to