Wednesday, January 18, 2012

Portable proplib: Vala bindings

We are investigating the Vala bindings for XBPS, but to make it fully work Portable proplib must be supported first. I started playing with `vala-gen-introspect` that created the .gi file required to create the final .vapi file with the Vala bindings; but after making the resulting vapi file couldn't compile a test code...

After some time I decided to modify the generated .vapi file manually, started reading the Vala Tutorial, and *et voilĂ *! the human modified vapi file works enough for simple test code.


This is the test code I used (please don't laugh, I've just started playing with Vala). This code just prints the value of the "pkgver" string object from plist file specified in argv[1]. The .vapi files is what specifies how to transform the Vala code into C code. The Vala bindings for the portable proplib can be found on: http://xbps.nopcode.org/distfiles/proplib.vapi

using PropLib;

int main(string[] args) {
    unowned PDict pkgd;
    string val = null;

    pkgd = prop_dictionary_internalize_from_zfile(args[1]);   
    if (prop_dictionary_get_cstring_nocopy(pkgd, "pkgver", out val) == false) {
        stderr.printf("Missing pkgver obj\n");
        return 1;
    }
    prop_object_release((PObject)pkgd);
    stdout.printf("%s\n", val);
    return 0;
}
To compile the source code use the following command (make sure that you have portable proplib installed with pkgconfig support):

$ valac --pkg proplib --vapidir=. test.vala
 If you've built successfully it should print something like:

$ ./test /var/db/xbps/metadata/kernel/props.plist
kernel-3.2.1

$