Recently Written · git

CSV

Convert CSV (in a reader macro, or in a file) in to Lisp lists.

git clone https://github.com/equwal/CSV

Log | Files | Refs


commit 4ab4cab04918986b3453b7e8c95999cfcf68134f
Spenser Truex <spensertruexonline@gmail.com>
2019-01-07 11:52:53 -0800

Include example.csv

 README.md   |  2 +-
 csv.lisp    | 21 ++++++++++++++++-----
 example.csv |  1 +
 3 files changed, 18 insertions(+), 6 deletions(-)
diff --git a/README.md b/README.md
index 2fd1c77..236e1bb 100644
--- a/README.md
+++ b/README.md
@@ -30,5 +30,5 @@ Use ASDF:
 
 ``` common-lisp
 CL-USER> (asdf:load-system :csv)
-T
+pT
 ```
diff --git a/csv.lisp b/csv.lisp
index 6d5bbb0..244b6e3 100644
--- a/csv.lisp
+++ b/csv.lisp
@@ -7,15 +7,23 @@
 (defun openp (prev curr)
   (declare (ignore curr))
   (char= #\" prev))
+(defun str->list (str)
+  (labels ((inner (str acc len)
+             (if (= 0 len)
+                 (nreverse acc)
+                 (inner (subseq str 1) (cons (elt str 0) acc) (1- len)))))
+    (inner str nil (length str))))
 (let ((in nil)
       (escape nil))
   (setf (fdefinition 'switches)
 	(lambda (prev curr str)
 	  (let ((str (if (array-has-fill-pointer-p str)
                          str
-                         (make-array 1 :fill-pointer 0
-                                       :adjustable t
-                                       :element-type 'character))))
+                         (make-array (length str)
+                                     :fill-pointer 0
+                                     :adjustable t
+                                     :element-type 'character
+                                     :initial-contents (str->list str)))))
             (if (null curr)
 	        (progn (setf in nil escape nil) str)
 	        (if in
@@ -49,7 +57,10 @@
     (dolines (line stream)
       (princ line s)
       (format s "~%"))))
-(defun slurp (path)
-  "Read in a CSV file. Resource intensive, invokes the lisp reader."
+(defun slurp-csv (path)
+  "Read in a CSV file to string. Resource intensive."
   (with-open-file (s path)
     (concatenate 'string "#!" (read-to-string s) "!#")))
+(defun slurp (path)
+  "Read in a CSV file to list. Resource intensive."
+  (read-from-string (slurp-csv path)))
diff --git a/example.csv b/example.csv
new file mode 100644
index 0000000..a3f7ccd
--- /dev/null
+++ b/example.csv
@@ -0,0 +1 @@
+"example","csv","list","result","like","this"