summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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))))