cffi:defcstruct, cffi:foreign-string-to-lisp question
2008-12-17 14:45:12 GMT
I don`t know if this is the right group to ask, but I would appreciate some help with the following.
The aim is to read a binary file from lisp.
The file contains a set of records, every record consists of the following data:
(name :char :count 32) ;; this is the problematic line 1
(alias :char :count 12) ;; this is the problematic line 2
Now the problematic line 1 should read 32 characters from an ascii-encoded file.
For that, I use the (defun test .....) (see below). The test converts the cstruct with-foreign-slots through (cffi:foreign-string-to-lisp name).
The output of (test) is:
(1 "ABC ABC ÙÎ÷Só< <at> fffffŽ` <at> "
"ABC ÙÎ÷Só< <at> fffffŽ` <at> " 28.9d0 132.4d0 3774.0d0 0.0d0)
(length (second *))
(length (third **))
whereas I would expect the output to be:
(1 "ABC "
"ABC " 28.9d0 132.4d0 3774.0d0 0.0d0)
with length second = 32 and length third = 12.
Am I missing something?
(cffi:foreign-string-to-lisp name 32)
(cffi:foreign-string-to-lisp name 12)
this works, the correct output is presented. But how can I get the length of the char in the cstruct PPP?
Thank you very much,
;;---- TEST.lisp ------
(eval-when (:compile-toplevel :load-toplevel) (require :cffi))
(defpackage :test (:use :cl))
(cffi:defcfun "fopen" :pointer (path :string) (mode :string))
(cffi:defcfun "fread" :unsigned-long
(cffi:defcfun "fclose" :int (fp :pointer))
(name :char :count 32)
(alias :char :count 12)
(defparameter *file-path* "C://the-file.bin") ;;
(defun test ())
(setf file (fopen *file-path* "rb"))
(cffi:with-foreign-object (struct 'ppp)
(fread struct (cffi:foreign-type-size 'ppp) 1 file)
(cffi:with-foreign-slots ((nr name alias m t p v)
(when file (fclose file))))
;; run this by (test)
_______________________________________________ cffi-devel mailing list cffi-devel <at> common-lisp.net http://common-lisp.net/cgi-bin/mailman/listinfo/cffi-devel