diff --git a/tsv2vcard.scm b/tsv2vcard.scm new file mode 100644 index 0000000..4ae665d --- /dev/null +++ b/tsv2vcard.scm @@ -0,0 +1,46 @@ +;; Copyright (C) 2023 Camden Dixie O'Brien +;; SPDX-License-Identifier: AGPL-3.0-only + +(define (tsv-fields line) + (reverse + (let iter ((len (string-length line)) + (rem (string-copy line)) + (idx 0) + (res '())) + (cond ((>= idx len) (cons rem res)) + ((char=? #\tab (string-ref rem idx)) + (iter (- len idx 1) + (substring rem (+ idx 1) len) + 0 + (cons (substring rem 0 idx) res))) + (else (iter len rem (+ idx 1) res)))))) + +(define (vcard name number) + (with-output-to-string + (lambda () + (display "BEGIN:VCARD") + (newline) + (display "N:;") + (display name) + (display ";;;") + (newline) + (display "TEL;TYPE=cell:") + (display number) + (newline) + (display "END:VCARD") + (newline)))) + +(define (tsv-contact->vcard line) + (let* ((fields (tsv-fields line)) + (number (list-ref fields 0)) + (name (list-ref fields 1))) + (vcard name number))) + +(define (foreach-line thunk) + (let loop () + (let ((line (get-line (current-input-port)))) + (when (not (eof-object? line)) + (display (thunk line)) + (loop))))) + +(foreach-line tsv-contact->vcard)