-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcsv.lisp
More file actions
396 lines (346 loc) · 12.6 KB
/
csv.lisp
File metadata and controls
396 lines (346 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
;;; -*- Mode: Lisp ; Base: 10 ; Syntax: ANSI-Common-Lisp -*-
;;; csv: reading files in Comma-Separated Values format.
#+xcvb (module (:depends-on ("package")))
#| "
HOME PAGE:
http://www.cliki.net/fare-csv
LICENSE:
http://tunes.org/legalese/bugroff.html
Also under no-restriction BSD license for those who insist.
DEPENDENCIES:
asdf
USAGE:
(asdf:load-system :fare-csv)
(read-csv-file "foo.csv")
(read-csv-stream stream)
(read-csv-line stream)
(write-csv-lines lines stream)
(write-csv-line fields stream)
EXAMPLE USE:
...
BUGS:
I implemented just enough of CSV to import a specific file
from a PC application that will remain unnamed.
If you need more, you can cont(r)act me, and/or hack it yourself.
CSV is intrinsically an underspecified lossy format,
and the particular PC application I'm using loses heavily
(i.e. no quoting convention at all, not even a pascal-like one)
when text fields contain the quote character. Ouch.
SEE ALSO:
This spec seems to explain popular usage, is refered by docs below.
http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm
This one says about the same:
http://edoceo.com/utilitas/csv-file-format
There's now an RFC that tries to standardize CSV:
http://www.rfc-editor.org/rfc/rfc4180.txt
Here's what Perl hackers think CSV is:
http://search.cpan.org/~hmbrand/Text-CSV_XS-0.59/CSV_XS.pm
Share and enjoy!
" |#
; -----------------------------------------------------------------------------
;;; Packaging stuff
(in-package :fare-csv)
; -----------------------------------------------------------------------------
;;; Optimization
(eval-when (:compile-toplevel :execute)
(declaim (optimize (speed 3) (safety 1) (debug 3))
#+sbcl (sb-ext:muffle-conditions sb-ext:compiler-note)))
; -----------------------------------------------------------------------------
;;; Thin compatibility layer
#| ;;; Not needed anymore
(eval-when (:compile-toplevel :load-toplevel :execute)
(unless (fboundp 'parse-number)
(defun parse-number (string)
(with-standard-io-syntax ()
(let* ((*read-eval* nil)
(*read-default-float-format* 'double-float)
(n (read-from-string string)))
(when (numberp n) n)))))) |#
; -----------------------------------------------------------------------------
;;; Parameters
(eval-when (:compile-toplevel :load-toplevel :execute)
(defparameter +cr+ #.(format nil "~A" #\Return) "String containing a CR (Carriage Return)")
(defparameter +lf+ #.(format nil "~A" #\Linefeed) "String containing a LF (Linefeed)")
(defparameter +crlf+ #.(format nil "~A~A" #\Return #\Linefeed) "String containing a CRLF line termination")
(defparameter *csv-variables* '())) ; list of (var rfc4180-value creativyst-value)
(eval-when (:compile-toplevel :load-toplevel :execute)
(macrolet
((def (var rfc4180 creativyst doc)
`(progn
(eval-when (:compile-toplevel :load-toplevel :execute)
(pushnew `(,',var ,,rfc4180 ,,creativyst) *csv-variables* :key #'car))
(defparameter ,var ,creativyst ,doc))))
(def *separator*
#\, #\,
"Separator between CSV fields")
(def *quote*
#\" #\"
"delimiter of string data; pascal-like quoted as double itself in a string.")
(def *unquoted-quotequote*
nil nil
"does a pair of quotes represent a quote outside of quotes?
M$, RFC says NIL, csv.3tcl says T")
(def *loose-quote*
nil nil
"can quotes appear anywhere in a field?")
(def *allow-binary*
t t
"do we accept non-ascii data?")
(def *eol*
+lf+ +crlf+
"line ending when exporting CSV")
(def *line-endings*
(list +crlf+ +lf+) (list +cr+ +lf+ +crlf+)
"acceptable line endings when importing CSV")
(def *skip-whitespace*
nil t
"shall we skip unquoted whitespace around separators?")))
(defun char-ascii-text-p (c)
(let ((i (char-code c)))
(or (<= #x20 i #x7E) (= i 10) (= i 13))))
(defmacro with-creativyst-csv-syntax ((&optional) &body body)
"bind CSV syntax parameters to the CREATIVYST standard around evaluation of BODY"
`(call-with-creativyst-csv-syntax (lambda () ,@body)))
(defun call-with-creativyst-csv-syntax (thunk)
(progv (mapcar #'first *csv-variables*) (mapcar #'third *csv-variables*)
(funcall thunk)))
(defmacro with-rfc4180-csv-syntax ((&optional) &body body)
"bind CSV syntax parameters to the RFC 4180 standard around evaluation of BODY"
`(call-with-rfc4180-csv-syntax (lambda () ,@body)))
(defun call-with-rfc4180-csv-syntax (thunk)
(progv (mapcar #'first *csv-variables*) (mapcar #'second *csv-variables*)
(funcall thunk)))
(defmacro with-strict-rfc4180-csv-syntax ((&optional) &body body)
"bind CSV syntax parameters to the strict RFC 4180 standard around evaluation of BODY,
forcing CRLF as line ending and disallowing binary data amongst values"
`(call-with-strict-rfc4180-csv-syntax (lambda () ,@body)))
(defun call-with-strict-rfc4180-csv-syntax (thunk)
(with-rfc4180-csv-syntax ()
(setf *line-endings* (list +crlf+)
*allow-binary* nil)
(funcall thunk)))
(defun valid-eol-p (x)
(member x (list +cr+ +lf+ +crlf+) :test #'equal))
(defun validate-csv-parameters ()
(assert (typep *separator* 'character) ())
(assert (typep *quote* 'character) ())
(assert (not (eql *separator* *quote*)) ())
(assert (typep *unquoted-quotequote* 'boolean) ())
(assert (typep *loose-quote* 'boolean) ())
(assert (valid-eol-p *eol*) ())
(assert (not (member (aref *eol* 0) (list *separator* *quote*))) ())
(assert (and *line-endings* (every #'valid-eol-p *line-endings*)) ())
(assert (typep *skip-whitespace* 'boolean) ()))
;; For internal use only
(defvar *accept-cr* t "internal: do we accept cr?")
(defvar *accept-lf* t "internal: do we accept lf?")
(defvar *accept-crlf* t "internal: do we accept crlf?")
; -----------------------------------------------------------------------------
;;; The parser
(defmacro defsubst (name arglist &body body)
"Declare an inline defun."
`(progn (declaim (inline ,name))
(defun ,name ,arglist ,@body)))
(defsubst char-space-p (c)
"Is character C some kind of white space?
NB: this only handles a tiny subset of whitespace characters,
even if restricted to ASCII. However, it's rather portable,
and is what the creativyst document specifies.
Be careful to not skip a separator, as it could be e.g. a tab!"
(declare (type (or null character) c))
(and c (member c '(#\Space #\Tab)) (not (eql c *separator*))))
(defsubst accept-p (x stream)
(let ((c (peek-char nil stream nil nil)))
(etypecase x
(character (eql x c))
((or function symbol) (funcall x c))
(integer (eql x (char-code c))))))
(defsubst accept (x stream)
(and (accept-p x stream)
(read-char stream)))
(defsubst accept-eof (stream)
(not (peek-char nil stream nil nil)))
(defsubst accept-eol (stream)
(block nil
(when (and *accept-lf* (accept #\Linefeed stream)) (return t))
(when (or *accept-crlf* *accept-cr*)
(when (accept #\Return stream)
(when *accept-crlf*
(if (accept #\Linefeed stream)
(return t)
(unless *accept-cr*
(error "Carriage-return without Linefeed!"))))
(return t)))
nil))
(defsubst accept-space (stream)
(accept #'char-space-p stream))
(defsubst accept-spaces (stream)
(loop :for x = (accept-space stream) :while x :collect x))
(defsubst accept-quote (stream)
(accept *quote* stream))
(defsubst accept-separator (stream)
(accept *separator* stream))
(defun read-csv-line (stream)
"Read one line from STREAM in CSV format, using the current syntax parameters.
Return a list of strings, one for each field in the line.
Entries are read as strings;
it is up to you to interpret the strings as whatever you want."
(validate-csv-parameters)
(let ((ss (make-string-output-stream))
(fields '())
(had-quotes nil)
(*accept-cr* (member +cr+ *line-endings* :test #'equal))
(*accept-lf* (member +lf+ *line-endings* :test #'equal))
(*accept-crlf* (member +crlf+ *line-endings* :test #'equal)))
(labels
((do-fields ()
(setf had-quotes nil)
(when *skip-whitespace*
(accept-spaces stream))
(cond
((and (null fields)
(or (accept-eol stream) (accept-eof stream)))
(done))
(t
(do-field-start))))
(do-field-start ()
(cond
((accept-separator stream)
(add "") (do-fields))
((accept-quote stream)
(cond
((and *unquoted-quotequote* (accept-quote stream))
(add-char *quote*) (do-field-unquoted))
(t
(do-field-quoted))))
(t
(do-field-unquoted))))
(do-field-quoted ()
(setf had-quotes t)
(cond
((accept-eof stream)
(error "unexpected end of stream in quotes"))
((accept-quote stream)
(cond
((accept-quote stream)
(quoted-field-char *quote*))
(*loose-quote*
(do-field-unquoted))
(t
(add (current-string))
(end-of-field))))
(t
(quoted-field-char (read-char stream)))))
(quoted-field-char (c)
(add-char c)
(do-field-quoted))
(do-field-unquoted ()
(if *skip-whitespace*
(let ((spaces (accept-spaces stream)))
(cond
((accept-separator stream)
(add (current-string))
(do-fields))
((or (accept-eol stream) (accept-eof stream))
(add (current-string))
(done))
(t
(map () #'add-char spaces)
(do-field-unquoted-no-skip))))
(do-field-unquoted-no-skip)))
(do-field-unquoted-no-skip ()
(cond
((accept-separator stream)
(add (current-string))
(do-fields))
((or (accept-eol stream) (accept-eof stream))
(add (current-string))
(done))
((accept-quote stream)
(cond
((and *unquoted-quotequote* (accept-quote stream))
(add-char *quote*) (do-field-unquoted))
(*loose-quote*
(do-field-quoted))
(t
(error "unexpected quote in middle of field"))))
(t
(add-char (read-char stream))
(do-field-unquoted))))
(end-of-field ()
(when *skip-whitespace*
(accept-spaces stream))
(cond
((or (accept-eol stream) (accept-eof stream))
(done))
((accept-separator stream)
(do-fields))
(t
(error "end of field expected"))))
(add (x)
(push x fields))
(add-char (c)
(unless (or *allow-binary* (char-ascii-text-p c))
(error "binary data not allowed ~s" c))
(write-char c ss))
(current-string ()
(get-output-stream-string ss))
(done ()
(nreverse fields)))
(do-fields))))
(defun read-csv-stream (stream)
"Read lines from STREAM in CSV format, using the current syntax parameters.
Return a list of list of strings, one entry for each line,
that contains one entry for each field.
Entries are read as strings;
it is up to you to interpret the strings as whatever you want."
(loop :until (accept-eof stream) :collect (read-csv-line stream)))
(defun read-csv-file (pathname &rest keys &key element-type external-format)
"Open the file designated by PATHNAME, using the provided keys if any,
and call READ-CSV-STREAM on it."
(declare (ignore element-type external-format))
(with-open-stream (stream (apply 'open pathname
:direction :input :if-does-not-exist :error keys))
(read-csv-stream stream)))
(defun char-needs-quoting (x)
(or (eql x *quote*)
(eql x *separator*)
(eql x #\linefeed)
(eql x #\return)
(not (char-ascii-text-p x))))
(defun string-needs-quoting (x)
(and (not (zerop (length x)))
(or (char-space-p (char x 0))
(char-space-p (char x (1- (length x))))
(some #'char-needs-quoting x))
t))
(defun write-csv-lines (lines stream)
"Given a list of LINES, each of them a list of fields, and a STREAM,
format those lines as CSV according to the current syntax parameters."
(dolist (x lines)
(write-csv-line x stream)))
(defun write-csv-line (fields stream)
"Format one line of FIELDS to STREAM in CSV format,
using the current syntax parameters."
(loop :for x :on fields :do
(write-csv-field (first x) stream)
(when (cdr x)
(write-char *separator* stream)))
(write-string *eol* stream))
(defun write-csv-field (field stream)
(etypecase field
(null t)
(number (princ field stream))
(string (write-csv-string-safely field stream))
(symbol (write-csv-string-safely (symbol-name field) stream))))
(defun write-csv-string-safely (string stream)
(if (string-needs-quoting string)
(write-quoted-string string stream)
(write-string string stream)))
(defun write-quoted-string (string stream)
(write-char *quote* stream)
(loop :for c :across string :do
(when (char= c *quote*)
(write-char c stream))
(write-char c stream))
(write-char *quote* stream))