[racket] FFI for struct that wraps around char array

From: Bartosz Przygoda (bprzygoda at gmail.com)
Date: Thu Nov 13 09:00:57 EST 2014

Hello,

I'm trying to make racket bindings for libgphoto2. Its API uses CameraText
struct for outputting strings, which just wraps char array.

My first attempt was:

(define _Camera-ptr (_cpointer 'Camera))
(define _CameraText-ptr (_cpointer 'CameraText))


(define-gphoto gp_camera_get_about
  (_fun _Camera-ptr (v : _CameraText-ptr) _GPContext-ptr
     -> _int))

But this was giving me segfaults (not to mention the pointer here is opaque
and there are no getter functions in API).

Later on I've stumbled across
https://github.com/dyoo/ffi-tutorial/blob/master/ffi/tutorial/examples/struct-with-array/struct-with-array.rkt
so I've tried this:

(define _Camera-ptr (_cpointer 'Camera))

(define camera-text-size (* 32 1024))
(define-cstruct _CameraText ([text (_bytes/len (camera-text-size))]))

(define-gphoto gp_camera_get_about
  (_fun _Camera-ptr (v : _CameraText-pointer) _GPContext-ptr
     -> _int))

(Where bytes/len is helper taken from that example)

How can I initialize this struct to mimic C CameraText txt;? Is there some
better way to handle char arrays (and converting them to strings) than
_byte array? Or should I go with something like (but this was segfaulting
as well):

(define-cstruct _CameraText ([text _string]))
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20141113/bf933472/attachment-0001.html>

Posted on the users mailing list.