chicken-users
[Top][All Lists]
Advanced

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

[Chicken-users] collecting binary strings from c code to scheme


From: Jason Meade
Subject: [Chicken-users] collecting binary strings from c code to scheme
Date: Sat, 5 Apr 2008 20:44:26 -0700

Hi all,

I'm working on a C module that I wish to expose to scheme. So far I have been able to transfer null terminated strings from C to scheme no problem. Now I wish to transfer binary strings from C to scheme.

I see that there is a C_string function that appears to do the trick, unfortunately I am not getting the results I would like.

Below is my code. Perhaps someone could suggest a way to get the binary string data from C to scheme correctly? I'm more than willing to entertain non-string solutions, such as transferring data as lists if need be. (Help with that is also appreciated)

=== handle.c ===
C_word PFBuffer_scm_delete_mark (int handle)
{
  C_word *ptr;
  C_word str;
  if (handle >= 0 && handle < PFBuffer_MAX_HANDLES) {
    PFBuffer* buf = *(PFBuffer_handles + handle);
    if (buf) {
      PFBuffer_delete_mark (buf);
      ptr = C_alloc(C_SIZEOF_STRING(buf->copytext_length));
      str = C_string(&ptr,buf->copytext_length,buf->copytext);
    }
  }
  return str;
}

=== pfbuffer.scm ===
#>
extern C_word PFBuffer_scm_delete_mark (int);
<#

(define pfbuffer:delete-marked-text
  (foreign-lambda scheme-object PFBuffer_scm_delete_mark int))

=== scheme repl ===
#;1> (require 'pfbuffer)
; loading /opt/local/lib/chicken/1x/pfbuffer.so ...
#;2> (define buf (pfbuffer:alloc))
#;3> (pfbuffer:attach-and-read-file buf "pfbuffer.so")
69488
#;4> (pfbuffer:set-point-and-mark buf 0)
0
#;5> (pfbuffer:set-point-and-mark buf 20)
20
#;6> (pfbuffer:delete-marked-text buf)
#(#<procedure (a2315 . rs608)>)
#;7>

Obviously I am not expecting to receive a procedure as a result, but just for fun, let's see what happens if I capture and run the proc.

#;7> (pfbuffer:set-point-and-mark buf 0)
0
#;8> (pfbuffer:set-point-and-mark buf 20)
20
#;9> (define func (pfbuffer:delete-marked-text buf))
#;10> (func)
Error: call of non-procedure: "\x1c\x04\x00\x00
\x00\x00\x00\x01\x00\x00\x00H\x01\x00\x00__TE"

        Call history:

        <syntax>          (func)
        <eval>            (func)  <--
#;10>

The function blows up, but it *seems* that I was at least able to fetch my 20 binary characters. It seems like I am close to figuring this out. Any help that anyone can give would be very much appreciated.

Thanks,

-Jason





reply via email to

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