[gameprogrammer] Re: va_start va_end inversion

  • From: "Dave Slutzkin" <daveslutzkin@xxxxxxxxxxx>
  • To: gameprogrammer@xxxxxxxxxxxxx
  • Date: Thu, 15 Jul 2004 15:36:06 +1000

On Wed, 14 Jul 2004 15:11:48 -0700, "Kevin Jenkins"
<gameprogrammer@xxxxxxxxxx> said:
> Using va_start and va_end I've encoded a variable set of parameters to a
> bytestream.
> 
> I want to invert this.  Given a bytestream, I want to get the original
> set
> of parameters.  I want to then call a function that takes that set of
> parameters.
> 
> How?

How did you encode them in the first place?  I presume you needed to
iterate through the va_list (using va_start and va_arg) and get each
argument.  To do this you need to know the type of each argument, right?
 How do you know the type of the next vararg argument?

When reading in the stream, how are you going to know the type of the
next argument?  And how are you going to know how many of them there
are?  Or are they just going to fill the entire stream.

The simple case is where they are all the same type, and you know how
long the stream is.  So by deduction you know how many of them there are
and can read each in separately.  The reading is then easy.

It could be trickier if you don't know how many there are.  Then you'd
have to write the length of the stream in a header to the packet.

It could also be trickier if they were different types.  Then you'd have
to write an encoding of a type list in a header to the packet - eg
"ICICCCI" for int char int char char char int.

But I assume these are all the easy parts...

The hard part is probably calling the function again once you've got the
arguments, right?  Well, you could use the Reflection API, then you can
find the Class they operate on and...  Oh, that's right, it's C not
Java.  ;-)

The only way I can think of to use a single function call statement to
call a function with a variable list of parameters is using a
varargs-type system.  But the problem is really how you create a va_list
structure, am I right?

Well, as far as I can see, the standard library doesn't provide a way to
'streamify' or 'unstream' va_lists.  So the only way I can think of to
do this would be to save the _actual bytes_ of the va_list structure, by
writing (pseudocode) "write(&arg_list, sizeof(arg_list))".  But this
will only work if the va_list is stored in memory contiguously, as an
array of arguments.  Which it may be.  On a particular platform.

So this would presumably be very unportable.  You'll hit problems with
byte-ordering, implementation details and all that stuff.  But it's the
only way I can think of that would work.

Sorry, I don't think that really helps...  :-(  Let us know if you work
something out.

Bergholt.
-- 
  Dave Slutzkin
  Melbourne, Australia
  daveslutzkin@xxxxxxxxxxx



---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html


Other related posts: