Skip to content

Instantly share code, notes, and snippets.

@jaeschliman
Created October 4, 2012 15:34
Show Gist options
  • Save jaeschliman/3834446 to your computer and use it in GitHub Desktop.
Save jaeschliman/3834446 to your computer and use it in GitHub Desktop.
include an external file in a binary for later use, with shell transcript
;;
;; in the directory /tmp/include-binary/
;; containing a file foo.jpg, this file, and product.lisp
;;
;;
;; $ ls -la
;; total 768
;; drwxr-xr-x 5 user wheel 170 Oct 4 08:28 .
;; drwxrwxrwt 19 root wheel 646 Oct 4 08:25 ..
;; -rw-r--r-- 1 user wheel 382132 Oct 4 08:24 foo.jpg
;; -rw-r--r-- 1 user wheel 453 Oct 4 08:28 include.lisp
;; -rw-r--r-- 1 user wheel 443 Oct 4 08:28 product.lisp
;;
;;
;; $ ccl -l include.lisp
;; compiled to : /private/tmp/include-binary/product.dx32fsl
;; Welcome to Clozure Common Lisp Version 1.7-r14925M (DarwinX8632)!
;; ? (quit)
;;
;; $ ls -la
;; total 1520
;; drwxr-xr-x 6 user wheel 204 Oct 4 08:31 .
;; drwxrwxrwt 19 root wheel 646 Oct 4 08:25 ..
;; -rw-r--r-- 1 user wheel 382132 Oct 4 08:24 foo.jpg
;; -rw-r--r-- 1 user wheel 967 Oct 4 08:31 include.lisp
;; -rw-r--r-- 1 user wheel 383921 Oct 4 08:30 product.dx32fsl
;; -rw-r--r-- 1 user wheel 443 Oct 4 08:28 product.lisp
;;
;; $ ccl -l product.dx32fsl
;; writing included data to /tmp/include-binary/foo-2.jpg
;; Welcome to Clozure Common Lisp Version 1.7-r14925M (DarwinX8632)!
;; ? (quit)
;;
;;
;; $ ls -la
;; total 2272
;; drwxr-xr-x 7 user wheel 238 Oct 4 08:32 .
;; drwxrwxrwt 19 root wheel 646 Oct 4 08:25 ..
;; -rw-r--r-- 1 user wheel 382132 Oct 4 08:32 foo-2.jpg
;; -rw-r--r-- 1 user wheel 382132 Oct 4 08:24 foo.jpg
;; -rw-r--r-- 1 user wheel 1534 Oct 4 08:32 include.lisp
;; -rw-r--r-- 1 user wheel 383921 Oct 4 08:30 product.dx32fsl
;; -rw-r--r-- 1 user wheel 443 Oct 4 08:28 product.lisp
;;
;; $ diff foo.jpg foo-2.jpg && echo 'no difference'
;; no difference
(defvar *include* #P"/tmp/include-binary/foo.jpg")
(defun slurp-file (path)
(with-open-file (s path :direction :input
:element-type '(unsigned-byte 8))
(let ((vector (make-array (file-length s)
:element-type '(unsigned-byte 8))))
(read-sequence vector s)
vector)))
(format t "compiled to : ~A~%" (compile-file #P"/tmp/include-binary/product.lisp"))
;;
;; this is the file that will be the 'product'
;; for usage see 'include.lisp'
(defvar *output-file* #P"/tmp/include-binary/foo-2.jpg")
(defvar *included-data* #.(slurp-file *include*))
(defun spit-file (vector path)
(with-open-file (s path :direction :output
:if-exists :supersede
:element-type '(unsigned-byte 8))
(write-sequence vector s)))
(format t "writing included data to ~A~%" *output-file*)
(spit-file *included-data* *output-file*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment