From 0a5ef01169caa418843db05577e5b6a2c29d2e81 Mon Sep 17 00:00:00 2001 From: Jaidyn Ann <10477760+JadedCtrl@users.noreply.github.com> Date: Mon, 5 Feb 2024 21:55:57 -0600 Subject: [PATCH] Switch to port-based reading/parsing --- vcarded.scm | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/vcarded.scm b/vcarded.scm index e167d7b..c05cd44 100644 --- a/vcarded.scm +++ b/vcarded.scm @@ -54,14 +54,6 @@ (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: ;; (KEY ("keyprop1=d" "keyprop2=b") "VALUE" ("valprop1=a" "valprop2=b")) (define (parse-vcard-line line) @@ -74,6 +66,25 @@ (cdr value-elements)))) -(define (vcard-string->alist string) - (map parse-vcard-line (lines string))) +;; Reader thunk. Read/parse an entire vcard into a “vcard alist.” +(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)))