chicken-users
[Top][All Lists]
Advanced

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

[Chicken-users] List -> C strings


From: Robin Lee Powell
Subject: [Chicken-users] List -> C strings
Date: Mon, 9 Jul 2007 16:12:01 -0700
User-agent: Mutt/1.5.13 (2006-08-11)

I need to take a Scheme list and pass it to a foreign function, so I
stole the following code from chasen:

#>
char **make_strings(C_word lst)
{
   int len, i;
   C_word tmp;
   char **hlist;

   len = C_unfix(C_i_length(lst));
   hlist = (char**) malloc(sizeof(char*) * (len + 1));

   for(i = 0, tmp = lst; i < len; i++, tmp = C_u_i_cdr(tmp)) {
        hlist[i] = C_c_string(C_u_i_car(tmp));
   }
   hlist[i] = NULL;
}
<#

And I call it like:

    (define-external colls_ext c-pointer (make_strings colls))

and then pass colls_ext in my FFI call.

I started seeing artifacts that looked like non-terminated strings
to me, and sure enough:

    Note that C_c_string() returns a pointer to the character buffer
    of the actual Scheme object and is not zero-terminated.

So I figured, no problem: I'll malloc a new string and strncpy.

Except: Problem: no way to get the length of the Scheme string!  I
can't even find documentation for the C_i_length call that the code
I stole uses!

So I have no idea what to do now.  Help?

-Robin


-- 
http://www.digitalkingdom.org/~rlpowell/ *** http://www.lojban.org/
Reason #237 To Learn Lojban: "Homonyms: Their Grate!"
Proud Supporter of the Singularity Institute - http://singinst.org/




reply via email to

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