Major directory cleanup
[cl-sane.git] / src / library.lisp
blobb2851b35001ccc75c024a0a4bf59c0a8a0248282
1 (in-package :sane)
3 (define-foreign-library libsane
4 (:unix (:or "libsane.so.1" "libsane.so"))
5 (t (:default "libsane")))
7 (defun sane-interpret-version (n)
8 "Read bitstring for version and output the correct major/minor/rev
9 version"
10 (list
11 (ash n -24) ; major
12 (boole boole-and (ash n -16) 255) ; minor
13 (boole boole-and n 65535)))
15 (defmacro with-init (version-sym &body body)
16 "Run BODY with VERSION-SYM set to a triple representing the version of libsane
17 that is running. Evaluates to BODY. If VERSION-SYM is nil, there is no symbol
18 bound to the version."
19 (let ((c-version (gensym)))
21 `(unwind-protect
22 (with-foreign-object (,c-version :int)
23 (use-foreign-library libsane)
24 (sane-lowlevel:ensure-ok
25 (sane-lowlevel::sane_init ,c-version (null-pointer))
26 "Failed to initialise sanelib.")
28 (let ,(when version-sym
29 `((,version-sym (sane-interpret-version
30 (mem-ref ,c-version :int)))))
31 ,@body))
32 (sane-lowlevel::sane_exit))))