-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparser.lisp
27 lines (25 loc) · 957 Bytes
/
parser.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
(defpackage #:pem/parser
(:use #:cl)
(:import-from #:cl-ppcre
#:create-scanner
#:register-groups-bind)
(:export #:parse
#:parse-file))
(in-package #:pem/parser)
(defun parse (pem)
(assert (eq (stream-element-type pem) 'character))
(loop for line = (read-line pem nil nil)
while line
collect (ppcre:register-groups-bind (label)
("^-----BEGIN (.+)-----" line)
(let ((end (ppcre:create-scanner (format nil "-----END ~A-----" label))))
(loop for line = (read-line pem)
if (ppcre:scan end line)
do (return (cons label
(format nil "~{~A~^~%~}" data)))
else
collect line into data)))))
(defun parse-file (pem)
(check-type pem pathname)
(with-open-file (in pem)
(parse in)))