Re: [Vala] gio: how to read N bytes into a buffer at offset M?



On 12/20/11 11:49, Evan Nemerson wrote:
On Tue, 2011-12-20 at 00:39 -0800, Matto Marjanovic wrote:
The subject line explains what I am trying to do.  In plain old C,
I would simply use plain old libgio:

     GInputStream* s = ...;
     guint8 buffer[4096];
     GError* error = NULL;
     ...
     g_input_stream_read(s, buffer + M, N, NULL,&error);

How do I do this in Vala?
...
Use array slicing. Something like stream.read (buffer[M:M + N]);

Ah-ha!  Thank you.

The docs make it sound like slicing always duplicates the (slice of the)
array --- but I now see that doesn't happen when the slicing happens in
the method call.  (Before writing my email, I had tried assigning a slice
to a temp local array variable, and that invoked a dup in the generate
C code....)

Here is a bonus follow-up question:  suppose I have a bare uint8* and
a count, then what do I do?  E.g.:

   void legacy_read_method(uint8* buffer, size_t count) {
     InputStream s = ...;
     ...
     s.read(???);
     ...
   }

How do I conjure up the requisite array/slice from a pointer+count?

...Playing around just now, it looks like:

     s.read( ((uint8[])buffer)[0:count] );

manages to produce reasonable C code --- is that the intended way to do this,
or is that just a dumb-luck side-effect of the current state of the code
generation?

(If I leave off the "[0:count]" slicing, the vala code still compiles,
but stuffs a "-1" for the size parameter --- seems like this should be
a compile-time error instead.)

-m



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]