From 2f59c22487ed9b8d17a42259e5c697e5821586d6 Mon Sep 17 00:00:00 2001 From: ckonstanski Date: Sun, 4 Jul 2021 23:29:04 -0600 Subject: file contents to hex --- src/file.lisp | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'src/file.lisp') diff --git a/src/file.lisp b/src/file.lisp index 7dfe480..363cd08 100644 --- a/src/file.lisp +++ b/src/file.lisp @@ -54,11 +54,21 @@ line at a time." (loop for ,line = (read-line ,stream nil nil) while ,line do ,@body)))) -(defun read-whole-file (filename) - (with-open-file (stream filename) - (let ((contents (make-string (file-length stream)))) - (read-sequence contents stream) - contents))) +(defun file-as-bytes (filename) + (let ((contents ())) + (with-open-file (stream filename :direction :input :element-type '(unsigned-byte 8)) + (loop for byte = (read-byte stream nil) + while byte + do (push byte contents))) + (reverse contents))) + +(defun file-as-hex (filename) + (let ((hex (make-array '(0) :element-type 'character :fill-pointer 0 :adjustable t))) + (with-output-to-string (stream hex) + (loop for byte in (file-as-bytes filename) + do (let ((pattern (if (< byte 16) "0~x" "~x"))) + (format stream pattern byte)))) + (string-downcase hex))) (defun get-mime-type (filename) (car (org-ckons-core::shell-wrapper (format nil "file --mime-type ~a | awk -F': ' '{print $2}'" filename)))) -- cgit v1.3