X-Git-Url: http://git.kpe.io/?p=kmrcl.git;a=blobdiff_plain;f=io.lisp;h=4f896ed7584219f32fd425f0fcab74b7cdf5a41b;hp=61298add617fceb73802eaba82e48457f7dd411b;hb=ea921dd2ce51a46bb3ca92a07df095d5ace99dcf;hpb=e9927af19ebf762b2311f296643c00e3aa9cbb00 diff --git a/io.lisp b/io.lisp index 61298ad..4f896ed 100644 --- a/io.lisp +++ b/io.lisp @@ -43,6 +43,17 @@ (with-open-file (in file :direction :input) (read-stream-to-string in)))) +(defun read-file-to-usb8-array (file) + "Opens a reads a file. Returns the contents as single unsigned-byte array" + (with-open-file (in file :direction :input :element-type '(unsigned-byte 8)) + (let* ((file-len (file-length in)) + (usb8 (make-array file-len :element-type '(unsigned-byte 8))) + (pos (read-sequence usb8 in))) + (unless (= file-len pos) + (error "Length read (~D) doesn't match file length (~D)~%" pos file-len)) + usb8))) + + (defun read-stream-to-strings (in) (let ((lines '()) (eof (gensym))) @@ -178,25 +189,10 @@ -#+openmcl -(defun open-device-stream (path direction) - (let* ((mode (ecase direction - (:input #.(read-from-string "#$O_RDONLY")) - (:output #.(read-from-string "#$O_WRONLY")) - (:io #.(read-from-string "#$O_RDWR")))) - (fd (ccl::fd-open (ccl::native-translated-namestring path) mode))) - (if (< fd 0) - (ccl::signal-file-error fd path) - (ccl::make-fd-stream fd :direction direction)))) - (defun null-output-stream () - #-openmcl - (when (probe-file #p"/dev/null") - (open #p"/dev/null" :direction :output :if-exists :overwrite)) - #+openmcl (when (probe-file #p"/dev/null") - (open-device-stream #p"/dev/null" :output)) + (open #p"/dev/null" :direction :output :if-exists :overwrite)) ) @@ -326,4 +322,9 @@ (write-char #\: stream) (write-string (aref +datetime-number-strings+ minute) stream))) +(defun copy-binary-stream (in out &key (chunk-size 16384)) + (do* ((buf (make-array chunk-size :element-type '(unsigned-byte 8))) + (pos (read-sequence buf in) (read-sequence buf in))) + ((zerop pos)) + (write-sequence buf out :end pos)))