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

Code fragments for returning C string to Lisp....

Charlene Abrams asked:
> Or alternately, how do I return a string from C back to Lisp?

Here is a codce we are writing

to the NCSA's HDF/Vset library:

;;; Here is the FF interface for a C function which returns
;;; pointers to both a string and an integer..
;; A record structure is defined for returning the longint...
(defrecord longint-ptr
  (integer :longint) )

  (Vinquire        "Vinquire")
   (pointer :ptr :no-check-arg) ;vg *
   (pointer :ptr :no-check-arg) ;longint-ptr
   (string :cstring :by-reference); char * (value returned here)

The function prototype in C looks like:
   int Vinquire (VGROUP *vg,int * nentries,char * vgname); 

And it is called by passing for the  Lisp character array:
  (let ( 
              (vg-ptr (Vattach df-pointer tag "r"))
              (vgname (make-array (list VSET_VGNAMELENMAX)
                                  :adjustable t 
                                  :element-type 'character
                                  :initial-element #\null

          (if (not (%null-ptr-p vg-ptr ))
            (%stack-block ((n-entries LONGINT_PTR_SIZE))
              (Vinquire vg-ptr n-entries vgname )))

          ; vgname now contains the string...
          ; and (rref n-entries longint-ptr.integer) gets the
          ; integer.

This seems to work just fine. Please let me know if there
is a more efficient way (Apropos reveals CCL::WITH-CSTR and 
CCL::WITH-CSTRS, but these aren't documented or exported from CCL.
I'd love to use them if they are going to stay around!)

Sean Doyle