Add read-template function

This commit is contained in:
Nikolay Puzanov 2022-11-30 11:32:22 +03:00
parent 23d2d07b1a
commit 1156b31463

View File

@ -4,7 +4,9 @@
(import (srfi srfi-1) (import (srfi srfi-1)
(srfi srfi-26) (srfi srfi-26)
(srfi srfi-60)) (srfi srfi-28)
(srfi srfi-60)
(ice-9 textual-ports))
(export log2 clog2 round-to (export log2 clog2 round-to
one? power-of-two? one? power-of-two?
@ -17,6 +19,7 @@
has-duplicates? find-duplicates has-duplicates? find-duplicates
insert-between insert-between
string-replace-text string-replace-text
read-template
make-mux-selectors)) make-mux-selectors))
(else (else
@ -190,3 +193,21 @@
(replace str (+ occ tlen 1)) (replace str (+ occ tlen 1))
str)) str))
str)))))) str))))))
;;; Read template and substitute replacements
;;; Returns list of strings (lines)
(define (read-template template-file template-format subst-list)
(let ((ls (call-with-input-file template-file
(lambda (port)
(let loop ((l '()))
(let ((s (get-line port)))
(if (eof-object? s)
(reverse l)
(loop (cons s l)))))))))
(map (lambda (str) (fold (lambda (s out)
(string-replace-text
out
(format template-format (first s))
(format "~a" (second s))))
str subst-list))
ls)))