summaryrefslogtreecommitdiffstats
path: root/share
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2017-10-27 23:35:58 -0700
committerKaz Kylheku <kaz@kylheku.com>2017-10-27 23:35:58 -0700
commitd7a93957e27bbfe6eaebc25b9d539f82dd9e4df3 (patch)
tree6166a78493e11d5f78243354cab0fa886b2ba355 /share
parentd73170b75f91bd13c0a2a93ac7aa51afa9a294c8 (diff)
downloadtxr-d7a93957e27bbfe6eaebc25b9d539f82dd9e4df3.tar.gz
txr-d7a93957e27bbfe6eaebc25b9d539f82dd9e4df3.tar.bz2
txr-d7a93957e27bbfe6eaebc25b9d539f82dd9e4df3.zip
New convenience I/O functions for buffers.
* lisplib.c (getput_set_entries): New autoload entries for file-get-buf, file-put-buf, file-append-buf, command-get-buf and command-put-buf. * share/txr/stdlib/getput.tl (sys:get-buf-common): New function. (file-get-buf, file-put-buf, file-append-buf, command-get-buf, command-put-buf): New functions. * txr.1: Documented.
Diffstat (limited to 'share')
-rw-r--r--share/txr/stdlib/getput.tl32
1 files changed, 32 insertions, 0 deletions
diff --git a/share/txr/stdlib/getput.tl b/share/txr/stdlib/getput.tl
index b42f6da7..cb3e621e 100644
--- a/share/txr/stdlib/getput.tl
+++ b/share/txr/stdlib/getput.tl
@@ -24,6 +24,18 @@
;; OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+(defun sys:get-buf-common (s)
+ (let ((b (make-buf 4096))
+ (o 0))
+ (while t
+ (let ((p (fill-buf b o s)))
+ (when (< p (len b))
+ (buf-set-length b (if (plusp p) p o))
+ (return))
+ (buf-set-length b (+ p (len b)))
+ (set o p)))
+ b))
+
(defun file-get (name)
(with-stream (s (open-file name))
(read s)))
@@ -59,6 +71,18 @@
(with-stream (s (open-file name "a"))
(put-lines lines s)))
+(defun file-get-buf (name)
+ (with-stream (s (open-file name "rb"))
+ (sys:get-buf-common s)))
+
+(defun file-put-buf (name buf)
+ (with-stream (s (open-file name "wb"))
+ (put-buf buf 0 s)))
+
+(defun file-append-buf (name buf)
+ (with-stream (s (open-file name "ab"))
+ (put-buf buf 0 s)))
+
(defun command-get (cmd)
(with-stream (s (open-command cmd))
(read s)))
@@ -81,3 +105,11 @@
(defun command-put-lines (cmd lines)
(with-stream (s (open-command cmd "w"))
(put-lines lines s)))
+
+(defun command-get-buf (cmd)
+ (with-stream (s (open-command cmd))
+ (sys:get-buf-common s)))
+
+(defun command-put-buf (cmd buf)
+ (with-stream (s (open-command cmd "w"))
+ (put-buf buf 0 s)))