[Vala] Initializing strings to be used by FileStream::gets



Hi Folks

I am playing with vala and am starting out by playing with the string
handling (always an interesting area when C and other languages meet).

I used the following code to attempt to simulate 'cat' with no
arguments.

var str = new string();
while (!stdin.eof()) {
        stdin.gets(str, 256);
        stdout.printf("%s", str);
}

Of course such code is broken because the string has only sufficient
space allocated for the '\0' terminator meaning that fgets() will
overrun the memory allocated for str (got this from review of the
generated C code and proved it with valgrind).

I've therefore been trying to figure out how best to grow a string to be
large enough to store the input from fgets. The best I have so far
developed without modifying the bindings is:

var str = "%*d".printf(256, 0);

This at least allows 256 to be a numeric constant somewhere and runs
cleanly with valgrind but it really is pretty grubby.

Have I missed something obvious? If not does anyone have any advice on
how I should improve the bindings?

-- 
Daniel Thompson <daniel07 redfelineninja org uk>




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