a0a09fcf271b

Add read-all functions
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Mon, 20 Feb 2017 15:40:36 +0000
parents 9bcadce51853
children 12fd42abb3ba
branches/tags (none)
files DOCUMENTATION.markdown losh.lisp make-docs.lisp package.lisp

Changes

--- a/DOCUMENTATION.markdown	Fri Feb 17 13:11:46 2017 +0000
+++ b/DOCUMENTATION.markdown	Mon Feb 20 15:40:36 2017 +0000
@@ -1008,6 +1008,22 @@
 
   
 
+## Package `LOSH.IO`
+
+Utilities for input/output/reading/etc.
+
+### `READ-ALL-FROM-FILE` (function)
+
+    (READ-ALL-FROM-FILE PATH)
+
+Read all forms from the file at `path` and return them as a fresh list.
+
+### `READ-ALL-FROM-STRING` (function)
+
+    (READ-ALL-FROM-STRING STRING)
+
+Read all forms from `string` and return them as a fresh list.
+
 ## Package `LOSH.ITERATE`
 
 Custom `iterate` drivers and clauses.
--- a/losh.lisp	Fri Feb 17 13:11:46 2017 +0000
+++ b/losh.lisp	Mon Feb 20 15:40:36 2017 +0000
@@ -2294,6 +2294,28 @@
   (apply #'make-bset list))
 
 
+;;;; IO -----------------------------------------------------------------------
+(defun read-all-from-string (string)
+  "Read all forms from `string` and return them as a fresh list."
+  (iterate
+    (with done = (gensym))
+    (with start = 0)
+    (for (values form pos) = (read-from-string string nil done
+                                               :start start))
+    (while (not (eq form done)))
+    (collect form)
+    (setf start pos)))
+
+(defun read-all-from-file (path)
+  "Read all forms from the file at `path` and return them as a fresh list."
+  (with-open-file (file path :direction :input)
+    (iterate
+      (with done = (gensym))
+      (for form = (read file nil done))
+      (while (not (eq form done)))
+      (collect form))))
+
+
 ;;;; Gnuplot ------------------------------------------------------------------
 (defun gnuplot-args% (&rest args)
   (mapcan (lambda (arg) (list "-e" arg))
--- a/make-docs.lisp	Fri Feb 17 13:11:46 2017 +0000
+++ b/make-docs.lisp	Mon Feb 20 15:40:36 2017 +0000
@@ -14,6 +14,7 @@
         "LOSH.GNUPLOT"
         "LOSH.HASH-SETS"
         "LOSH.HASH-TABLES"
+        "LOSH.IO"
         "LOSH.ITERATE"
         "LOSH.LICENSING"
         "LOSH.MATH"
--- a/package.lisp	Fri Feb 17 13:11:46 2017 +0000
+++ b/package.lisp	Mon Feb 20 15:40:36 2017 +0000
@@ -141,6 +141,12 @@
     :hash-table-contents
     :mutate-hash-values))
 
+(defpackage :losh.io
+  (:documentation "Utilities for input/output/reading/etc.")
+  (:export
+    :read-all-from-file
+    :read-all-from-string))
+
 (defpackage :losh.iterate
   (:use :iterate) ; need this for iterate's `for` symbol fuckery
   (:documentation "Custom `iterate` drivers and clauses.")
@@ -285,6 +291,7 @@
    :losh.gnuplot
    :losh.hash-sets
    :losh.hash-tables
+   :losh.io
    :losh.iterate
    :losh.licensing
    :losh.math