[Top][All Lists]

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

Re: [Chicken-users] SWIG pointers and Chicken

From: John Lenz
Subject: Re: [Chicken-users] SWIG pointers and Chicken
Date: Sun, 12 Dec 2004 21:36:50 +0000

On 12/12/04 12:17:36, Joel Reymont wrote:

Has anyone used SWIG with Chicken?

This is the structure that I'm trying to wrap:

struct Packet
        unsigned char* data;

It wraps correctly and I'm able to retrieve the data member but... The
type is <SWIG-pointer> which is neither pointer nor tagged-pointer. I
can't use the functions from the lolevel unit to access the individual
bytes pointed to by the data member.

How do I convert from SWIG-pointer to pointer or tagged-pointer in

    Thanks, Joel

You need to use a typemap (see the documentation).  It depends on how
actually you are using the data in the unsigned char.  The problem is we
can't convert directly to a chicken string, since chicken strings are
signed.  If you know that you are only using values in the unsigned char
array that would fit into a signed char, you can probably modify the char *
out typemap to be unsigned char.

This is from the Lib/chicken/chicken.swg file,

char *
{ char *s = (char*) $1;
if ($1 == NULL) {
  $result = C_SCHEME_FALSE;
else {
  int string_len = strlen ($1);
  C_word *string_space = C_alloc (C_SIZEOF_STRING (string_len));
  $result = C_string (&string_space, string_len, s);

Otherwise, you could write a typemap to return it as a vector.  Something
like  (this is untested...)

unsigned char *
if ($1 == NULL) {
  $result = C_SCHEME_FALSE;
} else {
  int string_len = strlen($1);
  C_word *vec_space = C_alloc(C_SIZEOF_VECTOR(string_len));
  $result = (C_word) vec_space;
  *(vec_space++) = C_VECTOR_TYPE | string_len;
  for (int i = 0; i < string_len; i++) {
    C_mutate(vec_space++, C_fix((int) $1[i]));

reply via email to

[Prev in Thread] Current Thread [Next in Thread]