diff --git a/embddr/common.scm b/embddr/common.scm index 47d3dff..50859de 100644 --- a/embddr/common.scm +++ b/embddr/common.scm @@ -4,7 +4,9 @@ (import (srfi srfi-1) (srfi srfi-26) - (srfi srfi-60)) + (srfi srfi-28) + (srfi srfi-60) + (ice-9 textual-ports)) (export log2 clog2 round-to one? power-of-two? @@ -17,6 +19,7 @@ has-duplicates? find-duplicates insert-between string-replace-text + read-template make-mux-selectors)) (else @@ -190,3 +193,21 @@ (replace str (+ occ tlen 1)) 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)))