#| This file is a part of stoe project. Copyright (c) 2014 Renaud Casenave-Péré (renaud@casenave-pere.fr) |# (uiop:define-package :stoe/core/file (:use :cl :blackbird :stoe/core/utils :stoe/core/jobs) (:export #:safe-read #:safe-read-from-string #:load-file)) (in-package :stoe/core/file) (let ((safe-readtable (copy-readtable nil))) (dolist (c '(#\# #\=)) (set-macro-character c nil nil safe-readtable)) (defun safe-read (&optional (stream *standard-input*) (eof-error-p t) eof-value recursive-p) (let ((*readtable* safe-readtable)) (restartable (read stream eof-error-p eof-value recursive-p)))) (locally #+sbcl (declare sb-ext::(muffle-conditions style-warning)) (defun safe-read-from-string (s &optional (eof-error-p t) eof-value &key (start 0) end preserve-whitespace) (let ((*readtable* safe-readtable) (*read-eval* nil)) (restartable (read-from-string s eof-error-p eof-value :start start :end end :preserve-whitespace preserve-whitespace)))))) (defun do-load-file (filepath type) "Load the file specified by `filepath' and store it in the object returned." (with-open-file (stream filepath :direction :input :element-type type) (when stream (let ((buffer (make-array (file-length stream) :element-type type))) (read-sequence buffer stream) buffer)))) (defun load-file (filepath &key sync (type '(unsigned-byte 8))) "Load the file specified by `filepath' asynchronally unless `sync' is true." (if sync (with-promise (resolve reject) (resolve (do-load-file filepath type))) (async-job (filepath type) (do-load-file filepath type))))