<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix">Hi,<br>
<br>
Version with conversion to/from string: <tt><br>
<br>
<br>
(define (_string/locale/len n) ; n - bytes, not characters
</tt><tt><br>
</tt><tt> (make-ctype (make-array-type _byte n)
</tt><tt><br>
</tt><tt> ;->C
</tt><tt><br>
</tt><tt> (lambda (in)
</tt><tt><br>
</tt><tt> (cond
</tt><tt><br>
</tt><tt> [(not in)
</tt><tt><br>
</tt><tt> (make-bytes n)]
</tt><tt><br>
</tt><tt> [(string? in)
</tt><tt><br>
</tt><tt> (define bs (string->bytes/locale
in))
</tt><tt><br>
</tt><tt> (cond
</tt><tt><br>
</tt><tt> [((bytes-length bs) . >= . n) ;
len+1 > n
</tt><tt><br>
</tt><tt> (error '_string/locale/len "string
is too long\n~v" in)]
</tt><tt><br>
</tt><tt> [else
</tt><tt><br>
</tt><tt> (define out (make-bytes n))
</tt><tt><br>
</tt><tt> (bytes-copy! out 0 bs)
</tt><tt><br>
</tt><tt> out])]
</tt><tt><br>
</tt><tt> [else
</tt><tt><br>
</tt><tt> (error '_string/locale/len "expected
string? or #f, got ~v" in)]))
</tt><tt><br>
</tt><tt> ;->racket
</tt><tt><br>
</tt><tt> (lambda (in)
</tt><tt><br>
</tt><tt> (define bs (make-sized-byte-string in
n))
</tt><tt><br>
</tt><tt> (define len
</tt><tt><br>
</tt><tt> (for/fold ([acc 0])
</tt><tt><br>
</tt><tt> ([b (in-bytes bs)]
</tt><tt><br>
</tt><tt> [pos (in-naturals)]
</tt><tt><br>
</tt><tt> #:final (zero? b))
</tt><tt><br>
</tt><tt> pos))
</tt><tt><br>
</tt><tt> (define sub-bs (subbytes bs 0 len))
</tt><tt><br>
</tt><tt> (bytes->string/locale sub-bs))))</tt><br>
<br>
<br>
I would use this:<br>
<pre wrap="">(define camera-text-size (* 32 1024))
(define-cstruct _CameraText ([value (_string/locale/len camera-text-size)]))</pre>
<br>
For passing as a parameter to 'gp_camera_get_about':<br>
<br>
<tt>(define cam-text (make-CameraText #f))</tt><br>
<br>
<br>
<br>
<br>
13.11.2014 20:00, Bartosz Przygoda пишет:<br>
</div>
<blockquote
cite="mid:CADV3fd2hB-LhBNjjEzD4boNa1E958NHB9nUxRA3oa7aLLTiJYg@mail.gmail.com"
type="cite">
<pre wrap="">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
<a class="moz-txt-link-freetext" href="https://github.com/dyoo/ffi-tutorial/blob/master/ffi/tutorial/examples/struct-with-array/struct-with-array.rkt">https://github.com/dyoo/ffi-tutorial/blob/master/ffi/tutorial/examples/struct-with-array/struct-with-array.rkt</a>
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]))
</pre>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">____________________
Racket Users list:
<a class="moz-txt-link-freetext" href="http://lists.racket-lang.org/users">http://lists.racket-lang.org/users</a>
</pre>
</blockquote>
<br>
</body>
</html>