Skip to content

Instantly share code, notes, and snippets.

@joelreymont
Created September 30, 2011 17:58
Show Gist options
  • Save joelreymont/1254505 to your computer and use it in GitHub Desktop.
Save joelreymont/1254505 to your computer and use it in GitHub Desktop.
(defmacro read-foreign (foreign-type buffer index)
(let ((size (fli:size-of foreign-type)))
`(let ((index ,index))
(fli:with-dynamic-foreign-objects ((value ,foreign-type))
(fli:with-coerced-pointer (bytes :type '(:unsigned :char)) value
(dotimes (byte-index ,size)
(let ((endian-byte-index #+LITTLE-ENDIAN byte-index
#-LITTLE-ENDIAN (- (1- limit) byte-index)))
(setf (fli:dereference bytes :index endian-byte-index)
(aref ,buffer index)))
(incf index))
(values (fli:dereference value) index)
)))))
(defmacro write-foreign (value foreign-type buffer index)
(let ((size (fli:size-of foreign-type)))
`(fli:with-dynamic-foreign-objects ((value ,foreign-type :initial-element ,value))
(fli:with-coerced-pointer (bytes :type '(:unsigned :char)) value
(dotimes (byte-index ,size)
(let ((endian-byte-index #+LITTLE-ENDIAN byte-index
#-LITTLE-ENDIAN (- (1- limit) byte-index)))
(setf (aref ,buffer ,index)
(fli:dereference bytes :index endian-byte-index)))
(incf ,index))
,index
))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment