Switch to port-based reading/parsing

This commit is contained in:
Jaidyn Ann 2024-02-05 21:55:57 -06:00
parent 916ebdbea2
commit 0a5ef01169

View File

@ -54,14 +54,6 @@
(irregex-extract "(\\\\;|[^;])*" key-or-value)) (irregex-extract "(\\\\;|[^;])*" key-or-value))
(define (parse-vcard-element kv-pair)
(case (car kv-pair)
('VERSION
(append (list (car kv-pair) (string->number (second kv-pair)))
(cddr kv-pair)))
(else kv-pair)))
;; Parse a line of a vcard file into an alist-friendly format: ;; Parse a line of a vcard file into an alist-friendly format:
;; (KEY ("keyprop1=d" "keyprop2=b") "VALUE" ("valprop1=a" "valprop2=b")) ;; (KEY ("keyprop1=d" "keyprop2=b") "VALUE" ("valprop1=a" "valprop2=b"))
(define (parse-vcard-line line) (define (parse-vcard-line line)
@ -74,6 +66,25 @@
(cdr value-elements)))) (cdr value-elements))))
(define (vcard-string->alist string) ;; Reader thunk. Read/parse an entire vcard into a “vcard alist.”
(map parse-vcard-line (lines string))) (define (read-vcard)
(let [(element (read-vcard-element))]
(if (not (eof-object? (peek-char)))
(append (list element) (read-vcard))
element)))
;; Read a single unfolded line into a vcard “element” list.
(define (read-vcard-element)
(parse-vcard-line (read-folded-line)))
;; Reader-thunk. Read a “logical” folded-line, where a line beginning with a
;; space is a continuation of the previous line — like with vcards.
(define (read-folded-line)
(let [(line (read-line))]
(if (eq? (peek-char) #\space)
(string-concatenate
(list line
(string-drop (read-element) 1)))
line)))