summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorckonstanski <carlos.konstanski@olo.com>2021-07-04 23:29:04 -0600
committerckonstanski <carlos.konstanski@olo.com>2021-07-04 23:29:04 -0600
commit2f59c22487ed9b8d17a42259e5c697e5821586d6 (patch)
tree2275b480a8fc94643dcef9a2aa22781c900324d1
parent4e8b2306e783417047b97f0c6fdf8f080f90d20d (diff)
file contents to hex
-rw-r--r--src/file.lisp20
1 files changed, 15 insertions, 5 deletions
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))))