forked from slyrus/soiree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.lisp
61 lines (53 loc) · 2.13 KB
/
utilities.lisp
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
(in-package :soiree)
(defun contents-of-stream (in)
"Returns a string with the entire contents of the specified file."
(with-output-to-string (contents)
(let* ((buffer-size 4096)
(buffer (make-string buffer-size)))
(labels ((read-chunks ()
(let ((size (read-sequence buffer in)))
(if (< size buffer-size)
(princ (subseq buffer 0 size) contents)
(progn
(princ buffer contents)
(read-chunks))))))
(read-chunks)))))
(defun contents-of-file (pathname)
(with-open-file (in pathname :direction :input)
(contents-of-stream in)))
(defmacro when-string (test &body forms)
(let ((str (gensym)))
`(let ((,str ,test))
(when (and ,str (not (equal ,str "")))
,@forms))))
(defun remove-keyword (key args)
(let ((pos (position key args)))
(if (and pos (evenp pos))
(concatenate (type-of args)
(subseq args 0 pos)
(subseq args (+ pos 2)))
args)))
(defun keep (item sequence &rest args &key from-end (test 'eql) test-not start
end count key)
(declare (ignorable from-end test-not start end count key))
(apply #'remove-if-not (lambda (x) (funcall test x item))
sequence (remove-keyword :test args)))
(defmacro when-let ((var form) &body body)
`(let ((,var ,form))
(when ,var
,@body)))
(defun convert-string-to-dos-line-endings (string)
(with-output-to-string (out)
(with-input-from-string (stream string)
(loop for c = (read-char stream nil nil)
while c
do (cond ((eq c #\return)
(write-char #\return out)
(let ((next (read-char stream)))
(write-char #\linefeed out)
(unless (eq next #\linefeed)
(write-char next out))))
((eq c #\linefeed)
(write-char #\return out)
(write-char #\linefeed out))
(t (write-char c out)))))))