cffi-interface.lisp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. ;; This software is Copyright (c) cage
  2. ;; cage grants you the rights to distribute
  3. ;; and use this software as governed by the terms
  4. ;; of the Lisp Lesser GNU Public License
  5. ;; (http://opensource.franz.com/preamble.html),
  6. ;; known as the LLGPL
  7. (in-package :cl-pslib)
  8. (define-foreign-library libps
  9. (:darwin "libps.dylib")
  10. (:unix (:or "libps.so.0" "libps.so"))
  11. (t (:default "libps")))
  12. (use-foreign-library libps)
  13. (defctype size :unsigned-int)
  14. ;size_t (*writeproc)(PSDoc *p, void *data, size_t size))
  15. (defparameter *callback-string* (string ""))
  16. (cffi:defcallback write-to-string size ((doc :pointer) (data :pointer) (size size))
  17. (declare (ignore doc))
  18. (setf *callback-string* (concatenate 'string *callback-string*
  19. (foreign-string-to-lisp data :count size)))
  20. size)
  21. (defmacro with-list->foreign-array ((arr type &optional (fun #'identity))
  22. lst &body body)
  23. (alexandria:with-gensyms (ct data)
  24. `(cffi:with-foreign-object (,arr ,type (length ,lst))
  25. (loop
  26. for ,ct from 0
  27. for ,data in ,lst do
  28. (setf (mem-aref ,arr ,type ,ct)
  29. (funcall ,fun ,data)))
  30. ,@body)))
  31. (defmacro with-vector->foreign-array ((arr type &optional (fun #'identity))
  32. vec &body body)
  33. (alexandria:with-gensyms (ct data)
  34. `(cffi:with-foreign-object (,arr ,type (length ,vec))
  35. (loop
  36. for ,ct from 0
  37. for ,data across ,vec do
  38. (setf (mem-aref ,arr ,type ,ct)
  39. (funcall ,fun ,data)))
  40. ,@body)))
  41. (defun pslib_errornum<0 (num)
  42. (if (< 0 num)
  43. nil
  44. t))
  45. (defun pslib_errornum<=0 (num)
  46. (if (<= 0 num)
  47. nil
  48. t))
  49. (defun truth-lisp->c (val)
  50. (if val
  51. 1
  52. 0))
  53. (defun truth-c->lisp (val)
  54. (if (= 0 val)
  55. nil
  56. t))