Re: [Vala] using a non-glib C library in vala



Mr. Maxwell, sorry for the dupe.  Forgot to cc the list.

On Tue, 2009-09-15 at 17:14 -0500, Mr. Maxwell . wrote:
What do I do with the typedef struct declarations? This is what I have
but I don't think it's right.

c code
  typedef struct { char _dummy; } ID3Tag;

vala code
    [CCode(cname = "typedef struct")]
    public struct ID3Tag { private char _dummy; } 

The type name for a non-typedef'd struct would be:

struct struct_name

Whereas the name for a typedef'd one is simply the name of the type.
So, for example, let's say that we have a structure that contains a IP
network address and could hold either an IPv4 or IPv6 address in C.
This could look like:

typedef enum _ip_address_type_t {
  IP_ADDRESS_V4,
  IP_ADDRESS_V6
} ip_address_type_t;

typedef struct _ip_address_t {
  ip_address_type_t type;
  unsigned char address[16];
} ip_address_t;

In Vala, this would be bound as (assuming this is in
ip_address_type.h): 

[CCode(cheader_filename = "ip_address_type.h",
       cprefix = "",
       c_lowercase_prefix = "")]
namespace Native {
  [CCode(cprefix = "IP_ADDRESS_")]
  public enum IpAddressType {
    V4,
    V6
  }

  [CCode(cname = "ip_address_t")]
  public struct IpAddress {
    public IpAddressType type;
    public uchar address[16];
  }
}

To use it:

using Native;

public class EntryPoint {
  public static int main(string[] args) {
    IpAddress addr = IpAddress();

    addr.type = IpAddressType.V4;
    addr.address[0] = 127;
    addr.address[3] = 1;

    stdout.printf("IP Address Type: %d\n", addr.type);
    stdout.printf("IP Address: %d.%d.%d.%d\n", addr.address[0],
                  addr.address[1], addr.address[2], addr.address[3]);

    return(0);
  }
}

However, there is one small problem:  You can *not* use the structure as
a [SimpleType], due to a bug in Vala (see bug 588280).  If you attempt
to do so, Vala will generate an incorrect .c code file to feed to gcc,
and gcc will (correctly) error out, stating that memset was not passed
enough parameters.  This was deemed to be not a problem, though in my
personal opinion, Vala should either throw an error or generate correct
C code, not generate incorrect C code to be fed to the compiler.

However, I don't know how to fix the issue in valac, so I can't really
reopen the bug.  Just keep in mind that using a SimpleType-decorated
struct is impossible in Vala, even if that is what you would do in C.

        --- Mike

-- 
Blog:  http://mike.trausch.us/blog/
Misc. Software:  http://mike.trausch.us/software/




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